Add tag index rendering

This commit is contained in:
2026-08-07 02:08:54 +02:00
parent 4bdf1b4f4e
commit f21e746858
2 changed files with 21 additions and 8 deletions
+10 -2
View File
@@ -115,6 +115,7 @@ class Site:
page_num: int, page_num: int,
total_pages: int, total_pages: int,
section: str | None, section: str | None,
tag: str | None = None,
) -> None: ) -> None:
template = self.jinja_env.get_template("index.html") template = self.jinja_env.get_template("index.html")
html = template.render( html = template.render(
@@ -123,6 +124,7 @@ class Site:
page_num=page_num, page_num=page_num,
total_pages=total_pages, total_pages=total_pages,
section=section, section=section,
tag=tag,
) )
out_file = out_dir / f"page-{page_num}.html" out_file = out_dir / f"page-{page_num}.html"
out_file.write_text(html, encoding="utf-8") out_file.write_text(html, encoding="utf-8")
@@ -132,16 +134,17 @@ class Site:
items: list[Content], items: list[Content],
out_dir: Path, out_dir: Path,
section: str | None, section: str | None,
tag: str | None = None,
) -> None: ) -> None:
out_dir.mkdir(parents=True, exist_ok=True) out_dir.mkdir(parents=True, exist_ok=True)
if not items: if not items:
log.warning("No content, generating empty index page") log.warning("No content, generating empty index page")
self.render_single_index_page(out_dir, (), 1, 1, section) self.render_single_index_page(out_dir, (), 1, 1, section, tag)
else: else:
num_pages = ceil(len(items) / self.items_per_page) num_pages = ceil(len(items) / self.items_per_page)
for i, page_items in enumerate(batched(items, self.items_per_page)): for i, page_items in enumerate(batched(items, self.items_per_page)):
self.render_single_index_page( self.render_single_index_page(
out_dir, page_items, i + 1, num_pages, section out_dir, page_items, i + 1, num_pages, section, tag
) )
index = out_dir / "index.html" index = out_dir / "index.html"
if index.exists() or index.is_symlink(): if index.exists() or index.is_symlink():
@@ -154,6 +157,11 @@ class Site:
for section, items in self.by_section.items(): for section, items in self.by_section.items():
sorted_items = sorted(items, key=lambda c: c.created, reverse=True) sorted_items = sorted(items, key=lambda c: c.created, reverse=True)
self.render_index_pages(sorted_items, self.output_path / section, section) self.render_index_pages(sorted_items, self.output_path / section, section)
for tag, items in self.by_tag.items():
sorted_items = sorted(items, key=lambda c: c.created, reverse=True)
self.render_index_pages(
sorted_items, self.output_path / "tags" / tag, None, tag
)
def build(self) -> None: def build(self) -> None:
if not self.origin: if not self.origin:
+11 -6
View File
@@ -1,11 +1,16 @@
{% extends "base.html" %} {% extends "base.html" %}
{% set prefix = (section + "/") if section else (("tags/" + tag + "/") if tag else "") %}
{% set heading = section or tag or "" %}
{% block title %}{% if section %}{{ section }} — {% endif %}{{ site.name }}{% if page_num > 1 %} — Page {{ page_num }}{% endif %}{% endblock %} {% block title %}{% if heading %}{{ heading }} — {% endif %}{{ site.name }}{% if page_num > 1 %} — Page {{ page_num }}{% endif %}{% endblock %}
{% block head %}<link rel="canonical" href="{{ site.origin }}{{ site.base_dir }}{% if section %}{{ section }}/{% endif %}{% if page_num == 1 %}{% else %}page-{{ page_num }}.html{% endif %}">{% endblock %} {% block head %}
<link rel="canonical" href="{{ site.origin }}{{ site.base_dir }}{{ prefix }}{% if page_num > 1 %}page-{{ page_num }}.html{% endif %}">
{% if page_num > 1 %}<meta name="robots" content="noindex, follow">{% endif %}
{% endblock %}
{% block main %} {% block main %}
{% if section %}<h1>{{ section }}</h1>{% endif %} {% if heading %}<h1>{{ heading }}</h1>{% endif %}
{% for item in items %} {% for item in items %}
<article> <article>
<header> <header>
@@ -18,15 +23,15 @@
{% if total_pages > 1 %} {% if total_pages > 1 %}
<nav> <nav>
{% if page_num > 1 %} {% if page_num > 1 %}
<a href="{{ site.base_dir }}{% if section %}{{ section }}/{% endif %}page-{{ page_num - 1 }}.html">← Newer</a> <a href="{{ site.base_dir }}{{ prefix }}page-{{ page_num - 1 }}.html">← Newer</a>
{% endif %} {% endif %}
{% for p in range(1, total_pages + 1) %} {% for p in range(1, total_pages + 1) %}
{% if p == page_num %}<strong>{{ p }}</strong> {% if p == page_num %}<strong>{{ p }}</strong>
{% else %}<a href="{{ site.base_dir }}{% if section %}{{ section }}/{% endif %}page-{{ p }}.html">{{ p }}</a> {% else %}<a href="{{ site.base_dir }}{{ prefix }}page-{{ p }}.html">{{ p }}</a>
{% endif %} {% endif %}
{% endfor %} {% endfor %}
{% if page_num < total_pages %} {% if page_num < total_pages %}
<a href="{{ site.base_dir }}{% if section %}{{ section }}/{% endif %}page-{{ page_num + 1 }}.html">Older →</a> <a href="{{ site.base_dir }}{{ prefix }}page-{{ page_num + 1 }}.html">Older →</a>
{% endif %} {% endif %}
</nav> </nav>
{% endif %} {% endif %}