`)
+ if strings.Contains(m, "—") {
+ t.Error("the manifesto still has an em dash")
+ }
+}
+
+// TestBuildAssetsCopied checks the assets listed in site.json.
+func TestBuildAssetsCopied(t *testing.T) {
+ out, err := runBuild(t, baseFiles())
+ if err != nil {
+ t.Fatal(err)
+ }
+ if got := readOut(t, out, "logo.svg"); got != "\n" {
+ t.Errorf("logo.svg = %q", got)
+ }
+}
+
+// TestBuildMissingAssetFails checks that an asset that is not there stops the build.
+func TestBuildMissingAssetFails(t *testing.T) {
+ files := baseFiles()
+ delete(files, "static/logo.svg")
+ _, err := runBuild(t, files)
+ if err == nil || !strings.Contains(err.Error(), "static/logo.svg") {
+ t.Errorf("Build() error = %v, want it to name static/logo.svg", err)
+ }
+}
+
+// TestBuildFeed checks the feed at the address of the news page.
+func TestBuildFeed(t *testing.T) {
+ out, err := runBuild(t, baseFiles())
+ if err != nil {
+ t.Fatal(err)
+ }
+ feed := readOut(t, out, "news/feed.xml")
+ mustContain(t, "feed", feed,
+ "https://kmalloc.org/news/2026/11/shop.html",
+ `<a href="https://kmalloc.org/people.html">people</a>`)
+}
+
+// TestBuildErrorsOnMissingTemplate checks that a missing layout template is named.
+func TestBuildErrorsOnMissingTemplate(t *testing.T) {
+ files := baseFiles()
+ delete(files, "templates/pages/manifesto.html")
+ _, err := runBuild(t, files)
+ if err == nil || !strings.Contains(err.Error(), "pages/manifesto") {
+ t.Errorf("Build() error = %v, want it to name pages/manifesto", err)
+ }
+}
diff --git a/internal/build/context.go b/internal/build/context.go
new file mode 100644
index 0000000..b133f5c
--- /dev/null
+++ b/internal/build/context.go
@@ -0,0 +1,210 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+package build
+
+import (
+ "fmt"
+ "strings"
+
+ "git.kmalloc.org/squid/motorino/internal/render"
+)
+
+// menuPages are the IDs of the pages in the menu; the current one is marked.
+var menuPages = []string{"home", "manifesto", "people", "services", "news"}
+
+// linkedPages are the IDs the templates link to with url_.
+var linkedPages = []string{"manifesto", "people", "services", "news", "history"}
+
+// blockNames are the placeholders that expand to generated HTML. Until they
+// are expanded they stand for themselves, so a page can hold them.
+var blockNames = []string{"news_list", "latest_news", "categories_list"}
+
+// document describes one output page for the shared page frame.
+type document struct {
+ // lang is the language code of the page.
+ lang string
+ // id is the page ID; it is empty for news and category pages, which have
+ // no translation.
+ id string
+ // nav is the menu entry to mark as current; empty for none.
+ nav string
+ // lead is the footer lead template: "home", "back" or "news".
+ lead string
+ // path is the address of the page.
+ path string
+ // title is the title of the page as plain text.
+ title string
+ // prefixed puts "kmalloc.org: " before the title in the browser title.
+ prefixed bool
+ // description is the plain text of the description meta.
+ description string
+ // switches overrides the address of the language links, by language code.
+ switches map[string]string
+}
+
+// copyContext returns a copy of ctx.
+func copyContext(ctx map[string]string) map[string]string {
+ out := make(map[string]string, len(ctx))
+ for k, v := range ctx {
+ out[k] = v
+ }
+ return out
+}
+
+// pageAddress returns the address of the page with this ID in lang, or in the
+// default language when lang has no such page.
+func (b *builder) pageAddress(lang, id string) string {
+ if p, ok := b.byKey[pageKey{id, lang}]; ok {
+ return p.Path
+ }
+ return b.byKey[pageKey{id, b.defaultLang}].Path
+}
+
+// languageContext returns the placeholders that depend on the language only:
+// the site strings, the language strings (escaped), the menu labels and the
+// addresses of the pages the templates link to.
+func (b *builder) languageContext(lang string) map[string]string {
+ if cached, ok := b.langCtx[lang]; ok {
+ return copyContext(cached)
+ }
+ l := b.langs[lang]
+ ctx := copyContext(b.cfg.Strings)
+ for k, v := range l.Strings {
+ ctx[k] = render.Escape(v)
+ }
+ ctx["lang"] = lang
+ for k, label := range l.Nav {
+ ctx["nav_"+k] = render.Escape(label)
+ }
+ for _, id := range linkedPages {
+ ctx["url_"+id] = b.pageAddress(lang, id)
+ }
+ ctx["history_attr"] = ""
+ if _, ok := b.byKey[pageKey{"history", lang}]; !ok {
+ ctx["history_attr"] = fmt.Sprintf(` hreflang="%s"`, b.defaultLang)
+ }
+ ctx["version_lc"] = render.Escape(strings.ToLower(l.Strings["version"]))
+ ctx["motorino_version"] = b.opts.Version
+ for _, name := range blockNames {
+ ctx[name] = "{{" + name + "}}"
+ }
+ b.langCtx[lang] = ctx
+ return copyContext(ctx)
+}
+
+// documentContext returns the placeholders of one page: the language ones plus
+// the style, the address, the titles, the menu marks and the language links.
+func (b *builder) documentContext(d document) map[string]string {
+ ctx := b.languageContext(d.lang)
+ ctx["css"] = b.css
+ ctx["path"] = d.path
+ docURL := b.cfg.Strings["base_url"] + d.path
+ ctx["doc_url"] = docURL
+ ctx["doc_q"] = strings.NewReplacer(":", "%3A", "/", "%2F").Replace(docURL)
+ title := d.title
+ if d.prefixed {
+ title = "kmalloc.org: " + title
+ }
+ ctx["title_html"] = render.Escape(title)
+ ctx["page_title"] = render.Escape(d.title)
+ ctx["description_attr"] = render.EscapeAttr(d.description)
+ for _, k := range menuPages {
+ ctx["cur_"+k] = ""
+ if d.nav == k {
+ ctx["cur_"+k] = ` aria-current="page"`
+ }
+ }
+ ctx["lang_links"], ctx["alt_links"] = b.languageLinks(d)
+ return ctx
+}
+
+// languageLinks returns the links to the other languages for the menu and the
+// alternate links for the head. A page without translations links to the home
+// of the first other language that has one.
+func (b *builder) languageLinks(d document) (links, alternates string) {
+ type target struct{ lang, path string }
+ var twins []target
+ if d.id != "" {
+ for _, l := range b.cfg.Languages {
+ if p, ok := b.byKey[pageKey{d.id, l}]; ok && l != d.lang {
+ twins = append(twins, target{l, p.Path})
+ }
+ }
+ }
+ if len(twins) == 0 {
+ for _, l := range b.cfg.Languages {
+ if p, ok := b.byKey[pageKey{"home", l}]; ok && l != d.lang {
+ twins = append(twins, target{l, p.Path})
+ break
+ }
+ }
+ }
+ anchors := make([]string, len(twins))
+ for i, t := range twins {
+ href := t.path
+ if override, ok := d.switches[t.lang]; ok {
+ href = override
+ }
+ anchors[i] = fmt.Sprintf(`%s`, href, t.lang, t.lang, render.Escape(b.langs[t.lang].Strings["lang_name"]))
+ }
+ links = strings.Join(anchors, "\n·\n")
+
+ if d.id == "" {
+ return links, ""
+ }
+ var alts []string
+ for _, l := range b.cfg.Languages {
+ if p, ok := b.byKey[pageKey{d.id, l}]; ok {
+ alts = append(alts, fmt.Sprintf(``, l, p.Path))
+ }
+ }
+ if len(alts) > 1 {
+ alternates = strings.Join(alts, "\n")
+ }
+ return links, alternates
+}
+
+// frame wraps the body of a page in the shared frame: lead, feed icon, badges,
+// header, footer and the base template. It also records the page for the
+// output and for robots.txt.
+func (b *builder) frame(d document, ctx map[string]string, body string) error {
+ ctx["content"] = body
+ steps := []struct{ key, template string }{
+ {"lead", "lead-" + d.lead},
+ {"feed", "feed"},
+ {"badges", "badges"},
+ {"header", b.headerTemplate(d.lang)},
+ {"footer", "footer"},
+ }
+ for _, s := range steps {
+ tmpl, err := b.template(s.template)
+ if err != nil {
+ return err
+ }
+ text, err := render.Substitute("templates/"+s.template+".html", tmpl, ctx)
+ if err != nil {
+ return err
+ }
+ ctx[s.key] = text
+ }
+ base, err := b.template("base")
+ if err != nil {
+ return err
+ }
+ html, err := render.Substitute("templates/base.html", base, ctx)
+ if err != nil {
+ return err
+ }
+ b.files[outputPath(d.path)] = html
+ b.indexable = append(b.indexable, d.path)
+ return nil
+}
+
+// headerTemplate returns the header template of a language: "header" unless
+// its file says otherwise with the header key.
+func (b *builder) headerTemplate(lang string) string {
+ if name := b.langs[lang].Strings["header"]; name != "" {
+ return name
+ }
+ return "header"
+}
diff --git a/internal/build/fixture_test.go b/internal/build/fixture_test.go
new file mode 100644
index 0000000..cbbd0e1
--- /dev/null
+++ b/internal/build/fixture_test.go
@@ -0,0 +1,193 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+package build
+
+import (
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+)
+
+// writeFiles writes the files (path relative to a new directory, then content)
+// and returns the directory.
+func writeFiles(t *testing.T, files map[string]string) string {
+ t.Helper()
+ dir := t.TempDir()
+ for name, content := range files {
+ path := filepath.Join(dir, filepath.FromSlash(name))
+ if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ }
+ return dir
+}
+
+// runBuild builds the site made of files into a fresh output folder and
+// returns the folder and the build error.
+func runBuild(t *testing.T, files map[string]string) (string, error) {
+ t.Helper()
+ out := filepath.Join(t.TempDir(), "public")
+ err := Build(Options{SiteDir: writeFiles(t, files), OutDir: out, Version: "1.2.3"})
+ return out, err
+}
+
+// readOut returns the content of a file of the output, failing the test when it
+// is missing.
+func readOut(t *testing.T, out, rel string) string {
+ t.Helper()
+ data, err := os.ReadFile(filepath.Join(out, filepath.FromSlash(rel)))
+ if err != nil {
+ t.Fatalf("output file %s: %v", rel, err)
+ }
+ return string(data)
+}
+
+// mustContain fails the test for each want that is not in got.
+func mustContain(t *testing.T, name, got string, wants ...string) {
+ t.Helper()
+ for _, want := range wants {
+ if !strings.Contains(got, want) {
+ t.Errorf("%s does not contain %q\n%s", name, want, got)
+ }
+ }
+}
+
+// page returns a page file with the given ID, title, extra header lines and body.
+func page(id, title, extra, body string) string {
+ return "Title: " + title + "\nID: " + id + "\nDescription: About " + title + ".\n" + extra + "-\n" + body
+}
+
+// baseFiles is a small complete site: English and Italian with every page, a
+// Spanish manifesto, three news with categories, templates and one asset.
+func baseFiles() map[string]string {
+ return map[string]string{
+ "site.json": `{
+ "base_url": "https://kmalloc.org",
+ "languages": ["en", "it", "es"],
+ "contact": "squid@kmalloc.org",
+ "mastodon_url": "https://social.kmalloc.org/@squid",
+ "motorino_url": "https://git.kmalloc.org/squid/motorino",
+ "manifesto_version": "1.2",
+ "manifesto_date": "2026-11-14",
+ "issues_url": "https://git.kmalloc.org/squid/kmalloc.org/issues",
+ "assets": {"logo.svg": "static/logo.svg"}
+}`,
+ "lang/en.json": `{"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",
+ "nav": {"manifesto": "Manifesto", "people": "People", "services": "Services", "news": "News"}}`,
+ "lang/it.json": `{"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",
+ "nav": {"manifesto": "Manifesto", "people": "Persone", "services": "Servizi", "news": "News"}}`,
+ "lang/es.json": `{"lang_name": "Espanol", "header": "header-min", "sections": "Secciones", "home_link": "/?lang=en",
+ "nojs": "Sin JavaScript.", "powered_by": "Powered by", "back": "kmalloc.org", "version": "Version", "toc": "Indice",
+ "history_a": "Historial", "translation_note": "Traduccion automatica.", "translation_note_a": "avisar",
+ "news_p": "x", "news_all": "x", "news_empty": "x", "category_h": "x",
+ "nav": {"manifesto": "Manifiesto", "news": "News"}}`,
+
+ "content/en/index.md": page("home", "kmalloc.org", "Layout: bare\n", "