feat: assemble the site from content, templates and assets
This commit is contained in:
parent
935bf3958f
commit
6bdf2ca05f
7 changed files with 1186 additions and 1 deletions
222
internal/build/build.go
Normal file
222
internal/build/build.go
Normal file
|
|
@ -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
|
||||||
|
}
|
||||||
306
internal/build/build_test.go
Normal file
306
internal/build/build_test.go
Normal file
|
|
@ -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"),
|
||||||
|
"<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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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>",
|
||||||
|
`<a href="https://kmalloc.org/people.html">people</a>`)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestBuildErrorsOnMissingTemplate checks that a missing layout template is named.
|
||||||
|
func TestBuildErrorsOnMissingTemplate(t *testing.T) {
|
||||||
|
files := baseFiles()
|
||||||
|
delete(files, "templates/pages/manifesto.html")
|
||||||
|
_, err := runBuild(t, files)
|
||||||
|
if err == nil || !strings.Contains(err.Error(), "pages/manifesto") {
|
||||||
|
t.Errorf("Build() error = %v, want it to name pages/manifesto", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
210
internal/build/context.go
Normal file
210
internal/build/context.go
Normal file
|
|
@ -0,0 +1,210 @@
|
||||||
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
|
package build
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.kmalloc.org/squid/motorino/internal/render"
|
||||||
|
)
|
||||||
|
|
||||||
|
// menuPages are the IDs of the pages in the menu; the current one is marked.
|
||||||
|
var menuPages = []string{"home", "manifesto", "people", "services", "news"}
|
||||||
|
|
||||||
|
// linkedPages are the IDs the templates link to with url_<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
|
||||||
|
}
|
||||||
|
|
||||||
|
// copyContext returns a copy of ctx.
|
||||||
|
func copyContext(ctx map[string]string) map[string]string {
|
||||||
|
out := make(map[string]string, len(ctx))
|
||||||
|
for k, v := range ctx {
|
||||||
|
out[k] = v
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// pageAddress returns the address of the page with this ID in lang, or in the
|
||||||
|
// default language when lang has no such page.
|
||||||
|
func (b *builder) pageAddress(lang, id string) string {
|
||||||
|
if p, ok := b.byKey[pageKey{id, lang}]; ok {
|
||||||
|
return p.Path
|
||||||
|
}
|
||||||
|
return b.byKey[pageKey{id, b.defaultLang}].Path
|
||||||
|
}
|
||||||
|
|
||||||
|
// languageContext returns the placeholders that depend on the language only:
|
||||||
|
// the site strings, the language strings (escaped), the menu labels and the
|
||||||
|
// addresses of the pages the templates link to.
|
||||||
|
func (b *builder) languageContext(lang string) map[string]string {
|
||||||
|
if cached, ok := b.langCtx[lang]; ok {
|
||||||
|
return copyContext(cached)
|
||||||
|
}
|
||||||
|
l := b.langs[lang]
|
||||||
|
ctx := copyContext(b.cfg.Strings)
|
||||||
|
for k, v := range l.Strings {
|
||||||
|
ctx[k] = render.Escape(v)
|
||||||
|
}
|
||||||
|
ctx["lang"] = lang
|
||||||
|
for k, label := range l.Nav {
|
||||||
|
ctx["nav_"+k] = render.Escape(label)
|
||||||
|
}
|
||||||
|
for _, id := range linkedPages {
|
||||||
|
ctx["url_"+id] = b.pageAddress(lang, id)
|
||||||
|
}
|
||||||
|
ctx["history_attr"] = ""
|
||||||
|
if _, ok := b.byKey[pageKey{"history", lang}]; !ok {
|
||||||
|
ctx["history_attr"] = fmt.Sprintf(` hreflang="%s"`, b.defaultLang)
|
||||||
|
}
|
||||||
|
ctx["version_lc"] = render.Escape(strings.ToLower(l.Strings["version"]))
|
||||||
|
ctx["motorino_version"] = b.opts.Version
|
||||||
|
for _, name := range blockNames {
|
||||||
|
ctx[name] = "{{" + name + "}}"
|
||||||
|
}
|
||||||
|
b.langCtx[lang] = ctx
|
||||||
|
return copyContext(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// documentContext returns the placeholders of one page: the language ones plus
|
||||||
|
// the style, the address, the titles, the menu marks and the language links.
|
||||||
|
func (b *builder) documentContext(d document) map[string]string {
|
||||||
|
ctx := b.languageContext(d.lang)
|
||||||
|
ctx["css"] = b.css
|
||||||
|
ctx["path"] = d.path
|
||||||
|
docURL := b.cfg.Strings["base_url"] + d.path
|
||||||
|
ctx["doc_url"] = docURL
|
||||||
|
ctx["doc_q"] = strings.NewReplacer(":", "%3A", "/", "%2F").Replace(docURL)
|
||||||
|
title := d.title
|
||||||
|
if d.prefixed {
|
||||||
|
title = "kmalloc.org: " + title
|
||||||
|
}
|
||||||
|
ctx["title_html"] = render.Escape(title)
|
||||||
|
ctx["page_title"] = render.Escape(d.title)
|
||||||
|
ctx["description_attr"] = render.EscapeAttr(d.description)
|
||||||
|
for _, k := range menuPages {
|
||||||
|
ctx["cur_"+k] = ""
|
||||||
|
if d.nav == k {
|
||||||
|
ctx["cur_"+k] = ` aria-current="page"`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ctx["lang_links"], ctx["alt_links"] = b.languageLinks(d)
|
||||||
|
return ctx
|
||||||
|
}
|
||||||
|
|
||||||
|
// languageLinks returns the links to the other languages for the menu and the
|
||||||
|
// alternate links for the head. A page without translations links to the home
|
||||||
|
// of the first other language that has one.
|
||||||
|
func (b *builder) languageLinks(d document) (links, alternates string) {
|
||||||
|
type target struct{ lang, path string }
|
||||||
|
var twins []target
|
||||||
|
if d.id != "" {
|
||||||
|
for _, l := range b.cfg.Languages {
|
||||||
|
if p, ok := b.byKey[pageKey{d.id, l}]; ok && l != d.lang {
|
||||||
|
twins = append(twins, target{l, p.Path})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(twins) == 0 {
|
||||||
|
for _, l := range b.cfg.Languages {
|
||||||
|
if p, ok := b.byKey[pageKey{"home", l}]; ok && l != d.lang {
|
||||||
|
twins = append(twins, target{l, p.Path})
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
anchors := make([]string, len(twins))
|
||||||
|
for i, t := range twins {
|
||||||
|
href := t.path
|
||||||
|
if override, ok := d.switches[t.lang]; ok {
|
||||||
|
href = override
|
||||||
|
}
|
||||||
|
anchors[i] = fmt.Sprintf(`<a href="%s" hreflang="%s" lang="%s">%s</a>`, href, t.lang, t.lang, render.Escape(b.langs[t.lang].Strings["lang_name"]))
|
||||||
|
}
|
||||||
|
links = strings.Join(anchors, "\n·\n")
|
||||||
|
|
||||||
|
if d.id == "" {
|
||||||
|
return links, ""
|
||||||
|
}
|
||||||
|
var alts []string
|
||||||
|
for _, l := range b.cfg.Languages {
|
||||||
|
if p, ok := b.byKey[pageKey{d.id, l}]; ok {
|
||||||
|
alts = append(alts, fmt.Sprintf(`<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
|
||||||
|
}
|
||||||
|
b.files[outputPath(d.path)] = html
|
||||||
|
b.indexable = append(b.indexable, d.path)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// headerTemplate returns the header template of a language: "header" unless
|
||||||
|
// its file says otherwise with the header key.
|
||||||
|
func (b *builder) headerTemplate(lang string) string {
|
||||||
|
if name := b.langs[lang].Strings["header"]; name != "" {
|
||||||
|
return name
|
||||||
|
}
|
||||||
|
return "header"
|
||||||
|
}
|
||||||
193
internal/build/fixture_test.go
Normal file
193
internal/build/fixture_test.go
Normal file
|
|
@ -0,0 +1,193 @@
|
||||||
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
|
package build
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// writeFiles writes the files (path relative to a new directory, then content)
|
||||||
|
// and returns the directory.
|
||||||
|
func writeFiles(t *testing.T, files map[string]string) string {
|
||||||
|
t.Helper()
|
||||||
|
dir := t.TempDir()
|
||||||
|
for name, content := range files {
|
||||||
|
path := filepath.Join(dir, filepath.FromSlash(name))
|
||||||
|
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dir
|
||||||
|
}
|
||||||
|
|
||||||
|
// runBuild builds the site made of files into a fresh output folder and
|
||||||
|
// returns the folder and the build error.
|
||||||
|
func runBuild(t *testing.T, files map[string]string) (string, error) {
|
||||||
|
t.Helper()
|
||||||
|
out := filepath.Join(t.TempDir(), "public")
|
||||||
|
err := Build(Options{SiteDir: writeFiles(t, files), OutDir: out, Version: "1.2.3"})
|
||||||
|
return out, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// readOut returns the content of a file of the output, failing the test when it
|
||||||
|
// is missing.
|
||||||
|
func readOut(t *testing.T, out, rel string) string {
|
||||||
|
t.Helper()
|
||||||
|
data, err := os.ReadFile(filepath.Join(out, filepath.FromSlash(rel)))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("output file %s: %v", rel, err)
|
||||||
|
}
|
||||||
|
return string(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
// mustContain fails the test for each want that is not in got.
|
||||||
|
func mustContain(t *testing.T, name, got string, wants ...string) {
|
||||||
|
t.Helper()
|
||||||
|
for _, want := range wants {
|
||||||
|
if !strings.Contains(got, want) {
|
||||||
|
t.Errorf("%s does not contain %q\n%s", name, want, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// page returns a page file with the given ID, title, extra header lines and body.
|
||||||
|
func page(id, title, extra, body string) string {
|
||||||
|
return "Title: " + title + "\nID: " + id + "\nDescription: About " + title + ".\n" + extra + "-\n" + body
|
||||||
|
}
|
||||||
|
|
||||||
|
// baseFiles is a small complete site: English and Italian with every page, a
|
||||||
|
// Spanish manifesto, three news with categories, templates and one asset.
|
||||||
|
func baseFiles() map[string]string {
|
||||||
|
return map[string]string{
|
||||||
|
"site.json": `{
|
||||||
|
"base_url": "https://kmalloc.org",
|
||||||
|
"languages": ["en", "it", "es"],
|
||||||
|
"contact": "squid@kmalloc.org",
|
||||||
|
"mastodon_url": "https://social.kmalloc.org/@squid",
|
||||||
|
"motorino_url": "https://git.kmalloc.org/squid/motorino",
|
||||||
|
"manifesto_version": "1.2",
|
||||||
|
"manifesto_date": "2026-11-14",
|
||||||
|
"issues_url": "https://git.kmalloc.org/squid/kmalloc.org/issues",
|
||||||
|
"assets": {"logo.svg": "static/logo.svg"}
|
||||||
|
}`,
|
||||||
|
"lang/en.json": `{"lang_name": "English", "sections": "Sections", "home_link": "/", "nojs": "No JavaScript.",
|
||||||
|
"powered_by": "Powered by", "back": "kmalloc.org", "version": "Version", "toc": "Contents", "history_a": "Manifesto history",
|
||||||
|
"news_p": "What is new.", "news_all": "All news", "news_empty": "No news yet.", "category_h": "Category",
|
||||||
|
"nav": {"manifesto": "Manifesto", "people": "People", "services": "Services", "news": "News"}}`,
|
||||||
|
"lang/it.json": `{"lang_name": "Italiano", "sections": "Sezioni", "home_link": "/it/", "nojs": "Niente JavaScript.",
|
||||||
|
"powered_by": "Powered by", "back": "kmalloc.org", "version": "Versione", "toc": "Indice", "history_a": "Cronologia",
|
||||||
|
"news_p": "Le novita.", "news_all": "Tutte le news", "news_empty": "Nessuna news.", "category_h": "Categoria",
|
||||||
|
"nav": {"manifesto": "Manifesto", "people": "Persone", "services": "Servizi", "news": "News"}}`,
|
||||||
|
"lang/es.json": `{"lang_name": "Espanol", "header": "header-min", "sections": "Secciones", "home_link": "/?lang=en",
|
||||||
|
"nojs": "Sin JavaScript.", "powered_by": "Powered by", "back": "kmalloc.org", "version": "Version", "toc": "Indice",
|
||||||
|
"history_a": "Historial", "translation_note": "Traduccion automatica.", "translation_note_a": "avisar",
|
||||||
|
"news_p": "x", "news_all": "x", "news_empty": "x", "category_h": "x",
|
||||||
|
"nav": {"manifesto": "Manifiesto", "news": "News"}}`,
|
||||||
|
|
||||||
|
"content/en/index.md": page("home", "kmalloc.org", "Layout: bare\n", "<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.\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 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}}
|
||||||
|
`,
|
||||||
|
}
|
||||||
|
}
|
||||||
226
internal/build/pages.go
Normal file
226
internal/build/pages.go
Normal file
|
|
@ -0,0 +1,226 @@
|
||||||
|
// 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,
|
||||||
|
}
|
||||||
|
if contains(menuPages, p.ID) {
|
||||||
|
d.nav = p.ID
|
||||||
|
}
|
||||||
|
if p.ID == "home" {
|
||||||
|
d.lead = "home"
|
||||||
|
}
|
||||||
|
ctx := b.documentContext(d)
|
||||||
|
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
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(" · " + 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(" · " + 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,
|
||||||
|
}
|
||||||
|
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() {
|
||||||
|
newsPath := b.byKey[pageKey{"news", b.defaultLang}].Path
|
||||||
|
text := feed(b.cfg.Strings["base_url"], b.langs[b.defaultLang], newsPath, b.rendered, feedLimit)
|
||||||
|
b.files[outputPath(newsPath+"feed.xml")] = 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
27
internal/build/robots.go
Normal 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"
|
||||||
|
}
|
||||||
|
|
@ -31,7 +31,8 @@ type Heading struct {
|
||||||
var engine = goldmark.New(goldmark.WithRendererOptions(html.WithUnsafe()))
|
var engine = goldmark.New(goldmark.WithRendererOptions(html.WithUnsafe()))
|
||||||
|
|
||||||
// Render converts md to HTML and returns the headings it found. Every heading
|
// Render converts md to HTML and returns the headings it found. Every heading
|
||||||
// gets an id attribute made by Slug, with -2, -3 and so on for duplicates.
|
// from level 2 gets an id attribute made by Slug, with -2, -3 and so on for
|
||||||
|
// duplicates; the level 1 heading is the title of the page and gets none.
|
||||||
func Render(md string) (string, []Heading, error) {
|
func Render(md string) (string, []Heading, error) {
|
||||||
src := []byte(md)
|
src := []byte(md)
|
||||||
doc := engine.Parser().Parse(text.NewReader(src))
|
doc := engine.Parser().Parse(text.NewReader(src))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue