Merge pull request 'Add the motorino generator' (#3) from feat/generator into main

This commit is contained in:
squid 2026-11-15 09:04:38 +00:00
commit 324f015737
40 changed files with 9928 additions and 0 deletions

25
Makefile Normal file
View file

@ -0,0 +1,25 @@
# SPDX-License-Identifier: GPL-3.0-or-later
GO ?= go
MODULE := git.kmalloc.org/squid/motorino
VERSION ?= dev
LDFLAGS := -s -w -X $(MODULE)/internal/version.value=$(VERSION)
.PHONY: check build dist
# check runs the formatter check, the vet, staticcheck and the tests.
check:
@test -z "$$(gofmt -l .)" || { echo "gofmt needed on:"; gofmt -l .; exit 1; }
$(GO) vet ./...
staticcheck ./...
$(GO) test ./...
# build compiles a local binary into bin/motorino.
build:
$(GO) build -trimpath -ldflags "$(LDFLAGS)" -o bin/motorino ./cmd/motorino
# dist compiles the release binary for the server and writes its SHA-256.
dist:
mkdir -p dist
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GO) build -trimpath -ldflags "$(LDFLAGS)" -o dist/motorino-linux-amd64 ./cmd/motorino
cd dist && sha256sum motorino-linux-amd64 > motorino-linux-amd64.sha256

65
README.md Normal file
View file

