Browse Source

Added next prev support to sorter

main
Lakshmi Vyasarajan 14 years ago
parent
commit
f33754169f
2 changed files with 52 additions and 1 deletions
  1. +18
    -1
      hyde/ext/plugins/sorter.py
  2. +34
    -0
      hyde/tests/ext/test_sorter.py

+ 18
- 1
hyde/ext/plugins/sorter.py View File

@@ -7,7 +7,7 @@ from hyde.plugin import Plugin
from hyde.site import Node, Resource from hyde.site import Node, Resource


from functools import partial from functools import partial
from itertools import ifilter
from itertools import ifilter, izip, tee
from operator import attrgetter from operator import attrgetter


import logging import logging
@@ -15,6 +15,10 @@ from logging import NullHandler
logger = logging.getLogger('hyde.engine') logger = logging.getLogger('hyde.engine')
logger.addHandler(NullHandler()) logger.addHandler(NullHandler())


def pairwalk(iterable):
a, b = tee(iterable)
next(b, None)
return izip(a, b)


def filter_method(item, settings=None): def filter_method(item, settings=None):
""" """
@@ -104,3 +108,16 @@ class SorterPlugin(Plugin):
add_method(Node, sort_method_name, sort_method, settings) add_method(Node, sort_method_name, sort_method, settings)
match_method_name = 'is_%s' % name match_method_name = 'is_%s' % name
add_method(Resource, match_method_name, filter_method, settings) 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)

+ 34
- 0
hyde/tests/ext/test_sorter.py View File

@@ -73,6 +73,40 @@ class TestMeta(object):


assert pages == sorted(expected) 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): def test_walk_resources_sorted_using_generator(self):
s = Site(TEST_SITE) s = Site(TEST_SITE)
cfg = """ cfg = """


Loading…
Cancel
Save