From dbae01713474f05f06d37971a474aee7e39a11cb Mon Sep 17 00:00:00 2001 From: Lakshmi Vyasarajan Date: Thu, 3 Feb 2011 12:47:58 +0530 Subject: [PATCH] Grouping template added --- hyde/ext/plugins/grouper.py | 8 +-- hyde/tests/ext/test_grouper.py | 96 +++++++++++++++++++++++++++------- hyde/tests/util.py | 4 +- 3 files changed, 83 insertions(+), 25 deletions(-) diff --git a/hyde/ext/plugins/grouper.py b/hyde/ext/plugins/grouper.py index 42af543..d2e0b04 100644 --- a/hyde/ext/plugins/grouper.py +++ b/hyde/ext/plugins/grouper.py @@ -9,11 +9,14 @@ from hyde.plugin import Plugin from hyde.site import Node, Resource from hyde.util import add_method +from collections import namedtuple from functools import partial from itertools import ifilter, izip, tee, product from operator import attrgetter +Grouper = namedtuple('Grouper', 'group resources') + class Group(Expando): """ A wrapper class for groups. Adds methods for @@ -70,10 +73,7 @@ class Group(Expando): walker = group.walk_groups() for g in walker: lister = g.walk_resources_in_node(node) - for r in lister: - yield g - break; - found = False + yield Grouper(group=g, resources=lister) def walk_groups(self): """ diff --git a/hyde/tests/ext/test_grouper.py b/hyde/tests/ext/test_grouper.py index 916f39b..f155bd0 100644 --- a/hyde/tests/ext/test_grouper.py +++ b/hyde/tests/ext/test_grouper.py @@ -11,6 +11,8 @@ from hyde.fs import File, Folder from hyde.generator import Generator from hyde.site import Site from hyde.model import Config, Expando + +from hyde.tests.util import assert_html_equals import yaml TEST_SITE = File(__file__).parent.parent.child_folder('_test') @@ -27,9 +29,9 @@ class TestGrouperSingleLevel(object): cfg = """ nodemeta: meta.yaml plugins: - - hyde.ext.meta.MetaPlugin - - hyde.ext.sorter.SorterPlugin - - hyde.ext.grouper.GrouperPlugin + - hyde.ext.plugins.meta.MetaPlugin + - hyde.ext.plugins.sorter.SorterPlugin + - hyde.ext.plugins.grouper.GrouperPlugin sorter: kind: attr: @@ -56,6 +58,10 @@ class TestGrouperSingleLevel(object): SorterPlugin(self.s).begin_site() GrouperPlugin(self.s).begin_site() + self.all = ['installation.html', 'overview.html', 'templating.html', 'plugins.html', 'tags.html'] + self.start = ['installation.html', 'overview.html', 'templating.html'] + self.plugins = ['plugins.html', 'tags.html'] + def tearDown(self): TEST_SITE.delete() @@ -77,22 +83,27 @@ class TestGrouperSingleLevel(object): def test_walk_section_groups(self): assert hasattr(self.s.content, 'walk_section_groups') - groups = dict([(g.name, g) for g in self.s.content.walk_section_groups()]) - assert len(groups) == 2 + groups = dict([(grouper.group.name, grouper) for grouper in self.s.content.walk_section_groups()]) + assert len(groups) == 3 + assert 'section' in groups assert 'start' in groups assert 'plugins' in groups + for name in ['start', 'plugins']: + res = [resource.name for resource in groups[name].resources] + assert res == getattr(self, name) def test_walk_start_groups(self): assert hasattr(self.s.content, 'walk_start_groups') - groups = dict([(g.name, g) for g in self.s.content.walk_start_groups()]) + groups = dict([(g.name, g) for g, resources in self.s.content.walk_start_groups()]) assert len(groups) == 1 assert 'start' in groups + def test_walk_plugins_groups(self): assert hasattr(self.s.content, 'walk_plugins_groups') - groups = dict([(g.name, g) for g in self.s.content.walk_plugins_groups()]) + groups = dict([(g.name, g) for g, resources in self.s.content.walk_plugins_groups()]) assert len(groups) == 1 assert 'plugins' in groups @@ -101,29 +112,74 @@ class TestGrouperSingleLevel(object): assert hasattr(self.s.content, 'walk_resources_grouped_by_section') resources = [resource.name for resource in self.s.content.walk_resources_grouped_by_section()] - assert len(resources) == 5 - assert 'installation.html' in resources - assert 'overview.html' in resources - assert 'templating.html' in resources - assert 'plugins.html' in resources - assert 'tags.html' in resources + assert resources == self.all + def test_walk_start_resources(self): assert hasattr(self.s.content, 'walk_resources_grouped_by_start') start_resources = [resource.name for resource in self.s.content.walk_resources_grouped_by_start()] - assert len(start_resources) == 3 - assert 'installation.html' in start_resources - assert 'overview.html' in start_resources - assert 'templating.html' in start_resources + assert start_resources == self.start def test_walk_plugins_resources(self): assert hasattr(self.s.content, 'walk_resources_grouped_by_plugins') plugin_resources = [resource.name for resource in self.s.content.walk_resources_grouped_by_plugins()] - assert len(plugin_resources) == 2 - assert 'plugins.html' in plugin_resources - assert 'tags.html' in plugin_resources + assert plugin_resources == self.plugins + + def test_nav_with_grouper(self): + text =""" +{% for group, resources in site.content.walk_section_groups() %} + +{% endfor %} + +""" + expected = """ + + + + +""" + gen = Generator(self.s) + gen.load_site_if_needed() + gen.load_template_if_needed() + out = gen.template.render(text, {'site':self.s}) + assert_html_equals(out, expected) \ No newline at end of file diff --git a/hyde/tests/util.py b/hyde/tests/util.py index d7d14b5..03a8f2c 100644 --- a/hyde/tests/util.py +++ b/hyde/tests/util.py @@ -1,9 +1,11 @@ +import re + def strip_spaces_between_tags(value): """ Stolen from `django.util.html` Returns the given HTML with spaces between tags removed. """ - return re.sub(r'>\s+<', '><', force_unicode(value)) + return re.sub(r'>\s+<', '><', unicode(value)) def assert_html_equals(expected, actual, sanitize=None): expected = strip_spaces_between_tags(expected.strip())