@ -0,0 +1,65 @@
# motorino
The static site generator of [kmalloc.org](https://kmalloc.org/). It turns Markdown pages, news, templates and language files into plain HTML with no JavaScript. It is made for that one site and is not a general-purpose generator.
Licence: GPL-3.0-or-later, see `LICENSE`.
## Usage
```
motorino build -site DIR -out DIR build the site in DIR into the output folder (defaults: . and public)
motorino version print the version
```
The binary is a single static Go program. The version comes from semantic-release.
## What a site looks like
```
site.json shared strings (contact, addresses, base_url), the language list, the assets
lang/<lang>.json menu, footer and other interface strings of one language
content/<lang>/**/*.md the pages; index.md is the home of a folder
news/<name>.md the news, one file each
templates/**/*.html the page chrome, the layouts, the news post and category page
static/style.css the stylesheet, inlined in every page
```
The first language in `site.json` is the default and lives at the root of the site; the others live under `/<lang>/`.
### Header
Every page and every news file starts with lines of `Key: value`, ended by a line that holds a single `-`. The body is Markdown, and raw HTML is allowed in it.
News:
```
Title: GNU/Linux Day 2026
Author: Matteo Bini
Date: 2026-09-18T17:00:45+0200
Categories: Linux Day, FLUG
Description: An optional one-line summary.
-
The text of the news.
```
A news file `news/name.md` is published as `/news/YYYY/MM/name.html`, with the year and the month of `Date` as written. Every category gets a page, `/news/category/<name>.html`.
Pages: `Title`, `ID` (the same in every language, it links the translations), `Description`, and optionally `Layout` (`page`, `bare` or `manifesto`), `Note`, `Switch` (`lang=/path, lang=/path`, overrides the language links) and `ManifestoVersion` and `ManifestoDate` (a translation of an older manifesto says which version it translates).
### Placeholders
`{{name}}`, with lowercase letters, digits and underscores, is replaced by a string from `site.json` or the language file. An unknown placeholder stops the build and names the file. `{{news_list}}`, `{{latest_news}}` and `{{categories_list}}` stand alone in a paragraph and expand to generated lists.
## Development
```
make check gofmt, go vet, staticcheck and the tests
make build a local binary in bin/motorino
make dist the release binary and its SHA-256 in dist/
```
Go code has a doc comment on every package and declaration, and is formatted with `gofmt`. Commits follow conventional commits.
## Release
semantic-release is run by hand from a clean `main`: `npm run release:dry`, then `npm run release`. It tags `vX.Y.Z`, updates `CHANGELOG.md`, builds the `linux/amd64` binary and publishes it, with its SHA-256, as a release on this repository. The site deploy pins a version and checks the hash before it runs the binary.

69
cmd/motorino/main.go Normal file
View file

@ -0,0 +1,69 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Command motorino builds the kmalloc.org site from Markdown, templates and
// language files.
//
// Usage:
//
// motorino build [-site DIR] [-out DIR]
// motorino version
package main
import (
"errors"
"flag"
"fmt"
"io"
"os"
"git.kmalloc.org/squid/motorino/internal/build"
"git.kmalloc.org/squid/motorino/internal/version"
)
// usage is the text printed for a missing or unknown command.
const usage = "usage: motorino build [-site DIR] [-out DIR] | motorino version"
// main runs the command line and exits with its status code.
func main() {
os.Exit(run(os.Args[1:], os.Stdout, os.Stderr))
}
// run executes one command line and returns the exit code: 0 for success, 1
// for a failed command and 2 for a mistake in the command line.
func run(args []string, stdout, stderr io.Writer) int {
if len(args) == 0 {
fmt.Fprintln(stderr, usage)
return 2
}
switch args[0] {
case "version":
fmt.Fprintln(stdout, "motorino", version.String())
return 0
case "build":
return runBuild(args[1:], stdout, stderr)
default:
fmt.Fprintf(stderr, "motorino: unknown command %q\n%s\n", args[0], usage)
return 2
}
}
// runBuild parses the flags of the build command and builds the site.
func runBuild(args []string, stdout, stderr io.Writer) int {
flags := flag.NewFlagSet("build", flag.ContinueOnError)
flags.SetOutput(stderr)
site := flags.String("site", ".", "the folder of the site")
out := flags.String("out", "public", "the folder the HTML is written to")
if err := flags.Parse(args); err != nil {
if errors.Is(err, flag.ErrHelp) {
return 0
}
return 2
}
result, err := build.Build(build.Options{SiteDir: *site, OutDir: *out, Version: version.String()})
if err != nil {
fmt.Fprintf(stderr, "motorino: %v\n", err)
return 1
}
fmt.Fprintf(stdout, "built %d pages into %s\n", result.Pages, *out)
return 0
}

110
cmd/motorino/main_test.go Normal file
View file

@ -0,0 +1,110 @@
// SPDX-License-Identifier: GPL-3.0-or-later
package main
import (
"bytes"
"os"
"path/filepath"
"strings"
"testing"
)
// miniSite writes the smallest site that builds: every required page in one
// language and the templates the frame needs. It returns the folder.
func miniSite(t *testing.T) string {
t.Helper()
files := map[string]string{
"site.json": `{"base_url": "https://x.org", "languages": ["en"], "mastodon_url": "m", "motorino_url": "u"}`,
"lang/en.json": `{"lang_name": "English", "home_link": "/", "back": "b", "news_all": "a", "news_p": "p",
"news_empty": "e", "nojs": "n", "history_a": "h", "version": "v", "powered_by": "P", "category_h": "c",
"nav": {"news": "News", "manifesto": "M"}}`,
"static/style.css": "body{}\n",
"templates/base.html": "{{header}}{{content}}{{footer}}\n",
"templates/header.html": "<header>{{nav_news}}</header>\n",
"templates/footer.html": "<footer>{{lead}}{{badges}}</footer>\n",
"templates/lead-home.html": "h\n", "templates/lead-back.html": "b\n", "templates/lead-news.html": "n\n",
"templates/badges.html": "{{feed}}\n", "templates/feed.html": "f\n",
"templates/pages/page.html": "<h1>{{page_title}}</h1>{{body}}\n",
"templates/post.html": "{{post_body}}\n", "templates/category.html": "{{category_posts}}\n",
}
for _, id := range []string{"home", "manifesto", "people", "services", "news", "history"} {
name := id
if id == "home" {
name = "index"
}
files["content/en/"+name+".md"] = "Title: " + id + "\nID: " + id + "\nDescription: d\n-\nBody of " + id + ".\n"
}
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
}
// TestRunVersion checks that the version command prints the name and the version.
func TestRunVersion(t *testing.T) {
var out, errOut bytes.Buffer
if code := run([]string{"version"}, &out, &errOut); code != 0 {
t.Fatalf("run(version) = %d, stderr %q", code, errOut.String())
}
if got := strings.TrimSpace(out.String()); got != "motorino dev" {
t.Errorf("output = %q, want %q", got, "motorino dev")
}
}
// TestRunBuildSuccess checks a build: exit 0, a summary line and the pages.
func TestRunBuildSuccess(t *testing.T) {
site := miniSite(t)
outDir := filepath.Join(t.TempDir(), "public")
var out, errOut bytes.Buffer
code := run([]string{"build", "-site", site, "-out", outDir}, &out, &errOut)
if code != 0 {
t.Fatalf("run(build) = %d, stderr %q", code, errOut.String())
}
if want := "built 6 pages into " + outDir; !strings.Contains(out.String(), want) {
t.Errorf("output %q does not contain %q", out.String(), want)
}
if _, err := os.Stat(filepath.Join(outDir, "index.html")); err != nil {
t.Errorf("no index.html in the output: %v", err)
}
}
// TestRunBuildErrorExit1 checks that a failed build exits 1 and says why.
func TestRunBuildErrorExit1(t *testing.T) {
var out, errOut bytes.Buffer
code := run([]string{"build", "-site", t.TempDir(), "-out", filepath.Join(t.TempDir(), "public")}, &out, &errOut)
if code != 1 {
t.Fatalf("run(build) = %d, want 1", code)
}
if !strings.HasPrefix(errOut.String(), "motorino: ") || !strings.Contains(errOut.String(), "site.json") {
t.Errorf("stderr = %q, want a line that starts with %q and names site.json", errOut.String(), "motorino: ")
}
}
// TestRunUnknownCommand checks the usage message and exit 2.
func TestRunUnknownCommand(t *testing.T) {
for _, args := range [][]string{{}, {"frobnicate"}} {
var out, errOut bytes.Buffer
if code := run(args, &out, &errOut); code != 2 {
t.Errorf("run(%v) = %d, want 2", args, code)
}
if !strings.Contains(errOut.String(), "usage:") {
t.Errorf("run(%v) stderr = %q, want a usage line", args, errOut.String())
}
}
}
// TestRunBuildBadFlag checks that an unknown flag is a usage error.
func TestRunBuildBadFlag(t *testing.T) {
var out, errOut bytes.Buffer
if code := run([]string{"build", "-nope"}, &out, &errOut); code != 2 {
t.Errorf("run(build -nope) = %d, want 2", code)
}
}

5
go.mod Normal file
View file

@ -0,0 +1,5 @@
module git.kmalloc.org/squid/motorino
go 1.27
require github.com/yuin/goldmark v1.8.7

2
go.sum Normal file
View file

@ -0,0 +1,2 @@
github.com/yuin/goldmark v1.8.7 h1:XT/tNqCIbabh0ln675w4x3dv04kQBKi8NwYqi9IJaxI=
github.com/yuin/goldmark v1.8.7/go.mod h1:ip/1k0VRfGynBgxOz0yCqHrbZXhcjxyuS66Brc7iBKg=

275
internal/build/build.go Normal file
View file

@ -0,0 +1,275 @@
// SPDX-License-Identifier: GPL-3.0-or-later
package build
import (
"fmt"
"os"
"path"
"path/filepath"
"sort"
"strings"
"git.kmalloc.org/squid/motorino/internal/content"
"git.kmalloc.org/squid/motorino/internal/site"
)
// Options tell Build where the site is, where to write it and which version
// of motorino to put in the generator meta.
type Options struct {
// SiteDir is the folder of the site: site.json, lang/, content/, news/,
// templates/ and static/.
SiteDir string
// OutDir is the folder the HTML is written to; it is created when missing.
OutDir string
// Version is the version of motorino, shown in the generator meta.
Version string
}
// requiredPages are the IDs every site must have in its default language,
// because the menu and the footer link to them.
var requiredPages = []string{"home", "manifesto", "people", "services", "news", "history"}
// newsOnHome is how many news the home page lists; feedLimit is how many the feed has.
const (
newsOnHome = 3
feedLimit = 20
)
// pageKey identifies a page by its ID and language.
type pageKey struct{ id, lang string }
// builder holds everything loaded for one build and the files it produced.
type builder struct {
opts Options
cfg site.Config
langs map[string]site.Lang
defaultLang string
pages []content.Page
posts []content.Post
categories []categoryEntry
templates map[string]string
css string
byKey map[pageKey]content.Page
// files holds the rendered output: relative path to content.
files map[string]string
// owners names, for each output file, what produces it.
owners map[string]string
// indexable holds the addresses listed in robots.txt.
indexable []string
// rendered holds the posts with their HTML, for the feed.
rendered []renderedPost
// langCtx caches the language level placeholders.
langCtx map[string]map[string]string
}
// categoryEntry is a category with the posts that have it.
type categoryEntry struct {
content.Category
posts []content.Post
}
// Result summarises a build.
type Result struct {
// Pages is the number of HTML pages written: pages, news and categories.
Pages int
}
// Build reads the site in opts.SiteDir and writes it to opts.OutDir. It renders
// everything in memory first, so a failure leaves the output folder untouched.
func Build(opts Options) (Result, error) {
b, err := load(opts)
if err != nil {
return Result{}, err
}
if err := b.renderPages(); err != nil {
return Result{}, err
}
if err := b.renderPosts(); err != nil {
return Result{}, err
}
if err := b.renderCategories(); err != nil {
return Result{}, err
}
if err := b.renderFeed(); err != nil {
return Result{}, err
}
if err := b.publish("robots.txt", "robots.txt", robots(b.indexable)); err != nil {
return Result{}, err
}
assets, err := b.readAssets()
if err != nil {
return Result{}, err
}
if err := b.write(assets); err != nil {
return Result{}, err
}
return Result{Pages: len(b.indexable)}, nil
}
// load reads the configuration, the content, the templates and the style.
func load(opts Options) (*builder, error) {
cfg, langs, err := site.Load(opts.SiteDir)
if err != nil {
return nil, err
}
b := &builder{
opts: opts, cfg: cfg, langs: langs, defaultLang: cfg.Languages[0],
byKey: map[pageKey]content.Page{}, files: map[string]string{}, owners: map[string]string{},
langCtx: map[string]map[string]string{},
}
if b.pages, err = content.LoadPages(opts.SiteDir, cfg.Languages); err != nil {
return nil, err
}
if b.posts, err = content.LoadPosts(opts.SiteDir); err != nil {
return nil, err
}
for _, p := range b.pages {
b.byKey[pageKey{p.ID, p.Lang}] = p
for lang := range p.Switch {
if _, known := langs[lang]; !known {
return nil, fmt.Errorf("%s: Switch names the language %q, which is not in site.json", p.Source, lang)
}
}
}
for _, id := range requiredPages {
if _, ok := b.byKey[pageKey{id, b.defaultLang}]; !ok {
return nil, fmt.Errorf("content/%s: there is no page with ID %q", b.defaultLang, id)
}
}
if b.templates, err = readTemplates(opts.SiteDir); err != nil {
return nil, err
}
css, err := os.ReadFile(filepath.Join(opts.SiteDir, "static", "style.css"))
if err != nil {
return nil, fmt.Errorf("read static/style.css: %w", err)
}
b.css = strings.Trim(string(css), "\n")
b.categories = groupCategories(b.posts)
return b, nil
}
// groupCategories lists the categories of the posts, sorted by name, each with
// its posts.
func groupCategories(posts []content.Post) []categoryEntry {
_, cats := categoriesList(posts)
out := make([]categoryEntry, len(cats))
for i, c := range cats {
out[i] = categoryEntry{Category: c, posts: postsIn(posts, c.Slug)}
}
return out
}
// readTemplates reads every templates/**/*.html; the key is the path under
// templates/ without the extension, such as "pages/manifesto".
func readTemplates(dir string) (map[string]string, error) {
root := filepath.Join(dir, "templates")
out := map[string]string{}
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() || !strings.HasSuffix(path, ".html") {
return err
}
data, err := os.ReadFile(path)
if err != nil {
return err
}
rel, err := filepath.Rel(root, path)
if err != nil {
return err
}
out[strings.TrimSuffix(filepath.ToSlash(rel), ".html")] = string(data)
return nil
})
if err != nil {
return nil, fmt.Errorf("read templates: %w", err)
}
return out, nil
}
// template returns a template by key or an error naming its file.
func (b *builder) template(key string) (string, error) {
t, ok := b.templates[key]
if !ok {
return "", fmt.Errorf("templates/%s.html: not found", key)
}
return t, nil
}
// claim records that source produces the output file rel, and returns an error
// naming both sources when another one already does.
func (b *builder) claim(rel, source string) error {
if other, taken := b.owners[rel]; taken {
return fmt.Errorf("%s and %s are both published as %s", other, source, rel)
}
b.owners[rel] = source
return nil
}
// publish stores a rendered file; source names what produced it.
func (b *builder) publish(rel, source, text string) error {
if err := b.claim(rel, source); err != nil {
return err
}
b.files[rel] = text
return nil
}
// readAssets reads the files listed in site.json; a missing source is an
// error, and so is an asset that has the address of a page or of another asset.
func (b *builder) readAssets() (map[string][]byte, error) {
names := make([]string, 0, len(b.cfg.Assets))
for name := range b.cfg.Assets {
names = append(names, name)
}
sort.Strings(names)
out := map[string][]byte{}
for _, name := range names {
source := b.cfg.Assets[name]
data, err := os.ReadFile(filepath.Join(b.opts.SiteDir, filepath.FromSlash(source)))
if err != nil {
return nil, fmt.Errorf("asset %q: %w", name, err)
}
rel := path.Clean(name)
if err := b.claim(rel, fmt.Sprintf("the asset %q (%s)", name, source)); err != nil {
return nil, err
}
out[rel] = data
}
return out, nil
}
// write creates the output folder and writes the pages and the assets.
func (b *builder) write(assets map[string][]byte) error {
for rel, text := range b.files {
if err := writeFile(b.opts.OutDir, rel, []byte(text)); err != nil {
return err
}
}
for rel, data := range assets {
if err := writeFile(b.opts.OutDir, rel, data); err != nil {
return err
}
}
return nil
}
// writeFile writes data to rel under dir, creating the folders.
func writeFile(dir, rel string, data []byte) error {
path := filepath.Join(dir, filepath.FromSlash(rel))
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return fmt.Errorf("create %s: %w", filepath.Dir(path), err)
}
if err := os.WriteFile(path, data, 0o644); err != nil {
return fmt.Errorf("write %s: %w", path, err)
}
return nil
}
// outputPath returns the file of an address: a folder address gets index.html.
func outputPath(address string) string {
rel := strings.TrimPrefix(address, "/")
if rel == "" || strings.HasSuffix(rel, "/") {
rel += "index.html"
}
return rel
}

