diff --git a/hyde/ext/plugins/tagger.py b/hyde/ext/plugins/tagger.py index 0c6d0d0..ac6d2aa 100644 --- a/hyde/ext/plugins/tagger.py +++ b/hyde/ext/plugins/tagger.py @@ -4,6 +4,7 @@ Contains classes and utilities related to tagging resources in hyde. """ import re +from hyde.fs import File, Folder from hyde.model import Expando from hyde.plugin import Plugin from hyde.site import Node, Resource @@ -58,16 +59,19 @@ class TaggerPlugin(Plugin): tagger: sorter: kind # How to sort the resources in a tag archives: - template: tagged_posts.j2 - target: tags - archive_extension: html + blog: + template: tagged_posts.j2 + source: blog + target: blog/tags + archive_extension: html """ def __init__(self, site): super(TaggerPlugin, self).__init__(site) def begin_site(self): """ - Initialize plugin. Add tag to the site context variable. + Initialize plugin. Add tag to the site context variable + and methods for walking tagged resources. """ self.logger.debug("Adding tags from metadata") @@ -92,4 +96,45 @@ class TaggerPlugin(Plugin): else: tags[tag].append(resource) - self.site.tagger = Expando(dict(tags=tags)) \ No newline at end of file + self.site.tagger = Expando(dict(tags=tags)) + + def site_complete(self): + """ + Generate archives. + """ + + content = self.site.content + archive_config = None + + try: + archive_config = attrgetter("tagger.archives")(self.site.config) + except AttributeError: + return + + self.logger.debug("Generating archives for tags") + + for name, config in archive_config.to_dict().iteritems(): + + if not 'template' in config: + raise self.template.exception_class( + "No Template sepecified in tagger configuration.") + source = content.node_from_relative_path(config.get('source', '')) + target = self.site.config.deploy_root_path.child_folder( + config.get('target', 'tags')) + extension = config.get('extension', 'html') + + if not target.exists: + target.make() + + template = config['template'] + text = "{%% extends \"%s\" %%}" % template + + for tag, resources in self.site.tagger.tags.to_dict().iteritems(): + archive_text = self.template.render(text, dict( + site=self.site, + node=source, + walker=getattr(source, + "walk_resources_tagged_with_%s" % tag) + )) + archive_file = File(target.child("%s.%s" % (tag, extension))) + archive_file.write(archive_text) diff --git a/hyde/tests/ext/test_tagger.py b/hyde/tests/ext/test_tagger.py index 2de9333..eb6dc66 100644 --- a/hyde/tests/ext/test_tagger.py +++ b/hyde/tests/ext/test_tagger.py @@ -65,26 +65,52 @@ class TestTagger(object): assert len(sad_thought_posts) == 1 assert "sad-post.html" in sad_thought_posts + def test_tagger_archives_generated(self): + gen = Generator(self.s) + gen.load_site_if_needed() + gen.load_template_if_needed() + gen.generate_all() + tags_folder = self.deploy.child_folder('blog/tags') + + assert tags_folder.exists + tags = ['sad', 'happy', 'angry', 'thoughts', 'events'] + + archives = (File(tags_folder.child("%s.html" % tag)) for tag in tags) + + for archive in archives: + assert archive.exists + + from pyquery import PyQuery + + q = PyQuery(File(tags_folder.child('sad.html')).read_all()) + assert q + + assert q('li').length == 2 + assert q('li a:first-child').attr('href') == '/blog/another-sad-post.html' + assert q('li a:eq(1)').attr('href') == '/blog/sad-post.html' + + q = PyQuery(File(tags_folder.child('happy.html')).read_all()) + assert q + + assert q('li').length == 1 + assert q('li a:first-child').attr('href') == '/blog/happy-post.html' + + q = PyQuery(File(tags_folder.child('angry.html')).read_all()) + assert q + + assert q('li').length == 1 + assert q('li a:first-child').attr('href') == '/blog/angry-post.html' + q = PyQuery(File(tags_folder.child('thoughts.html')).read_all()) + assert q + assert q('li').length == 3 + assert q('li a:eq(0)').attr('href') == '/blog/happy-post.html' + assert q('li a:eq(1)').attr('href') == '/blog/angry-post.html' + assert q('li a:eq(2)').attr('href') == '/blog/sad-post.html' + q = PyQuery(File(tags_folder.child('events.html')).read_all()) + assert q - # def test_tagger_archives_generated(): - # gen = Generator(self.s) - # gen.load_site_if_needed() - # gen.load_template_if_needed() - # gen.generate_all() - # tags_folder = self.deploy.child_folder('blog/tags') - # - # blog_node = self.s.node_from_relative_path('blog') - # res_by_tag = - # for resource in blog_node.walk_resources(): - # - # - # assert tags_folder.exists - # tags = ['sad', 'happy', 'angry', 'thoughts'] - # - # archives = (File(tags_folder.child("%s.html" % tag)) for tag in tags) - # for archive in archives: - # assert archive.exists - # assert + assert q('li').length == 1 + assert q('li a:first-child').attr('href') == '/blog/another-sad-post.html' diff --git a/hyde/tests/sites/test_tagger/layout/tagged_posts.j2 b/hyde/tests/sites/test_tagger/layout/tagged_posts.j2 index 19f9384..0bd551b 100644 --- a/hyde/tests/sites/test_tagger/layout/tagged_posts.j2 +++ b/hyde/tests/sites/test_tagger/layout/tagged_posts.j2 @@ -1,9 +1,8 @@ -{% if tag and tag.resources -%} +

Posts tagged: {{ tag }} in {{ node.name|title }}

-{%- endif %} \ No newline at end of file + \ No newline at end of file