diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e89b5e..05d521d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## [1.2.0](https://git.kmalloc.org/squid/motorino/compare/v1.1.0...v1.2.0) (2026-11-15) + +### Features + +* take the menu from the site, not from the code ([eb2a8dc](https://git.kmalloc.org/squid/motorino/commit/eb2a8dc)) + ## [1.1.0](https://git.kmalloc.org/squid/motorino/compare/v1.0.1...v1.1.0) (2026-11-15) ### Features diff --git a/internal/build/build.go b/internal/build/build.go index d43a902..a38c938 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -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_ and mark it with cur_. +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) diff --git a/internal/build/build_test.go b/internal/build/build_test.go index 1d6623d..2d750ba 100644 --- a/internal/build/build_test.go +++ b/internal/build/build_test.go @@ -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"] = `
+Labs +News +
+` + out, err := runBuild(t, files) + if err != nil { + t.Fatal(err) + } + mustContain(t, "labs.html", readOut(t, out, "labs.html"), + `Labs`, `News`) + mustContain(t, "index.html", readOut(t, out, "index.html"), `Labs`) +} + +// 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) + } +} diff --git a/internal/build/context.go b/internal/build/context.go index a46a049..1ab2e25 100644 --- a/internal/build/context.go +++ b/internal/build/context.go @@ -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_. -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) diff --git a/internal/build/pages.go b/internal/build/pages.go index 31849d3..b161e8e 100644 --- a/internal/build/pages.go +++ b/internal/build/pages.go @@ -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 -}