motorino/internal/build/news.go
tgiachi 71cf14006d
All checks were successful
check / check (pull_request) Successful in 30s
feat(news): show the posts of the lists as note panels
The blog list, the pages of a category and the pages of an archive show every post as a
note panel: a small line with the long date and the categories, the title in bold with
a full stop (unless it ends with a full stop, a question mark or an exclamation mark),
the description and a read more link whose aria-label names the post. The page of a
post, {{news_list}} and the latest news of the home page are unchanged.

A language without read_more gets the title as the link on the category and archive
pages, as those pages worked before; the blog list still needs the string.

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

179 lines
5.9 KiB
Go

// 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. A
// post is written in its own language, so on a page in another language the
// link says which one.
func newsItem(p content.Post, lang string, withDescription bool) string {
mark := ""
if p.Language != "" && p.Language != lang {
mark = fmt.Sprintf(` hreflang="%s" lang="%s"`, p.Language, p.Language)
}
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>"
}
// newsPanel returns a post for a list of the news as a note panel: first a small
// line with the date and the categories, then the title in bold with a full stop
// (unless it ends with one, a question mark or an exclamation mark), then the
// description, then the link to read more, which names the post for a reader that
// hears the links out of context. lang is the language of the page; the link of a
// post in another language says so. Without the string read_more the title is the
// link and there is no link to read more, unless needMore is set: the blog list has
// no other link to the post, so there it is an error.
func (b *builder) newsPanel(p content.Post, lang string, needMore bool) (string, error) {
l := b.langs[lang]
more := l.Strings["read_more"]
if more == "" && needMore {
return "", fmt.Errorf("lang/%s.json: the language has no read_more string, which the blog list needs", lang)
}
mark := ""
if p.Language != "" && p.Language != lang {
mark = fmt.Sprintf(` hreflang="%s" lang="%s"`, p.Language, p.Language)
}
meta := fmt.Sprintf(`<time datetime="%s">%s</time>`, p.Date.Format(dateOnly), render.Escape(longDate(p.Date, l)))
if links := categoryLinks(p); links != "" {
meta += " &middot; " + links
}
title := p.Title
if !strings.HasSuffix(title, ".") && !strings.HasSuffix(title, "?") && !strings.HasSuffix(title, "!") {
title += "."
}
var out strings.Builder
out.WriteString("<p class=\"note\" role=\"note\">\n")
fmt.Fprintf(&out, "<span class=\"meta\">%s</span><br>\n", meta)
if more == "" {
fmt.Fprintf(&out, "<strong><a href=\"%s\"%s>%s</a></strong>\n", p.Path, mark, render.Escape(title))
} else {
fmt.Fprintf(&out, "<strong>%s</strong>\n", render.Escape(title))
}
if p.Description != "" {
fmt.Fprintf(&out, "%s\n", render.Escape(p.Description))
}
if more != "" {
fmt.Fprintf(&out, "<a href=\"%s\" aria-label=\"%s: %s\"%s>%s</a>.\n", p.Path, render.EscapeAttr(more), render.EscapeAttr(p.Title), mark, render.Escape(more))
}
out.WriteString("</p>")
return out.String(), nil
}
// newsPanels returns the panels of the posts of a category or of an archive,
// one after the other, or an empty string when there are none.
func (b *builder) newsPanels(posts []content.Post, lang string) (string, error) {
panels := make([]string, len(posts))
for i, p := range posts {
panel, err := b.newsPanel(p, lang, false)
if err != nil {
return "", err
}
panels[i] = panel
}
return strings.Join(panels, "\n"), nil
}
// 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
}