From cc6d73b541d21e4d2d8a7ace7e453715099e281c Mon Sep 17 00:00:00 2001 From: Kenshi Kawaguchi Date: Tue, 23 Dec 2014 14:06:21 -0700 Subject: [PATCH 1/2] Extract tag_cloud generation into its own method --- pelican/generators.py | 55 +++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/pelican/generators.py b/pelican/generators.py index 5122fa6d..025f6a01 100644 --- a/pelican/generators.py +++ b/pelican/generators.py @@ -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""" From 8a5b09d6f7fdb9bef39e74be1b0b8d2a886711d9 Mon Sep 17 00:00:00 2001 From: Kenshi Kawaguchi Date: Tue, 23 Dec 2014 14:12:39 -0700 Subject: [PATCH 2/2] Whitespace --- pelican/generators.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pelican/generators.py b/pelican/generators.py index 025f6a01..592c2aef 100644 --- a/pelican/generators.py +++ b/pelican/generators.py @@ -596,7 +596,7 @@ class ArticlesGenerator(CachingGenerator): ( tag, int(math.floor(steps - (steps - 1) * math.log(count) - / (math.log(max_count)or 1))) + / (math.log(max_count) or 1))) ) for tag, count in tag_cloud ]