From b99e76d5df9039dbecac0e84a7840b0e1ce255b5 Mon Sep 17 00:00:00 2001 From: hede5562 Date: Fri, 22 May 2026 09:45:56 +0200 Subject: [PATCH] add back and forth buttons to articles --- blag/blag.py | 30 ++++++++++++++++++++++++++---- blag/templates/article.html | 22 ++++++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/blag/blag.py b/blag/blag.py index 99b70e2..c5ea1db 100644 --- a/blag/blag.py +++ b/blag/blag.py @@ -335,17 +335,39 @@ def process_markdown( # everything else are just pages if meta and "date" in meta: articles.append((dst, context)) - result = article_template.render(context) + # articles will be rendered after sorting, + # to add next&prev metadata else: pages.append((dst, context)) + # we are can already render pages (no sorting needed) result = page_template.render(context) - with open(f"{output_dir}/{dst}", "w") as fh_dest: - fh_dest.write(result) + with open(f"{output_dir}/{dst}", "w") as fh_dest: + fh_dest.write(result) # sort articles by date, descending articles = sorted(articles, key=lambda x: x[1]["date"], reverse=True) - return articles, pages + # with the sorted list of articles + # add metadata for previous and next pages + for i in range(0,len(articles)): + + if (i>0): + articles[i][1]["prv"]="/" + articles[i-1][0] + else: + articles[i][1]["prv"]="/" + + if (i<(len(articles)-1)): + articles[i][1]["nxt"]="/" + articles[i+1][0] + else: + articles[i][1]["nxt"]="/" + + # generate articles html files + result = article_template.render(articles[i][1]) + dst=articles[i][0] + with open(f"{output_dir}/{dst}", "w") as fh_dest: + fh_dest.write(result) + + return articles, pages def generate_feed( articles: list[tuple[str, dict[str, Any]]], diff --git a/blag/templates/article.html b/blag/templates/article.html index 889186d..ef76750 100644 --- a/blag/templates/article.html +++ b/blag/templates/article.html @@ -4,6 +4,17 @@ {% block content %} + {% if prv or nxt %} +
+ {% endif %} + {% if title %}

{{ title }}

{% endif %} @@ -24,4 +35,15 @@

{{ title }}

{{ content }} + {% if prv or nxt %} +
+ {% endif %} + {% endblock %}