View file

@ -0,0 +1,402 @@
// SPDX-License-Identifier: GPL-3.0-or-later
package build
import (
"os"
"path/filepath"
"strings"
"testing"
)
// htmlFiles returns the relative paths of every HTML file of the output.
func htmlFiles(t *testing.T, out string) []string {
t.Helper()
var files []string
err := filepath.WalkDir(out, func(p string, d os.DirEntry, err error) error {
if err == nil && !d.IsDir() && strings.HasSuffix(p, ".html") {
rel, _ := filepath.Rel(out, p)
files = append(files, filepath.ToSlash(rel))
}
return err
})
if err != nil {
t.Fatal(err)
}
return files
}
// TestBuildWritesPagesAndPaths checks that every page lands at its address.
func TestBuildWritesPagesAndPaths(t *testing.T) {
out, err := runBuild(t, baseFiles())
if err != nil {
t.Fatalf("Build() error = %v", err)
}
for _, rel := range []string{
"index.html", "it/index.html", "manifesto.html", "it/manifesto.html", "es/manifesto.html",
"manifesto-history.html", "it/cronologia-manifesto.html", "people.html", "it/persone.html",
"services.html", "it/servizi.html", "news/index.html",
} {
if _, err := os.Stat(filepath.Join(out, rel)); err != nil {
t.Errorf("missing output %s: %v", rel, err)
}
}
mustContain(t, "services.html", readOut(t, out, "services.html"),
"<title>Services</title>", `<meta name="description" content="About Services.">`,
"<strong>squid (at) kmalloc.org</strong>", `<link rel="me" href="https://social.kmalloc.org/@squid">`)
}
// TestBuildNewsPathsDated checks the address and the byline of a post.
func TestBuildNewsPathsDated(t *testing.T) {
out, err := runBuild(t, baseFiles())
if err != nil {
t.Fatal(err)
}
post := readOut(t, out, "news/2026/09/gnu-linux-day-2026.html")
mustContain(t, "post", post,
"<h1>GNU/Linux Day 2026</h1>", `<time datetime="2026-09-18">2026-09-18</time>`,
"Matteo Bini", `<a href="/news/category/linux-day.html">Linux Day</a>`, `<a href="/news/category/flug.html">FLUG</a>`,
"<title>kmalloc.org: GNU/Linux Day 2026</title>", `<a href="/people.html">people</a>`)
readOut(t, out, "news/2026/11/shop.html")
readOut(t, out, "news/2026/11/status.html")
}
// TestBuildCategoryPages checks the page of each category and its posts.
func TestBuildCategoryPages(t *testing.T) {
out, err := runBuild(t, baseFiles())
if err != nil {
t.Fatal(err)
}
flug := readOut(t, out, "news/category/flug.html")
mustContain(t, "flug", flug, "<h1>Category: FLUG</h1>", "GNU/Linux Day 2026", "A shop")
if strings.Contains(flug, "Status page") {
t.Error("category FLUG lists a post that is not in it")
}
day := readOut(t, out, "news/category/linux-day.html")
mustContain(t, "linux-day", day, "GNU/Linux Day 2026")
if strings.Contains(day, "A shop") {
t.Error("category Linux Day lists a post that is not in it")
}
index := readOut(t, out, "news/index.html")
mustContain(t, "news index", index,
`<a href="/news/category/flug.html">FLUG</a> (2)`, `<a href="/news/category/linux-day.html">Linux Day</a> (1)`)
}
// TestBuildRobotsAllowsEverything checks robots.txt and that no page asks to be
// left out of the index.
func TestBuildRobotsAllowsEverything(t *testing.T) {
out, err := runBuild(t, baseFiles())
if err != nil {
t.Fatal(err)
}
robots := readOut(t, out, "robots.txt")
mustContain(t, "robots.txt", robots,
"User-agent: *", "Allow: /$", "Allow: /?lang=en$", "Allow: /it/servizi.html$", "Allow: /news/$",
"Allow: /news/2026/11/shop.html$", "Allow: /news/category/flug.html$", "Allow: /es/manifesto.html$")
if !strings.HasSuffix(robots, "Disallow: /\n") {
t.Errorf("robots.txt does not end with Disallow: /\n%s", robots)
}
for _, rel := range htmlFiles(t, out) {
if strings.Contains(readOut(t, out, rel), `name="robots"`) {
t.Errorf("%s has a robots meta", rel)
}
}
}
// TestBuildHreflangAndLangLinks checks the links between languages.
func TestBuildHreflangAndLangLinks(t *testing.T) {
out, err := runBuild(t, baseFiles())
if err != nil {
t.Fatal(err)
}
en := readOut(t, out, "services.html")
mustContain(t, "en services", en,
`<link rel="alternate" hreflang="en" href="/services.html">`, `<link rel="alternate" hreflang="it" href="/it/servizi.html">`,
`<a href="/it/servizi.html" hreflang="it" lang="it">Italiano</a>`)
it := readOut(t, out, "it/servizi.html")
mustContain(t, "it services", it, `<a href="/services.html" hreflang="en" lang="en">English</a>`)
// A page with no translation points to the home of another language.
post := readOut(t, out, "news/2026/11/shop.html")
mustContain(t, "post", post, `<a href="/it/" hreflang="it" lang="it">Italiano</a>`)
if strings.Contains(post, `rel="alternate" hreflang`) {
t.Error("a page without translation has alternate links")
}
// The Spanish manifesto has the short header and links to its siblings.
es := readOut(t, out, "es/manifesto.html")
mustContain(t, "es manifesto", es, "Traduccion automatica.", "Traduccion de la version 1.0.",
`<a href="/manifesto.html" hreflang="en" lang="en">English</a>`)
}
// TestBuildMenuMarksTheCurrentPage checks aria-current on the menu.
func TestBuildMenuMarksTheCurrentPage(t *testing.T) {
out, err := runBuild(t, baseFiles())
if err != nil {
t.Fatal(err)
}
mustContain(t, "manifesto", readOut(t, out, "manifesto.html"), `<a href="/manifesto.html" aria-current="page">Manifesto</a>`)
mustContain(t, "post", readOut(t, out, "news/2026/11/shop.html"), `<a href="/news/" aria-current="page">News</a>`)
if strings.Contains(readOut(t, out, "people.html"), `aria-current="page">News`) {
t.Error("the News entry is marked on the People page")
}
}
// TestBuildBlockPlaceholders checks the generated lists inside pages.
func TestBuildBlockPlaceholders(t *testing.T) {
out, err := runBuild(t, baseFiles())
if err != nil {
t.Fatal(err)
}
mustContain(t, "news index", readOut(t, out, "news/index.html"),
`<li><time datetime="2026-11-14">2026-11-14</time> <a href="/news/2026/11/shop.html">A shop</a>: About the shop.</li>`)
home := readOut(t, out, "index.html")
mustContain(t, "home", home, `<h2 id="news">News</h2>`, `<a href="/news/2026/11/shop.html">A shop</a>`)
it := readOut(t, out, "it/index.html")
mustContain(t, "it home", it, `<a href="/news/2026/11/shop.html" hreflang="en" lang="en">A shop</a>`)
if strings.Contains(home, "{{") || strings.Contains(home, "<p><ul>") {
t.Errorf("home has a leftover placeholder or a wrapped list:\n%s", home)
}
}
// TestBuildNoNews checks the pages when the site has no news.
func TestBuildNoNews(t *testing.T) {
files := baseFiles()
for _, name := range []string{"news/gnu-linux-day-2026.md", "news/shop.md", "news/status.md"} {
delete(files, name)
}
out, err := runBuild(t, files)
if err != nil {
t.Fatal(err)
}
mustContain(t, "news index", readOut(t, out, "news/index.html"), "No news yet.")
if strings.Contains(readOut(t, out, "index.html"), `<h2 id="news">`) {
t.Error("the home shows a news section but there are no news")
}
}
// TestBuildUnknownPlaceholderFailsAndWritesNothing checks the all-or-nothing rule.
func TestBuildUnknownPlaceholderFailsAndWritesNothing(t *testing.T) {
files := baseFiles()
files["content/en/people.md"] = page("people", "People", "", "Hello {{nope}}.\n")
out, err := runBuild(t, files)
if err == nil || !strings.Contains(err.Error(), "content/en/people.md") || !strings.Contains(err.Error(), "nope") {
t.Fatalf("Build() error = %v, want it to name content/en/people.md and nope", err)
}
if _, statErr := os.Stat(out); statErr == nil {
entries, _ := os.ReadDir(out)
if len(entries) != 0 {
t.Errorf("the output has %d entries after a failed build", len(entries))
}
}
}
// TestBuildLeavesNonPlaceholders checks that text like {{.Name}} reaches the page.
func TestBuildLeavesNonPlaceholders(t *testing.T) {
files := baseFiles()
files["content/en/people.md"] = page("people", "People", "", "A template looks like `{{.Name}}`.\n")
out, err := runBuild(t, files)
if err != nil {
t.Fatal(err)
}
mustContain(t, "people", readOut(t, out, "people.html"), "<code>{{.Name}}</code>")
}
// TestBuildBlockPlaceholderMustStandAlone checks the rule for generated lists.
func TestBuildBlockPlaceholderMustStandAlone(t *testing.T) {
files := baseFiles()
files["content/en/people.md"] = page("people", "People", "", "Look {{news_list}} here.\n")
_, err := runBuild(t, files)
if err == nil || !strings.Contains(err.Error(), "content/en/people.md") || !strings.Contains(err.Error(), "news_list") {
t.Errorf("Build() error = %v, want it to name content/en/people.md and news_list", err)
}
}
// TestBuildNeedsRequiredPages checks that the menu pages must exist in the default language.
func TestBuildNeedsRequiredPages(t *testing.T) {
files := baseFiles()
delete(files, "content/en/news/index.md")
_, err := runBuild(t, files)
if err == nil || !strings.Contains(err.Error(), "news") {
t.Errorf("Build() error = %v, want it to name the missing news page", err)
}
}
// TestBuildFooterPoweredBy checks the credit line on every page.
func TestBuildFooterPoweredBy(t *testing.T) {
out, err := runBuild(t, baseFiles())
if err != nil {
t.Fatal(err)
}
files := htmlFiles(t, out)
if len(files) < 12 {
t.Fatalf("only %d html files", len(files))
}
for _, rel := range files {
mustContain(t, rel, readOut(t, out, rel), `Powered by <a href="https://git.kmalloc.org/squid/motorino">motorino</a>`)
}
}
// TestBuildGeneratorMeta checks the generator meta on every page.
func TestBuildGeneratorMeta(t *testing.T) {
out, err := runBuild(t, baseFiles())
if err != nil {
t.Fatal(err)
}
for _, rel := range htmlFiles(t, out) {
mustContain(t, rel, readOut(t, out, rel), `<meta name="generator" content="motorino 1.2.3">`)
}
}
// TestBuildManifestoLayoutRewritesEmDash checks the manifesto rewrite and its table of contents.
func TestBuildManifestoLayoutRewritesEmDash(t *testing.T) {
out, err := runBuild(t, baseFiles())
if err != nil {
t.Fatal(err)
}
m := readOut(t, out, "manifesto.html")
mustContain(t, "manifesto", m,
"<h1>Manifesto</h1>", "Version 1.2, <time datetime=\"2026-11-14\">2026-11-14</time>",
`<h2 id="01">01. Antifascist</h2>`, `<h2 id="commitments">Our commitments</h2>`,
`<li><a href="#01">01. Antifascist</a></li>`, `<li><a href="#commitments">Our commitments</a></li>`)
if strings.Contains(m, "—") {
t.Error("the manifesto still has an em dash")
}
}
// TestBuildManifestoVersionOverride checks that a translation can say which
// version of the manifesto it translates, while the other pages use the current one.
func TestBuildManifestoVersionOverride(t *testing.T) {
out, err := runBuild(t, baseFiles())
if err != nil {
t.Fatal(err)
}
mustContain(t, "es manifesto", readOut(t, out, "es/manifesto.html"), `Version 1.0, <time datetime="2026-11-12">2026-11-12</time>`)
mustContain(t, "en manifesto", readOut(t, out, "manifesto.html"), `Version 1.2, <time datetime="2026-11-14">2026-11-14</time>`)
}
// TestBuildEscapesQuotesInTitle checks that a title with quotes cannot break
// out of an attribute such as og:title.
func TestBuildEscapesQuotesInTitle(t *testing.T) {
files := baseFiles()
files["news/quoted.md"] = "Title: A \"shop\" with <mugs>\nDate: 2026-11-15T12:00:00+0100\n-\nText.\n"
out, err := runBuild(t, files)
if err != nil {
t.Fatal(err)
}
post := readOut(t, out, "news/2026/11/quoted.html")
mustContain(t, "post", post, `content="kmalloc.org: A &quot;shop&quot; with &lt;mugs&gt;"`)
if strings.Contains(post, `content="kmalloc.org: A "shop"`) {
t.Error("the title broke out of the attribute")
}
}
// TestBuildRejectsSameAddress checks that two outputs with one address are an
// error naming both sources, instead of one silently replacing the other.
func TestBuildRejectsSameAddress(t *testing.T) {
tests := []struct {
name string
extra map[string]string
want []string
}{
{"two pages", map[string]string{"content/en/it/index.md": page("other", "Other", "", "x\n")},
[]string{"content/en/it/index.md", "content/it/index.md"}},
{"page and category", map[string]string{"content/en/news/category/flug.md": page("flugpage", "Flug", "", "x\n")},
[]string{"content/en/news/category/flug.md", "flug"}},
{"page and post", map[string]string{"content/en/news/2026/11/shop.md": page("shoppage", "Shop", "", "x\n")},
[]string{"content/en/news/2026/11/shop.md", "news/shop.md"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
files := baseFiles()
for k, v := range tt.extra {
files[k] = v
}
_, err := runBuild(t, files)
if err == nil {
t.Fatal("Build() error = nil")
}
for _, want := range tt.want {
if !strings.Contains(err.Error(), want) {
t.Errorf("error %q does not contain %q", err, want)
}
}
})
}
}
// TestBuildRejectsAssetOverPage checks that an asset cannot take the address of a page.
func TestBuildRejectsAssetOverPage(t *testing.T) {
files := baseFiles()
files["site.json"] = strings.Replace(files["site.json"], `"logo.svg": "static/logo.svg"`, `"index.html": "static/logo.svg"`, 1)
_, err := runBuild(t, files)
if err == nil || !strings.Contains(err.Error(), "index.html") {
t.Errorf("Build() error = %v, want it to name index.html", err)
}
}
// TestBuildEscapesSwitchHref checks that a language switch cannot inject an attribute.
func TestBuildEscapesSwitchHref(t *testing.T) {
files := baseFiles()
files["content/en/services.md"] = page("services", "Services", "Switch: it=/x\" onmouseover=\"y\n", "Body.\n")
out, err := runBuild(t, files)
if err != nil {
t.Fatal(err)
}
got := readOut(t, out, "services.html")
mustContain(t, "services", got, `href="/x&quot; onmouseover=&quot;y"`)
if strings.Contains(got, `onmouseover="y"`) {
t.Error("the switch value injected an attribute")
}
}
// TestBuildRejectsSwitchToUnknownLanguage checks that a typo in Switch is not ignored.
func TestBuildRejectsSwitchToUnknownLanguage(t *testing.T) {
files := baseFiles()
files["content/en/services.md"] = page("services", "Services", "Switch: xx=/x\n", "Body.\n")
_, err := runBuild(t, files)
if err == nil || !strings.Contains(err.Error(), "content/en/services.md") || !strings.Contains(err.Error(), "xx") {
t.Errorf("Build() error = %v, want it to name content/en/services.md and xx", err)
}
}
// 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 != "<svg/>\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,
"<guid isPermaLink=\"true\">https://kmalloc.org/news/2026/11/shop.html</guid>",
`&lt;a href="https://kmalloc.org/people.html"&gt;people&lt;/a&gt;`)
}
// 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)
}
}

