diff --git a/blag/blag.py b/blag/blag.py index 7d4393b..884025c 100644 --- a/blag/blag.py +++ b/blag/blag.py @@ -133,6 +133,8 @@ def build(args): page_template = env.get_template('page.html') article_template = env.get_template('article.html') archive_template = env.get_template('archive.html') + tags_template = env.get_template('tags.html') + tag_template = env.get_template('tag.html') articles, pages = process_markdown( convertibles, @@ -150,6 +152,7 @@ def build(args): blog_author=config['author'], ) generate_archive(articles, archive_template, args.output_dir) + generate_tags(articles, tags_template, tag_template, args.output_dir) def process_markdown(convertibles, input_dir, output_dir, @@ -247,6 +250,39 @@ def generate_archive(articles, template, output_dir): fh.write(result) +def generate_tags(articles, tags_template, tag_template, output_dir): + os.makedirs(f'{output_dir}/tags', exist_ok=True) + + # get tags number of occurrences + all_tags = {} + for _, context in articles: + tags = context.get('tags', None) + for tag in tags: + all_tags[tag] = all_tags.get(tag, 0) + 1 + # sort by occurrence + all_tags = sorted(all_tags.items(), key=lambda x: x[1], reverse=True) + + result = tags_template.render(dict(tags=all_tags)) + with open(f'{output_dir}/tags/index.html', 'w') as fh: + fh.write(result) + + # get tags and archive per tag + all_tags = {} + for dst, context in articles: + tags = context.get('tags', None) + for tag in tags: + archive = all_tags.get(tag, []) + entry = context.copy() + entry['dst'] = dst + archive.append(entry) + all_tags[tag] = archive + + for tag, archive in all_tags.items(): + result = tag_template.render(dict(archive=archive, tag=tag)) + with open(f'{output_dir}/tags/{tag}.html', 'w') as fh: + fh.write(result) + + def quickstart(args): base_url = input("Hostname (and path) to the root? " "[https://example.com/]: ") diff --git a/blag/templates/tag.html b/blag/templates/tag.html new file mode 100644 index 0000000..198dc67 --- /dev/null +++ b/blag/templates/tag.html @@ -0,0 +1,15 @@ +{% extends "base.html" %} + +{% block title %}Tag {{ tag }}{% endblock %} + +{% block content %} +{% for entry in archive %} + + {% if entry.title %} +
Written on {{ entry.date.date() }}.
+ +{% endfor %} +{% endblock %} diff --git a/blag/templates/tags.html b/blag/templates/tags.html new file mode 100644 index 0000000..0a3ed10 --- /dev/null +++ b/blag/templates/tags.html @@ -0,0 +1,14 @@ +{% extends "base.html" %} + +{% block title %}Tags{% endblock %} + +{% block content %} +