Skip to content

Commit

Permalink
Improving post sorting.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfontein committed Jan 11, 2017
1 parent 170e772 commit ffca646
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
23 changes: 18 additions & 5 deletions nikola/nikola.py
Expand Up @@ -1984,6 +1984,20 @@ def create_hierarchy(cat_hierarchy, parent=None):
# Next, flatten the hierarchy
self.category_hierarchy = utils.flatten_tree_structure(root_list)

def sort_posts(self, posts, lang=None):
"""Return sorted list of posts."""
# Last tie breaker: sort by source path
posts = sorted(posts, key=lambda p: p.source_path)
# Next tie breaker: sort by title if language is given
if lang is not None:
posts = natsort.natsorted(posts, key=lambda p: p.title(lang), alg=natsort.ns.F | natsort.ns.IC)
# Next tie breaker: sort by date
posts = sorted(posts, key=lambda p: p.date, reverse=True)
# Finally, sort by priority
posts = sorted(posts, key=lambda p: int(p.meta('priority')) if p.meta('priority') else 0, reverse=True)
# Return result
return posts

def scan_posts(self, really=False, ignore_quit=False, quiet=False):
"""Scan all the posts.
Expand Down Expand Up @@ -2068,11 +2082,10 @@ def scan_posts(self, really=False, ignore_quit=False, quiet=False):

# Sort everything.

for thing in self.timeline, self.posts, self.all_posts, self.pages:
thing.sort(key=lambda p:
(int(p.meta('priority')) if p.meta('priority') else 0,
p.date, p.source_path))
thing.reverse()
self.timeline = self.sort_posts(self.timeline)
self.posts = self.sort_posts(self.posts)
self.all_posts = self.sort_posts(self.all_posts)
self.pages = self.sort_posts(self.pages)
self._sort_category_hierarchy()

for i, p in enumerate(self.posts[1:]):
Expand Down
5 changes: 1 addition & 4 deletions nikola/plugins/misc/taxonomies_classifier.py
Expand Up @@ -98,10 +98,7 @@ def _do_classification(self, site):
# Convert sets to lists and sort them
for classification in list(posts_per_classification.keys()):
posts = list(posts_per_classification[classification])
posts.sort(key=lambda p:
(int(p.meta('priority')) if p.meta('priority') else 0,
p.date, p.source_path))
posts.reverse()
posts = self.site.sort_posts(posts)
taxonomy.sort_posts(posts, classification, lang)
posts_per_classification[classification] = posts
# Create hierarchy information
Expand Down

0 comments on commit ffca646

Please sign in to comment.