Skip to content
Permalink
Browse files

Added sidebar plugin.

  • Loading branch information
felixfontein committed Jan 6, 2017
1 parent c5b814a commit 8ab3dd7ae48d7ef7a1fe20f9d64380b04e93edcc
@@ -0,0 +1,5 @@
This plugin renders a sidebar ``output/sidebar-LANG.inc`` defined by a template ``sidebar.tmpl``. The sidebar can contain the most recent posts, all categories, all tags, the archives, all sections, and other taxonomies and arbitrary other content. The packaged ``sidebar.tmpl`` and ``sidebar-helper.tmpl`` shows how this information can be shown.

The generated include file, one per language, must be somehow included in the rest of the blog. This can be done by using JavaScript to dynamically include the file, or by using tools like `File Tree Subs <https://github.com/felixfontein/filetreesubs/>`__.
The number of most recent posts can be changed by defining ``SIDEBAR_MAXIMAL_POST_COUNT = number`` in the Nikola configuration.
@@ -0,0 +1,12 @@
[Core]
Name = sidebar
Module = sidebar

[Documentation]
Author = Felix Fontein
Version = 1.0
Website = https://felix.fontein.de
Description = Sidebar include renderer.

[Nikola]
plugincategory = Task
@@ -0,0 +1,155 @@
# -*- coding: utf-8 -*-

# Copyright © 2014-2017 Felix Fontein
#
# Permission is hereby granted, free of charge, to any
# person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the
# Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the
# Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice
# shall be included in all copies or substantial portions of
# the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

"""Render a sidebar to be included in all pages."""

from nikola.plugin_categories import Task
from nikola import utils

import natsort
import os
import os.path

_LOGGER = utils.get_logger('render_sidebar', utils.STDERR_HANDLER)


class RenderSidebar(Task):
"""Render a sidebar."""

name = "render_sidebar"

def set_site(self, site):
"""Set site."""
super(RenderSidebar, self).set_site(site)

def _build_post_list(self, lang, max_count):
"""Build list of the at most ``max_count`` most recent posts for the given language."""
posts = list(self.site.posts)
posts = sorted(posts, key=lambda post: post.date)
posts.reverse()
if not self.site.config['SHOW_UNTRANSLATED_POSTS']:
posts = [post for post in posts if post.is_translation_available(lang)]
return posts[:max_count]

def _build_taxonomy_list_and_hierarchy(self, taxonomy_name, lang):
"""Build taxonomy list and hierarchy for the given taxnonmy name and language."""
if taxonomy_name not in self.site.posts_per_classification or taxonomy_name not in self.site.taxonomy_plugins:
return None, None
posts_per_tag = self.site.posts_per_classification[taxonomy_name][lang]
taxonomy = self.site.taxonomy_plugins[taxonomy_name]

def acceptor(post):
return True if self.site.config['SHOW_UNTRANSLATED_POSTS'] else post.is_translation_available(lang)

# Build classification list
classifications = [(taxonomy.get_classification_friendly_name(tag, lang, only_last_component=False), tag) for tag in posts_per_tag.keys()]
if classifications:
# Sort classifications
classifications = natsort.humansorted(classifications)
# Build items list
result = list()
for classification_name, classification in classifications:
count = len([post for post in posts_per_tag[classification] if acceptor(post)])
result.append((classification_name, count, self.site.link(taxonomy_name, classification, lang)))
# Build hierarchy
if taxonomy.has_hierarchy:
# Special post-processing for archives: get rid of root and cut off tree at month level
if taxonomy_name == 'archive':
root_list = self.site.hierarchy_per_classification[taxonomy_name][lang]
root_list = utils.clone_treenode(root_list[0]).children

def cut_depth(node, cutoff):
if cutoff <= 1:
node.children = []
else:
for node in node.children:
cut_depth(node, cutoff - 1)

def invert_order(node):
node.children.reverse()
for node in node.children:
invert_order(node)

# Make sure that days don't creep in
for node in root_list:
cut_depth(node, 2)
invert_order(node)
root_list.reverse()
flat_hierarchy = utils.flatten_tree_structure(root_list)
else:
flat_hierarchy = self.site.flat_hierarchy_per_classification[taxonomy_name][lang]
# Build flattened hierarchy list
hierarchy = [(taxonomy.get_classification_friendly_name(node.classification_name, lang, only_last_component=False),
node.classification_name, node.classification_path,
self.site.link(taxonomy_name, node.classification_name, lang),
node.indent_levels, node.indent_change_before,
node.indent_change_after,
len(node.children),
len([post for post in posts_per_tag[node.classification_name] if acceptor(post)]))
for node in flat_hierarchy]
else:
# Fake hierarchy
hierarchy = [(classification_name, classification, taxonomy.extract_hierarchy(classification),
self.site.link(taxonomy_name, classification, lang),
(i, len(result)), 1 if i == 0 else 0, -1 if i == len(result) - 1 else 0, 0,
count)
for i, ((_, count, link), (classification_name, classification)) in enumerate(zip(result, classifications))]
return result, hierarchy
else:
return None, None

def _prepare_sidebar(self, destination, lang, template):
"""Generates the sidebar task for the given language."""
context = {}
deps_dict = {}

posts = self._build_post_list(lang, self.site.config.get("SIDEBAR_MAXIMAL_POST_COUNT", 10))
context['global_posts'] = posts
deps_dict['global_posts'] = [(post.permalink(lang), post.title(lang), post.date) for post in posts]

for taxonomy in self.site.taxonomy_plugins.keys():
taxonomy_items, taxonomy_hierarchy = self._build_taxonomy_list_and_hierarchy(taxonomy, lang)
context['global_{}_items'.format(taxonomy)] = taxonomy_items
context['global_{}_hierarchy'.format(taxonomy)] = taxonomy_hierarchy
deps_dict['global_{}_hierarchy'.format(taxonomy)] = taxonomy_hierarchy

