motorino/internal/build/feed.go
tgiachi 3f62c79603 feat(feed): carry the full text in content:encoded
The description of an item is now the plain summary of the post, the full text with
absolute links goes in content:encoded (the end marker of a CDATA section inside
the text is split), and an item has its categories and its author (dc:creator).

Refs #9
2026-11-16 12:09:03 +01:00

69 lines
2.7 KiB
Go

// SPDX-License-Identifier: GPL-3.0-or-later
package build
import (
"regexp"
"strings"
"time"
"git.kmalloc.org/squid/motorino/internal/render"
"git.kmalloc.org/squid/motorino/internal/site"
)
// siteRelative matches a link that starts at the root of the site, such as
// href="/people.html", but not one that is relative to the protocol (href="//host/x").
var siteRelative = regexp.MustCompile(`\b(href|src)="/([^/])`)
// cdata wraps text in a CDATA section; the end marker inside the text is split
// over two sections, so it cannot close the first one.
func cdata(text string) string {
return "<![CDATA[" + strings.ReplaceAll(text, "]]>", "]]]]><![CDATA[>") + "]]>"
}
// feed returns the RSS 2.0 feed of the latest limit posts, which must be sorted
// newest first. Each item has the description as a plain summary and the full
// text, with absolute links, in content:encoded. The channel is described by
// the language l, and newsURL is the address of the news page, such as "/news/".
func feed(base string, l site.Lang, newsURL string, posts []renderedPost, limit int) string {
if limit < len(posts) {
posts = posts[:limit]
}
var items strings.Builder
for _, p := range posts {
link := base + p.Path
text := siteRelative.ReplaceAllString(p.HTML, `$1="`+base+`/$2`)
items.WriteString("<item>\n")
items.WriteString("<title>" + render.Escape(p.Title) + "</title>\n")
items.WriteString("<link>" + link + "</link>\n")
items.WriteString(`<guid isPermaLink="true">` + link + "</guid>\n")
items.WriteString("<pubDate>" + p.Date.Format(time.RFC1123Z) + "</pubDate>\n")
if p.Description != "" {
items.WriteString("<description>" + render.Escape(p.Description) + "</description>\n")
}
if p.Author != "" {
items.WriteString("<dc:creator>" + render.Escape(p.Author) + "</dc:creator>\n")
}
for _, c := range p.Categories {
items.WriteString("<category>" + render.Escape(c.Name) + "</category>\n")
}
items.WriteString("<content:encoded>" + cdata(text) + "</content:encoded>\n")
items.WriteString("</item>\n")
}
built := ""
if len(posts) > 0 {
built = "<lastBuildDate>" + posts[0].Date.Format(time.RFC1123Z) + "</lastBuildDate>\n"
}
url := base + newsURL
return `<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>kmalloc.org: ` + render.Escape(l.Nav["news"]) + `</title>
<link>` + url + `</link>
<description>` + render.Escape(l.Strings["news_p"]) + `</description>
<language>` + l.Code + `</language>
` + built + `<atom:link href="` + url + `feed.xml" rel="self" type="application/rss+xml"/>
` + items.String() + `</channel>
</rss>
`
}