From 7bc7b8789f1ae7780be526bc488bbfed140b7ffc Mon Sep 17 00:00:00 2001 From: squid Date: Thu, 12 Nov 2026 18:41:38 +0100 Subject: [PATCH] feat: add an RSS 2.0 feed for the news - build /news/feed.xml and /it/news/feed.xml with the latest 20 posts, full text and absolute links - add the feed autodiscovery link to every page and a visible link on the news pages - document the feed in the README --- README.md | 2 +- build.py | 40 ++++++++++++++++++++++++++++++++++++++++ content/en/news.html | 4 ++++ content/it/news.html | 4 ++++ templates/base.html | 1 + 5 files changed, 50 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5df8087..09f6bce 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ A post is a Markdown file named `YYYY-MM-DD-slug.md` in `content//news/`: ## A heading -The post is published at `/news/slug.html` (`/it/news/slug.html` for Italian). A post with the same file name in both languages is linked as a translation. The news index and the latest three posts on the home page are built from the files. No em dashes: the build refuses them. +The post is published at `/news/slug.html` (`/it/news/slug.html` for Italian). A post with the same file name in both languages is linked as a translation. The news index, the latest three posts on the home page and an RSS 2.0 feed (`/news/feed.xml`, `/it/news/feed.xml`, the latest 20 posts with full text) are built from the files. Every page links its feed in the head. No em dashes: the build refuses them. ## Publishing diff --git a/build.py b/build.py index 4661e3a..20f0e7e 100644 --- a/build.py +++ b/build.py @@ -22,12 +22,16 @@ A page starts with a front matter block: Placeholders {{name}} are replaced with values from site.toml, lang.toml and the page itself. An unknown placeholder stops the build.""" import html, os, re, shutil, sys, tomllib +from email.utils import format_datetime +from datetime import datetime, timezone +from xml.sax.saxutils import escape as xml_escape HERE = os.path.dirname(os.path.abspath(__file__)) OUT = os.path.abspath(sys.argv[1] if len(sys.argv) > 1 else os.path.join(HERE, "preview")) LANGS = ("en", "it") NAV = ("home", "manifesto", "people", "services", "news") NEWS_ON_HOME = 3 +NEWS_IN_FEED = 20 POST_NAME = re.compile(r"^(\d{4}-\d{2}-\d{2})-([a-z0-9]+(?:-[a-z0-9]+)*)$") # Files copied as they are: published name -> source. @@ -216,6 +220,39 @@ def load_pages(lang): return pages +def rss(lang, st, site, lang_pages): + """RSS 2.0 feed of the news of one language: the latest posts, full text, absolute links.""" + base = site["base_url"] + posts = sorted((p for p in lang_pages if p["kind"] == "post"), key=lambda p: p["date"], reverse=True)[:NEWS_IN_FEED] + + def stamp(d): + return format_datetime(datetime.strptime(d, "%Y-%m-%d").replace(tzinfo=timezone.utc)) + items = [] + for p in posts: + text = md_to_html(p["body"])[2].replace('href="/', f'href="{base}/') + items.append(f""" +{xml_escape(p["title"])} +{base}{p["path"]} +{base}{p["path"]} +{stamp(p["date"])} +{xml_escape(text)} + +""") + built = f"{stamp(posts[0]['date'])}\n" if posts else "" + url = f"{base}{st['url_news']}" + return f""" + + +kmalloc.org: {xml_escape(st["nav_news"])} +{url} +{xml_escape(st["news_p"])} +{lang} +{built} +{"".join(items)} + +""" + + def build(): css = read("static", "style.css").strip("\n") tpl = {n[:-5]: read("templates", n) for n in os.listdir(os.path.join(HERE, "templates")) if n.endswith(".html")} @@ -288,6 +325,9 @@ def build(): rel = p["path"].lstrip("/") write(rel + "index.html" if rel == "" or rel.endswith("/") else rel, render(tpl["base"], ctx, "templates/base.html")) + for lang in LANGS: + write(strings[lang]["url_news"].lstrip("/") + "feed.xml", rss(lang, strings[lang], site, pages[lang])) + for dest, src in ASSETS.items(): target = os.path.join(OUT, dest) os.makedirs(os.path.dirname(target), exist_ok=True) diff --git a/content/en/news.html b/content/en/news.html index db62c33..fa0bcf7 100644 --- a/content/en/news.html +++ b/content/en/news.html @@ -12,3 +12,7 @@ Updates from kmalloc.org.

{{news_list}} + +

+RSS feed +

diff --git a/content/it/news.html b/content/it/news.html index 1c5a41e..7b9be50 100644 --- a/content/it/news.html +++ b/content/it/news.html @@ -12,3 +12,7 @@ Novità da kmalloc.org.

{{news_list}} + +

+feed RSS +

diff --git a/templates/base.html b/templates/base.html index 822100f..8fb65a6 100644 --- a/templates/base.html +++ b/templates/base.html @@ -10,6 +10,7 @@ {{alt_links}} +