mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Extract tag_cloud generation into its own method
This commit is contained in:
parent
7e961061e1
commit
cc6d73b541
1 changed files with 29 additions and 24 deletions
|
|
@ -553,31 +553,7 @@ class ArticlesGenerator(CachingGenerator):
|
|||
self.dates.sort(key=attrgetter('date'),
|
||||
reverse=self.context['NEWEST_FIRST_ARCHIVES'])
|
||||
|
||||
# create tag cloud
|
||||
tag_cloud = defaultdict(int)
|
||||
for article in self.articles:
|
||||
for tag in getattr(article, 'tags', []):
|
||||
tag_cloud[tag] += 1
|
||||
|
||||
tag_cloud = sorted(tag_cloud.items(), key=itemgetter(1), reverse=True)
|
||||
tag_cloud = tag_cloud[:self.settings.get('TAG_CLOUD_MAX_ITEMS')]
|
||||
|
||||
tags = list(map(itemgetter(1), tag_cloud))
|
||||
if tags:
|
||||
max_count = max(tags)
|
||||
steps = self.settings.get('TAG_CLOUD_STEPS')
|
||||
|
||||
# calculate word sizes
|
||||
self.tag_cloud = [
|
||||
(
|
||||
tag,
|
||||
int(math.floor(steps - (steps - 1) * math.log(count)
|
||||
/ (math.log(max_count)or 1)))
|
||||
)
|
||||
for tag, count in tag_cloud
|
||||
]
|
||||
# put words in chaos
|
||||
random.shuffle(self.tag_cloud)
|
||||
self.tag_cloud = self._tag_cloud_from_articles(self.articles)
|
||||
|
||||
# and generate the output :)
|
||||
|
||||
|
|
@ -601,6 +577,35 @@ class ArticlesGenerator(CachingGenerator):
|
|||
signals.article_writer_finalized.send(self, writer=writer)
|
||||
|
||||
|
||||
def _tag_cloud_from_articles(self, articles):
|
||||
tag_cloud = defaultdict(int)
|
||||
for article in articles:
|
||||
for tag in getattr(article, 'tags', []):
|
||||
tag_cloud[tag] += 1
|
||||
|
||||
tag_cloud = sorted(tag_cloud.items(), key=itemgetter(1), reverse=True)
|
||||
tag_cloud = tag_cloud[:self.settings.get('TAG_CLOUD_MAX_ITEMS')]
|
||||
|
||||
tags = list(map(itemgetter(1), tag_cloud))
|
||||
if tags:
|
||||
max_count = max(tags)
|
||||
steps = self.settings.get('TAG_CLOUD_STEPS')
|
||||
|
||||
# calculate word sizes
|
||||
tag_cloud = [
|
||||
(
|
||||
tag,
|
||||
int(math.floor(steps - (steps - 1) * math.log(count)
|
||||
/ (math.log(max_count)or 1)))
|
||||
)
|
||||
for tag, count in tag_cloud
|
||||
]
|
||||
# put words in chaos
|
||||
random.shuffle(tag_cloud)
|
||||
|
||||
return tag_cloud
|
||||
|
||||
|
||||
class PagesGenerator(CachingGenerator):
|
||||
"""Generate pages"""
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue