aboutsummaryrefslogtreecommitdiff
path: root/feed.typ
blob: 3a5fd2110707f255e2aa5eef336a4f9e153a5728 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// RSS 2.0 and Atom feeds for the blog posts, emitted as feed.xml and
// 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 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"
    )
}

// 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 feed-xml(site-info) = {
    let items = _entries(site-info, (title, url, p) => {
        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>")
        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")
}

// Atom (RFC 4287) feed, emitted as atom.xml by main.typ.
#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")
}