motorino/internal/build/pages.go
tgiachi eb2a8dc9b4 feat: take the menu from the site, not from the code
The menu entries were three lists in Go (the menu, the linked pages and
the required pages), so a new menu entry meant a new motorino release.
Now every page ID gets url_<id> and cur_<id> for the templates, and
header.html alone decides what is in the menu and in which order. Only
the home page and the news section stay required, since motorino builds
them itself.

Output of the real kmalloc.org site is byte for byte the same as 1.1.0.
2026-11-15 18:08:42 +01:00

237 lines
6.9 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 _, p := range b.posts {
d := document{
lang: b.defaultLang, 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, b.defaultLang, 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)
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 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(" &middot; " + strings.Join(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.defaultLang, nav: "news", lead: "news", path: categoryPath(c.Slug), title: c.Name, prefixed: true,
description: b.langs[b.defaultLang].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.defaultLang)
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 {
newsPath := b.byKey[pageKey{"news", b.defaultLang}].Path
text := feed(b.cfg.Strings["base_url"], b.langs[b.defaultLang], newsPath, b.rendered, feedLimit)
return b.publish(outputPath(newsPath+"feed.xml"), "the feed", text)
}