From 71cf14006d48084281779b83ee3d0b07fbe91024 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Mon, 16 Nov 2026 12:32:09 +0100 Subject: [PATCH] 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 --- README.md | 4 +- internal/build/blog.go | 37 ++--------- internal/build/blog_test.go | 123 +++++++++++++++++++++++++++++++++--- internal/build/news.go | 58 +++++++++++++++++ internal/build/pages.go | 6 +- 5 files changed, 185 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 96cf016..1b58d2c 100644 --- a/README.md +++ b/README.md @@ -63,8 +63,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 (`

`: 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`. diff --git a/internal/build/blog.go b/internal/build/blog.go index b0e47cf..1395840 100644 --- a/internal/build/blog.go +++ b/internal/build/blog.go @@ -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(``, p.Date.Format(dateOnly), render.Escape(longDate(p.Date, l))) - if links := categoryLinks(p); links != "" { - meta += " · " + links - } - var out strings.Builder - out.WriteString("

\n") - fmt.Fprintf(&out, "

%s

\n", p.Path, mark, render.Escape(p.Title)) - fmt.Fprintf(&out, "

%s

\n", meta) - if p.Description != "" { - fmt.Fprintf(&out, "

%s

\n", render.Escape(p.Description)) - } - fmt.Fprintf(&out, "

%s

\n", p.Path, render.EscapeAttr(more), render.EscapeAttr(p.Title), mark, render.Escape(more)) - out.WriteString("
") - 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 diff --git a/internal/build/blog_test.go b/internal/build/blog_test.go index 83b112e..2835a32 100644 --- a/internal/build/blog_test.go +++ b/internal/build/blog_test.go @@ -324,13 +324,20 @@ func TestNewsBlogEntries(t *testing.T) { } index := readOut(t, out, "news/index.html") mustContain(t, "news index", index, - "
\n

A shop

", - `

· FLUG

`, - "

About the shop.

", - `

Read more

`, + "

\n"+ + ` · FLUG
`+"\n"+ + "A shop.\n"+ + "About the shop.\n"+ + `Read more.`+"\n

", + // a post with two categories, and one with a description made of its first sentence + `Linux Day, FLUG
`, + `Status page.`, ) - if got := strings.Count(index, "
"); 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, "`, `Page 1 of 3`, ``) - if strings.Contains(first, `rel="prev"`) || strings.Count(first, "
") != 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`, ``, ``, "News, Page 2 of 3", "Post 14", "Post 05") - if strings.Count(second, "
") != 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`, ``, "GNU/Linux Day 2026") - if strings.Contains(third, `rel="next"`) || strings.Count(third, "
") != 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, "Why a shop?", "It is open!", "Done.", "Tom & Jerry.", + `aria-label="Read more: Tom & Jerry"`) + for _, wrong := range []string{"shop?.", "open!.", "Done..", "Done.."} { + 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"), + "

\n"+ + `
`+"\n"+ + "Bare.\n"+ + `Read more.`+"\n

") +} + +// 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 := "A shop.\nAbout the shop.\n" + `Read more.` + 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, "
  • A shop.`+"\nAbout the shop.\n

    ") + 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, `Read more.`) + if strings.Count(index, `hreflang="it" lang="it">Read more`) != 1 { + t.Error("an English post is marked as written in Italian") + } +} diff --git a/internal/build/news.go b/internal/build/news.go index 93ea37f..eafa38c 100644 --- a/internal/build/news.go +++ b/internal/build/news.go @@ -41,6 +41,64 @@ func newsItem(p content.Post, lang string, withDescription bool) string { return item + "
  • " } +// 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(``, p.Date.Format(dateOnly), render.Escape(longDate(p.Date, l))) + if links := categoryLinks(p); links != "" { + meta += " · " + links + } + title := p.Title + if !strings.HasSuffix(title, ".") && !strings.HasSuffix(title, "?") && !strings.HasSuffix(title, "!") { + title += "." + } + var out strings.Builder + out.WriteString("

    \n") + fmt.Fprintf(&out, "%s
    \n", meta) + if more == "" { + fmt.Fprintf(&out, "%s\n", p.Path, mark, render.Escape(title)) + } else { + fmt.Fprintf(&out, "%s\n", render.Escape(title)) + } + if p.Description != "" { + fmt.Fprintf(&out, "%s\n", render.Escape(p.Description)) + } + if more != "" { + fmt.Fprintf(&out, "%s.\n", p.Path, render.EscapeAttr(more), render.EscapeAttr(p.Title), mark, render.Escape(more)) + } + out.WriteString("

    ") + 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
      , or an empty string // when there are no posts. func newsList(posts []content.Post, lang string) string { diff --git a/internal/build/pages.go b/internal/build/pages.go index 0136168..04d04bc 100644 --- a/internal/build/pages.go +++ b/internal/build/pages.go @@ -255,7 +255,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