| @@ -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 | |||||
| @@ -126,13 +126,6 @@ class Plugin(object): | |||||
| """ | """ | ||||
| pass | 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): | def generation_complete(self): | ||||
| """ | """ | ||||
| Called when generation is completed. | Called when generation is completed. | ||||
| @@ -88,6 +88,7 @@ class Resource(Processable): | |||||
| self.site.content.resource_deploy_path_changed(self) | self.site.content.resource_deploy_path_changed(self) | ||||
| relative_deploy_path = property(get_relative_deploy_path, set_relative_deploy_path) | relative_deploy_path = property(get_relative_deploy_path, set_relative_deploy_path) | ||||
| url = relative_deploy_path | |||||
| class Node(Processable): | class Node(Processable): | ||||
| """ | """ | ||||
| @@ -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 | |||||
| @@ -148,6 +148,7 @@ def test_relative_deploy_path(): | |||||
| s.load() | s.load() | ||||
| for page in s.content.walk_resources(): | for page in s.content.walk_resources(): | ||||
| assert page.relative_deploy_path == Folder(page.relative_path) | assert page.relative_deploy_path == Folder(page.relative_path) | ||||
| assert page.url == page.relative_deploy_path | |||||
| def test_relative_deploy_path_override(): | def test_relative_deploy_path_override(): | ||||
| s = Site(TEST_SITE_ROOT) | s = Site(TEST_SITE_ROOT) | ||||