214
internal/build/context.go Normal file
View file

@ -0,0 +1,214 @@
// 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_<id>.
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
// source names what produces the page, for error messages.
source 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.EscapeAttr(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(`<a href="%s" hreflang="%s" lang="%s">%s</a>`, render.EscapeAttr(href), t.lang, t.lang, render.Escape(b.langs[t.lang].Strings["lang_name"]))
}
links = strings.Join(anchors, "\n&middot;\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(`<link rel="alternate" hreflang="%s" href="%s">`, 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
}
if err := b.publish(outputPath(d.path), d.source, html); err != nil {
return err
}
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"
}

49
internal/build/feed.go Normal file
View file

@ -0,0 +1,49 @@
// SPDX-License-Identifier: GPL-3.0-or-later
package build
import (
"strings"
"time"
"git.kmalloc.org/squid/motorino/internal/render"
"git.kmalloc.org/squid/motorino/internal/site"
)
// feed returns the RSS 2.0 feed of the latest limit posts, which must be sorted
// newest first. Each item carries the full text with absolute links. The
// channel is described by the language l, and newsURL is the address of the
// news page, such as "/news/".
func feed(base string, l site.Lang, newsURL string, posts []renderedPost, limit int) string {
if limit < len(posts) {
posts = posts[:limit]
}
var items strings.Builder
for _, p := range posts {
link := base + p.Path
text := strings.ReplaceAll(p.HTML, `href="/`, `href="`+base+`/`)
items.WriteString("<item>\n")
items.WriteString("<title>" + render.Escape(p.Title) + "</title>\n")
items.WriteString("<link>" + link + "</link>\n")
items.WriteString(`<guid isPermaLink="true">` + link + "</guid>\n")
items.WriteString("<pubDate>" + p.Date.Format(time.RFC1123Z) + "</pubDate>\n")
items.WriteString("<description>" + render.Escape(text) + "</description>\n")
items.WriteString("</item>\n")
}
built := ""
if len(posts) > 0 {
built = "<lastBuildDate>" + posts[0].Date.Format(time.RFC1123Z) + "</lastBuildDate>\n"
}
url := base + newsURL
return `<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>kmalloc.org: ` + render.Escape(l.Nav["news"]) + `</title>
<link>` + url + `</link>
<description>` + render.Escape(l.Strings["news_p"]) + `</description>
<language>` + l.Code + `</language>
` + built + `<atom:link href="` + url + `feed.xml" rel="self" type="application/rss+xml"/>
` + items.String() + `</channel>
</rss>
`
}

View file

@ -0,0 +1,61 @@
// SPDX-License-Identifier: GPL-3.0-or-later
package build
import (
"encoding/xml"
"strings"
"testing"
"git.kmalloc.org/squid/motorino/internal/site"
)
// feedLang is the language the feed header is built from.
func feedLang() site.Lang {
return site.Lang{Code: "en", Strings: map[string]string{"news_p": "The news & more."}, Nav: map[string]string{"news": "News"}}
}
// TestFeedItems checks the channel, the item fields, the limit and the dates.
func TestFeedItems(t *testing.T) {
items := []renderedPost{
{Post: testPost("new", "2026-11-14", "New <post>", ""), HTML: `<p>See <a href="/people.html">people</a>.</p>`},
{Post: testPost("old", "2026-11-12", "Old", ""), HTML: "<p>Old text.</p>"},
{Post: testPost("older", "2026-11-10", "Older", ""), HTML: "<p>Older text.</p>"},
}
got := feed("https://kmalloc.org", feedLang(), "/news/", items, 2)
for _, want := range []string{
"<title>kmalloc.org: News</title>",
"<link>https://kmalloc.org/news/</link>",
"<description>The news &amp; more.</description>",
"<language>en</language>",
`<atom:link href="https://kmalloc.org/news/feed.xml" rel="self" type="application/rss+xml"/>`,
"<title>New &lt;post&gt;</title>",
"<link>https://kmalloc.org/news/2026/11/new.html</link>",
`<guid isPermaLink="true">https://kmalloc.org/news/2026/11/new.html</guid>`,
"<pubDate>Sat, 14 Nov 2026 12:00:00 +0100</pubDate>",
"<lastBuildDate>Sat, 14 Nov 2026 12:00:00 +0100</lastBuildDate>",
`&lt;a href="https://kmalloc.org/people.html"&gt;`,
} {
if !strings.Contains(got, want) {
t.Errorf("feed does not contain %q\n%s", want, got)
}
}
if strings.Contains(got, "older.html") {
t.Error("feed has a third item but the limit is 2")
}
if err := xml.Unmarshal([]byte(got), new(any)); err != nil {
t.Errorf("feed is not well-formed XML: %v", err)
}
}
// TestFeedWithoutPosts checks that an empty feed is still a valid channel.
func TestFeedWithoutPosts(t *testing.T) {
got := feed("https://kmalloc.org", feedLang(), "/news/", nil, 20)
if strings.Contains(got, "lastBuildDate") || strings.Contains(got, "<item>") {
t.Errorf("empty feed has dates or items:\n%s", got)
}
if err := xml.Unmarshal([]byte(got), new(any)); err != nil {
t.Errorf("empty feed is not well-formed XML: %v", err)
}
}

View file

