Skip to content
Snippets Groups Projects
Commit 43e5c9fd authored by Vincent MAZENOD's avatar Vincent MAZENOD
Browse files

Merge branch 'working-docker' into 'master'

Working docker

See merge request !4
parents 841ad440 b4ca4b82
No related branches found
No related tags found
1 merge request!4Working docker
Pipeline #9339 passed
tag_cloud
=========
This plugin generates a tag-cloud.
Installation
------------
In order to use to use this plugin, you have to edit(*) or create(+) the following files::
blog/
├── pelicanconf.py *
├── content
├── plugins +
│ └── tag_cloud.py +
└── themes
└── mytheme
├── templates
│ └── base.html *
└── static
└── css
└── style.css *
In **pelicanconf.py** you have to activate the plugin::
PLUGIN_PATHS = ["plugins"]
PLUGINS = ["tag_cloud"]
Into your **plugins** folder, you should add tag_cloud.py (from this repository).
In your theme files, you should change **base.html** to apply formats (and sizes) defined in **style.css**, as specified in "Settings", below.
Settings
--------
================================================ =====================================================
Setting name (followed by default value) What does it do?
================================================ =====================================================
``TAG_CLOUD_STEPS = 4`` Count of different font sizes in the tag
cloud.
``TAG_CLOUD_MAX_ITEMS = 100`` Maximum number of tags in the cloud.
``TAG_CLOUD_SORTING = 'random'`` The tag cloud ordering scheme. Valid values:
random, alphabetically, alphabetically-rev, size and
size-rev
``TAG_CLOUD_BADGE = True`` Optionnal setting : can bring **badges**, which mean
say : display the number of each tags present
on all articles.
================================================ =====================================================
The default theme does not include a tag cloud, but it is pretty easy to add one::
<ul class="tagcloud">
{% for tag in tag_cloud %}
<li class="tag-{{ tag.1 }}">
<a href="{{ SITEURL }}/{{ tag.0.url }}">
{{ tag.0 }}
{% if TAG_CLOUD_BADGE %}
<span class="badge">{{ tag.2 }}</span>
{% endif %}
</a>
</li>
{% endfor %}
</ul>
You should then also define CSS styles with appropriate classes (tag-1 to tag-N,
where N matches ``TAG_CLOUD_STEPS``), tag-1 being the most frequent, and
define a ``ul.tagcloud`` class with appropriate list-style to create the cloud.
You should copy/paste this **badge** CSS rule ``ul.tagcloud .list-group-item <span>.badge``
if you're using ``TAG_CLOUD_BADGE`` setting. (this rule, potentially long , is suggested to avoid
conflicts with CSS libs as twitter Bootstrap)
For example::
ul.tagcloud {
list-style: none;
padding: 0;
}
ul.tagcloud li {
display: inline-block;
}
li.tag-1 {
font-size: 150%;
}
li.tag-2 {
font-size: 120%;
}
...
ul.tagcloud .list-group-item <span>.badge {
background-color: grey;
color: white;
}
By default the tags in the cloud are sorted randomly, but if you prefers to have it alphabetically use the `alphabetically` (ascending) and `alphabetically-rev` (descending). Also is possible to sort the tags by it's size (number of articles with this specific tag) using the values `size` (ascending) and `size-rev` (descending).
\ No newline at end of file
from .tag_cloud import *
'''
tag_cloud
===================================
This plugin generates a tag cloud from available tags
'''
from __future__ import unicode_literals
from collections import defaultdict
from operator import itemgetter
import logging
import math
import random
from pelican import signals
logger = logging.getLogger(__name__)
def set_default_settings(settings):
settings.setdefault('TAG_CLOUD_STEPS', 4)
settings.setdefault('TAG_CLOUD_MAX_ITEMS', 100)
settings.setdefault('TAG_CLOUD_SORTING', 'random')
settings.setdefault('TAG_CLOUD_BADGE', False)
def init_default_config(pelican):
from pelican.settings import DEFAULT_CONFIG
set_default_settings(DEFAULT_CONFIG)
if(pelican):
set_default_settings(pelican.settings)
def generate_tag_cloud(generator):
tag_cloud = defaultdict(int)
for article in generator.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[:generator.settings.get('TAG_CLOUD_MAX_ITEMS')]
tags = list(map(itemgetter(1), tag_cloud))
if tags:
max_count = max(tags)
steps = generator.settings.get('TAG_CLOUD_STEPS')
# calculate word sizes
def generate_tag(tag, count):
tag = (
tag,
int(math.floor(steps - (steps - 1) * math.log(count)
/ (math.log(max_count)or 1)))
)
if generator.settings.get('TAG_CLOUD_BADGE'):
tag += (count,)
return tag
tag_cloud = [
generate_tag(tag, count)
for tag, count in tag_cloud
]
sorting = generator.settings.get('TAG_CLOUD_SORTING')
if sorting == 'alphabetically':
tag_cloud.sort(key=lambda elem: elem[0].name)
elif sorting == 'alphabetically-rev':
tag_cloud.sort(key=lambda elem: elem[0].name, reverse=True)
elif sorting == 'size':
tag_cloud.sort(key=lambda elem: elem[1])
elif sorting == 'size-rev':
tag_cloud.sort(key=lambda elem: elem[1], reverse=True)
elif sorting == 'random':
random.shuffle(tag_cloud)
else:
logger.warning("setting for TAG_CLOUD_SORTING not recognized: %s, "
"falling back to 'random'", sorting)
random.shuffle(tag_cloud)
# make available in context
generator.tag_cloud = tag_cloud
generator._update_context(['tag_cloud'])
def register():
signals.initialized.connect(init_default_config)
signals.article_generator_finalized.connect(generate_tag_cloud)
Title: Article1
tags: fun, pelican, plugins
content, yeah!
\ No newline at end of file
Title: Article2
tags: pelican, plugins, python
content2, yeah!
Title: Article3
tags: pelican, plugins
content3, yeah!
Title: Article4
tags: pelican, fun
content4, yeah!
Title: Article5
tags: plugins, pelican, fun
content5, yeah!
import unittest
import os
import six
import tag_cloud
from pelican.generators import ArticlesGenerator
from pelican.tests.support import get_settings
from pelican.urlwrappers import Tag
CUR_DIR = os.path.dirname(__file__)
CONTENT_DIR = os.path.join(CUR_DIR, 'test_data')
class TestTagCloudGeneration(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls._settings = get_settings(filenames={})
cls._settings['DEFAULT_CATEGORY'] = 'Default'
cls._settings['DEFAULT_DATE'] = (1970, 1, 1)
cls._settings['READERS'] = {'asc': None}
cls._settings['CACHE_CONTENT'] = False
tag_cloud.set_default_settings(cls._settings)
cls.generator = ArticlesGenerator(
context=cls._settings.copy(), settings=cls._settings,
path=CONTENT_DIR, theme=cls._settings['THEME'], output_path=None)
cls.generator.generate_context()
def test_tag_cloud_random(self):
self.generator.settings['TAG_CLOUD_STEPS'] = 10
self.generator.settings['TAG_CLOUD_BADGE'] = False
tag_cloud.generate_tag_cloud(self.generator)
expected = [
(Tag('pelican', self._settings), 1),
(Tag('plugins', self._settings), 2),
(Tag('fun', self._settings), 3),
(Tag('python', self._settings), 10)
]
six.assertCountEqual(self, self.generator.tag_cloud, expected)
def test_tag_cloud_badge(self):
self.generator.settings['TAG_CLOUD_STEPS'] = 10
self.generator.settings['TAG_CLOUD_BADGE'] = True
tag_cloud.generate_tag_cloud(self.generator)
expected = [
(Tag('pelican', self._settings), 1, 5),
(Tag('plugins', self._settings), 2, 4),
(Tag('fun', self._settings), 3, 3),
(Tag('python', self._settings), 10, 1)
]
six.assertCountEqual(self, self.generator.tag_cloud, expected)
def test_tag_cloud_alphabetical(self):
self.generator.settings['TAG_CLOUD_STEPS'] = 10
self.generator.settings['TAG_CLOUD_SORTING'] = 'alphabetically'
tag_cloud.generate_tag_cloud(self.generator)
expected = [
(Tag('fun', self._settings), 3),
(Tag('pelican', self._settings), 1),
(Tag('plugins', self._settings), 2),
(Tag('python', self._settings), 10)
]
self.assertEqual(self.generator.tag_cloud, expected)
def test_tag_cloud_alphabetical_rev(self):
self.generator.settings['TAG_CLOUD_STEPS'] = 10
self.generator.settings['TAG_CLOUD_SORTING'] = 'alphabetically-rev'
tag_cloud.generate_tag_cloud(self.generator)
expected = [
(Tag('python', self._settings), 10),
(Tag('plugins', self._settings), 2),
(Tag('pelican', self._settings), 1),
(Tag('fun', self._settings), 3)
]
self.assertEqual(self.generator.tag_cloud, expected)
def test_tag_cloud_size(self):
self.generator.settings['TAG_CLOUD_STEPS'] = 10
self.generator.settings['TAG_CLOUD_SORTING'] = 'size'
tag_cloud.generate_tag_cloud(self.generator)
expected = [
(Tag('pelican', self._settings), 1),
(Tag('plugins', self._settings), 2),
(Tag('fun', self._settings), 3),
(Tag('python', self._settings), 10)
]
self.assertEqual(self.generator.tag_cloud, expected)
def test_tag_cloud_size_rev(self):
self.generator.settings['TAG_CLOUD_STEPS'] = 10
self.generator.settings['TAG_CLOUD_SORTING'] = 'size-rev'
tag_cloud.generate_tag_cloud(self.generator)
expected = [
(Tag('python', self._settings), 10),
(Tag('fun', self._settings), 3),
(Tag('plugins', self._settings), 2),
(Tag('pelican', self._settings), 1)
]
self.assertEqual(self.generator.tag_cloud, expected)
if __name__ == "__main__":
unittest.main()
......@@ -2,7 +2,7 @@
<section class="well well-sm">
<ul class="list-group list-group-flush">
{% include 'includes/sidebar/oim.html' %}
{% include 'includes/sidebar/search.html' %}
{#% include 'includes/sidebar/search.html' %#}
{% include 'includes/sidebar/optional_top.html' ignore missing %}
{% include 'includes/sidebar/social.html' %}
{% include 'includes/sidebar/recent_posts.html' %}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment