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
|
|
@ -346,37 +346,8 @@ class ArticlesGenerator(Generator):
|
||||||
|
|
||||||
self.authors = list(self.authors.items())
|
self.authors = list(self.authors.items())
|
||||||
self.authors.sort(key=lambda item: item[0].name)
|
self.authors.sort(key=lambda item: item[0].name)
|
||||||
|
|
||||||
# related_posts
|
|
||||||
for article in self.articles:
|
|
||||||
article.related_posts = []
|
|
||||||
relation_score = {}
|
|
||||||
try:
|
|
||||||
article_tags = article.tags
|
|
||||||
for tag in article_tags:
|
|
||||||
for related_article in self.tags[tag]:
|
|
||||||
article.related_posts.append(related_article)
|
|
||||||
article.related_posts.reverse()
|
|
||||||
|
|
||||||
except: # can't find any related posts
|
|
||||||
pass
|
|
||||||
#article.related_posts = all_articles[:5].reverse()
|
|
||||||
|
|
||||||
relation_score = dict( \
|
|
||||||
zip(set(article.related_posts), \
|
|
||||||
map(article.related_posts.count, \
|
|
||||||
set(article.related_posts))))
|
|
||||||
|
|
||||||
ranked_related = sorted(relation_score, key=relation_score.get)
|
|
||||||
|
|
||||||
article.related_posts = ranked_related
|
|
||||||
|
|
||||||
try: # make sure article doesn't appear in its own related posts
|
|
||||||
article.related_posts.remove(article)
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
self._update_context(('articles', 'dates', 'tags', 'categories',
|
self._update_context(('articles', 'dates', 'tags', 'categories',
|
||||||
'tag_cloud', 'authors', 'related_posts'))
|
'tag_cloud', 'authors', 'related_posts'))
|
||||||
|
|
||||||
def generate_output(self, writer):
|
def generate_output(self, writer):
|
||||||
|
|
|
||||||
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