motorino/internal/build/feed.go

49 lines
1.7 KiB
Go

// SPDX-License-Identifier: GPL-3.0-or-later
package build
import (
"strings"
"time"
"git.kmalloc.org/squid/motorino/internal/render"
"git.kmalloc.org/squid/motorino/internal/site"
)
// feed returns the RSS 2.0 feed of the latest limit posts, which must be sorted
// newest first. Each item carries the full text with absolute links. 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 := strings.ReplaceAll(p.HTML, `href="/`, `href="`+base+`/`)
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")
items.WriteString("<description>" + render.Escape(text) + "</description>\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">
<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>
`
}