diff --git a/README.md b/README.md index 81c0168..06b5921 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ Pages: `Title`, `ID` (the same in every language, it links the translations), `D ### 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. +`{{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. Fenced code blocks (found the way CommonMark finds them, in lists and quotes too) are left as written, so an article can show a placeholder there. Anywhere else a backslash before the placeholder (`\{{name}}`) keeps it as written, in a code span too; two backslashes are one literal backslash followed by the value. Code spans are otherwise substituted, which is how a page shows a value in monospace. The id of a heading is not checked against ids that a template writes itself, such as the `news` of the latest news block. `{{news_list}}`, `{{latest_news}}` and `{{categories_list}}` stand alone in a paragraph and expand to generated lists. ## Development diff --git a/cmd/motorino/main.go b/cmd/motorino/main.go index ce40c01..a1997f1 100644 --- a/cmd/motorino/main.go +++ b/cmd/motorino/main.go @@ -15,6 +15,7 @@ import ( "fmt" "io" "os" + "strings" "git.kmalloc.org/squid/motorino/internal/build" "git.kmalloc.org/squid/motorino/internal/version" @@ -59,6 +60,10 @@ func runBuild(args []string, stdout, stderr io.Writer) int { } return 2 } + if flags.NArg() > 0 { + fmt.Fprintf(stderr, "motorino: unexpected arguments: %s\n%s\n", strings.Join(flags.Args(), " "), usage) + 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) diff --git a/cmd/motorino/main_test.go b/cmd/motorino/main_test.go index 8e2ffd6..4090294 100644 --- a/cmd/motorino/main_test.go +++ b/cmd/motorino/main_test.go @@ -30,8 +30,11 @@ func miniSite(t *testing.T) string { } for _, id := range []string{"home", "manifesto", "people", "services", "news", "history"} { name := id - if id == "home" { + switch id { + case "home": name = "index" + case "news": + name = "news/index" } files["content/en/"+name+".md"] = "Title: " + id + "\nID: " + id + "\nDescription: d\n-\nBody of " + id + ".\n" } @@ -101,6 +104,17 @@ func TestRunUnknownCommand(t *testing.T) { } } +// TestRunBuildRejectsExtraArguments checks that arguments that are not flags are not ignored. +func TestRunBuildRejectsExtraArguments(t *testing.T) { + var out, errOut bytes.Buffer + if code := run([]string{"build", "somesite", "someout"}, &out, &errOut); code != 2 { + t.Errorf("run(build somesite someout) = %d, want 2", code) + } + if !strings.Contains(errOut.String(), "unexpected") { + t.Errorf("stderr = %q, want it to say what was unexpected", errOut.String()) + } +} + // TestRunBuildBadFlag checks that an unknown flag is a usage error. func TestRunBuildBadFlag(t *testing.T) { var out, errOut bytes.Buffer diff --git a/internal/build/build.go b/internal/build/build.go index 7975c88..a6d2ea3 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -76,7 +76,10 @@ type Result struct { } // 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. +// everything in memory first, so a failure in the content leaves the output +// folder untouched. Writing the files is not atomic, and files of an earlier +// build that are no longer produced are not removed: build into a fresh folder +// when that matters, as the deploy does. func Build(opts Options) (Result, error) { b, err := load(opts) if err != nil { @@ -137,6 +140,9 @@ func load(opts Options) (*builder, error) { return nil, fmt.Errorf("content/%s: there is no page with ID %q", b.defaultLang, id) } } + if news := b.byKey[pageKey{"news", b.defaultLang}]; !strings.HasSuffix(news.Path, "/") { + return nil, fmt.Errorf("%s: the news page must be an index (news/index.md), so that the feed has an address next to it", news.Source) + } if b.templates, err = readTemplates(opts.SiteDir); err != nil { return nil, err } diff --git a/internal/build/build_test.go b/internal/build/build_test.go index b46db52..1d6623d 100644 --- a/internal/build/build_test.go +++ b/internal/build/build_test.go @@ -358,6 +358,72 @@ func TestBuildRejectsSwitchToUnknownLanguage(t *testing.T) { } } +// TestBuildFeedMakesImagesAbsolute checks that src links get the full address like href links. +func TestBuildFeedMakesImagesAbsolute(t *testing.T) { + files := baseFiles() + files["news/pic.md"] = "Title: Pic\nDate: 2026-11-15T12:00:00+0100\n-\n![the logo](/logo.svg)\n" + out, err := runBuild(t, files) + if err != nil { + t.Fatal(err) + } + mustContain(t, "feed", readOut(t, out, "news/feed.xml"), `src="https://kmalloc.org/logo.svg"`) +} + +// TestBuildNewsPageMustBeAFolderIndex checks that the feed has a sound address. +func TestBuildNewsPageMustBeAFolderIndex(t *testing.T) { + files := baseFiles() + delete(files, "content/en/news/index.md") + files["content/en/news.md"] = page("news", "News", "", "{{news_list}}\n") + _, err := runBuild(t, files) + if err == nil || !strings.Contains(err.Error(), "content/en/news.md") || !strings.Contains(err.Error(), "index") { + t.Errorf("Build() error = %v, want it to say that content/en/news.md must be an index", err) + } +} + +// TestBuildBlockNameInATitleIsNotAnError checks that a generated list that shows +// the text of a block placeholder (in a news title) is not mistaken for a +// placeholder that is not alone in its paragraph. +func TestBuildBlockNameInATitleIsNotAnError(t *testing.T) { + files := baseFiles() + files["news/tpl.md"] = "Title: Use {{latest_news}} in a page\nDate: 2026-11-15T12:00:00+0100\n-\nA note.\n" + out, err := runBuild(t, files) + if err != nil { + t.Fatalf("Build() error = %v", err) + } + mustContain(t, "home", readOut(t, out, "index.html"), "Use {{latest_news}} in a page") +} + +// TestBuildPlaceholdersAndCode checks the three ways a page can use a placeholder +// next to code: a value in a code span (the pages show the GPG fingerprint that +// way), a fenced block that shows a placeholder as written, and an escaped one. +func TestBuildPlaceholdersAndCode(t *testing.T) { + files := baseFiles() + files["content/en/people.md"] = page("people", "People", "", "Value `{{lang}}` and escaped \\{{lang}}.\n\n```\n{{lang}} {{nope}}\n```\n\nAnd {{lang}}.\n") + out, err := runBuild(t, files) + if err != nil { + t.Fatalf("Build() error = %v", err) + } + got := readOut(t, out, "people.html") + mustContain(t, "people", got, "en", "escaped {{lang}}.", "{{lang}} {{nope}}", "And en.") +} + +// TestBuildEscapedPlaceholders checks the escape with block placeholders: an +// escaped one inside a sentence or alone in a paragraph is shown, not expanded +// and not an error, and a code span can show a placeholder as written. +func TestBuildEscapedPlaceholders(t *testing.T) { + files := baseFiles() + files["content/en/people.md"] = page("people", "People", "", "See \\{{news_list}} here.\n\n\\{{latest_news}}\n\nAnd `\\{{lang}}`.\n") + out, err := runBuild(t, files) + if err != nil { + t.Fatalf("Build() error = %v", err) + } + got := readOut(t, out, "people.html") + mustContain(t, "people", got, "See {{news_list}} here.", "

{{latest_news}}

", "{{lang}}") + if strings.Contains(got, "