Browse Source

Ensured the is_processable is always accounted for when sorting

main
Lakshmi Vyasarajan 14 years ago
parent
commit
f70b1b3495
2 changed files with 54 additions and 3 deletions
  1. +6
    -3
      hyde/ext/plugins/sorter.py
  2. +48
    -0
      hyde/tests/ext/test_sorter.py

+ 6
- 3
hyde/ext/plugins/sorter.py View File

@@ -26,8 +26,8 @@ def filter_method(item, settings=None):
given settings evaluate to True. given settings evaluate to True.
""" """


all_match = True
if settings and hasattr(settings, 'filters'):
all_match = item.is_processable
if all_match and settings and hasattr(settings, 'filters'):
filters = settings.filters filters = settings.filters
for field, value in filters.__dict__.items(): for field, value in filters.__dict__.items():
try: try:
@@ -49,7 +49,10 @@ def sort_method(node, settings=None):
attr = settings.attr attr = settings.attr
filter_ = partial(filter_method, settings=settings) filter_ = partial(filter_method, settings=settings)
resources = ifilter(filter_, node.walk_resources()) resources = ifilter(filter_, node.walk_resources())
return sorted(resources, key=attrgetter(attr))
reverse = False
if settings and hasattr(settings, 'reverse'):
reverse = settings.reverse
return sorted(resources, key=attrgetter(attr), reverse=reverse)


def make_method(method_name, method_): def make_method(method_name, method_):
def method__(self): def method__(self):


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

@@ -49,6 +49,31 @@ class TestMeta(object):


assert pages == sorted(expected, key=lambda f: File(f).kind) assert pages == sorted(expected, key=lambda f: File(f).kind)


def test_walk_resources_sorted_reverse(self):
s = Site(TEST_SITE)
s.load()
s.config.plugins = ['hyde.ext.sorter.SorterPlugin']
s.config.sorter = Expando(dict(kind=dict(attr='source_file.kind', reverse=True)))

SorterPlugin(s).begin_site()

assert hasattr(s.content, 'walk_resources_sorted_by_kind')
expected = ["404.html",
"about.html",
"apple-touch-icon.png",
"merry-christmas.html",
"crossdomain.xml",
"favicon.ico",
"robots.txt",
"site.css"
]

pages = [page.name for page in
s.content.walk_resources_sorted_by_kind()]


assert pages == sorted(expected, key=lambda f: File(f).kind, reverse=True)

def test_walk_resources_sorted_with_filters(self): def test_walk_resources_sorted_with_filters(self):
s = Site(TEST_SITE) s = Site(TEST_SITE)
cfg = """ cfg = """
@@ -73,6 +98,29 @@ class TestMeta(object):


assert pages == sorted(expected) assert pages == sorted(expected)


def test_walk_resources_sorted_default_is_processable(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()
p_404 = s.content.resource_from_relative_path('404.html')
p_404.is_processable = False
SorterPlugin(s).begin_site()

assert hasattr(s.content, 'walk_resources_sorted_by_kind2')
expected = ["about.html", "merry-christmas.html"]

pages = [page.name for page in s.content.walk_resources_sorted_by_kind2()]

assert pages == sorted(expected)

def test_prev_next(self): def test_prev_next(self):
s = Site(TEST_SITE) s = Site(TEST_SITE)
cfg = """ cfg = """


Loading…
Cancel
Save