mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
moved related_posts to plugin
This commit is contained in:
parent
875734f963
commit
d9b5ee10de
2 changed files with 34 additions and 30 deletions
33
pelican/plugins/related_posts.py
Normal file
33
pelican/plugins/related_posts.py
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
from pelican import signals
|
||||
|
||||
"""
|
||||
Related posts plugin for Pelican
|
||||
================================
|
||||
|
||||
Adds related_posts variable to article's context
|
||||
"""
|
||||
related_posts = []
|
||||
def add_related_posts(generator, metadata):
|
||||
if 'tags' in metadata:
|
||||
for tag in metadata['tags']:
|
||||
#print tag
|
||||
for related_article in generator.tags[tag]:
|
||||
related_posts.append(related_article)
|
||||
|
||||
if len(related_posts) < 1:
|
||||
return
|
||||
|
||||
relation_score = dict( \
|
||||
zip(set(related_posts), \
|
||||
map(related_posts.count, \
|
||||
set(related_posts))))
|
||||
ranked_related = sorted(relation_score, key=relation_score.get)
|
||||
ranked_related.reverse()
|
||||
|
||||
metadata["related_posts"] = ranked_related[:5]
|
||||
|
||||
else:
|
||||
return
|
||||
|
||||
def register():
|
||||
signals.article_generate_context.connect(add_related_posts)
|
||||
Loading…
Add table
Add a link
Reference in a new issue