@ -0,0 +1,194 @@
// 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", "<h1 class=\"hero\">Technology.</h1>\n\n{{latest_news}}\n"),
"content/en/manifesto.md": page("manifesto", "Manifesto", "Layout: manifesto\n", "## 01 — Antifascist\n\nText one.\n\n## Our commitments\n\nText two.\n"),
"content/en/manifesto-history.md": page("history", "Manifesto history", "", "Versions.\n"),
"content/en/people.md": page("people", "People", "", "## squid\n\nI run it.\n"),
"content/en/services.md": page("services", "Services", "Switch: it=/it/servizi.html\n", "Write to **{{contact_shown}}**.\n"),
"content/en/news/index.md": page("news", "News", "", "{{news_list}}\n\n{{categories_list}}\n"),
"content/it/index.md": page("home", "kmalloc.org", "Layout: bare\n", "{{latest_news}}\n"),
"content/it/manifesto.md": page("manifesto", "Manifesto", "Layout: manifesto\n", "## 01 — Antifascisti\n\nTesto.\n"),
"content/it/cronologia-manifesto.md": page("history", "Cronologia", "", "Versioni.\n"),
"content/it/persone.md": page("people", "Persone", "", "Io.\n"),
"content/it/servizi.md": page("services", "Servizi", "", "Scrivi a {{contact_shown}}.\n"),
"content/es/manifesto.md": page("manifesto", "Manifiesto", "Layout: manifesto\nNote: Traduccion de la version 1.0.\nManifestoVersion: 1.0\nManifestoDate: 2026-11-12\n", "## 01 — Antifascistas\n\nTexto.\n"),
"news/gnu-linux-day-2026.md": "Title: GNU/Linux Day 2026\nAuthor: Matteo Bini\nDate: 2026-09-18T17:00:45+0200\nCategories: Linux Day, FLUG\n-\nThe programme is out. See [people](/people.html).\n",
"news/shop.md": "Title: A shop\nAuthor: squid\nDate: 2026-11-14T12:00:00+0100\nCategories: FLUG\nDescription: About the shop.\n-\nThere is a shop.\n",
"news/status.md": "Title: Status page\nDate: 2026-11-12T12:00:00+0100\n-\nThe status page.\n",
"static/style.css": "body { margin: 0 }\n",
"static/logo.svg": "<svg/>\n",
"templates/base.html": `<!doctype html>
<html lang="{{lang}}">
<head>
<meta charset="utf-8">
<meta name="generator" content="motorino {{motorino_version}}">
<title>{{title_html}}</title>
<meta property="og:title" content="{{title_html}}">
<meta name="description" content="{{description_attr}}">
<link rel="me" href="{{mastodon_url}}">
{{alt_links}}
<style>
{{css}}
</style>
</head>
<body>
{{header}}
{{content}}
{{footer}}
</body>
</html>
`,
"templates/header.html": `<header>
<a href="{{home_link}}"{{cur_home}}>home</a>
<a href="{{url_manifesto}}"{{cur_manifesto}}>{{nav_manifesto}}</a>
<a href="{{url_news}}"{{cur_news}}>{{nav_news}}</a>
{{lang_links}}
</header>
`,
"templates/header-min.html": `<header>
<a href="{{url_manifesto}}"{{cur_manifesto}}>{{nav_manifesto}}</a>
{{lang_links}}
<p class="note">{{translation_note}} <a href="{{issues_url}}">{{translation_note_a}}</a></p>
</header>
`,
"templates/footer.html": `<footer>
{{lead}}
<p>{{nojs}}</p>
<p><a href="{{url_history}}"{{history_attr}}>{{history_a}}</a></p>
<p>{{powered_by}} <a href="{{motorino_url}}">motorino</a></p>
{{badges}}
</footer>
`,
"templates/lead-home.html": "<p>lead home</p>\n",
"templates/lead-back.html": "<p><a href=\"{{home_link}}\">{{back}}</a></p>\n",
"templates/lead-news.html": "<p><a href=\"{{url_news}}\">{{news_all}}</a> <a href=\"{{home_link}}\">{{back}}</a></p>\n",
"templates/badges.html": "<p class=\"badges\">{{feed}} {{doc_q}}</p>\n",
"templates/feed.html": "<a href=\"{{url_news}}feed.xml\">RSS</a>\n",
"templates/latest-news.html": `<h2 id="news">{{nav_news}}</h2>
<ul>
{{news_items}}
</ul>
`,
"templates/pages/page.html": "<h1>{{page_title}}</h1>\n\n{{body}}\n",
"templates/pages/bare.html": "{{body}}\n",
"templates/pages/manifesto.html": `<h1>{{page_title}}</h1>
<p><strong>{{version}} {{manifesto_version}}, <time datetime="{{manifesto_date}}">{{manifesto_date}}</time></strong></p>
{{note_block}}<h2>{{toc}}</h2>
<ul>
{{manifesto_toc}}
</ul>
{{body}}
`,
"templates/post.html": `<h1>{{post_title}}</h1>
<p>
<time datetime="{{date}}">{{date}}</time>{{post_byline}}
</p>
{{post_body}}
`,
"templates/category.html": `<h1>{{category_h}}: {{category_name}}</h1>
{{category_posts}}
`,
}
}

120
internal/build/news.go Normal file
View file

@ -0,0 +1,120 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Package build assembles a site from its configuration, content, templates
// and assets, and writes it to a folder.
package build
import (
"fmt"
"sort"
"strings"
"git.kmalloc.org/squid/motorino/internal/content"
"git.kmalloc.org/squid/motorino/internal/render"
)
// dateOnly is the layout of the dates shown in lists.
const dateOnly = "2006-01-02"
// renderedPost is a post together with its body already converted to HTML.
type renderedPost struct {
content.Post
// HTML is the body of the post as HTML, placeholders already replaced.
HTML string
}
// newsItem returns the <li> of a post for a list: the date, the title as a
// link and, when withDescription is set and there is one, the description. News
// are written in English, so on any other language the link says so.
func newsItem(p content.Post, lang string, withDescription bool) string {
mark := ""
if lang != "en" {
mark = ` hreflang="en" lang="en"`
}
day := p.Date.Format(dateOnly)
item := fmt.Sprintf(`<li><time datetime="%s">%s</time> <a href="%s"%s>%s</a>`,
day, day, p.Path, mark, render.Escape(p.Title))
if withDescription && p.Description != "" {
item += ": " + render.Escape(p.Description)
}
return item + "</li>"
}
// newsList returns the full list of the news as a <ul>, or an empty string
// when there are no posts.
func newsList(posts []content.Post, lang string) string {
if len(posts) == 0 {
return ""
}
items := make([]string, len(posts))
for i, p := range posts {
items[i] = newsItem(p, lang, true)
}
return "<ul>\n" + strings.Join(items, "\n") + "\n</ul>"
}
// latestNews returns the <li> items of the first n posts, without the <ul>
// and without descriptions, for the home page.
func latestNews(posts []content.Post, lang string, n int) string {
if n > len(posts) {
n = len(posts)
}
items := make([]string, n)
for i := 0; i < n; i++ {
items[i] = newsItem(posts[i], lang, false)
}
return strings.Join(items, "\n")
}
// categoriesList returns the list of the categories, sorted by name, each with
// the number of posts, and the categories themselves. With no categories both
// are empty.
func categoriesList(posts []content.Post) (string, []content.Category) {
counts := map[string]int{}
byslug := map[string]content.Category{}
for _, p := range posts {
for _, c := range p.Categories {
counts[c.Slug]++
byslug[c.Slug] = c
}
}
if len(byslug) == 0 {
return "", nil
}
cats := make([]content.Category, 0, len(byslug))
for _, c := range byslug {
cats = append(cats, c)
}
sort.Slice(cats, func(i, j int) bool {
a, b := strings.ToLower(cats[i].Name), strings.ToLower(cats[j].Name)
if a != b {
return a < b
}
return cats[i].Name < cats[j].Name
})
items := make([]string, len(cats))
for i, c := range cats {
items[i] = fmt.Sprintf(`<li><a href="%s">%s</a> (%d)</li>`, categoryPath(c.Slug), render.Escape(c.Name), counts[c.Slug])
}
return "<ul>\n" + strings.Join(items, "\n") + "\n</ul>", cats
}
// categoryPath returns the address of the page of a category.
func categoryPath(slug string) string {
return "/news/category/" + slug + ".html"
}
// postsIn returns the posts that have the category with the given address
// name, in the order they were given.
func postsIn(posts []content.Post, slug string) []content.Post {
var out []content.Post
for _, p := range posts {
for _, c := range p.Categories {
if c.Slug == slug {
out = append(out, p)
break
}
}
}
return out
}

