feat: take the menu from the site, not from the code

The menu entries were three lists in Go (the menu, the linked pages and
the required pages), so a new menu entry meant a new motorino release.
Now every page ID gets url_<id> and cur_<id> for the templates, and
header.html alone decides what is in the menu and in which order. Only
the home page and the news section stay required, since motorino builds
them itself.

Output of the real kmalloc.org site is byte for byte the same as 1.1.0.
This commit is contained in:
tgiachi 2026-11-15 18:08:42 +01:00
commit eb2a8dc9b4
4 changed files with 50 additions and 27 deletions

View file

@ -27,8 +27,10 @@ type Options struct {
}
// 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", "projects", "services", "news", "history"}
// because motorino builds the home page and the news section itself. The
// other pages, and the menu that links to them, are the site's own: a template
// can link to any page with url_<id> and mark it with cur_<id>.
var requiredPages = []string{"home", "news"}
// newsOnHome is how many news the home page lists; feedLimit is how many the feed has.
const (
@ -51,6 +53,8 @@ type builder struct {
templates map[string]string
css string
byKey map[pageKey]content.Page
// pageIDs lists the ID of every page, once, whatever the language.
pageIDs []string
// files holds the rendered output: relative path to content.
files map[string]string
// owners names, for each output file, what produces it.
@ -127,8 +131,13 @@ func load(opts Options) (*builder, error) {
if b.posts, err = content.LoadPosts(opts.SiteDir); err != nil {
return nil, err
}
seenID := map[string]bool{}
for _, p := range b.pages {
b.byKey[pageKey{p.ID, p.Lang}] = p
if !seenID[p.ID] {
seenID[p.ID] = true
b.pageIDs = append(b.pageIDs, p.ID)
}
for lang := range p.Switch {
if _, known := langs[lang]; !known {
return nil, fmt.Errorf("%s: Switch names the language %q, which is not in site.json", p.Source, lang)

View file

@ -466,3 +466,33 @@ func TestBuildErrorsOnMissingTemplate(t *testing.T) {
t.Errorf("Build() error = %v, want it to name pages/manifesto", err)
}
}
// TestBuildMenuComesFromTheSite checks that a page with an ID motorino has never
// heard of can be linked and marked from a template, with no change to the code.
func TestBuildMenuComesFromTheSite(t *testing.T) {
files := baseFiles()
files["content/en/labs.md"] = page("labs", "Labs", "", "Experiments.\n")
files["templates/header.html"] = `<header>
<a href="{{url_labs}}"{{cur_labs}}>Labs</a>
<a href="{{url_news}}"{{cur_news}}>News</a>
</header>
`
out, err := runBuild(t, files)
if err != nil {
t.Fatal(err)
}
mustContain(t, "labs.html", readOut(t, out, "labs.html"),
`<a href="/labs.html" aria-current="page">Labs</a>`, `<a href="/news/">News</a>`)
mustContain(t, "index.html", readOut(t, out, "index.html"), `<a href="/labs.html">Labs</a>`)
}
// TestBuildPagesBesidesHomeAndNewsAreOptional checks that only the home page and
// the news section are required: the rest of the menu is the site's choice.
func TestBuildPagesBesidesHomeAndNewsAreOptional(t *testing.T) {
files := baseFiles()
delete(files, "content/en/people.md")
delete(files, "content/it/persone.md")
if _, err := runBuild(t, files); err != nil {
t.Errorf("Build() without a people page: %v", err)
}
}

View file

@ -9,12 +9,6 @@ import (
"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", "projects", "services", "news"}
// linkedPages are the IDs the templates link to with url_<id>.
var linkedPages = []string{"manifesto", "people", "projects", "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"}
@ -78,8 +72,10 @@ func (b *builder) languageContext(lang string) map[string]string {
for k, label := range l.Nav {
ctx["nav_"+k] = render.Escape(label)
}
for _, id := range linkedPages {
ctx["url_"+id] = b.pageAddress(lang, id)
for _, id := range b.pageIDs {
if _, ok := b.byKey[pageKey{id, b.defaultLang}]; ok {
ctx["url_"+id] = b.pageAddress(lang, id)
}
}
ctx["history_attr"] = ""
if _, ok := b.byKey[pageKey{"history", lang}]; !ok {
@ -110,10 +106,10 @@ func (b *builder) documentContext(d document) map[string]string {
ctx["title_html"] = render.EscapeAttr(title)
ctx["page_title"] = render.Escape(d.title)
ctx["description_attr"] = render.EscapeAttr(d.description)
for _, k := range menuPages {
ctx["cur_"+k] = ""
if d.nav == k {
ctx["cur_"+k] = ` aria-current="page"`
for _, id := range b.pageIDs {
ctx["cur_"+id] = ""
if d.nav == id {
ctx["cur_"+id] = ` aria-current="page"`
}
}
ctx["lang_links"], ctx["alt_links"] = b.languageLinks(d)

View file

@ -29,9 +29,7 @@ func (b *builder) renderPage(p content.Page) error {
lang: p.Lang, id: p.ID, lead: "back", path: p.Path, title: p.Title,
description: p.Description, switches: p.Switch, source: p.Source,
}
if contains(menuPages, p.ID) {
d.nav = p.ID
}
d.nav = p.ID
if p.ID == "home" {
d.lead = "home"
}
@ -237,13 +235,3 @@ func (b *builder) renderFeed() error {
text := feed(b.cfg.Strings["base_url"], b.langs[b.defaultLang], newsPath, b.rendered, feedLimit)
return b.publish(outputPath(newsPath+"feed.xml"), "the feed", text)
}
// contains reports whether list has s.
func contains(list []string, s string) bool {
for _, item := range list {
if item == s {
return true
}
}
return false
}