diff --git a/hyde/ext/plugins/folders.py b/hyde/ext/plugins/folders.py new file mode 100644 index 0000000..6ac1f69 --- /dev/null +++ b/hyde/ext/plugins/folders.py @@ -0,0 +1,43 @@ +# -*- coding: utf-8 -*- +""" +Plugins related to folders and paths +""" + +from hyde.plugin import Plugin +from hyde.fs import Folder + +class FlattenerPlugin(Plugin): + """ + The plugin class for syntax text replacement. + """ + def __init__(self, site): + super(FlattenerPlugin, self).__init__(site) + + def begin_site(self): + """ + Finds all the folders that need flattening and changes the + relative deploy path of all resources in those folders. + """ + items = [] + try: + items = self.site.config.flattener.items + except AttributeError: + pass + + for item in items: + node = None + target = '' + try: + node = self.site.content.node_from_relative_path(item.source) + target = Folder(item.target) + except AttributeError: + continue + if node: + for resource in node.walk_resources(): + target_path = target.child(resource.name) + self.logger.debug( + 'Flattening resource path[%s] to [%s]' % + (resource, target_path)) + resource.relative_deploy_path = target_path + + diff --git a/hyde/plugin.py b/hyde/plugin.py index 5042ed2..2267f1a 100644 --- a/hyde/plugin.py +++ b/hyde/plugin.py @@ -126,13 +126,6 @@ class Plugin(object): """ pass - def site_complete(self): - """ - Called when the generation process is complete. This method is called - only when the entire site is generated. - """ - pass - def generation_complete(self): """ Called when generation is completed. diff --git a/hyde/site.py b/hyde/site.py index 08e35a5..a0ab809 100644 --- a/hyde/site.py +++ b/hyde/site.py @@ -88,6 +88,7 @@ class Resource(Processable): self.site.content.resource_deploy_path_changed(self) relative_deploy_path = property(get_relative_deploy_path, set_relative_deploy_path) + url = relative_deploy_path class Node(Processable): """ diff --git a/hyde/tests/ext/test_flattener.py b/hyde/tests/ext/test_flattener.py new file mode 100644 index 0000000..de4a329 --- /dev/null +++ b/hyde/tests/ext/test_flattener.py @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- +""" +Use nose +`$ pip install nose` +`$ nosetests` +""" +from hyde.fs import File, Folder +from hyde.generator import Generator +from hyde.site import Site +from hyde.model import Expando, Config + + +TEST_SITE = File(__file__).parent.parent.child_folder('_test') + + +class TestFlattner(object): + + def setUp(self): + TEST_SITE.make() + TEST_SITE.parent.child_folder( + 'sites/test_jinja').copy_contents_to(TEST_SITE) + + def tearDown(self): + TEST_SITE.delete() + + def test_can_flattener(self): + s = Site(TEST_SITE) + cfg = """ + plugins: + - hyde.ext.plugins.folders.FlattenerPlugin + flattener: + items: + - + source: blog + target: '' + """ + import yaml + s.config = Config(TEST_SITE, config_dict=yaml.load(cfg)) + s.load() + gen = Generator(s) + gen.generate_all() + + assert not s.config.deploy_root_path.child_folder('blog').exists + assert File(s.config.deploy_root_path.child('merry-christmas.html')).exists + diff --git a/hyde/tests/test_site.py b/hyde/tests/test_site.py index b5d0baa..ca462e2 100644 --- a/hyde/tests/test_site.py +++ b/hyde/tests/test_site.py @@ -148,6 +148,7 @@ def test_relative_deploy_path(): s.load() for page in s.content.walk_resources(): assert page.relative_deploy_path == Folder(page.relative_path) + assert page.url == page.relative_deploy_path def test_relative_deploy_path_override(): s = Site(TEST_SITE_ROOT)