Address code review comments from PR getpelican/pelican#1348

The text about the sort-by-key function comes from:
https://docs.python.org/2/library/stdtypes.html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange

Minor style cleanup as well.
This commit is contained in:
Mark Lee 2014-05-20 13:53:02 -07:00
commit 144cddaf39
2 changed files with 26 additions and 24 deletions

View file

@ -260,16 +260,19 @@ posts for the month at ``posts/2011/Aug/index.html``.
arrive at an appropriate archive of posts, without having to specify arrive at an appropriate archive of posts, without having to specify
a page name. a page name.
====================================================== ======================================================== ====================================================== ==============================================================
Setting name (followed by default value, if any) What does it do? Setting name (followed by default value, if any) What does it do?
====================================================== ======================================================== ====================================================== ==============================================================
``ARTICLE_URL = '{slug}.html'`` The URL to refer to an article. ``ARTICLE_URL = '{slug}.html'`` The URL to refer to an article.
``ARTICLE_SAVE_AS = '{slug}.html'`` The place where we will save an article. ``ARTICLE_SAVE_AS = '{slug}.html'`` The place where we will save an article.
``ARTICLE_ORDER_BY = 'slug'`` The metadata attribute used to sort articles. By default ``ARTICLE_ORDER_BY = 'slug'`` The metadata attribute used to sort articles. By default,
the articles_page.object_list template variable is the ``articles_page.object_list`` template variable is
ordered by slug. If you modify this, make sure all ordered by slug. If you modify this, make sure all
articles contain the attribute you specify. You can articles contain the attribute you specify. You can also
also specify a sorting function. specify a "sorting" function of one argument that is used
to extract a comparison key from each article. For example,
sorting by title without using the built-in functionality
would use the function ``operator.attrgetter('title')``.
``ARTICLE_LANG_URL = '{slug}-{lang}.html'`` The URL to refer to an article which doesn't use the ``ARTICLE_LANG_URL = '{slug}-{lang}.html'`` The URL to refer to an article which doesn't use the
default language. default language.
``ARTICLE_LANG_SAVE_AS = '{slug}-{lang}.html'`` The place where we will save an article which ``ARTICLE_LANG_SAVE_AS = '{slug}-{lang}.html'`` The place where we will save an article which
@ -285,13 +288,16 @@ Setting name (followed by default value, if any) What does it do?
the same as PAGE_URL or you need to use a rewrite in the same as PAGE_URL or you need to use a rewrite in
your server config. your server config.
``PAGE_ORDER_BY = 'filename'`` The metadata attribute used to sort pages. By default ``PAGE_ORDER_BY = 'basename'`` The metadata attribute used to sort pages. By default
the PAGES template variable is ordered by filename the ``PAGES`` template variable is ordered by basename
(path not included). Note that the option 'filename' (i.e., path not included). Note that the option ``'basename'``
is a special option supported in the source code. If is a special option supported in the source code. If
you modify this settings, make sure all pages contain you modify this setting, make sure all pages contain
the attribute you specify. You can also specify a the attribute you specify. You can also specify a "sorting"
sorting function. function of one argument that is used to extract a comparison
key from each page. For example, the basename function looks
similar to
``lambda x: os.path.basename(getattr(x, 'source_path', ''))``.
``PAGE_LANG_URL = 'pages/{slug}-{lang}.html'`` The URL we will use to link to a page which doesn't ``PAGE_LANG_URL = 'pages/{slug}-{lang}.html'`` The URL we will use to link to a page which doesn't
use the default language. use the default language.
``PAGE_LANG_SAVE_AS = 'pages/{slug}-{lang}.html'`` The location we will save the page which doesn't ``PAGE_LANG_SAVE_AS = 'pages/{slug}-{lang}.html'`` The location we will save the page which doesn't
@ -309,7 +315,7 @@ Setting name (followed by default value, if any) What does it do?
non-alphanumerics when generating slugs. Specified non-alphanumerics when generating slugs. Specified
as a list of 2-tuples of ``(from, to)`` which are as a list of 2-tuples of ``(from, to)`` which are
applied in order. applied in order.
====================================================== ======================================================== ====================================================== ==============================================================
.. note:: .. note::

View file

@ -465,17 +465,13 @@ def process_translations(content_list, order_by=None):
a.translations = [x for x in items if x != a] a.translations = [x for x in items if x != a]
if order_by: if order_by:
if hasattr(order_by, '__call__'): if callable(order_by):
try: try:
index.sort(key=order_by) index.sort(key=order_by)
except: except Exception:
if hasattr(order_by, 'func_name'): logger.error('Error sorting with function {}'.format(order_by))
logger.error("Error sorting with function %s" % order_by.func_name) elif order_by == 'basename':
else: index.sort(key=lambda x: os.path.basename(x.source_path or ''))
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': elif order_by != 'slug':
index.sort(key=attrgetter(order_by)) index.sort(key=attrgetter(order_by))