motorino/internal/build/pages.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

235 lines
6.8 KiB
Go

// SPDX-License-Identifier: GPL-3.0-or-later
package build
import (
"fmt"
"regexp"
"strings"
"git.kmalloc.org/squid/motorino/internal/content"
"git.kmalloc.org/squid/motorino/internal/mdx"
"git.kmalloc.org/squid/motorino/internal/render"
)
// renderPages renders every page of every language.
func (b *builder) renderPages() error {
for _, p := range b.pages {
if err := b.renderPage(p); err != nil {
return err
}
}
return nil
}
// renderPage converts the body of a page to HTML, expands the generated lists,
// puts it in its layout and in the shared frame.
func (b *builder) renderPage(p content.Page) error {
d := document{
lang: p.Lang, id: p.ID, lead: "back", path: p.Path, title: p.Title,
description: p.Description, switches: p.Switch, source: p.Source,
}
d.nav = p.ID
if p.ID == "home" {
d.lead = "home"
}
ctx := b.documentContext(d)
if p.ManifestoVersion != "" {
ctx["manifesto_version"] = render.Escape(p.ManifestoVersion)
}
if p.ManifestoDate != "" {
ctx["manifesto_date"] = render.Escape(p.ManifestoDate)
}
source := p.Body
if p.Layout == "manifesto" {
var err error
if source, err = mdx.NoEmDash(source); err != nil {
return fmt.Errorf("%s: %w", p.Source, err)
}
}
html, headings, err := b.markdown(p.Source, source, p.Lang, ctx)
if err != nil {
return err
}
ctx["body"] = html
ctx["note_block"] = ""
if p.Note != "" {
ctx["note_block"] = "<p>\n" + render.Escape(p.Note) + "\n</p>\n\n"
}
var toc []string
for _, h := range headings {
if h.Level == 2 {
toc = append(toc, fmt.Sprintf(`<li><a href="#%s">%s</a></li>`, render.EscapeAttr(h.ID), render.Escape(h.Text)))
}
}
ctx["manifesto_toc"] = strings.Join(toc, "\n")
layout, err := b.template("pages/" + p.Layout)
if err != nil {
return err
}
body, err := render.Substitute("templates/pages/"+p.Layout+".html", layout, ctx)
if err != nil {
return err
}
return b.frame(d, ctx, body)
}
// markdown replaces the placeholders of a source, converts it to HTML and
// expands the generated lists. The name is used in error messages.
func (b *builder) markdown(name, source, lang string, ctx map[string]string) (string, []mdx.Heading, error) {
text, err := mdx.SubstituteMarkdown(name, source, ctx)
if err != nil {
return "", nil, err
}
html, headings, err := mdx.Render(text)
if err != nil {
return "", nil, fmt.Errorf("%s: %w", name, err)
}
if html, err = b.expandBlocks(name, html, lang, ctx); err != nil {
return "", nil, err
}
for i := range headings {
headings[i].Text = render.Unescape(headings[i].Text)
}
return render.Unescape(html), headings, nil
}
// codeElement matches a code element, where a placeholder is shown as written.
var codeElement = regexp.MustCompile(`(?s)<code[^>]*>.*?</code>`)
// expandBlocks replaces each paragraph that holds only a block placeholder by
// the generated HTML. A block placeholder inside other text is an error. All
// blocks are checked before any is expanded, so the text of a generated list
// (a news title may say {{latest_news}}) is never taken for a placeholder.
func (b *builder) expandBlocks(name, html, lang string, ctx map[string]string) (string, error) {
outside := codeElement.ReplaceAllString(html, "")
for _, block := range blockNames {
token := "{{" + block + "}}"
if strings.Count(outside, token) != strings.Count(outside, "<p>"+token+"</p>") {
return "", fmt.Errorf("%s: {{%s}} must stand alone in a paragraph", name, block)
}
}
for _, block := range blockNames {
marker := "<p>{{" + block + "}}</p>"
if !strings.Contains(html, marker) {
continue
}
generated, err := b.block(block, lang, ctx)
if err != nil {
return "", err
}
html = strings.ReplaceAll(html, marker, generated)
}
return html, nil
}
// block returns the HTML of a generated list.
func (b *builder) block(name, lang string, ctx map[string]string) (string, error) {
switch name {
case "news_list":
if list := newsList(b.posts, lang); list != "" {
return list, nil
}
return "<p>\n" + ctx["news_empty"] + "\n</p>", nil
case "latest_news":
if len(b.posts) == 0 {
return "", nil
}
tmpl, err := b.template("latest-news")
if err != nil {
return "", err
}
with := copyContext(ctx)
with["news_items"] = latestNews(b.posts, lang, newsOnHome)
return render.Substitute("templates/latest-news.html", tmpl, with)
default:
list, _ := categoriesList(b.posts)
return list, nil
}
}
// renderPosts renders every news item and keeps its HTML for the feed.
func (b *builder) renderPosts() error {
tmpl, err := b.template("post")
if err != nil {
return err
}
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,
}
ctx := b.documentContext(d)
html, _, err := b.markdown(p.Source, p.Body, p.Language, ctx)
if err != nil {
return err
}
day := p.Date.Format(dateOnly)
ctx["post_title"] = render.Escape(p.Title)
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
}
if err := b.frame(d, ctx, body); err != nil {
return err
}
b.rendered = append(b.rendered, renderedPost{Post: p, HTML: html})
}
return nil
}
// byline returns the author and the category links of a post, each after a
// middle dot, or an empty string when the post has neither.
func byline(p content.Post) string {
var out strings.Builder
if p.Author != "" {
out.WriteString(" &middot; " + render.Escape(p.Author))
}
if links := categoryLinks(p); links != "" {
out.WriteString(" &middot; " + links)
}
return out.String()
}
// renderCategories renders the page of every category.
func (b *builder) renderCategories() error {
if len(b.categories) == 0 {
return nil
}
tmpl, err := b.template("category")
if err != nil {
return err
}
for _, c := range b.categories {
d := document{
lang: b.newsLang, nav: "news", lead: "news", path: categoryPath(c.Slug), title: c.Name, prefixed: true,
description: b.langs[b.newsLang].Strings["category_h"] + ": " + c.Name,
source: fmt.Sprintf("the page of the category %q", c.Name),
}
ctx := b.documentContext(d)
ctx["category_name"] = render.Escape(c.Name)
ctx["category_posts"] = newsList(c.posts, b.newsLang)
body, err := render.Substitute("templates/category.html", tmpl, ctx)
if err != nil {
return err
}
if err := b.frame(d, ctx, body); err != nil {
return err
}
}
return nil
}
// renderFeed writes the feed next to the news page.
func (b *builder) renderFeed() error {
text := feed(b.cfg.Strings["base_url"], b.langs[b.newsLang], b.newsPath, b.rendered, feedLimit)
return b.publish(outputPath(b.newsPath+"feed.xml"), "the feed", text)
}