feat: build the news lists, category lists and the feed

This commit is contained in:
tgiachi 2026-11-15 09:46:58 +01:00
commit 2f5cb15f21
4 changed files with 337 additions and 0 deletions

49
internal/build/feed.go Normal file
View file

@ -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("<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>
`
}

View file

@ -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 <post>", ""), HTML: `<p>See <a href="/people.html">people</a>.</p>`},
{Post: testPost("old", "2026-11-12", "Old", ""), HTML: "<p>Old text.</p>"},
{Post: testPost("older", "2026-11-10", "Older", ""), HTML: "<p>Older text.</p>"},
}
got := feed("https://kmalloc.org", feedLang(), "/news/", items, 2)
for _, want := range []string{
"<title>kmalloc.org: News</title>",
"<link>https://kmalloc.org/news/</link>",
"<description>The news &amp; more.</description>",
"<language>en</language>",
`<atom:link href="https://kmalloc.org/news/feed.xml" rel="self" type="application/rss+xml"/>`,
"<title>New &lt;post&gt;</title>",
"<link>https://kmalloc.org/news/2026/11/new.html</link>",
`<guid isPermaLink="true">https://kmalloc.org/news/2026/11/new.html</guid>`,
"<pubDate>Sat, 14 Nov 2026 12:00:00 +0100</pubDate>",
"<lastBuildDate>Sat, 14 Nov 2026 12:00:00 +0100</lastBuildDate>",
`&lt;a href="https://kmalloc.org/people.html"&gt;`,
} {
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, "<item>") {
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)
}
}

120
internal/build/news.go Normal file
View file

@ -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 <li> 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(`<li><time datetime="%s">%s</time> <a href="%s"%s>%s</a>`,
day, day, p.Path, mark, render.Escape(p.Title))
if withDescription && p.Description != "" {
item += ": " + render.Escape(p.Description)
}
return item + "</li>"
}
// newsList returns the full list of the news as a <ul>, or an empty string
// when there are no posts.
func newsList(posts []content.Post, lang string) string {
if len(posts) == 0 {
return ""
}
items := make([]string, len(posts))
for i, p := range posts {
items[i] = newsItem(p, lang, true)
}
return "<ul>\n" + strings.Join(items, "\n") + "\n</ul>"
}
// latestNews returns the <li> items of the first n posts, without the <ul>
// and without descriptions, for the home page.
func latestNews(posts []content.Post, lang string, n int) string {
if n > len(posts) {
n = len(posts)
}
items := make([]string, n)
for i := 0; i < n; i++ {
items[i] = newsItem(posts[i], lang, false)
}
return strings.Join(items, "\n")
}
// categoriesList returns the list of the categories, sorted by name, each with
// the number of posts, and the categories themselves. With no categories both
// are empty.
func categoriesList(posts []content.Post) (string, []content.Category) {
counts := map[string]int{}
byslug := map[string]content.Category{}
for _, p := range posts {
for _, c := range p.Categories {
counts[c.Slug]++
byslug[c.Slug] = c
}
}
if len(byslug) == 0 {
return "", nil
}
cats := make([]content.Category, 0, len(byslug))
for _, c := range byslug {
cats = append(cats, c)
}
sort.Slice(cats, func(i, j int) bool {
a, b := strings.ToLower(cats[i].Name), strings.ToLower(cats[j].Name)
if a != b {
return a < b
}
return cats[i].Name < cats[j].Name
})
items := make([]string, len(cats))
for i, c := range cats {
items[i] = fmt.Sprintf(`<li><a href="%s">%s</a> (%d)</li>`, categoryPath(c.Slug), render.Escape(c.Name), counts[c.Slug])
}
return "<ul>\n" + strings.Join(items, "\n") + "\n</ul>", cats
}
// categoryPath returns the address of the page of a category.
func categoryPath(slug string) string {
return "/news/category/" + slug + ".html"
}
// postsIn returns the posts that have the category with the given address
// name, in the order they were given.
func postsIn(posts []content.Post, slug string) []content.Post {
var out []content.Post
for _, p := range posts {
for _, c := range p.Categories {
if c.Slug == slug {
out = append(out, p)
break
}
}
}
return out
}

107
internal/build/news_test.go Normal file
View file

