From bf50b2e1c2d835340415c776886e1c533f047765 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Mon, 16 Nov 2026 12:06:55 +0100 Subject: [PATCH] feat(news): a post page with date, author, categories, summary and links New placeholders for templates/post.html, written in the language of the post: date_long, post_summary, post_meta, post_reading, post_mastodon and post_nav (older, newer and all news), plus og_type and article_meta for Open Graph, which every other page sets to website. A post with a Mastodon address needs the reply_mastodon string; the other strings are optional and a feature without its string is left out. Refs #9 --- internal/build/blog_test.go | 172 ++++++++++++++++++++++++++++++++++++ internal/build/context.go | 2 + internal/build/pages.go | 13 ++- internal/build/post.go | 113 +++++++++++++++++++++++ 4 files changed, 293 insertions(+), 7 deletions(-) create mode 100644 internal/build/post.go diff --git a/internal/build/blog_test.go b/internal/build/blog_test.go index 392675f..6cedf98 100644 --- a/internal/build/blog_test.go +++ b/internal/build/blog_test.go @@ -127,3 +127,175 @@ func TestNewsLanguage(t *testing.T) { }) } } + +// monthsJSON writes the month_01..month_12 strings of a test language. +func monthsJSON(names ...string) string { + var b strings.Builder + for i, n := range names { + b.WriteString(`"month_` + string(rune('0'+(i+1)/10)) + string(rune('0'+(i+1)%10)) + `": "` + n + `", `) + } + return b.String() +} + +// blogLangEn and blogLangIt are the English and Italian language files with the strings the blog features need. +var ( + blogLangEn = `{"lang_name": "English", "sections": "Sections", "home_link": "/", "nojs": "No JavaScript.", + "powered_by": "Powered by", "back": "kmalloc.org", "version": "Version", "toc": "Contents", "history_a": "Manifesto history", + "news_p": "What is new.", "news_all": "All news", "news_empty": "No news yet.", "category_h": "Category", + "date_long": "{d} {month} {y}", "date_month_year": "{month} {y}", "reading_time": "{n} min read", + ` + monthsJSON("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December") + ` + "post_older": "Older", "post_newer": "Newer", "post_nav": "Other news", "reply_mastodon": "Reply on Mastodon", + "read_more": "Read more", "pager_label": "News pages", "pager_newer": "Newer news", "pager_older": "Older news", "pager_page": "Page {n} of {total}", + "archive_h": "Archive", "archive_year_h": "Year", "archive_up": "Archive of the year", + "nav": {"manifesto": "Manifesto", "people": "People", "services": "Services", "news": "News"}}` + blogLangIt = `{"lang_name": "Italiano", "sections": "Sezioni", "home_link": "/it/", "nojs": "Niente JavaScript.", + "powered_by": "Powered by", "back": "kmalloc.org", "version": "Versione", "toc": "Indice", "history_a": "Cronologia", + "news_p": "Le novita.", "news_all": "Tutte le news", "news_empty": "Nessuna news.", "category_h": "Categoria", + "date_long": "{d} {month} {y}", "date_month_year": "{month} {y}", "reading_time": "{n} min di lettura", + ` + monthsJSON("gennaio", "febbraio", "marzo", "aprile", "maggio", "giugno", "luglio", "agosto", "settembre", "ottobre", "novembre", "dicembre") + ` + "post_older": "Piu vecchia", "post_newer": "Piu recente", "post_nav": "Altre news", "reply_mastodon": "Rispondi su Mastodon", + "read_more": "Leggi tutto", "pager_label": "Pagine delle news", "pager_newer": "News piu recenti", "pager_older": "News piu vecchie", "pager_page": "Pagina {n} di {total}", + "archive_h": "Archivio", "archive_year_h": "Anno", "archive_up": "Archivio dell'anno", + "nav": {"manifesto": "Manifesto", "people": "Persone", "services": "Servizi", "news": "News"}}` +) + +// blogPostTemplate is a post page that uses every placeholder of the blog. +const blogPostTemplate = `

{{post_title}}

+ +{{post_summary}} +{{post_meta}} +{{post_body}} +{{post_mastodon}} +{{post_nav}} +` + +// blogBaseTemplate is the base template with the Open Graph placeholders. +const blogBaseTemplate = ` + + + +{{title_html}} + + +{{article_meta}} + +{{alt_links}} + + + +{{header}} + +{{content}} +{{footer}} + + +` + +// blogSite is the base site with the blog language strings and templates. +func blogSite(extra map[string]string) map[string]string { + files := map[string]string{ + "lang/en.json": blogLangEn, "lang/it.json": blogLangIt, + "templates/post.html": blogPostTemplate, "templates/base.html": blogBaseTemplate, + "news/shop.md": "Title: A shop\nAuthor: squid\nDate: 2026-11-14T12:00:00+0100\nCategories: FLUG\nDescription: About the shop.\nMastodon: https://social.kmalloc.org/@squid/115\n-\nThere is a shop.\n", + } + for k, v := range extra { + files[k] = v + } + return withFiles(files) +} + +// TestPostPageHasTheBlogFields checks the long date, the byline, the summary, +// the reading time, the Mastodon link and the links to the other news. +func TestPostPageHasTheBlogFields(t *testing.T) { + out, err := buildWith(t, blogSite(nil), nil) + if err != nil { + t.Fatalf("Build() error = %v", err) + } + shop := readOut(t, out, "news/2026/11/shop.html") + mustContain(t, "shop post", shop, + `

About the shop.

`, + `

· squid · FLUG · 1 min read

`, + `

Reply on Mastodon

`, + `