aboutsummaryrefslogtreecommitdiff
path: root/feed.typ
blob: a72a5361b68da8fcedd4e5aec218fb21143ac333 (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
55
56
57
58
59
60
61
62
63
64
// Atom (RFC 4287) feed for the blog posts, emitted as atom.xml by
// main.typ. Entries 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 3339 ("2026-07-28T00:00:00Z").
#let rfc3339(iso) = iso + "T00:00:00Z"

#let _entries(site-info, item-fn) = {
    let pages = site-info.pages
    let ids = pages.keys().filter(k => k.starts-with("post/"))

    let entries = ()
    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"
        entries.push(item-fn(title, url, p))
    }
    entries
}

#let atom-xml(site-info) = {
    let entries = _entries(site-info, (title, url, p) => {
        let lines = (
            "  <entry>",
            "    <title>" + xml-escape(title) + "</title>",
            "    <link href=\"" + url + "\"/>",
            "    <id>" + url + "</id>",
        )
        if "date" in p {
            lines.push("    <updated>" + rfc3339(p.date) + "</updated>")
        }
        if "description" in p {
            lines.push("    <summary>" + xml-escape(p.description) + "</summary>")
        }
        lines.push("  </entry>")
        lines.join("\n")
    })

    // newest post first, so its date is the feed's last-update time
    let updated = rfc3339(site-info.pages.at(
        site-info.pages.keys().filter(k => k.starts-with("post/")).first()
    ).date)

    (
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
        "<feed xmlns=\"http://www.w3.org/2005/Atom\">",
        "  <title>" + xml-escape(site-info.title) + "</title>",
        "  <link href=\"" + baseurl + "/\"/>",
        "  <link rel=\"self\" href=\"" + baseurl + "/atom.xml\"/>",
        "  <id>" + baseurl + "/</id>",
        "  <updated>" + updated + "</updated>",
        "  <author>",
        "    <name>Kumar</name>",
        "  </author>",
        entries.join("\n"),
        "</feed>",
        "",
    ).join("\n")
}