Merge branch 'feat/blog-news' into feat/gemini-output
All checks were successful
check / check (pull_request) Successful in 29s

This commit is contained in:
tgiachi 2026-11-16 12:32:39 +01:00
commit cc90df8c6a
5 changed files with 185 additions and 43 deletions

View file

@ -65,8 +65,8 @@ The news exist once, not once per language. Their language is `news_language` in
Everything below is opt in, so a site that does not use it builds as it did.
- **The post page** (`templates/post.html`) has, besides `post_title`, `date`, `post_body` and `post_byline`: `date_long` (such as "14 November 2026"), `post_summary` (the written description as a lead paragraph), `post_meta` (a line with the date, the author, the category links and the reading time), `post_reading`, `post_mastodon` (the reply link) and `post_nav` (the newer post, the older post and all the news). `og_type` is `article` there and `website` on every other page, and `article_meta` holds the published time and a tag for each category, for the head of `base.html`.
- **The blog list**: a paragraph with only `{{news_blog}}` on the news index lists the posts as entries (title, long date, categories, description, a read more link), 10 to a page. The other pages are written under the index, at `page/2/`, `page/3/`, with a pager. The block needs an index page.
- **The archive**: when `templates/archive.html` exists, every year and month with posts gets a page, `/news/2026/` and `/news/2026/11/`, with `archive_name`, `archive_nav` and `archive_posts`. `{{news_archive}}` lists the years and the months with their counts, and needs the template.
- **The blog list**: a paragraph with only `{{news_blog}}` on the news index lists the posts as note panels (`<p class="note" role="note">`: a small line with the long date and the categories, the title in bold with a full stop unless it ends with `.`, `?` or `!`, the description, and a read more link named after the post), 10 to a page. The other pages are written under the index, at `page/2/`, `page/3/`, with a pager. The block needs an index page.
- **The archive**: when `templates/archive.html` exists, every year and month with posts gets a page, `/news/2026/` and `/news/2026/11/`, with `archive_name`, `archive_nav` and `archive_posts`. The posts of an archive page, of a category page and of the blog list are the same panels; where the language has no `read_more` the title is the link instead, except on the blog list, where it is an error. `{{news_list}}` and the latest news of the home page stay plain lists. `{{news_archive}}` lists the years and the months with their counts, and needs the template.
- **The feed** (`/news/feed.xml`) has the summary in `description` and the full text, with absolute links, in `content:encoded`, plus the categories and the author.
The strings come from the language files. These are required when the feature is used: `read_more`, `pager_label`, `pager_newer`, `pager_older` and `pager_page` (`Page {n} of {total}`) for the blog list, and `reply_mastodon` for a post with a Mastodon address. These are optional, and without them the feature is left out: `date_long` (`{d} {month} {y}`) and `month_01` to `month_12` (without them the date is `2026-11-14`), `date_month_year` (`{month} {y}`), `reading_time` (`{n} min read`), and `post_older`, `post_newer` and `post_nav` for the links between posts. The archive uses `archive_h` in its template and, optionally, `archive_up`.

View file

