From 6bdf2ca05f2fec4242c67b7819a5707cca134d9d Mon Sep 17 00:00:00 2001 From: tgiachi Date: Sun, 15 Nov 2026 09:50:58 +0100 Subject: [PATCH] feat: assemble the site from content, templates and assets --- internal/build/build.go | 222 ++++++++++++++++++++++++ internal/build/build_test.go | 306 +++++++++++++++++++++++++++++++++ internal/build/context.go | 210 ++++++++++++++++++++++ internal/build/fixture_test.go | 193 +++++++++++++++++++++ internal/build/pages.go | 226 ++++++++++++++++++++++++ internal/build/robots.go | 27 +++ internal/mdx/mdx.go | 3 +- 7 files changed, 1186 insertions(+), 1 deletion(-) create mode 100644 internal/build/build.go create mode 100644 internal/build/build_test.go create mode 100644 internal/build/context.go create mode 100644 internal/build/fixture_test.go create mode 100644 internal/build/pages.go create mode 100644 internal/build/robots.go diff --git a/internal/build/build.go b/internal/build/build.go new file mode 100644 index 0000000..cdabad4 --- /dev/null +++ b/internal/build/build.go @@ -0,0 +1,222 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package build + +import ( + "fmt" + "os" + "path/filepath" + "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 + // 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 +} + +// 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) error { + b, err := load(opts) + if err != nil { + return err + } + if err := b.renderPages(); err != nil { + return err + } + if err := b.renderPosts(); err != nil { + return err + } + if err := b.renderCategories(); err != nil { + return err + } + b.renderFeed() + b.files["robots.txt"] = robots(b.indexable) + assets, err := b.readAssets() + if err != nil { + return err + } + return b.write(assets) +} + +// 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{}, 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 _, 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 +} + +// readAssets reads the files listed in site.json; a missing source is an error. +func (b *builder) readAssets() (map[string][]byte, error) { + out := map[string][]byte{} + for name, source := range b.cfg.Assets { + data, err := os.ReadFile(filepath.Join(b.opts.SiteDir, filepath.FromSlash(source))) + if err != nil { + return nil, fmt.Errorf("asset %q: %w", name, err) + } + out[name] = 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 +} diff --git a/internal/build/build_test.go b/internal/build/build_test.go new file mode 100644 index 0000000..6766732 --- /dev/null +++ b/internal/build/build_test.go @@ -0,0 +1,306 @@ +// 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"), + "Services", ``, + "squid (at) kmalloc.org", ``) +} + +// 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, + "

GNU/Linux Day 2026

", ``, + "Matteo Bini", `Linux Day`, `FLUG`, + "kmalloc.org: GNU/Linux Day 2026", `people`) + 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, "

Category: FLUG

", "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, + `FLUG (2)`, `Linux Day (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, + ``, ``, + `Italiano`) + it := readOut(t, out, "it/servizi.html") + mustContain(t, "it services", it, `English`) + // 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, `Italiano`) + 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.", + `English`) +} + +// 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"), `Manifesto`) + mustContain(t, "post", readOut(t, out, "news/2026/11/shop.html"), `News`) + 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"), + `
  • A shop: About the shop.
  • `) + home := readOut(t, out, "index.html") + mustContain(t, "home", home, `

    News

    `, `A shop`) + it := readOut(t, out, "it/index.html") + mustContain(t, "it home", it, `A shop`) + if strings.Contains(home, "{{") || strings.Contains(home, "