aboutsummaryrefslogtreecommitdiff
path: root/plugins/related_posts
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@chimrod.com>2015-03-19 22:42:40 +0100
committerSébastien Dailly <sebastien@chimrod.com>2015-03-19 22:42:40 +0100
commite90c342c6b3487840f0b7067fd2ed04678d00db3 (patch)
treeb27c61028e350e85170b5a3b13cf77188c24da4e /plugins/related_posts
parent0ad73de1d89956739738716f2d32652ca83164b5 (diff)
Do not load disqus comment automaticaly
Diffstat (limited to 'plugins/related_posts')
-rw-r--r--plugins/related_posts/related_posts.py92
1 files changed, 56 insertions, 36 deletions
diff --git a/plugins/related_posts/related_posts.py b/plugins/related_posts/related_posts.py
index 4000e03..dff03c5 100644
--- a/plugins/related_posts/related_posts.py
+++ b/plugins/related_posts/related_posts.py
@@ -1,36 +1,56 @@
-"""
-Related posts plugin for Pelican
-================================
-
-Adds related_posts variable to article's context
-"""
-
-from pelican import signals
-from collections import Counter
-
-
-def add_related_posts(generator):
- # get the max number of entries from settings
- # or fall back to default (5)
- numentries = generator.settings.get('RELATED_POSTS_MAX', 5)
-
- for article in generator.articles:
- # no tag, no relation
- if not hasattr(article, 'tags'):
- continue
-
- # score = number of common tags
- scores = Counter()
- for tag in article.tags:
- scores += Counter(generator.tags[tag])
-
- # remove itself
- scores.pop(article)
-
- article.related_posts = [other for other, count
- in scores.most_common(numentries)]
-
-
-def register():
- signals.article_generator_finalized.connect(add_related_posts)
-
+"""
+Related posts plugin for Pelican
+================================
+
+Adds related_posts variable to article's context
+"""
+
+from pelican import signals
+from collections import Counter
+from itertools import chain
+
+
+def add_related_posts(generator):
+ # get the max number of entries from settings
+ # or fall back to default (5)
+ numentries = generator.settings.get('RELATED_POSTS_MAX', 5)
+ # Skip all posts in the same category as the article
+ skipcategory = generator.settings.get('RELATED_POSTS_SKIP_SAME_CATEGORY', False)
+ for article in generator.articles:
+ # set priority in case of forced related posts
+ if hasattr(article,'related_posts'):
+ # split slugs
+ related_posts = article.related_posts.split(',')
+ posts = []
+ # get related articles
+ for slug in related_posts:
+ i = 0
+ slug = slug.strip()
+ for a in generator.articles:
+ if i >= numentries: # break in case there are max related psots
+ break
+ if a.slug == slug:
+ posts.append(a)
+ i += 1
+
+ article.related_posts = posts
+ else:
+ # no tag, no relation
+ if not hasattr(article, 'tags'):
+ continue
+
+ # score = number of common tags
+ related = chain(*(generator.tags[tag] for tag in article.tags))
+ if skipcategory:
+ related = (other for other in related
+ if other.category != article.category)
+ scores = Counter(related)
+
+ # remove itself
+ scores.pop(article, None)
+
+ article.related_posts = [other for other, count
+ in scores.most_common(numentries)]
+
+def register():
+ signals.article_generator_finalized.connect(add_related_posts) \ No newline at end of file