diff --git a/README.md b/README.md index d31e678..8de6877 100644 --- a/README.md +++ b/README.md @@ -11,12 +11,15 @@ motorino build -site DIR -out DIR build the site in DIR into the output folde motorino build -drafts ... the same, with the drafts and the posts dated in the future, for a preview motorino gemini -site DIR -out DIR write the same site as gemtext, for a Gemini server (defaults: . and capsule) motorino gemini -lang CODE ... the language at the root of the capsule; -drafts works as for build +motorino build -clean ... empty the output folder first, if it is new, empty or holds .motorino-out motorino new -site DIR "A title" write news/a-title.md, a draft with the header ready, and print its path motorino version print the version ``` The binary is a single static Go program. The version comes from semantic-release. +A build writes the files it makes and leaves the others in the output folder, so the pages of a list that got shorter (`news/page/3/`) stay behind. When that happens the command prints a warning that names the old files. The deploy never has them: it builds into a new folder and publishes it with `rsync --delete`. For a folder you reuse, `-clean` empties it first. It does that only for a folder that is new, empty or holds the `.motorino-out` file that an earlier `-clean` left, never for any other folder with files in it and never for the folder of the site or one that holds it. A build without `-clean` writes no extra file. + ## What a site looks like ``` diff --git a/cmd/motorino/main.go b/cmd/motorino/main.go index dae0be3..247c26b 100644 --- a/cmd/motorino/main.go +++ b/cmd/motorino/main.go @@ -5,8 +5,8 @@ // // Usage: // -// motorino build [-site DIR] [-out DIR] [-drafts] -// motorino gemini [-site DIR] [-out DIR] [-lang CODE] [-drafts] +// motorino build [-site DIR] [-out DIR] [-drafts] [-clean] +// motorino gemini [-site DIR] [-out DIR] [-lang CODE] [-drafts] [-clean] // motorino new [-site DIR] "title" // motorino version package main @@ -27,7 +27,7 @@ import ( ) // usage is the text printed for a missing or unknown command. -const usage = "usage: motorino build [-site DIR] [-out DIR] [-drafts] | motorino gemini [-site DIR] [-out DIR] [-lang CODE] [-drafts] | motorino new [-site DIR] \"title\" | motorino version" +const usage = "usage: motorino build [-site DIR] [-out DIR] [-drafts] [-clean] | motorino gemini [-site DIR] [-out DIR] [-lang CODE] [-drafts] [-clean] | motorino new [-site DIR] \"title\" | motorino version" // main runs the command line and exits with its status code. func main() { @@ -64,6 +64,7 @@ func runBuild(args []string, stdout, stderr io.Writer) int { site := flags.String("site", ".", "the folder of the site") out := flags.String("out", "public", "the folder the HTML is written to") drafts := flags.Bool("drafts", false, "publish the drafts and the posts dated in the future, for a preview") + clean := flags.Bool("clean", false, "empty the output folder first, if it is new, empty or was emptied this way before") if err := flags.Parse(args); err != nil { if errors.Is(err, flag.ErrHelp) { return 0 @@ -74,15 +75,29 @@ func runBuild(args []string, stdout, stderr io.Writer) int { 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(), Drafts: *drafts}) + result, err := build.Build(build.Options{SiteDir: *site, OutDir: *out, Version: version.String(), Drafts: *drafts, Clean: *clean}) if err != nil { fmt.Fprintf(stderr, "motorino: %v\n", err) return 1 } fmt.Fprintf(stdout, "built %d pages into %s\n", result.Pages, *out) + warnStale(stderr, *out, result.Stale) return 0 } +// warnStale tells the files of the output folder that the build did not write, +// which stay there unless -clean is used. +func warnStale(stderr io.Writer, out string, stale []string) { + if len(stale) == 0 { + return + } + noun := "files" + if len(stale) == 1 { + noun = "file" + } + fmt.Fprintf(stderr, "motorino: warning: %d %s in %s were not written by this build, such as %s; build into a new folder or use -clean\n", len(stale), noun, out, stale[0]) +} + // runGemini parses the flags of the gemini command and writes the site as a // Gemini capsule. func runGemini(args []string, stdout, stderr io.Writer) int { @@ -92,6 +107,7 @@ func runGemini(args []string, stdout, stderr io.Writer) int { out := flags.String("out", "capsule", "the folder the gemtext is written to") lang := flags.String("lang", "", "the language at the root of the capsule (default: gemini_language of site.json, then the default language)") drafts := flags.Bool("drafts", false, "publish the drafts and the posts dated in the future, for a preview") + clean := flags.Bool("clean", false, "empty the output folder first, if it is new, empty or was emptied this way before") if err := flags.Parse(args); err != nil { if errors.Is(err, flag.ErrHelp) { return 0 @@ -102,12 +118,13 @@ func runGemini(args []string, stdout, stderr io.Writer) int { fmt.Fprintf(stderr, "motorino: unexpected arguments: %s\n%s\n", strings.Join(flags.Args(), " "), usage) return 2 } - result, err := build.BuildGemini(build.GeminiOptions{SiteDir: *site, OutDir: *out, Version: version.String(), Drafts: *drafts, Language: *lang}) + result, err := build.BuildGemini(build.GeminiOptions{SiteDir: *site, OutDir: *out, Version: version.String(), Drafts: *drafts, Language: *lang, Clean: *clean}) if err != nil { fmt.Fprintf(stderr, "motorino: %v\n", err) return 1 } fmt.Fprintf(stdout, "wrote %d gemtext files into %s\n", result.Pages, *out) + warnStale(stderr, *out, result.Stale) return 0 } diff --git a/cmd/motorino/main_test.go b/cmd/motorino/main_test.go index 5743d54..bf82569 100644 --- a/cmd/motorino/main_test.go +++ b/cmd/motorino/main_test.go @@ -202,10 +202,10 @@ func TestRunGemini(t *testing.T) { if code := run([]string{"gemini", "-site", dir, "-out", capsule, "-lang", "en"}, &out, &errOut); code != 0 { t.Fatalf("run(gemini) = %d, stderr %q", code, errOut.String()) } - if want := "wrote 8 gemtext files into " + capsule; !strings.Contains(out.String(), want) { + if want := "gemtext files into " + capsule; !strings.Contains(out.String(), want) { t.Errorf("output %q does not contain %q", out.String(), want) } - for _, rel := range []string{"index.gmi", "news/index.gmi", "news/feed.gmi", ".meta"} { + for _, rel := range []string{"index.gmi", "manifesto.gmi", "people.gmi", "services.gmi", "news/index.gmi", "news/feed.gmi", ".meta"} { if _, err := os.Stat(filepath.Join(capsule, filepath.FromSlash(rel))); err != nil { t.Errorf("no %s in the capsule: %v", rel, err) } @@ -233,3 +233,81 @@ func TestRunGeminiErrors(t *testing.T) { t.Errorf("an extra argument: code %d, stderr %q, want 2", code, errOut.String()) } } + +// TestRunBuildWarnsAboutOldFiles checks that old files in the output folder are named in a warning and left alone. +func TestRunBuildWarnsAboutOldFiles(t *testing.T) { + site := miniSite(t) + outDir := t.TempDir() + if err := os.WriteFile(filepath.Join(outDir, "old.html"), []byte("old"), 0o644); err != nil { + t.Fatal(err) + } + var out, errOut bytes.Buffer + if code := run([]string{"build", "-site", site, "-out", outDir}, &out, &errOut); code != 0 { + t.Fatalf("run(build) = %d, stderr %q", code, errOut.String()) + } + for _, want := range []string{"warning", "1 file", "old.html", "-clean"} { + if !strings.Contains(errOut.String(), want) { + t.Errorf("stderr %q does not contain %q", errOut.String(), want) + } + } + if _, err := os.Stat(filepath.Join(outDir, "old.html")); err != nil { + t.Error("a build without -clean removed the old file") + } + errOut.Reset() + if code := run([]string{"build", "-site", site, "-out", filepath.Join(t.TempDir(), "new")}, &out, &errOut); code != 0 || errOut.Len() != 0 { + t.Errorf("a build into a new folder: code %d, stderr %q, want 0 and nothing", code, errOut.String()) + } +} + +// TestRunBuildClean checks -clean on a folder that is marked and on one that is not. +func TestRunBuildClean(t *testing.T) { + site := miniSite(t) + var out, errOut bytes.Buffer + outDir := filepath.Join(t.TempDir(), "public") + if code := run([]string{"build", "-clean", "-site", site, "-out", outDir}, &out, &errOut); code != 0 { + t.Fatalf("run(build -clean) into a new folder = %d, stderr %q", code, errOut.String()) + } + if err := os.WriteFile(filepath.Join(outDir, "old.html"), []byte("old"), 0o644); err != nil { + t.Fatal(err) + } + if code := run([]string{"build", "-clean", "-site", site, "-out", outDir}, &out, &errOut); code != 0 || errOut.Len() != 0 { + t.Fatalf("run(build -clean) again = %d, stderr %q", code, errOut.String()) + } + if _, err := os.Stat(filepath.Join(outDir, "old.html")); err == nil { + t.Error("-clean left the old file") + } + other := t.TempDir() + if err := os.WriteFile(filepath.Join(other, "mine.txt"), []byte("mine"), 0o644); err != nil { + t.Fatal(err) + } + if code := run([]string{"build", "-clean", "-site", site, "-out", other}, &out, &errOut); code != 1 || !strings.Contains(errOut.String(), ".motorino-out") { + t.Errorf("run(build -clean) into a folder that is not motorino's = %d, stderr %q, want 1 and the marker named", code, errOut.String()) + } + if _, err := os.Stat(filepath.Join(other, "mine.txt")); err != nil { + t.Error("-clean removed a file of a folder that is not motorino's") + } +} + +// TestRunGeminiOldFilesAndClean checks the warning about old files and -clean for the gemini command. +func TestRunGeminiOldFilesAndClean(t *testing.T) { + site := miniSite(t) + var out, errOut bytes.Buffer + capsule := t.TempDir() + if err := os.WriteFile(filepath.Join(capsule, "old.gmi"), []byte("old"), 0o644); err != nil { + t.Fatal(err) + } + if code := run([]string{"gemini", "-site", site, "-out", capsule}, &out, &errOut); code != 0 || !strings.Contains(errOut.String(), "old.gmi") { + t.Fatalf("run(gemini) = %d, stderr %q, want 0 and a warning that names old.gmi", code, errOut.String()) + } + errOut.Reset() + if code := run([]string{"gemini", "-clean", "-site", site, "-out", capsule}, &out, &errOut); code != 1 || !strings.Contains(errOut.String(), ".motorino-out") { + t.Fatalf("run(gemini -clean) into a folder that is not motorino's = %d, stderr %q", code, errOut.String()) + } + fresh := filepath.Join(t.TempDir(), "capsule") + for i := 0; i < 2; i++ { + errOut.Reset() + if code := run([]string{"gemini", "-clean", "-site", site, "-out", fresh}, &out, &errOut); code != 0 || errOut.Len() != 0 { + t.Fatalf("run(gemini -clean) %d = %d, stderr %q", i+1, code, errOut.String()) + } + } +} diff --git a/internal/build/blog.go b/internal/build/blog.go index 1395840..0a8a7a5 100644 --- a/internal/build/blog.go +++ b/internal/build/blog.go @@ -4,6 +4,7 @@ package build import ( "fmt" + "sort" "strconv" "strings" @@ -109,24 +110,43 @@ type ( } ) -// groupArchive groups posts, newest first, by year and by month, as written in -// their dates. The years and the months are newest first too. +// groupArchive groups posts by year and by month, as written in their dates. +// The years and the months are newest first, and the posts of a group keep the +// order they were given. A post is put in its group by its own year and month, +// not by the group before it: two posts near midnight at the end of a month, +// written with different offsets, can be in the order of their instants and in +// the opposite order of their months. func groupArchive(posts []content.Post) []archiveYear { - var years []archiveYear + type key struct{ year, month int } + years := map[int]*archiveYear{} + months := map[key]*archiveMonth{} for _, p := range posts { y, m := p.Date.Year(), int(p.Date.Month()) - if len(years) == 0 || years[len(years)-1].year != y { - years = append(years, archiveYear{year: y}) + year := years[y] + if year == nil { + year = &archiveYear{year: y} + years[y] = year } - year := &years[len(years)-1] year.posts = append(year.posts, p) - if len(year.months) == 0 || year.months[len(year.months)-1].month != m { - year.months = append(year.months, archiveMonth{year: y, month: m}) + month := months[key{y, m}] + if month == nil { + month = &archiveMonth{year: y, month: m} + months[key{y, m}] = month } - month := &year.months[len(year.months)-1] month.posts = append(month.posts, p) } - return years + var out []archiveYear + for _, year := range years { + for k, month := range months { + if k.year == year.year { + year.months = append(year.months, *month) + } + } + sort.Slice(year.months, func(i, j int) bool { return year.months[i].month > year.months[j].month }) + out = append(out, *year) + } + sort.Slice(out, func(i, j int) bool { return out[i].year > out[j].year }) + return out } // yearPath and monthPath are the addresses of an archive year and month. @@ -151,14 +171,15 @@ func (b *builder) monthList(y archiveYear, lang string) string { } // newsArchive returns the nested list of the years and the months that have -// posts, or the news_empty text when there are none. It needs the archive pages. +// posts, or nothing when there are none: the blog list says there is no news. It +// needs the archive pages. func (b *builder) newsArchive(lang string, ctx map[string]string) (string, error) { if _, err := b.template("archive"); err != nil { return "", fmt.Errorf("the {{news_archive}} block needs the archive pages: %w", err) } years := groupArchive(b.posts) if len(years) == 0 { - return "
\n" + ctx["news_empty"] + "\n
", nil + return "", nil } items := make([]string, len(years)) for i, y := range years { diff --git a/internal/build/blog_test.go b/internal/build/blog_test.go index 2835a32..fe21863 100644 --- a/internal/build/blog_test.go +++ b/internal/build/blog_test.go @@ -3,6 +3,7 @@ package build import ( + "encoding/xml" "path/filepath" "strings" "testing" @@ -526,3 +527,180 @@ func TestPanelOfAPostInAnotherLanguage(t *testing.T) { t.Error("an English post is marked as written in Italian") } } + +// TestArchiveGroupsByTheDateAsWritten checks that two posts either side of midnight, written with different +// offsets, land in their own months and that no month is listed twice, whatever the order of the instants. +func TestArchiveGroupsByTheDateAsWritten(t *testing.T) { + files := blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate, + "news/late.md": "Title: Late\nDate: 2026-11-30T23:45:00+0000\n-\nText.\n", + "news/early.md": "Title: Early\nDate: 2026-12-01T00:30:00+0100\n-\nText.\n", + "news/mid.md": "Title: Mid\nDate: 2026-11-29T12:00:00+0100\n-\nText.\n"}) + out, err := buildWith(t, files, func(o *Options) { o.Now = time.Date(2026, 12, 20, 12, 0, 0, 0, time.UTC) }) + if err != nil { + t.Fatalf("Build() error = %v", err) + } + index := readOut(t, out, "news/index.html") + mustContain(t, "news index", index, `2026 (6)`) + dec, nov, sep := strings.Index(index, ">December 2026 (1)"), strings.Index(index, ">November 2026 (4)"), strings.Index(index, ">September 2026 (1)") + if dec < 0 || nov < dec || sep < nov { + t.Errorf("the months are not December, November, September, one each:\n%s", index) + } + if strings.Count(index, "November 2026") != 1 { + t.Error("November is listed more than once") + } + month := readOut(t, out, "news/2026/11/index.html") + late, mid, shop := strings.Index(month, "Late."), strings.Index(month, "Mid."), strings.Index(month, "A shop.") + if late < 0 || mid < late || shop < mid || strings.Contains(month, "Early.") { + t.Errorf("the month of November is wrong:\n%s", month) + } + mustContain(t, "month of December", readOut(t, out, "news/2026/12/index.html"), "Early.") +} + +// TestNoNewsAtAll checks that a site without posts says so once, and that the empty lists take their headings with them. +func TestNoNewsAtAll(t *testing.T) { + index := "Title: News\nID: news\nDescription: About News.\n-\n# News\n\n{{news_blog}}\n\n## Categories\n\n{{categories_list}}\n\n## Archive\n\n{{news_archive}}\n" + files := blogSite(map[string]string{"content/en/news/index.md": index, "templates/archive.html": archiveTemplate, + "news/shop.md": "", "news/gnu-linux-day-2026.md": "", "news/status.md": ""}) + out, err := buildWith(t, files, nil) + if err != nil { + t.Fatalf("Build() error = %v", err) + } + got := readOut(t, out, "news/index.html") + if n := strings.Count(got, "No news yet."); n != 1 { + t.Errorf("the index says there is no news %d times, want once:\n%s", n, got) + } + for _, heading := range []string{">Categories", ">Archive"} { + if strings.Contains(got, heading) { + t.Errorf("the index keeps the heading %q of an empty list", heading) + } + } + mustContain(t, "news index", got, "/. It is the language the server serves by default
// (Agate's --lang). Empty means gemini_language of site.json, then the
@@ -42,6 +44,9 @@ type GeminiOptions struct {
type GeminiResult struct {
// Pages is the number of gemtext files written.
Pages int
+ // Stale lists the files that were already in the output folder and that this
+ // build did not write; see Result.Stale.
+ Stale []string
}
// gemNews is the address of the news in the capsule. They exist once, whatever
@@ -104,17 +109,18 @@ func BuildGemini(opts GeminiOptions) (GeminiResult, error) {
if err != nil {
return GeminiResult{}, err
}
+ files := make(map[string][]byte, len(g.gemFiles)+len(assets))
for rel, text := range g.gemFiles {
- if err := writeFile(opts.OutDir, rel, []byte(text)); err != nil {
- return GeminiResult{}, err
- }
+ files[rel] = []byte(text)
}
for rel, data := range assets {
- if err := writeFile(opts.OutDir, rel, data); err != nil {
- return GeminiResult{}, err
- }
+ files[rel] = data
}
- return GeminiResult{Pages: pages}, nil
+ stale, err := writeOutput(opts.OutDir, opts.SiteDir, opts.Clean, files)
+ if err != nil {
+ return GeminiResult{}, err
+ }
+ return GeminiResult{Pages: pages, Stale: stale}, nil
}
// rootLanguage returns the language at the root of the capsule: the option,
diff --git a/internal/build/output.go b/internal/build/output.go
new file mode 100644
index 0000000..0dbe133
--- /dev/null
+++ b/internal/build/output.go
@@ -0,0 +1,105 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+package build
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+ "sort"
+ "strings"
+)
+
+// motorinoMarker is the file motorino leaves in a folder it emptied with the
+// clean option. A folder that has it, or has nothing in it, may be emptied again;
+// any other folder never is. It is only written when the option is used, so the
+// folders of a build without it hold the site and nothing else.
+const motorinoMarker = ".motorino-out"
+
+// writeOutput writes the files (path with slashes, relative to out) into out and
+// returns the files that were already in out and are not among them: the pages
+// of an earlier build that this one no longer makes. With clean set it first
+// empties out, but only a folder that is new, empty or marked by an earlier clean
+// build, and never the folder of the site or one that holds it. It does not
+// touch the folder before every file is known, so a content error leaves it as
+// it was.
+func writeOutput(out, siteDir string, clean bool, files map[string][]byte) ([]string, error) {
+ if clean {
+ if err := emptyOutput(out, siteDir); err != nil {
+ return nil, err
+ }
+ files = withMarker(files)
+ }
+ for rel, data := range files {
+ if err := writeFile(out, rel, data); err != nil {
+ return nil, err
+ }
+ }
+ return staleFiles(out, files)
+}
+
+// withMarker returns the files and the marker.
+func withMarker(files map[string][]byte) map[string][]byte {
+ marked := make(map[string][]byte, len(files)+1)
+ for rel, data := range files {
+ marked[rel] = data
+ }
+ marked[motorinoMarker] = []byte("This folder is written by motorino and emptied by motorino build -clean and motorino gemini -clean.\n")
+ return marked
+}
+
+// emptyOutput removes what is in out, after checking that it may be removed.
+func emptyOutput(out, siteDir string) error {
+ absOut, err := filepath.Abs(out)
+ if err != nil {
+ return err
+ }
+ absSite, err := filepath.Abs(siteDir)
+ if err != nil {
+ return err
+ }
+ if rel, err := filepath.Rel(absOut, absSite); err == nil && rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
+ return fmt.Errorf("refusing to empty %s: it is the folder of the site or holds it", out)
+ }
+ entries, err := os.ReadDir(out)
+ if os.IsNotExist(err) || (err == nil && len(entries) == 0) {
+ return nil
+ }
+ if err != nil {
+ return fmt.Errorf("read %s: %w", out, err)
+ }
+ if info, err := os.Lstat(filepath.Join(out, motorinoMarker)); err != nil || !info.Mode().IsRegular() {
+ return fmt.Errorf("refusing to empty %s: it has files and no %s, which motorino leaves in a folder it emptied; empty it yourself this first time", out, motorinoMarker)
+ }
+ for _, entry := range entries {
+ if err := os.RemoveAll(filepath.Join(out, entry.Name())); err != nil {
+ return fmt.Errorf("empty %s: %w", out, err)
+ }
+ }
+ return nil
+}
+
+// staleFiles lists the files under out that are not among the written ones, with
+// slashes, sorted. The marker is never one of them.
+func staleFiles(out string, written map[string][]byte) ([]string, error) {
+ var stale []string
+ err := filepath.WalkDir(out, func(path string, entry os.DirEntry, err error) error {
+ if err != nil || entry.IsDir() {
+ return err
+ }
+ rel, err := filepath.Rel(out, path)
+ if err != nil {
+ return err
+ }
+ rel = filepath.ToSlash(rel)
+ if _, ok := written[rel]; !ok && rel != motorinoMarker {
+ stale = append(stale, rel)
+ }
+ return nil
+ })
+ if err != nil {
+ return nil, fmt.Errorf("read %s: %w", out, err)
+ }
+ sort.Strings(stale)
+ return stale, nil
+}
diff --git a/internal/build/output_gemini_test.go b/internal/build/output_gemini_test.go
new file mode 100644
index 0000000..c69a639
--- /dev/null
+++ b/internal/build/output_gemini_test.go
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+package build
+
+import (
+ "path/filepath"
+ "strings"
+ "testing"
+)
+
+// init adds the Gemini output to the tests of the output folder in output_test.go.
+func init() {
+ extraBuilders["gemini"] = func(site string) func(string, bool) ([]string, error) {
+ return func(out string, clean bool) ([]string, error) {
+ r, err := BuildGemini(GeminiOptions{SiteDir: site, OutDir: out, Version: "1.2.3", Now: fixedNow, Clean: clean, Language: "en"})
+ return r.Stale, err
+ }
+ }
+}
+
+// TestGeminiCleanRefusesAFolderThatHoldsTheSite checks that -clean never empties the folder of the site, or one above it.
+func TestGeminiCleanRefusesAFolderThatHoldsTheSite(t *testing.T) {
+ parent := t.TempDir()
+ site := filepath.Join(parent, "site")
+ for rel, text := range withFiles(nil) {
+ put(t, site, rel, text)
+ }
+ put(t, parent, motorinoMarker, "x")
+ for _, out := range []string{site, parent} {
+ if _, err := BuildGemini(GeminiOptions{SiteDir: site, OutDir: out, Now: fixedNow, Clean: true}); err == nil || !strings.Contains(err.Error(), "site") {
+ t.Errorf("BuildGemini into %s: error = %v, want one about the site", out, err)
+ }
+ }
+ if !exists(t, site, "site.json") {
+ t.Error("the site is gone")
+ }
+}
diff --git a/internal/build/output_test.go b/internal/build/output_test.go
new file mode 100644
index 0000000..8fd0efa
--- /dev/null
+++ b/internal/build/output_test.go
@@ -0,0 +1,162 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+package build
+
+import (
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+)
+
+// put writes a file of a test folder.
+func put(t *testing.T, dir, rel, text string) {
+ t.Helper()
+ path := filepath.Join(dir, filepath.FromSlash(rel))
+ if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(path, []byte(text), 0o644); err != nil {
+ t.Fatal(err)
+ }
+}
+
+// extraBuilders are the other outputs the tests of the output folder run against, by name: each takes the
+// folder of a site and returns a function that builds it into a folder.
+var extraBuilders = map[string]func(site string) func(out string, clean bool) ([]string, error){}
+
+// builders are the outputs, each as a function that builds the site made of files into a folder and returns
+// the files of the folder that the build did not write.
+func builders(t *testing.T, files map[string]string) map[string]func(out string, clean bool) ([]string, error) {
+ t.Helper()
+ site := writeFiles(t, files)
+ all := map[string]func(string, bool) ([]string, error){
+ "html": func(out string, clean bool) ([]string, error) {
+ r, err := Build(Options{SiteDir: site, OutDir: out, Version: "1.2.3", Now: fixedNow, Clean: clean})
+ return r.Stale, err
+ },
+ }
+ for name, newBuilder := range extraBuilders {
+ all[name] = newBuilder(site)
+ }
+ return all
+}
+
+// TestStaleFilesAreReported checks that a build into a folder with files it did not write reports them and leaves them.
+func TestStaleFilesAreReported(t *testing.T) {
+ for name, run := range builders(t, withFiles(nil)) {
+ t.Run(name, func(t *testing.T) {
+ out := t.TempDir()
+ put(t, out, "news/page/2/index.html", "old")
+ put(t, out, "old.txt", "old")
+ stale, err := run(out, false)
+ if err != nil {
+ t.Fatalf("build error = %v", err)
+ }
+ if strings.Join(stale, ",") != "news/page/2/index.html,old.txt" {
+ t.Errorf("stale = %v, want the two old files", stale)
+ }
+ if !exists(t, out, "old.txt") {
+ t.Error("a build without -clean removed a file")
+ }
+ if exists(t, out, motorinoMarker) {
+ t.Error("a build without -clean wrote the marker into the folder")
+ }
+ })
+ }
+}
+
+// TestFreshFolderHasNothingStale checks that a build into a new folder reports nothing and writes only the site.
+func TestFreshFolderHasNothingStale(t *testing.T) {
+ for name, run := range builders(t, withFiles(nil)) {
+ t.Run(name, func(t *testing.T) {
+ stale, err := run(filepath.Join(t.TempDir(), "new"), false)
+ if err != nil || len(stale) != 0 {
+ t.Errorf("stale = %v, error = %v", stale, err)
+ }
+ })
+ }
+}
+
+// TestCleanEmptiesAFolderItMarked checks that -clean removes the old files of a folder that has the marker,
+// the pages of a list that got shorter included, and writes the marker again.
+func TestCleanEmptiesAFolderItMarked(t *testing.T) {
+ for name, run := range builders(t, withFiles(nil)) {
+ t.Run(name, func(t *testing.T) {
+ out := t.TempDir()
+ put(t, out, motorinoMarker, "x")
+ put(t, out, "news/page/2/index.html", "old")
+ put(t, out, "old/deep/file.txt", "old")
+ stale, err := run(out, true)
+ if err != nil {
+ t.Fatalf("build error = %v", err)
+ }
+ if len(stale) != 0 {
+ t.Errorf("stale = %v after a clean build", stale)
+ }
+ for _, gone := range []string{"news/page/2/index.html", "old/deep/file.txt"} {
+ if exists(t, out, gone) {
+ t.Errorf("%s is still there after -clean", gone)
+ }
+ }
+ if !exists(t, out, motorinoMarker) {
+ t.Error("the marker is gone after -clean")
+ }
+ })
+ }
+}
+
+// TestCleanTakesAnEmptyOrNewFolder checks that -clean needs no marker for a folder with nothing in it, and marks it.
+func TestCleanTakesAnEmptyOrNewFolder(t *testing.T) {
+ for name, run := range builders(t, withFiles(nil)) {
+ t.Run(name, func(t *testing.T) {
+ for _, out := range []string{t.TempDir(), filepath.Join(t.TempDir(), "new", "deeper")} {
+ if _, err := run(out, true); err != nil {
+ t.Fatalf("build into %s error = %v", out, err)
+ }
+ if !exists(t, out, motorinoMarker) {
+ t.Errorf("no marker in %s", out)
+ }
+ }
+ })
+ }
+}
+
+// TestCleanRefusesAFolderThatIsNotMotorinos checks that -clean leaves a folder with files and no marker alone.
+func TestCleanRefusesAFolderThatIsNotMotorinos(t *testing.T) {
+ for name, run := range builders(t, withFiles(nil)) {
+ t.Run(name, func(t *testing.T) {
+ out := t.TempDir()
+ put(t, out, "precious.txt", "keep me")
+ _, err := run(out, true)
+ if err == nil || !strings.Contains(err.Error(), motorinoMarker) {
+ t.Fatalf("error = %v, want one that names %s", err, motorinoMarker)
+ }
+ if got := readOut(t, out, "precious.txt"); got != "keep me" {
+ t.Errorf("precious.txt = %q", got)
+ }
+ if exists(t, out, "index.html") || exists(t, out, "index.gmi") || exists(t, out, "en/index.gmi") {
+ t.Error("the build wrote into a folder it refused to empty")
+ }
+ })
+ }
+}
+
+// TestCleanRefusesAFolderThatHoldsTheSite checks that -clean never empties the folder of the site or one above it,
+// even when it has the marker.
+func TestCleanRefusesAFolderThatHoldsTheSite(t *testing.T) {
+ parent := t.TempDir()
+ site := filepath.Join(parent, "site")
+ for rel, text := range withFiles(nil) {
+ put(t, site, rel, text)
+ }
+ put(t, parent, motorinoMarker, "x")
+ for _, out := range []string{site, parent} {
+ if _, err := Build(Options{SiteDir: site, OutDir: out, Now: fixedNow, Clean: true}); err == nil || !strings.Contains(err.Error(), "site") {
+ t.Errorf("Build into %s: error = %v, want one about the site", out, err)
+ }
+ }
+ if !exists(t, site, "site.json") {
+ t.Error("the site is gone")
+ }
+}
diff --git a/internal/build/pages.go b/internal/build/pages.go
index f6296ba..cc6ffa6 100644
--- a/internal/build/pages.go
+++ b/internal/build/pages.go
@@ -134,9 +134,10 @@ func (b *builder) markdown(name, source, lang string, ctx map[string]string) (st
var codeElement = regexp.MustCompile(`(?s)]*>.*?`)
// replaceBlocks replaces each paragraph that holds only a block placeholder by
-// what replace returns for the name of the block. A block placeholder inside
-// other text is an error. All blocks are checked before any is replaced, so the
-// text of a generated list (a news title may say {{latest_news}}) is never
+// what replace returns for the name of the block. An empty list of categories or
+// of the archive goes with the heading right before it. A block placeholder
+// inside other text is an error. All blocks are checked before any is replaced,
+// so the text of a generated list (a news title may say {{latest_news}}) is never
// taken for a placeholder.
func replaceBlocks(name, html string, replace func(block string) (string, error)) (string, error) {
outside := codeElement.ReplaceAllString(html, "")
@@ -155,6 +156,10 @@ func replaceBlocks(name, html string, replace func(block string) (string, error)
if err != nil {
return "", err
}
+ if generated == "" && headedBlocks[block] {
+ html = removeWithHeading(html, marker)
+ continue
+ }
html = strings.ReplaceAll(html, marker, generated)
}
return html, nil
@@ -165,6 +170,32 @@ func (b *builder) expandBlocks(name, html, lang string, ctx map[string]string) (
return replaceBlocks(name, html, func(block string) (string, error) { return b.block(block, lang, ctx) })
}
+// headedBlocks are the lists a page puts under a heading of its own, which goes
+// when the list is empty. The latest news bring their heading with them, and a
+// heading before them is the page's.
+var headedBlocks = map[string]bool{"categories_list": true, "news_archive": true}
+
+// headingEnd matches the end of a heading element at the end of a text.
+var headingEnd = regexp.MustCompile(`$`)
+
+// removeWithHeading removes every marker of an empty block and the heading right
+// before it, which would be left with nothing under it.
+func removeWithHeading(html, marker string) string {
+ for {
+ at := strings.Index(html, marker)
+ if at < 0 {
+ return html
+ }
+ before := strings.TrimRight(html[:at], " \t\r\n")
+ if m := headingEnd.FindStringSubmatchIndex(before); m != nil {
+ if open := strings.LastIndex(before[:m[0]], "= 0 {
+ before = before[:open]
+ }
+ }
+ html = before + "\n" + strings.TrimLeft(html[at+len(marker):], "\n")
+ }
+}
+
// block returns the HTML of a generated list.
func (b *builder) block(name, lang string, ctx map[string]string) (string, error) {
switch name {