107
internal/build/news_test.go Normal file
View file

@ -0,0 +1,107 @@
// SPDX-License-Identifier: GPL-3.0-or-later
package build
import (
"strings"
"testing"
"time"
"git.kmalloc.org/squid/motorino/internal/content"
)
// testPost returns a post dated on the given day (noon, +0100).
func testPost(slug, day, title, description string, cats ...string) content.Post {
date, err := time.Parse("2006-01-02T15:04:05-0700", day+"T12:00:00+0100")
if err != nil {
panic(err)
}
var categories []content.Category
for _, name := range cats {
slugName, _ := content.CategorySlug(name)
categories = append(categories, content.Category{Name: name, Slug: slugName})
}
return content.Post{
Slug: slug, Path: content.PostPath(date, slug), Title: title, Description: description,
Date: date, Categories: categories, Source: "news/" + slug + ".md",
}
}
// TestNewsListMarksEnglishOnItalian checks the hreflang mark on other languages.
func TestNewsListMarksEnglishOnItalian(t *testing.T) {
posts := []content.Post{testPost("shop", "2026-11-14", "A shop <new>", "About the shop.")}
en := newsList(posts, "en")
it := newsList(posts, "it")
want := `<li><time datetime="2026-11-14">2026-11-14</time> <a href="/news/2026/11/shop.html">A shop &lt;new&gt;</a>: About the shop.</li>`
if !strings.Contains(en, want) {
t.Errorf("en list %q does not contain %q", en, want)
}
if !strings.Contains(it, `<a href="/news/2026/11/shop.html" hreflang="en" lang="en">`) {
t.Errorf("it list %q lacks the hreflang mark", it)
}
if !strings.HasPrefix(en, "<ul>\n") || !strings.HasSuffix(en, "\n</ul>") {
t.Errorf("list is not wrapped in <ul>: %q", en)
}
}
// TestNewsListWithoutDescription checks that an empty description adds no colon.
func TestNewsListWithoutDescription(t *testing.T) {
got := newsList([]content.Post{testPost("a", "2026-01-01", "Title", "")}, "en")
if strings.Contains(got, ": ") {
t.Errorf("list %q has a colon but no description", got)
}
}
// TestNewsListEmpty checks that no posts give an empty string.
func TestNewsListEmpty(t *testing.T) {
if got := newsList(nil, "en"); got != "" {
t.Errorf("newsList(nil) = %q, want empty", got)
}
}
// TestLatestNewsLimit checks that only the first n posts are listed.
func TestLatestNewsLimit(t *testing.T) {
posts := []content.Post{
testPost("a", "2026-03-01", "A", ""), testPost("b", "2026-02-01", "B", ""), testPost("c", "2026-01-01", "C", ""),
}
got := latestNews(posts, "en", 2)
if strings.Count(got, "<li>") != 2 || strings.Contains(got, ">C<") || strings.Contains(got, "<ul>") {
t.Errorf("latestNews() = %q, want two items and no <ul>", got)
}
}
// TestCategoriesListCountsAndSorts checks the category index.
func TestCategoriesListCountsAndSorts(t *testing.T) {
posts := []content.Post{
testPost("a", "2026-03-01", "A", "", "Linux Day", "FLUG"),
testPost("b", "2026-02-01", "B", "", "FLUG"),
testPost("c", "2026-01-01", "C", "", "News"),
}
html, cats := categoriesList(posts)
if len(cats) != 3 || cats[0].Name != "FLUG" || cats[1].Name != "Linux Day" || cats[2].Name != "News" {
t.Errorf("categories = %+v, want FLUG, Linux Day, News", cats)
}
for _, want := range []string{
`<a href="/news/category/flug.html">FLUG</a> (2)`,
`<a href="/news/category/linux-day.html">Linux Day</a> (1)`,
} {
if !strings.Contains(html, want) {
t.Errorf("html %q does not contain %q", html, want)
}
}
if emptyHTML, emptyCats := categoriesList(nil); emptyHTML != "" || len(emptyCats) != 0 {
t.Errorf("categoriesList(nil) = %q, %v, want empty", emptyHTML, emptyCats)
}
}
// TestPostsIn checks that posts are picked by category address.
func TestPostsIn(t *testing.T) {
posts := []content.Post{
testPost("a", "2026-03-01", "A", "", "FLUG"),
testPost("b", "2026-02-01", "B", "", "News"),
}
got := postsIn(posts, "flug")
if len(got) != 1 || got[0].Slug != "a" {
t.Errorf("postsIn(flug) = %+v, want only post a", got)
}
}

233
internal/build/pages.go Normal file
View file

