Skip to content
Permalink
Browse files
Improving post sorting.
  • Loading branch information
felixfontein committed Jan 11, 2017
1 parent 170e772 commit ffca646993c70e58469f1b420ebac6ba433aa193
Showing with 19 additions and 9 deletions.
  1. +18 −5 nikola/nikola.py
  2. +1 −4 nikola/plugins/misc/taxonomies_classifier.py
@@ -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.
@@ -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:]):
@@ -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

0 comments on commit ffca646

Please sign in to comment.