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
This commit is contained in:
parent
c8b7b6a571
commit
7bc7b8789f
1 changed files with 50 additions and 1 deletions
40
build.py
40
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"""<item>
|
||||
<title>{xml_escape(p["title"])}</title>
|
||||
<link>{base}{p["path"]}</link>
|
||||
<guid isPermaLink="true">{base}{p["path"]}</guid>
|
||||
<pubDate>{stamp(p["date"])}</pubDate>
|
||||
<description>{xml_escape(text)}</description>
|
||||
</item>
|
||||
""")
|
||||
built = f"<lastBuildDate>{stamp(posts[0]['date'])}</lastBuildDate>\n" if posts else ""
|
||||
url = f"{base}{st['url_news']}"
|
||||
return f"""<?xml version="1.0" encoding="utf-8"?>
|
||||
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
|
||||
<channel>
|
||||
<title>kmalloc.org: {xml_escape(st["nav_news"])}</title>
|
||||
<link>{url}</link>
|
||||
<description>{xml_escape(st["news_p"])}</description>
|
||||
<language>{lang}</language>
|
||||
{built}<atom:link href="{url}feed.xml" rel="self" type="application/rss+xml"/>
|
||||
{"".join(items)}</channel>
|
||||
</rss>
|
||||
"""
|
||||
|
||||
|
||||
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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue