1
0
Fork 0
forked from github/pelican

Support ordering pages and articles when iterating in templates.

Order can be set to a metadata attribute or a sorting function.
Default to order by slug for articles and order by filename for pages.
This commit is contained in:
David Marble 2013-11-14 12:29:01 -08:00 committed by Mark Lee
commit 0c69f4ad84
4 changed files with 43 additions and 3 deletions

View file

@ -409,7 +409,8 @@ class ArticlesGenerator(Generator):
(repr(article.status),
repr(f)))
self.articles, self.translations = process_translations(all_articles)
self.articles, self.translations = process_translations(all_articles,
order_by=self.settings['ARTICLE_ORDER_BY'])
for article in self.articles:
# only main articles are listed in categories and tags
@ -517,7 +518,8 @@ class PagesGenerator(Generator):
(repr(page.status),
repr(f)))
self.pages, self.translations = process_translations(all_pages)
self.pages, self.translations = process_translations(all_pages,
order_by=self.settings['PAGE_ORDER_BY'])
self.hidden_pages, self.hidden_translations = (
process_translations(hidden_pages))

View file

@ -57,10 +57,12 @@ DEFAULT_CONFIG = {
'OUTPUT_RETENTION': (),
'ARTICLE_URL': '{slug}.html',
'ARTICLE_SAVE_AS': '{slug}.html',
'ARTICLE_ORDER_BY': 'slug',
'ARTICLE_LANG_URL': '{slug}-{lang}.html',
'ARTICLE_LANG_SAVE_AS': '{slug}-{lang}.html',
'PAGE_URL': 'pages/{slug}.html',
'PAGE_SAVE_AS': os.path.join('pages', '{slug}.html'),
'PAGE_ORDER_BY': 'filename',
'PAGE_LANG_URL': 'pages/{slug}-{lang}.html',
'PAGE_LANG_SAVE_AS': os.path.join('pages', '{slug}-{lang}.html'),
'STATIC_URL': '{path}',

View file

@ -430,7 +430,7 @@ def truncate_html_words(s, num, end_text='...'):
return out
def process_translations(content_list):
def process_translations(content_list, order_by=None):
""" Finds translation and returns them.
Returns a tuple with two lists (index, translations). Index list includes
@ -440,6 +440,14 @@ def process_translations(content_list):
the same slug have that metadata.
For each content_list item, sets the 'translations' attribute.
order_by can be a string of an attribute or sorting function. If order_by
is defined, content will be ordered by that attribute or sorting function.
By default, content is ordered by slug.
Different content types can have default order_by attributes defined
in settings, e.g. PAGES_ORDER_BY='sort-order', in which case `sort-order`
should be a defined metadata attribute in each page.
"""
content_list.sort(key=attrgetter('slug'))
grouped_by_slugs = groupby(content_list, attrgetter('slug'))
@ -485,6 +493,22 @@ def process_translations(content_list):
translations.extend([x for x in items if x not in default_lang_items])
for a in items:
a.translations = [x for x in items if x != a]
if order_by:
if hasattr(order_by, '__call__'):
try:
index.sort(key=order_by)
except:
if hasattr(order_by, 'func_name'):
logger.error("Error sorting with function %s" % order_by.func_name)
else:
logger.error("Error sorting with function %r" % order_by)
elif order_by == 'filename':
index.sort(key=lambda x:os.path.basename(
x.source_path or ''))
elif order_by != 'slug':
index.sort(key=attrgetter(order_by))
return index, translations