@ -40,35 +40,6 @@ func (b *builder) pageLabel(lang string, n, total int) (string, error) {
return strings.NewReplacer("{n}", strconv.Itoa(n), "{total}", strconv.Itoa(total)).Replace(text), nil
}
// blogEntry returns one entry of the blog list: the title as a link, the date
// and the categories, the description as a preview and a link to read more.
// lang is the language of the page; a post in another language is marked.
func (b *builder) blogEntry(p content.Post, lang string) (string, error) {
l := b.langs[lang]
more := l.Strings["read_more"]
if more == "" {
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
}
var out strings.Builder
out.WriteString("<article>\n")
fmt.Fprintf(&out, "<h2><a href=\"%s\"%s>%s</a></h2>\n", p.Path, mark, render.Escape(p.Title))
fmt.Fprintf(&out, "<p class=\"meta\">%s</p>\n", meta)
if p.Description != "" {
fmt.Fprintf(&out, "<p>%s</p>\n", render.Escape(p.Description))
}
fmt.Fprintf(&out, "<p><a href=\"%s\" aria-label=\"%s: %s\"%s>%s</a></p>\n", p.Path, render.EscapeAttr(more), render.EscapeAttr(p.Title), mark, render.Escape(more))
out.WriteString("</article>")
return out.String(), nil
}
// newsBlog returns the page n of the blog list of the language lang, with the
// pager when the list has more than one page. A site without posts gets the
// news_empty text.
@ -86,7 +57,7 @@ func (b *builder) newsBlog(lang string, n int, ctx map[string]string) (string, e
}
entries := make([]string, 0, newsPerPage)
for _, p := range b.posts[(n-1)*newsPerPage : end] {
entry, err := b.blogEntry(p, lang)
entry, err := b.newsPanel(p, lang, true)
if err != nil {
return "", err
}
@ -216,7 +187,11 @@ func (b *builder) renderArchives() error {
ctx := b.documentContext(d)
ctx["archive_name"] = render.Escape(name)
ctx["archive_nav"] = nav
ctx["archive_posts"] = newsList(posts, lang)
panels, err := b.newsPanels(posts, lang)
if err != nil {
return err
}
ctx["archive_posts"] = panels
body, err := render.Substitute("templates/archive.html", tmpl, ctx)
if err != nil {
return err

View file

@ -324,13 +324,20 @@ func TestNewsBlogEntries(t *testing.T) {
}
index := readOut(t, out, "news/index.html")
mustContain(t, "news index", index,
"<article>\n<h2><a href=\"/news/2026/11/shop.html\">A shop</a></h2>",
`<p class="meta"><time datetime="2026-11-14">14 November 2026</time> &middot; <a href="/news/category/flug.html">FLUG</a></p>`,
"<p>About the shop.</p>",
`<p><a href="/news/2026/11/shop.html" aria-label="Read more: A shop">Read more</a></p>`,
"<p class=\"note\" role=\"note\">\n"+
`<span class="meta"><time datetime="2026-11-14">14 November 2026</time> &middot; <a href="/news/category/flug.html">FLUG</a></span><br>`+"\n"+
"<strong>A shop.</strong>\n"+
"About the shop.\n"+
`<a href="/news/2026/11/shop.html" aria-label="Read more: A shop">Read more</a>.`+"\n</p>",
// a post with two categories, and one with a description made of its first sentence
`<a href="/news/category/linux-day.html">Linux Day</a>, <a href="/news/category/flug.html">FLUG</a></span><br>`,
`<strong>Status page.</strong>`,
)
if got := strings.Count(index, "<article>"); got != 3 {
t.Errorf("the index has %d entries, want 3", got)
if got := strings.Count(index, `role="note"`); got != 3 {
t.Errorf("the index has %d panels, want 3", got)
}
if strings.Contains(index, "<article") {
t.Error("the index has an article element, the panels replaced them")
}
if strings.Contains(index, "News pages") {
t.Error("a list that fits one page has a pager")
@ -346,18 +353,18 @@ func TestNewsBlogPagination(t *testing.T) {
}
first := readOut(t, out, "news/index.html")
mustContain(t, "page 1", first, `<nav aria-label="News pages">`, `Page 1 of 3`, `<a href="/news/page/2/" rel="next">Older news</a>`)
if strings.Contains(first, `rel="prev"`) || strings.Count(first, "<article>") != 10 || !strings.Contains(first, "Post 15") || strings.Contains(first, "Post 14") {
if strings.Contains(first, `rel="prev"`) || strings.Count(first, `role="note"`) != 10 || !strings.Contains(first, "Post 15") || strings.Contains(first, "Post 14") {
t.Errorf("page 1 is wrong:\n%s", first)
}
second := readOut(t, out, "news/page/2/index.html")
mustContain(t, "page 2", second, `Page 2 of 3`, `<a href="/news/" rel="prev">Newer news</a>`, `<a href="/news/page/3/" rel="next">Older news</a>`,
"<title>News, Page 2 of 3</title>", "Post 14", "Post 05")
if strings.Count(second, "<article>") != 10 || strings.Contains(second, "Post 15") || strings.Contains(second, "Post 04") {
if strings.Count(second, `role="note"`) != 10 || strings.Contains(second, "Post 15") || strings.Contains(second, "Post 04") {
t.Errorf("page 2 is wrong:\n%s", second)
}
third := readOut(t, out, "news/page/3/index.html")
mustContain(t, "page 3", third, `Page 3 of 3`, `<a href="/news/page/2/" rel="prev">Newer news</a>`, "GNU/Linux Day 2026")
if strings.Contains(third, `rel="next"`) || strings.Count(third, "<article>") != 5 {
if strings.Contains(third, `rel="next"`) || strings.Count(third, `role="note"`) != 5 {
t.Errorf("page 3 is wrong:\n%s", third)
}
if exists(t, out, "news/page/4/index.html") || exists(t, out, "news/page/1/index.html") {
@ -421,3 +428,101 @@ func TestNoArchiveWithoutTemplate(t *testing.T) {
t.Errorf("error = %v, want one that names the archive template", err)
}
}
// TestNewsPanelTitlePunctuation checks the full stop after the title of a panel: added, unless the title ends with one, a question mark or an exclamation mark.
func TestNewsPanelTitlePunctuation(t *testing.T) {
files := blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate,
"news/q.md": "Title: Why a shop?\nDate: 2026-11-01T12:00:00+0100\n-\nText.\n",
"news/e.md": "Title: It is open!\nDate: 2026-11-02T12:00:00+0100\n-\nText.\n",
"news/d.md": "Title: Done.\nDate: 2026-11-03T12:00:00+0100\n-\nText.\n",
"news/a.md": "Title: Tom & Jerry\nDate: 2026-11-04T12:00:00+0100\n-\nText.\n"})
out, err := buildWith(t, files, nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
index := readOut(t, out, "news/index.html")
mustContain(t, "news index", index, "<strong>Why a shop?</strong>", "<strong>It is open!</strong>", "<strong>Done.</strong>", "<strong>Tom &amp; Jerry.</strong>",
`aria-label="Read more: Tom &amp; Jerry"`)
for _, wrong := range []string{"shop?.", "open!.", "Done..", "Done.</strong>."} {
if strings.Contains(index, wrong) {
t.Errorf("the index has %q", wrong)
}
}
}
// TestNewsPanelWithoutCategoriesOrDescription checks the two optional lines of a panel.
func TestNewsPanelWithoutCategoriesOrDescription(t *testing.T) {
out, err := buildWith(t, blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate,
"news/bare.md": "Title: Bare\nDate: 2026-11-13T12:00:00+0100\nDescription:\n-\n"}), nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
mustContain(t, "news index", readOut(t, out, "news/index.html"),
"<p class=\"note\" role=\"note\">\n"+
`<span class="meta"><time datetime="2026-11-13">13 November 2026</time></span><br>`+"\n"+
"<strong>Bare.</strong>\n"+
`<a href="/news/2026/11/bare.html" aria-label="Read more: Bare">Read more</a>.`+"\n</p>")
}
// TestPanelsOnCategoryAndArchivePages checks that the lists of the categories and of the archives are panels too,
// and that the page of a post and the latest news of the home page are not.
func TestPanelsOnCategoryAndArchivePages(t *testing.T) {
out, err := buildWith(t, blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate}), nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
shop := "<strong>A shop.</strong>\nAbout the shop.\n" + `<a href="/news/2026/11/shop.html" aria-label="Read more: A shop">Read more</a>.`
for rel, want := range map[string]int{"news/category/flug.html": 2, "news/2026/index.html": 3, "news/2026/11/index.html": 2} {
got := readOut(t, out, rel)
if n := strings.Count(got, `role="note"`); n != want {
t.Errorf("%s has %d panels, want %d", rel, n, want)
}
if !strings.Contains(got, shop) {
t.Errorf("%s lacks the panel of the shop post", rel)
}
if strings.Contains(got, "<li><time") {
t.Errorf("%s still has the list of dates", rel)
}
}
if post := readOut(t, out, "news/2026/11/shop.html"); strings.Contains(post, `role="note"`) {
t.Error("the page of a post has a panel")
}
if home := readOut(t, out, "index.html"); strings.Contains(home, `role="note"`) || !strings.Contains(home, "<li><time") {
t.Error("the latest news of the home page changed")
}
}
// TestPanelsWithoutReadMore checks that a language without read_more gets panels whose title is the link on the
// category and archive pages, as before the panels, and an error on the blog list, which has no other link.
func TestPanelsWithoutReadMore(t *testing.T) {
withoutMore := strings.Replace(blogLangEn, `"read_more": "Read more", `, "", 1)
out, err := buildWith(t, blogSite(map[string]string{"lang/en.json": withoutMore, "templates/archive.html": archiveTemplate}), nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
for _, rel := range []string{"news/category/flug.html", "news/2026/11/index.html"} {
got := readOut(t, out, rel)
mustContain(t, rel, got, `<strong><a href="/news/2026/11/shop.html">A shop.</a></strong>`+"\nAbout the shop.\n</p>")
if strings.Contains(got, "aria-label") || strings.Contains(got, "Read more") {
t.Errorf("%s has a read more link without the string", rel)
}
}
_, err = buildWith(t, blogSite(map[string]string{"lang/en.json": withoutMore, "content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate}), nil)
if err == nil || !strings.Contains(err.Error(), "read_more") {
t.Errorf("error = %v, want one that names read_more", err)
}
}
// TestPanelOfAPostInAnotherLanguage checks that the link of a panel says the language of a post that is not in the language of the page.
func TestPanelOfAPostInAnotherLanguage(t *testing.T) {
out, err := buildWith(t, blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate,
"news/ciao.md": "Title: Ciao\nDate: 2026-11-05T09:00:00+0100\nLanguage: it\n-\nUn testo.\n"}), nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
index := readOut(t, out, "news/index.html")
mustContain(t, "news index", index, `<a href="/news/2026/11/ciao.html" aria-label="Read more: Ciao" hreflang="it" lang="it">Read more</a>.`)
if strings.Count(index, `hreflang="it" lang="it">Read more`) != 1 {
t.Error("an English post is marked as written in Italian")
}
}

View file

@ -41,6 +41,64 @@ func newsItem(p content.Post, lang string, withDescription bool) string {
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 {

View file

@ -261,7 +261,11 @@ func (b *builder) renderCategories() error {
}
ctx := b.documentContext(d)
ctx["category_name"] = render.Escape(c.Name)
ctx["category_posts"] = newsList(c.posts, b.newsLang)
panels, err := b.newsPanels(c.posts, b.newsLang)
if err != nil {
return err
}
ctx["category_posts"] = panels
body, err := render.Substitute("templates/category.html", tmpl, ctx)
if err != nil {
return err