diff --git a/internal/build/feed.go b/internal/build/feed.go new file mode 100644 index 0000000..485cc8a --- /dev/null +++ b/internal/build/feed.go @@ -0,0 +1,49 @@ +// 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("\n") + items.WriteString("" + render.Escape(p.Title) + "\n") + items.WriteString("" + link + "\n") + items.WriteString(`` + link + "\n") + items.WriteString("" + p.Date.Format(time.RFC1123Z) + "\n") + items.WriteString("" + render.Escape(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() + ` + +` +} diff --git a/internal/build/feed_test.go b/internal/build/feed_test.go new file mode 100644 index 0000000..5c508ab --- /dev/null +++ b/internal/build/feed_test.go @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package build + +import ( + "encoding/xml" + "strings" + "testing" + + "git.kmalloc.org/squid/motorino/internal/site" +) + +// feedLang is the language the feed header is built from. +func feedLang() site.Lang { + return site.Lang{Code: "en", Strings: map[string]string{"news_p": "The news & more."}, Nav: map[string]string{"news": "News"}} +} + +// TestFeedItems checks the channel, the item fields, the limit and the dates. +func TestFeedItems(t *testing.T) { + items := []renderedPost{ + {Post: testPost("new", "2026-11-14", "New ", ""), HTML: `

See people.

`}, + {Post: testPost("old", "2026-11-12", "Old", ""), HTML: "

Old text.

"}, + {Post: testPost("older", "2026-11-10", "Older", ""), HTML: "

Older text.

"}, + } + got := feed("https://kmalloc.org", feedLang(), "/news/", items, 2) + + for _, want := range []string{ + "kmalloc.org: News", + "https://kmalloc.org/news/", + "The news & more.", + "en", + ``, + "New <post>", + "https://kmalloc.org/news/2026/11/new.html", + `https://kmalloc.org/news/2026/11/new.html`, + "Sat, 14 Nov 2026 12:00:00 +0100", + "Sat, 14 Nov 2026 12:00:00 +0100", + `<a href="https://kmalloc.org/people.html">`, + } { + if !strings.Contains(got, want) { + t.Errorf("feed does not contain %q\n%s", want, got) + } + } + if strings.Contains(got, "older.html") { + t.Error("feed has a third item but the limit is 2") + } + if err := xml.Unmarshal([]byte(got), new(any)); err != nil { + t.Errorf("feed is not well-formed XML: %v", err) + } +} + +// TestFeedWithoutPosts checks that an empty feed is still a valid channel. +func TestFeedWithoutPosts(t *testing.T) { + got := feed("https://kmalloc.org", feedLang(), "/news/", nil, 20) + if strings.Contains(got, "lastBuildDate") || strings.Contains(got, "") { + t.Errorf("empty feed has dates or items:\n%s", got) + } + if err := xml.Unmarshal([]byte(got), new(any)); err != nil { + t.Errorf("empty feed is not well-formed XML: %v", err) + } +} diff --git a/internal/build/news.go b/internal/build/news.go new file mode 100644 index 0000000..a9f6e89 --- /dev/null +++ b/internal/build/news.go @@ -0,0 +1,120 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +// Package build assembles a site from its configuration, content, templates +// and assets, and writes it to a folder. +package build + +import ( + "fmt" + "sort" + "strings" + + "git.kmalloc.org/squid/motorino/internal/content" + "git.kmalloc.org/squid/motorino/internal/render" +) + +// dateOnly is the layout of the dates shown in lists. +const dateOnly = "2006-01-02" + +// renderedPost is a post together with its body already converted to HTML. +type renderedPost struct { + content.Post + // HTML is the body of the post as HTML, placeholders already replaced. + HTML string +} + +// newsItem returns the
  • of a post for a list: the date, the title as a +// link and, when withDescription is set and there is one, the description. News +// are written in English, so on any other language the link says so. +func newsItem(p content.Post, lang string, withDescription bool) string { + mark := "" + if lang != "en" { + mark = ` hreflang="en" lang="en"` + } + day := p.Date.Format(dateOnly) + item := fmt.Sprintf(`
  • %s`, + day, day, p.Path, mark, render.Escape(p.Title)) + if withDescription && p.Description != "" { + item += ": " + render.Escape(p.Description) + } + return item + "
  • " +} + +// newsList returns the full list of the news as a