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
This commit is contained in:
parent
26edf78449
commit
bf50b2e1c2
4 changed files with 293 additions and 7 deletions
|
|
@ -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 = `<h1>{{post_title}}</h1>
|
||||
|
||||
{{post_summary}}
|
||||
{{post_meta}}
|
||||
{{post_body}}
|
||||
{{post_mastodon}}
|
||||
{{post_nav}}
|
||||
`
|
||||
|
||||
// blogBaseTemplate is the base template with the Open Graph placeholders.
|
||||
const blogBaseTemplate = `<!doctype html>
|
||||
<html lang="{{lang}}">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>{{title_html}}</title>
|
||||
<meta property="og:type" content="{{og_type}}">
|
||||
<meta property="og:title" content="{{title_html}}">
|
||||
{{article_meta}}
|
||||
<meta name="description" content="{{description_attr}}">
|
||||
{{alt_links}}
|
||||
<style>
|
||||
{{css}}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
{{header}}
|
||||
|
||||
{{content}}
|
||||
{{footer}}
|
||||
</body>
|
||||
</html>
|
||||
`
|
||||
|
||||
// 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,
|
||||
`<p class="lede">About the shop.</p>`,
|
||||
`<p class="meta"><time datetime="2026-11-14">14 November 2026</time> · squid · <a href="/news/category/flug.html">FLUG</a> · 1 min read</p>`,
|
||||
`<p><a href="https://social.kmalloc.org/@squid/115" rel="noopener">Reply on Mastodon</a></p>`,
|
||||
`<nav aria-label="Other news">`,
|
||||
`<li><a href="/news/2026/11/status.html" rel="prev">Older: Status page</a></li>`,
|
||||
`<li><a href="/news/">All news</a></li>`,
|
||||
)
|
||||
if strings.Contains(shop, "Newer:") {
|
||||
t.Error("the newest post links to a newer one")
|
||||
}
|
||||
status := readOut(t, out, "news/2026/11/status.html")
|
||||
mustContain(t, "status post", status,
|
||||
`<p class="meta"><time datetime="2026-11-12">12 November 2026</time> · 1 min read</p>`,
|
||||
`<li><a href="/news/2026/11/shop.html" rel="next">Newer: A shop</a></li>`,
|
||||
`<li><a href="/news/2026/09/gnu-linux-day-2026.html" rel="prev">Older: GNU/Linux Day 2026</a></li>`,
|
||||
)
|
||||
for _, absent := range []string{"lede", "Reply on Mastodon"} {
|
||||
if strings.Contains(status, absent) {
|
||||
t.Errorf("a post without a written description or a Mastodon address shows %q", absent)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestPostPageSkipsFeaturesWithoutStrings checks that a language file without
|
||||
// the optional strings gives plain pages and not an error.
|
||||
func TestPostPageSkipsFeaturesWithoutStrings(t *testing.T) {
|
||||
files := blogSite(map[string]string{"lang/en.json": baseFiles()["lang/en.json"], "news/shop.md": baseFiles()["news/shop.md"]})
|
||||
out, err := buildWith(t, files, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Build() error = %v", err)
|
||||
}
|
||||
status := readOut(t, out, "news/2026/11/status.html")
|
||||
mustContain(t, "status post", status, `<time datetime="2026-11-12">2026-11-12</time>`)
|
||||
for _, absent := range []string{"min read", "<nav", "Older"} {
|
||||
if strings.Contains(status, absent) {
|
||||
t.Errorf("without the strings the page shows %q", absent)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestMastodonNeedsItsString checks that an address with no label is an error, not a silent omission.
|
||||
func TestMastodonNeedsItsString(t *testing.T) {
|
||||
files := blogSite(map[string]string{"lang/en.json": baseFiles()["lang/en.json"]})
|
||||
_, err := buildWith(t, files, nil)
|
||||
if err == nil || !strings.Contains(err.Error(), "reply_mastodon") {
|
||||
t.Errorf("error = %v, want one that names reply_mastodon", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPostInAnotherLanguage checks that a post with Language uses the strings and the page language of that language.
|
||||
func TestPostInAnotherLanguage(t *testing.T) {
|
||||
out, err := buildWith(t, blogSite(map[string]string{
|
||||
"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)
|
||||
}
|
||||
ciao := readOut(t, out, "news/2026/11/ciao.html")
|
||||
mustContain(t, "italian post", ciao, `<html lang="it">`, `5 novembre 2026`, `1 min di lettura`, `<nav aria-label="Altre news">`)
|
||||
list := readOut(t, out, "news/index.html")
|
||||
mustContain(t, "news index", list, `hreflang="it" lang="it">Ciao</a>`)
|
||||
if strings.Contains(list, `hreflang="en"`) {
|
||||
t.Error("an English post on the English index is marked as another language")
|
||||
}
|
||||
}
|
||||
|
||||
// TestPostOpenGraphArticle checks og:type and the article meta on posts only.
|
||||
func TestPostOpenGraphArticle(t *testing.T) {
|
||||
out, err := buildWith(t, blogSite(nil), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Build() error = %v", err)
|
||||
}
|
||||
mustContain(t, "post", readOut(t, out, "news/2026/09/gnu-linux-day-2026.html"),
|
||||
`<meta property="og:type" content="article">`,
|
||||
`<meta property="article:published_time" content="2026-09-18T17:00:45+02:00">`,
|
||||
`<meta property="article:tag" content="Linux Day">`,
|
||||
`<meta property="article:tag" content="FLUG">`)
|
||||
for _, rel := range []string{"index.html", "news/index.html", "news/category/flug.html", "it/index.html"} {
|
||||
got := readOut(t, out, rel)
|
||||
mustContain(t, rel, got, `<meta property="og:type" content="website">`)
|
||||
if strings.Contains(got, "article:") {
|
||||
t.Errorf("%s has article meta", rel)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -106,6 +106,8 @@ func (b *builder) documentContext(d document) map[string]string {
|
|||
ctx["title_html"] = render.EscapeAttr(title)
|
||||
ctx["page_title"] = render.Escape(d.title)
|
||||
ctx["description_attr"] = render.EscapeAttr(d.description)
|
||||
ctx["og_type"] = "website"
|
||||
ctx["article_meta"] = ""
|
||||
for _, id := range b.pageIDs {
|
||||
ctx["cur_"+id] = ""
|
||||
if d.nav == id {
|
||||
|
|
|
|||
|
|
@ -156,7 +156,7 @@ func (b *builder) renderPosts() error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, p := range b.posts {
|
||||
for i, p := range b.posts {
|
||||
d := document{
|
||||
lang: p.Language, nav: "news", lead: "news", path: p.Path, title: p.Title,
|
||||
prefixed: true, description: p.Description, source: p.Source,
|
||||
|
|
@ -171,6 +171,9 @@ func (b *builder) renderPosts() error {
|
|||
ctx["date"] = day
|
||||
ctx["post_body"] = html
|
||||
ctx["post_byline"] = byline(p)
|
||||
if err := b.postFields(i, ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
body, err := render.Substitute("templates/post.html", tmpl, ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -190,12 +193,8 @@ func byline(p content.Post) string {
|
|||
if p.Author != "" {
|
||||
out.WriteString(" · " + render.Escape(p.Author))
|
||||
}
|
||||
if len(p.Categories) > 0 {
|
||||
links := make([]string, len(p.Categories))
|
||||
for i, c := range p.Categories {
|
||||
links[i] = fmt.Sprintf(`<a href="%s">%s</a>`, categoryPath(c.Slug), render.Escape(c.Name))
|
||||
}
|
||||
out.WriteString(" · " + strings.Join(links, ", "))
|
||||
if links := categoryLinks(p); links != "" {
|
||||
out.WriteString(" · " + links)
|
||||
}
|
||||
return out.String()
|
||||
}
|
||||
|
|
|
|||
113
internal/build/post.go
Normal file
113
internal/build/post.go
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package build
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.kmalloc.org/squid/motorino/internal/content"
|
||||
"git.kmalloc.org/squid/motorino/internal/render"
|
||||
"git.kmalloc.org/squid/motorino/internal/site"
|
||||
)
|
||||
|
||||
// categoryLinks returns the links to the categories of a post, separated by a
|
||||
// comma, or an empty string when the post has none.
|
||||
func categoryLinks(p content.Post) string {
|
||||
links := make([]string, len(p.Categories))
|
||||
for i, c := range p.Categories {
|
||||
links[i] = fmt.Sprintf(`<a href="%s">%s</a>`, categoryPath(c.Slug), render.Escape(c.Name))
|
||||
}
|
||||
return strings.Join(links, ", ")
|
||||
}
|
||||
|
||||
// postFields adds to ctx the placeholders of the page of the post b.posts[i],
|
||||
// written in the language of the post:
|
||||
//
|
||||
// date_long the date for a reader, such as "14 November 2026"
|
||||
// post_summary the written description as a lead paragraph, or nothing
|
||||
// post_meta a line with the date, the author, the categories, the reading time
|
||||
// post_reading the reading time sentence, or nothing
|
||||
// post_mastodon the link to reply on Mastodon, or nothing
|
||||
// post_nav the links to the older and the newer post and to all the news
|
||||
// og_type article
|
||||
// article_meta the published time and a tag for each category
|
||||
//
|
||||
// Whatever needs a string that the language file lacks is left out, except the
|
||||
// Mastodon link: an address that was written and cannot be labelled is an error.
|
||||
func (b *builder) postFields(i int, ctx map[string]string) error {
|
||||
p := b.posts[i]
|
||||
l := b.langs[p.Language]
|
||||
long := render.Escape(longDate(p.Date, l))
|
||||
ctx["date_long"] = long
|
||||
|
||||
ctx["post_summary"] = ""
|
||||
if p.HasDescription {
|
||||
ctx["post_summary"] = `<p class="lede">` + render.Escape(p.Description) + "</p>"
|
||||
}
|
||||
|
||||
parts := []string{fmt.Sprintf(`<time datetime="%s">%s</time>`, p.Date.Format(dateOnly), long)}
|
||||
if p.Author != "" {
|
||||
parts = append(parts, render.Escape(p.Author))
|
||||
}
|
||||
if links := categoryLinks(p); links != "" {
|
||||
parts = append(parts, links)
|
||||
}
|
||||
reading := render.Escape(readingText(p.ReadingMinutes(), l))
|
||||
ctx["post_reading"] = reading
|
||||
if reading != "" {
|
||||
parts = append(parts, reading)
|
||||
}
|
||||
ctx["post_meta"] = `<p class="meta">` + strings.Join(parts, " · ") + "</p>"
|
||||
|
||||
ctx["post_mastodon"] = ""
|
||||
if p.Mastodon != "" {
|
||||
label := l.Strings["reply_mastodon"]
|
||||
if label == "" {
|
||||
return fmt.Errorf("%s: the post has a Mastodon address, but lang/%s.json has no reply_mastodon to label the link", p.Source, p.Language)
|
||||
}
|
||||
ctx["post_mastodon"] = fmt.Sprintf(`<p><a href="%s" rel="noopener">%s</a></p>`, render.EscapeAttr(p.Mastodon), render.Escape(label))
|
||||
}
|
||||
|
||||
ctx["post_nav"] = b.postNav(i, l)
|
||||
|
||||
ctx["og_type"] = "article"
|
||||
meta := []string{fmt.Sprintf(`<meta property="article:published_time" content="%s">`, p.Date.Format(time.RFC3339))}
|
||||
for _, c := range p.Categories {
|
||||
meta = append(meta, fmt.Sprintf(`<meta property="article:tag" content="%s">`, render.EscapeAttr(c.Name)))
|
||||
}
|
||||
ctx["article_meta"] = strings.Join(meta, "\n")
|
||||
return nil
|
||||
}
|
||||
|
||||
// postNav returns the navigation of a post page: the newer post, the older
|
||||
// post and the link to all the news. It is empty when the language lacks the
|
||||
// strings post_older, post_newer and post_nav, or when there is nothing to link.
|
||||
func (b *builder) postNav(i int, l site.Lang) string {
|
||||
older, newer, label := l.Strings["post_older"], l.Strings["post_newer"], l.Strings["post_nav"]
|
||||
if older == "" || newer == "" || label == "" {
|
||||
return ""
|
||||
}
|
||||
link := func(p content.Post, rel, text string) string {
|
||||
title := render.Escape(p.Title)
|
||||
if p.Language != "" && p.Language != l.Code {
|
||||
title = fmt.Sprintf(`<span lang="%s">%s</span>`, p.Language, title)
|
||||
}
|
||||
return fmt.Sprintf(`<li><a href="%s" rel="%s">%s: %s</a></li>`, p.Path, rel, render.Escape(text), title)
|
||||
}
|
||||
var items []string
|
||||
if i > 0 {
|
||||
items = append(items, link(b.posts[i-1], "next", newer))
|
||||
}
|
||||
if i+1 < len(b.posts) {
|
||||
items = append(items, link(b.posts[i+1], "prev", older))
|
||||
}
|
||||
if all := l.Strings["news_all"]; all != "" {
|
||||
items = append(items, fmt.Sprintf(`<li><a href="%s">%s</a></li>`, b.newsPath, render.Escape(all)))
|
||||
}
|
||||
if len(items) == 0 {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf("<nav aria-label=\"%s\">\n<ul>\n%s\n</ul>\n</nav>", render.EscapeAttr(label), strings.Join(items, "\n"))
|
||||
}
|
||||
Loading…
Reference in a new issue