diff --git a/hyde/ext/plugins/grouper.py b/hyde/ext/plugins/grouper.py index 598cd10..efe27a6 100644 --- a/hyde/ext/plugins/grouper.py +++ b/hyde/ext/plugins/grouper.py @@ -21,23 +21,22 @@ class Group(Expando): """ def __init__(self, grouping, parent=None): + self.name = 'groups' self.parent = parent self.root = self self.root = parent.root if parent else self - super(Group, self).__init__(grouping) - if hasattr(parent, 'sorter') and not hasattr(self, 'sorter'): + self.groups = [] + self.sorter = getattr(grouping, 'sorter', None) + if hasattr(parent, 'sorter'): self.sorter = parent.sorter - - name = 'groups' - if hasattr(grouping, 'name'): - name = grouping.name + super(Group, self).__init__(grouping) add_method(Node, 'walk_%s_groups' % self.name, Group.walk_groups_in_node, group=self) add_method(Node, - 'walk_resources_grouped_by_%s' % name, + 'walk_resources_grouped_by_%s' % self.name, Group.walk_resources, group=self) @@ -58,7 +57,9 @@ class Group(Expando): object for walking the resources in the node that belong to this group. """ - return group.list_resources(node) + for group in group.walk_groups(): + for resource in group.list_resources(node): + yield resource @staticmethod def walk_groups_in_node(node, group): @@ -69,25 +70,19 @@ class Group(Expando): walker = group.walk_groups() for g in walker: lister = g.list_resources(node) - found = False for r in lister: - found = True yield g break; - if not found: - walker.send(True) found = False - def walk_groups(self): """ Walks the groups in the current group """ + yield self for group in self.groups: - skip = (yield group) - if not skip: - group.walk_groups() - + for child in group.walk_groups(): + yield child def list_resources(self, node): """ @@ -96,7 +91,7 @@ class Group(Expando): group. """ walker = 'walk_resources' - if hasattr(self, 'sorter'): + if hasattr(self, 'sorter') and self.sorter: walker = 'walk_resources_sorted_by_' + self.sorter walker = getattr(node, walker, getattr(node, 'walk_resources')) for resource in walker(): @@ -104,11 +99,9 @@ class Group(Expando): group_value = getattr(resource.meta, self.root.name) except AttributeError: continue - if group_value == self.name: yield resource - class GrouperPlugin(Plugin): """ Grouper plugin for hyde. Adds the ability to do diff --git a/hyde/tests/ext/test_grouper.py b/hyde/tests/ext/test_grouper.py index 44410f9..49931c5 100644 --- a/hyde/tests/ext/test_grouper.py +++ b/hyde/tests/ext/test_grouper.py @@ -29,6 +29,7 @@ class TestGrouper(object): def test_walk_resources_sorted_with_grouping_one_level(self): s = Site(TEST_SITE) cfg = """ + nodemeta: meta.yaml plugins: - hyde.ext.meta.MetaPlugin - hyde.ext.sorter.SorterPlugin @@ -37,6 +38,8 @@ class TestGrouper(object): kind: attr: - source_file.kind + filters: + is_processable: True grouper: section: description: Sections in the site @@ -57,12 +60,18 @@ class TestGrouper(object): SorterPlugin(s).begin_site() GrouperPlugin(s).begin_site() - + print [resource.name for resource in s.content.walk_resources_sorted_by_kind()] groups = dict([(g.name, g) for g in s.grouper['section'].groups]) assert len(groups) == 2 assert 'start' in groups assert 'plugins' in groups - + + groups = dict([(g.name, g) for g in s.grouper['section'].walk_groups()]) + assert len(groups) == 3 + assert 'section' in groups + assert 'start' in groups + assert 'plugins' in groups + assert hasattr(s.content, 'walk_section_groups') groups = dict([(g.name, g) for g in s.content.walk_section_groups()]) assert len(groups) == 2 @@ -70,8 +79,10 @@ class TestGrouper(object): assert 'plugins' in groups assert hasattr(s.content, 'walk_resources_grouped_by_section') - - + + resources = [resource.name for resource in s.content.walk_resources_grouped_by_section()] + + assert len(resources) == 5 # assert hasattr(s, 'sectional') # assert hasattr(s.sectional, 'groups')