url_type = self.site.config['URL_TYPE']
if url_type == 'rel_path':
url_type = 'full_path'

task = self.site.generic_renderer(lang, destination, template, self.site.config['FILTERS'], context=context, context_deps_remove=['global_posts'], post_deps_dict=deps_dict, url_type=url_type, is_fragment=True)
task['basename'] = self.name
yield task

def gen_tasks(self):
"""Generate tasks."""
self.site.scan_posts()
yield self.group_task()

for lang in self.site.config['TRANSLATIONS'].keys():
destination = os.path.join(self.site.config['OUTPUT_FOLDER'], 'sidebar-{0}.inc'.format(lang))
template = 'sidebar.tmpl'
yield self._prepare_sidebar(destination, lang, template)
@@ -0,0 +1,55 @@
{# -*- coding: utf-8 -*- #}

{% macro posts(posts) %}
<div id="global-recent-posts">
<h2>
{{ messages("Recent Posts", lang) }}
</h2>
<ul class="post-list">
{% for post in posts %}
<li>
<a href="{{ post.permalink() }}" class="listtitle">{{ post.title(lang) }}</a>
</li>
{% endfor %}
</ul>
</div>
{% endmacro %}

{% macro list_taxonomy(title, items=None, hierarchy=None, type='tag') %}
{% if items or hierarchy %}
<div id="global-{{ type }}-list">
<h2>
{{ messages(title, lang) }}
</h2>
{% if hierarchy %}
{% for text, full_name, path, link, indent_levels, indent_change_before, indent_change_after, children_count, count in hierarchy %}
{% for i in range(indent_change_before) %}
<ul class="{{ type }}-level {{ type }}-level-{{ indent_levels|length + i }}">
{% endfor %}
<li><a class="reference" href="{{ link }}">{{ text|e }}</a> ({{ count }})
{% if indent_change_after <= 0 %}
</li>
{% endif %}
{% for i in range(-indent_change_after) %}
</ul>
{% if i + 1 < indent_levels|length %}
</li>
{% endif %}
{% endfor %}
{% endfor %}
{% elif items %}
<ul>
{% for text, count, link in items %}
{% if text %}
<li><a class="reference" href="{{ link }}">{{ text|e }}</a> ({{ count }})</li>
{% endif %}
{% endfor %}
</ul>
{% endif %}
</div>
{% endif %}
{% endmacro %}

{% macro archives(archives) %}
{{ list_taxonomy('Archives', hierarchy=archives, type='archive') }}
{% endmacro %}
@@ -0,0 +1,23 @@
{# -*- coding: utf-8 -*- #}
{% import 'sidebar-helper.tmpl' as helper with context %}

<div>
<h2>
About.
</h2>
<div>
This is a blog.
</div>
</div>
{% if global_posts %}
{{ helper.posts(global_posts) }}
{% endif %}
{% if global_category_items %}
{{ helper.list_taxonomy("Categories", global_category_items, global_category_hierarchy, type='category') }}
{% endif %}
{% if global_archive_hierarchy %}
{{ helper.archives(global_archive_hierarchy) }}
{% endif %}
{% if global_tag_items %}
{{ helper.list_taxonomy("Tags", global_tag_items, type='tag') }}
{% endif %}
@@ -0,0 +1,55 @@
## -*- coding: utf-8 -*-

<%def name="posts(posts)">
<div id="global-recent-posts">
<h2>
${ messages("Recent Posts", lang) }
</h2>
<ul class="post-list">
% for post in posts:
<li>
<a href="${ post.permalink() }" class="listtitle">${ post.title(lang) }</a>
</li>
% endfor
</ul>
</div>
</%def>

<%def name="list_taxonomy(title, items=None, hierarchy=None, type='tag')">
% if items or hierarchy:
<div id="global-${ type }-list">
<h2>
${ messages(title, lang) }
</h2>
% if hierarchy:
% for text, full_name, path, link, indent_levels, indent_change_before, indent_change_after, children_count, count in hierarchy:
% for i in range(indent_change_before):
<ul class="${ type }-level ${ type }-level-${ len(indent_levels) + i }">
% endfor
<li><a class="reference" href="${ link }">${ text|h }</a> (${ count })
% if indent_change_after <= 0:
</li>
% endif
% for i in range(-indent_change_after):
</ul>
% if i + 1 < len(indent_levels):
</li>
% endif
% endfor
% endfor
% elif items:
<ul>
% for text, count, link in items:
% if text:
<li><a class="reference" href="${ link }">${ text|h }</a> (${ count })</li>
% endif
% endfor
</ul>
% endif
</div>
% endif
</%def>

<%def name="archives(archives)">
${ list_taxonomy('Archives', hierarchy=archives, type='archive') }
</%def>
@@ -0,0 +1,23 @@
## -*- coding: utf-8 -*-
<%namespace name="helper" file="sidebar-helper.tmpl"/>

<div>
<h2>
About.
</h2>
<div>
This is a blog.
</div>
</div>
% if global_posts:
${ helper.posts(global_posts) }
% endif
% if global_category_items:
${ helper.list_taxonomy("Categories", global_category_items, global_category_hierarchy, type='category') }}
% endif
% if global_archive_hierarchy:
${ helper.archives(global_archive_hierarchy) }
% endif
% if global_tag_items:
${ helper.list_taxonomy("Tags", global_tag_items, type='tag') }
% endif

0 comments on commit 8ab3dd7

Please sign in to comment.
You can’t perform that action at this time.