From 18f2e1d5217c423398ac198e4781409292146df1 Mon Sep 17 00:00:00 2001 From: Lakshmi Vyasarajan Date: Wed, 29 May 2013 14:55:56 +0530 Subject: [PATCH] Make sorter's prev/next links circular. (Issue #208) --- CHANGELOG.rst | 5 +++++ README.rst | 2 +- hyde/ext/plugins/meta.py | 13 ++++++++++++ hyde/tests/ext/test_sorter.py | 37 +++++++++++++++++++++++++++++++++++ hyde/version.py | 2 +- 5 files changed, 57 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index acf3651..a5fbb96 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,3 +1,8 @@ +Version 0.8.7a5 +============================================================ + +* Make sorter's prev/next links circular. (Issue #208) + Version 0.8.7a4 ============================================================ diff --git a/README.rst b/README.rst index e3b6365..0be2191 100644 --- a/README.rst +++ b/README.rst @@ -1,4 +1,4 @@ -Version 0.8.7a4 +Version 0.8.7a5 A brand new **hyde** ==================== diff --git a/hyde/ext/plugins/meta.py b/hyde/ext/plugins/meta.py index 967b40c..ff86b6f 100644 --- a/hyde/ext/plugins/meta.py +++ b/hyde/ext/plugins/meta.py @@ -529,10 +529,23 @@ class SorterPlugin(Plugin): walker = getattr(self.site.content, sort_method_name, self.site.content.walk_resources) + first, last = None, None for prev, next in pairwalk(walker()): + if not first: + first = prev + last = next setattr(prev, next_att, next) setattr(next, prev_att, prev) + try: + circular = settings.circular + except AttributeError: + circular = False + + if circular and first: + setattr(first, prev_att, last) + setattr(last, next_att, first) + # # Grouping diff --git a/hyde/tests/ext/test_sorter.py b/hyde/tests/ext/test_sorter.py index 685f335..2cb81ab 100644 --- a/hyde/tests/ext/test_sorter.py +++ b/hyde/tests/ext/test_sorter.py @@ -198,6 +198,43 @@ class TestSorter(object): assert hasattr(p_mc, 'next_by_kind2') assert not p_mc.next_by_kind2 + def test_prev_next_looped(self): + s = Site(TEST_SITE) + cfg = """ + plugins: + - hyde.ext.meta.SorterPlugin + sorter: + kind2: + circular: true + filters: + source_file.kind: html + attr: + - name + """ + 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 p_404.prev_by_kind2 == p_mc + 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 p_mc.next_by_kind2 == p_404 + def test_prev_next_reversed(self): s = Site(TEST_SITE) cfg = """ diff --git a/hyde/version.py b/hyde/version.py index a58e5da..3f3439d 100644 --- a/hyde/version.py +++ b/hyde/version.py @@ -2,4 +2,4 @@ """ Handles hyde version. """ -__version__ = '0.8.7a4' +__version__ = '0.8.7a5'