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
5 changed files with 50 additions and 1 deletions
|
|
@ -43,7 +43,7 @@ A post is a Markdown file named `YYYY-MM-DD-slug.md` in `content/<lang>/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
|
||||
|
||||
|
|
|
|||
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)
|
||||
|
|
|
|||
|
|
@ -12,3 +12,7 @@ Updates from kmalloc.org.
|
|||
</p>
|
||||
|
||||
{{news_list}}
|
||||
|
||||
<p>
|
||||
<a href="{{url_news}}feed.xml">RSS feed</a>
|
||||
</p>
|
||||
|
|
|
|||
|
|
@ -12,3 +12,7 @@ Novità da kmalloc.org.
|
|||
</p>
|
||||
|
||||
{{news_list}}
|
||||
|
||||
<p>
|
||||
<a href="{{url_news}}feed.xml">feed RSS</a>
|
||||
</p>
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@
|
|||
<link rel="icon" href="/favicon.ico" sizes="any">
|
||||
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
|
||||
{{alt_links}}
|
||||
<link rel="alternate" type="application/rss+xml" title="kmalloc.org: {{nav_news}}" href="{{url_news}}feed.xml">
|
||||
<style>
|
||||
{{css}}
|
||||
</style>
|
||||
|
|
|
|||
Loading…
Reference in a new issue