@ -0,0 +1,233 @@
// SPDX-License-Identifier: GPL-3.0-or-later
package build
import (
"fmt"
"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,
}
if contains(menuPages, p.ID) {
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>`, 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 := render.Substitute(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)
}
html, err = b.expandBlocks(name, html, lang, ctx)
return html, headings, err
}
// expandBlocks replaces each paragraph that holds only a block placeholder by
// the generated HTML. A block placeholder inside other text is an error.
func (b *builder) expandBlocks(name, html, lang string, ctx map[string]string) (string, error) {
for _, block := range blockNames {
marker := "<p>{{" + block + "}}</p>"
if strings.Contains(html, marker) {
generated, err := b.block(block, lang, ctx)
if err != nil {
return "", err
}
html = strings.ReplaceAll(html, marker, generated)
}
if strings.Contains(html, "{{"+block+"}}") {
return "", fmt.Errorf("%s: {{%s}} must stand alone in a paragraph", name, block)
}
}
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)
}
// contains reports whether list has s.
func contains(list []string, s string) bool {
for _, item := range list {
if item == s {
return true
}
}
return false
}

27
internal/build/robots.go Normal file
View file

@ -0,0 +1,27 @@
// SPDX-License-Identifier: GPL-3.0-or-later
package build
import (
"sort"
"strings"
)
// robots returns robots.txt: it allows each address exactly ("$" anchors the
// end) and blocks everything else, so downloads and assets are left out.
func robots(addresses []string) string {
allow := map[string]bool{}
for _, a := range addresses {
allow["Allow: "+a+"$"] = true
if a == "/" {
// The English home as the other languages link to it.
allow["Allow: /?lang=en$"] = true
}
}
lines := make([]string, 0, len(allow))
for line := range allow {
lines = append(lines, line)
}
sort.Strings(lines)
return "User-agent: *\n" + strings.Join(lines, "\n") + "\nDisallow: /\n"
}

View file

@ -0,0 +1,49 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Package content loads the pages and the news of a site from Markdown files
// and works out their addresses.
package content
import (
"fmt"
"regexp"
"strings"
)
// Category is a news category: the name as written and the name used in its
// address.
type Category struct {
// Name is the category as written in the header of a news file.
Name string
// Slug is the name in the address, such as "linux-day".
Slug string
}
// accents maps the accented letters of the Latin alphabets to ASCII, so a
// category address stays readable without an extra dependency.
var accents = strings.NewReplacer(
"à", "a", "á", "a", "â", "a", "ã", "a", "ä", "a", "å", "a", "ā", "a", "ă", "a", "ą", "a",
"æ", "ae", "ç", "c", "ć", "c", "č", "c", "ď", "d", "đ", "d",
"è", "e", "é", "e", "ê", "e", "ë", "e", "ē", "e", "ė", "e", "ę", "e", "ě", "e",
"ğ", "g", "ì", "i", "í", "i", "î", "i", "ï", "i", "ī", "i", "į", "i", "ı", "i",
"ł", "l", "ñ", "n", "ń", "n", "ň", "n",
"ò", "o", "ó", "o", "ô", "o", "õ", "o", "ö", "o", "ø", "o", "ō", "o", "ő", "o", "œ", "oe",
"ř", "r", "ś", "s", "š", "s", "ş", "s", "ß", "ss", "ť", "t", "ț", "t",
"ù", "u", "ú", "u", "û", "u", "ü", "u", "ū", "u", "ů", "u", "ű", "u",
"ý", "y", "ÿ", "y", "ź", "z", "ž", "z", "ż", "z",
)
// nonSlug matches a run of characters that cannot be part of an address.
var nonSlug = regexp.MustCompile(`[^a-z0-9]+`)
// CategorySlug turns a category name into the name used in its address:
// lowercase, without accents, with hyphens between words ("Linux Day" becomes
// "linux-day"). A name without any letter or digit is an error.
func CategorySlug(name string) (string, error) {
slug := nonSlug.ReplaceAllString(accents.Replace(strings.ToLower(name)), "-")
slug = strings.Trim(slug, "-")
if slug == "" {
return "", fmt.Errorf("category %q has no letters or digits", name)
}
return slug, nil
}

View file

@ -0,0 +1,35 @@
// SPDX-License-Identifier: GPL-3.0-or-later
package content
import "testing"
// TestCategorySlug checks the name that goes into a category address.
func TestCategorySlug(t *testing.T) {
tests := []struct {
name string
want string
wantErr bool
}{
{"Linux Day", "linux-day", false},
{"FLUG", "flug", false},
{"Università", "universita", false},
{"Crème brûlée", "creme-brulee", false},
{"C++", "c", false},
{" Spaced out ", "spaced-out", false},
{"Straße", "strasse", false},
{"", "", true},
{"+++", "", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CategorySlug(tt.name)
if (err != nil) != tt.wantErr {
t.Fatalf("CategorySlug(%q) error = %v, wantErr %v", tt.name, err, tt.wantErr)
}
if got != tt.want {
t.Errorf("CategorySlug(%q) = %q, want %q", tt.name, got, tt.want)
}
})
}
}

View file

@ -0,0 +1,26 @@
// SPDX-License-Identifier: GPL-3.0-or-later
package content
import (
"os"
"path/filepath"
"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
}

187
internal/content/pages.go Normal file
View file

@ -0,0 +1,187 @@
// SPDX-License-Identifier: GPL-3.0-or-later
package content
import (
"fmt"
"io/fs"
"os"
"path"
"path/filepath"
"strings"
"git.kmalloc.org/squid/motorino/internal/header"
"git.kmalloc.org/squid/motorino/internal/mdx"
)
// Page is a page of the site in one language.
type Page struct {
// Lang is the language code, such as "en".
Lang string
// ID names the page; the translations of a page share it.
ID string
// Path is the address of the page, such as "/services.html".
Path string
// Title, Description and Note come from the header.
Title, Description, Note string
// Layout is "page", "bare" or "manifesto".
Layout string
// Switch overrides the address of the language links, by language code.
Switch map[string]string
// ManifestoVersion and ManifestoDate, when set, replace the current
// manifesto version and date on this page: a translation of an older
// version says which one it translates.
ManifestoVersion, ManifestoDate string
// Source is the file the page was read from, relative to the site.
Source string
// Body is the Markdown after the header.
Body string
}
// pageKeys are the keys a page header may have.
var pageKeys = []string{"Title", "ID", "Description", "Layout", "Note", "Switch", "ManifestoVersion", "ManifestoDate"}
// layouts are the values of the Layout key; the first is the default.
var layouts = []string{"page", "bare", "manifesto"}
// PagePath returns the address of the page file rel, given with slashes and
// relative to the folder of its language. An index.md is the address of its
// folder; the default language lives at the root and the others under
// /<lang>/.
func PagePath(defaultLang, lang, rel string) string {
rel = strings.TrimSuffix(rel, ".md")
var p string
switch {
case rel == "index":
p = "/"
case strings.HasSuffix(rel, "/index"):
p = "/" + strings.TrimSuffix(rel, "index")
default:
p = "/" + rel + ".html"
}
if lang != defaultLang {
p = "/" + lang + p
}
return p
}
// LoadPages reads every Markdown file under content/<lang>/ for each language.
// The first language is the default. The em dash rule applies to every page
// except those with the manifesto layout, which is rewritten when it is built.
func LoadPages(dir string, languages []string) ([]Page, error) {
if len(languages) == 0 {
return nil, fmt.Errorf("no languages")
}
var pages []Page
for _, lang := range languages {
folder := path.Join("content", lang)
if info, err := os.Stat(filepath.Join(dir, filepath.FromSlash(folder))); err != nil || !info.IsDir() {
return nil, fmt.Errorf("%s: the folder of language %q is missing", folder, lang)
}
found, err := loadLanguagePages(dir, languages[0], lang, folder)
if err != nil {
return nil, err
}
pages = append(pages, found...)
}
return pages, nil
}
// loadLanguagePages reads the pages of one language and rejects duplicate IDs.
func loadLanguagePages(dir, defaultLang, lang, folder string) ([]Page, error) {
var pages []Page
seen := map[string]string{}
root := filepath.Join(dir, filepath.FromSlash(folder))
err := filepath.WalkDir(root, func(file string, d fs.DirEntry, err error) error {
if err != nil || d.IsDir() || !strings.HasSuffix(file, ".md") {
return err
}
rel, err := filepath.Rel(root, file)
if err != nil {
return err
}
rel = filepath.ToSlash(rel)
page, err := loadPage(file, path.Join(folder, rel), defaultLang, lang, rel)
if err != nil {
return err
}
if other, dup := seen[page.ID]; dup {
return fmt.Errorf("%s: the ID %q is also used by %s", page.Source, page.ID, other)
}
seen[page.ID] = page.Source
pages = append(pages, page)
return nil
})
return pages, err
}
// loadPage reads and checks one page file.
func loadPage(file, source, defaultLang, lang, rel string) (Page, error) {
data, err := os.ReadFile(file)
if err != nil {
return Page{}, fmt.Errorf("read %s: %w", source, err)
}
h, body, err := header.Parse(source, data)
if err != nil {
return Page{}, err
}
if err := h.Only(source, pageKeys...); err != nil {
return Page{}, err
}
if err := h.Require(source, "Title", "ID", "Description"); err != nil {
return Page{}, err
}
layout := h.Get("Layout")
if layout == "" {
layout = layouts[0]
}
if !contains(layouts, layout) {
return Page{}, fmt.Errorf("%s: unknown Layout %q (use %s)", source, layout, strings.Join(layouts, ", "))
}
if layout != "manifesto" {
if err := mdx.RejectEmDash(source, string(data)); err != nil {
return Page{}, err
}
}
switches, err := parseSwitch(source, h.Get("Switch"))
if err != nil {
return Page{}, err
}
return Page{
Lang: lang, ID: h.Get("ID"), Path: PagePath(defaultLang, lang, rel),
Title: h.Get("Title"), Description: h.Get("Description"), Note: h.Get("Note"),
Layout: layout, Switch: switches, Source: source, Body: body,
ManifestoVersion: h.Get("ManifestoVersion"), ManifestoDate: h.Get("ManifestoDate"),
}, nil
}
// parseSwitch reads "en=/path, de=/path" into a map; an empty value is an
// empty map.
func parseSwitch(source, value string) (map[string]string, error) {
out := map[string]string{}
if strings.TrimSpace(value) == "" {
return out, nil
}
for _, entry := range strings.Split(value, ",") {
lang, target, ok := strings.Cut(strings.TrimSpace(entry), "=")
lang, target = strings.TrimSpace(lang), strings.TrimSpace(target)
if !ok || lang == "" || !strings.HasPrefix(target, "/") {
return nil, fmt.Errorf("%s: Switch entry %q is not of the form lang=/path", source, strings.TrimSpace(entry))
}
if _, dup := out[lang]; dup {
return nil, fmt.Errorf("%s: Switch lists the language %q twice", source, lang)
}
out[lang] = target
}
return out, nil
}
// contains reports whether list has s.
func contains(list []string, s string) bool {
for _, item := range list {
if item == s {
return true
}
}
return false
}

Some files were not shown because too many files have changed in this diff Show more