@ -0,0 +1,107 @@
// SPDX-License-Identifier: GPL-3.0-or-later
package build
import (
"strings"
"testing"
"time"
"git.kmalloc.org/squid/motorino/internal/content"
)
// testPost returns a post dated on the given day (noon, +0100).
func testPost(slug, day, title, description string, cats ...string) content.Post {
date, err := time.Parse("2006-01-02T15:04:05-0700", day+"T12:00:00+0100")
if err != nil {
panic(err)
}
var categories []content.Category
for _, name := range cats {
slugName, _ := content.CategorySlug(name)
categories = append(categories, content.Category{Name: name, Slug: slugName})
}
return content.Post{
Slug: slug, Path: content.PostPath(date, slug), Title: title, Description: description,
Date: date, Categories: categories, Source: "news/" + slug + ".md",
}
}
// TestNewsListMarksEnglishOnItalian checks the hreflang mark on other languages.
func TestNewsListMarksEnglishOnItalian(t *testing.T) {
posts := []content.Post{testPost("shop", "2026-11-14", "A shop <new>", "About the shop.")}
en := newsList(posts, "en")
it := newsList(posts, "it")
want := `<li><time datetime="2026-11-14">2026-11-14</time> <a href="/news/2026/11/shop.html">A shop &lt;new&gt;</a>: About the shop.</li>`
if !strings.Contains(en, want) {
t.Errorf("en list %q does not contain %q", en, want)
}
if !strings.Contains(it, `<a href="/news/2026/11/shop.html" hreflang="en" lang="en">`) {
t.Errorf("it list %q lacks the hreflang mark", it)
}
if !strings.HasPrefix(en, "<ul>\n") || !strings.HasSuffix(en, "\n</ul>") {
t.Errorf("list is not wrapped in <ul>: %q", en)
}
}
// TestNewsListWithoutDescription checks that an empty description adds no colon.
func TestNewsListWithoutDescription(t *testing.T) {
got := newsList([]content.Post{testPost("a", "2026-01-01", "Title", "")}, "en")
if strings.Contains(got, ": ") {
t.Errorf("list %q has a colon but no description", got)
}
}
// TestNewsListEmpty checks that no posts give an empty string.
func TestNewsListEmpty(t *testing.T) {
if got := newsList(nil, "en"); got != "" {
t.Errorf("newsList(nil) = %q, want empty", got)
}
}
// TestLatestNewsLimit checks that only the first n posts are listed.
func TestLatestNewsLimit(t *testing.T) {
posts := []content.Post{
testPost("a", "2026-03-01", "A", ""), testPost("b", "2026-02-01", "B", ""), testPost("c", "2026-01-01", "C", ""),
}
got := latestNews(posts, "en", 2)
if strings.Count(got, "<li>") != 2 || strings.Contains(got, ">C<") || strings.Contains(got, "<ul>") {
t.Errorf("latestNews() = %q, want two items and no <ul>", got)
}
}
// TestCategoriesListCountsAndSorts checks the category index.
func TestCategoriesListCountsAndSorts(t *testing.T) {
posts := []content.Post{
testPost("a", "2026-03-01", "A", "", "Linux Day", "FLUG"),
testPost("b", "2026-02-01", "B", "", "FLUG"),
testPost("c", "2026-01-01", "C", "", "News"),
}
html, cats := categoriesList(posts)
if len(cats) != 3 || cats[0].Name != "FLUG" || cats[1].Name != "Linux Day" || cats[2].Name != "News" {
t.Errorf("categories = %+v, want FLUG, Linux Day, News", cats)
}
for _, want := range []string{
`<a href="/news/category/flug.html">FLUG</a> (2)`,
`<a href="/news/category/linux-day.html">Linux Day</a> (1)`,
} {
if !strings.Contains(html, want) {
t.Errorf("html %q does not contain %q", html, want)
}
}
if emptyHTML, emptyCats := categoriesList(nil); emptyHTML != "" || len(emptyCats) != 0 {
t.Errorf("categoriesList(nil) = %q, %v, want empty", emptyHTML, emptyCats)
}
}
// TestPostsIn checks that posts are picked by category address.
func TestPostsIn(t *testing.T) {
posts := []content.Post{
testPost("a", "2026-03-01", "A", "", "FLUG"),
testPost("b", "2026-02-01", "B", "", "News"),
}
got := postsIn(posts, "flug")
if len(got) != 1 || got[0].Slug != "a" {
t.Errorf("postsIn(flug) = %+v, want only post a", got)
}
}