contents: Add rich comparisons to URLWrapper for easy sorting

There have been earlier attempts to sort categories and authors
[1,2,3], but they either sorted based on the object id [3], or only
sorted the main author and categories list.

This patch uses rich comparisons (keyed off URLWrapper.name, but
easily adjustable in subclasses) to make the objects sortable without
specifying a key for each sort.  For example, now

  {% for tag, articles in tags|sort %}

works as expected in a Jinja template.

The functools.total_ordering decorator fills in the missing rich
comparisons [4,5].

[1]: 877d454c8f
[2]: 7f36e0ed20
[3]: d0ec18f4db
[4]: http://docs.python.org/2/library/functools.html#functools.total_ordering
[5]: http://docs.python.org/3/library/functools.html#functools.total_ordering
This commit is contained in:
W. Trevor King 2013-01-03 13:54:56 -05:00
commit 2c434ebac1
2 changed files with 16 additions and 3 deletions

View file

@ -416,11 +416,10 @@ class ArticlesGenerator(Generator):
# order the categories per name
self.categories = list(self.categories.items())
self.categories.sort(
key=lambda item: item[0].name,
reverse=self.settings['REVERSE_CATEGORY_ORDER'])
self.authors = list(self.authors.items())
self.authors.sort(key=lambda item: item[0].name)
self.authors.sort()
self._update_context(('articles', 'dates', 'tags', 'categories',
'tag_cloud', 'authors', 'related_posts'))