1
0
mirror of https://github.com/venthur/blag.git synced 2025-11-25 20:52:43 +00:00

added tags

This commit is contained in:
Bastian Venthur
2021-02-09 21:56:44 +01:00
parent 5c06af6d0e
commit a4d7360d8c
3 changed files with 65 additions and 0 deletions

View File

@@ -133,6 +133,8 @@ def build(args):
page_template = env.get_template('page.html') page_template = env.get_template('page.html')
article_template = env.get_template('article.html') article_template = env.get_template('article.html')
archive_template = env.get_template('archive.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( articles, pages = process_markdown(
convertibles, convertibles,
@@ -150,6 +152,7 @@ def build(args):
blog_author=config['author'], blog_author=config['author'],
) )
generate_archive(articles, archive_template, args.output_dir) 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, def process_markdown(convertibles, input_dir, output_dir,
@@ -247,6 +250,39 @@ def generate_archive(articles, template, output_dir):
fh.write(result) 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): def quickstart(args):
base_url = input("Hostname (and path) to the root? " base_url = input("Hostname (and path) to the root? "
"[https://example.com/]: ") "[https://example.com/]: ")

15
blag/templates/tag.html Normal file
View File

@@ -0,0 +1,15 @@
{% extends "base.html" %}
{% block title %}Tag {{ tag }}{% endblock %}
{% block content %}
{% for entry in archive %}
{% if entry.title %}
<h1><a href="/{{entry.dst}}">{{entry.title}}</a></h1>
{% endif %}
<p>Written on {{ entry.date.date() }}.</p>
{% endfor %}
{% endblock %}

14
blag/templates/tags.html Normal file
View File

@@ -0,0 +1,14 @@
{% extends "base.html" %}
{% block title %}Tags{% endblock %}
{% block content %}
<h2>Tags</h2>
<ul>
{% for tag, size in tags %}
<li>
<a href="{{ tag }}.html">{{ tag }} ({{ size }} articles)</a>
</li>
{% endfor %}
</ul>
{% endblock %}