aboutsummaryrefslogtreecommitdiff
path: root/feed.typ
blob: 75639e090bdffc7a03444e0aaa32a319354b8b02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// RSS 2.0 feed for the blog posts, emitted as feed.xml by main.typ.
// Items are built from the metadata in site-info.pages (newest first).

#import "/global/common.typ": baseurl

#let xml-escape(s) = s.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")

// ISO date ("2026-07-28") to RFC 822 ("Tue, 28 Jul 2026 00:00:00 GMT").
#let rfc822(iso) = {
    let (y, m, d) = iso.split("-").map(int)
    datetime(year: y, month: m, day: d).display(
        "[weekday repr:short], [day] [month repr:short] [year] 00:00:00 GMT"
    )
}

#let feed-xml(site-info) = {
    let pages = site-info.pages
    let ids = pages.keys().filter(k => k.starts-with("post/"))

    let items = ()
    for id in ids {
        let p = pages.at(id)
        let title = if "long-title" in p { p.long-title } else { p.title }
        let url = baseurl + "/" + id + ".html"

        let lines = (
            "  <item>",
            "    <title>" + xml-escape(title) + "</title>",
            "    <link>" + url + "</link>",
            "    <guid>" + url + "</guid>",
        )
        if "date" in p {
            lines.push("    <pubDate>" + rfc822(p.date) + "</pubDate>")
        }
        if "description" in p {
            lines.push("    <description>" + xml-escape(p.description) + "</description>")
        }
        lines.push("  </item>")
        items.push(lines.join("\n"))
    }

    (
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
        "<rss version=\"2.0\">",
        "<channel>",
        "  <title>" + xml-escape(site-info.title) + "</title>",
        "  <link>" + baseurl + "/</link>",
        "  <description>Posts from " + xml-escape(site-info.title) + " (" + baseurl + ")</description>",
        items.join("\n"),
        "</channel>",
        "</rss>",
        "",
    ).join("\n")
}