motorino/internal/build/post.go
tgiachi bf50b2e1c2 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
2026-11-16 12:06:55 +01:00

113 lines
4.2 KiB
Go

// 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, " &middot; ") + "</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"))
}