// 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 "", "]]]]>") + "]]>" } // 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("\n") items.WriteString("" + render.Escape(p.Title) + "\n") items.WriteString("" + link + "\n") items.WriteString(`` + link + "\n") items.WriteString("" + p.Date.Format(time.RFC1123Z) + "\n") if p.Description != "" { items.WriteString("" + render.Escape(p.Description) + "\n") } if p.Author != "" { items.WriteString("" + render.Escape(p.Author) + "\n") } for _, c := range p.Categories { items.WriteString("" + render.Escape(c.Name) + "\n") } items.WriteString("" + cdata(text) + "\n") items.WriteString("\n") } built := "" if len(posts) > 0 { built = "" + posts[0].Date.Format(time.RFC1123Z) + "\n" } url := base + newsURL return ` kmalloc.org: ` + render.Escape(l.Nav["news"]) + ` ` + url + ` ` + render.Escape(l.Strings["news_p"]) + ` ` + l.Code + ` ` + built + ` ` + items.String() + ` ` }