From f33754169fe99ab667271666efc2b2eb424ba8c9 Mon Sep 17 00:00:00 2001 From: Lakshmi Vyasarajan Date: Fri, 7 Jan 2011 10:25:41 +0530 Subject: [PATCH] Added next prev support to sorter --- hyde/ext/plugins/sorter.py | 19 ++++++++++++++++++- hyde/tests/ext/test_sorter.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/hyde/ext/plugins/sorter.py b/hyde/ext/plugins/sorter.py index f9d63f5..d993cc8 100644 --- a/hyde/ext/plugins/sorter.py +++ b/hyde/ext/plugins/sorter.py @@ -7,7 +7,7 @@ from hyde.plugin import Plugin from hyde.site import Node, Resource from functools import partial -from itertools import ifilter +from itertools import ifilter, izip, tee from operator import attrgetter import logging @@ -15,6 +15,10 @@ from logging import NullHandler logger = logging.getLogger('hyde.engine') logger.addHandler(NullHandler()) +def pairwalk(iterable): + a, b = tee(iterable) + next(b, None) + return izip(a, b) def filter_method(item, settings=None): """ @@ -104,3 +108,16 @@ class SorterPlugin(Plugin): add_method(Node, sort_method_name, sort_method, settings) match_method_name = 'is_%s' % name add_method(Resource, match_method_name, filter_method, settings) + + prev_att = 'prev_by_%s' % name + next_att = 'next_by_%s' % name + + setattr(Resource, prev_att, None) + setattr(Resource, next_att, None) + + walker = getattr(self.site.content, + sort_method_name, + self.site.content.walk_resources) + for prev, next in pairwalk(walker()): + setattr(prev, next_att, next) + setattr(next, prev_att, prev) diff --git a/hyde/tests/ext/test_sorter.py b/hyde/tests/ext/test_sorter.py index 805d526..6b83857 100644 --- a/hyde/tests/ext/test_sorter.py +++ b/hyde/tests/ext/test_sorter.py @@ -73,6 +73,40 @@ class TestMeta(object): assert pages == sorted(expected) + def test_prev_next(self): + s = Site(TEST_SITE) + cfg = """ + plugins: + - hyde.ext.sorter.SorterPlugin + sorter: + kind2: + filters: + source_file.kind: html + """ + s.config = Config(TEST_SITE, config_dict=yaml.load(cfg)) + s.load() + SorterPlugin(s).begin_site() + + p_404 = s.content.resource_from_relative_path('404.html') + p_about = s.content.resource_from_relative_path('about.html') + p_mc = s.content.resource_from_relative_path( + 'blog/2010/december/merry-christmas.html') + + assert hasattr(p_404, 'prev_by_kind2') + assert not p_404.prev_by_kind2 + assert hasattr(p_404, 'next_by_kind2') + assert p_404.next_by_kind2 == p_about + + assert hasattr(p_about, 'prev_by_kind2') + assert p_about.prev_by_kind2 == p_404 + assert hasattr(p_about, 'next_by_kind2') + assert p_about.next_by_kind2 == p_mc + + assert hasattr(p_mc, 'prev_by_kind2') + assert p_mc.prev_by_kind2 == p_about + assert hasattr(p_mc, 'next_by_kind2') + assert not p_mc.next_by_kind2 + def test_walk_resources_sorted_using_generator(self): s = Site(TEST_SITE) cfg = """