diff --git a/docs/settings.rst b/docs/settings.rst index e58243bc..0b70e461 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -60,6 +60,8 @@ Setting name what it does ? `STATIC_THEME_PATHS` Static theme paths you want to copy. Default values is `static`, but if your theme have others static paths, you can put them here. +`TAG_CLOUD_STEPS` Count of different font sizes in the tag cloud. +`TAG_CLOUD_MAX_ITEMS` Maximum tags count in the cloud. `THEME` theme to use to product the output. can be the complete static path to a theme folder, or chosen between the list of default themes (see below) diff --git a/pelican/generators.py b/pelican/generators.py index 88ee6919..1f72b87e 100755 --- a/pelican/generators.py +++ b/pelican/generators.py @@ -1,9 +1,11 @@ -from operator import attrgetter +from operator import attrgetter, itemgetter from itertools import chain from functools import partial from datetime import datetime from collections import defaultdict import os +import math +import random from jinja2 import Environment, FileSystemLoader from jinja2.exceptions import TemplateNotFound @@ -193,8 +195,34 @@ class ArticlesGenerator(Generator): self.dates = list(self.articles) self.dates.sort(key=attrgetter('date'), reverse=self.context['REVERSE_ARCHIVE_ORDER']) + + # create tag cloud + tag_cloud = defaultdict(int) + for article in self.articles: + for tag in 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')] + + max_count = max(map(itemgetter(1), tag_cloud)) + 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)) + ) + ) + for tag, count in tag_cloud + ] + # put words in chaos + random.shuffle(self.tag_cloud) + # and generate the output :) - self._update_context(('articles', 'dates', 'tags', 'categories')) + self._update_context(('articles', 'dates', 'tags', 'categories', 'tag_cloud')) def generate_output(self, writer): self.generate_feeds(writer) diff --git a/pelican/settings.py b/pelican/settings.py index 8f0dcdbd..d931cf02 100644 --- a/pelican/settings.py +++ b/pelican/settings.py @@ -22,6 +22,8 @@ _DEFAULT_CONFIG = {'PATH': None, 'CLEAN_URLS': False, # use /blah/ instead /blah.html in urls 'RELATIVE_URLS': True, 'DEFAULT_LANG': 'en', + 'TAG_CLOUD_STEPS': 4, + 'TAG_CLOUD_MAX_ITEMS': 100, } def read_settings(filename):