aboutsummaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@chimrod.com>2013-04-28 19:54:45 +0200
committerSébastien Dailly <sebastien@chimrod.com>2013-05-01 10:33:08 +0200
commitdc214056449119b29ad7b19b8d47e81b343ff453 (patch)
treee5fbae31ed73509852d45354ab682e8c96e5c42f /plugins
parentbfc347fdea8ebf0246902cbf7c95cfd5b518b4e8 (diff)
Updated theme and templates
Updated to pelican 3.2
Diffstat (limited to 'plugins')
-rw-r--r--plugins/related_posts/__init__.py1
-rw-r--r--plugins/related_posts/related_posts.py36
2 files changed, 37 insertions, 0 deletions
diff --git a/plugins/related_posts/__init__.py b/plugins/related_posts/__init__.py
new file mode 100644
index 0000000..c78861e
--- /dev/null
+++ b/plugins/related_posts/__init__.py
@@ -0,0 +1 @@
+from .related_posts import *
diff --git a/plugins/related_posts/related_posts.py b/plugins/related_posts/related_posts.py
new file mode 100644
index 0000000..4000e03
--- /dev/null
+++ b/plugins/related_posts/related_posts.py
@@ -0,0 +1,36 @@
+"""
+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)
+