Compare commits

...
Author SHA1 Message Date
tgiachi
b2f43c34bb chore(release): 1.2.0 [skip ci] 2026-11-15 18:08:42 +01:00
tgiachi
eb2a8dc9b4 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.
2026-11-15 18:08:42 +01:00
5 changed files with 56 additions and 27 deletions

View file

@ -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) ## [1.1.0](https://git.kmalloc.org/squid/motorino/compare/v1.0.1...v1.1.0) (2026-11-15)
### Features ### Features

View file

@ -27,8 +27,10 @@ type Options struct {
} }
// requiredPages are the IDs every site must have in its default language, // requiredPages are the IDs every site must have in its default language,
// because the menu and the footer link to them. // because motorino builds the home page and the news section itself. The
var requiredPages = []string{"home", "manifesto", "people", "projects", "services", "news", "history"} // 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. // newsOnHome is how many news the home page lists; feedLimit is how many the feed has.
const ( const (
@ -51,6 +53,8 @@ type builder struct {
templates map[string]string templates map[string]string
css string css string
byKey map[pageKey]content.Page 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 holds the rendered output: relative path to content.
files map[string]string files map[string]string
// owners names, for each output file, what produces it. // 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 { if b.posts, err = content.LoadPosts(opts.SiteDir); err != nil {
return nil, err return nil, err
} }
seenID := map[string]bool{}
for _, p := range b.pages { for _, p := range b.pages {
b.byKey[pageKey{p.ID, p.Lang}] = p 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 { for lang := range p.Switch {
if _, known := langs[lang]; !known { if _, known := langs[lang]; !known {
return nil, fmt.Errorf("%s: Switch names the language %q, which is not in site.json", p.Source, lang) 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) 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" "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 // blockNames are the placeholders that expand to generated HTML. Until they
// are expanded they stand for themselves, so a page can hold them. // are expanded they stand for themselves, so a page can hold them.
var blockNames = []string{"news_list", "latest_news", "categories_list"} 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 { for k, label := range l.Nav {
ctx["nav_"+k] = render.Escape(label) ctx["nav_"+k] = render.Escape(label)
} }
for _, id := range linkedPages { for _, id := range b.pageIDs {
ctx["url_"+id] = b.pageAddress(lang, id) if _, ok := b.byKey[pageKey{id, b.defaultLang}]; ok {
ctx["url_"+id] = b.pageAddress(lang, id)
}
} }
ctx["history_attr"] = "" ctx["history_attr"] = ""
if _, ok := b.byKey[pageKey{"history", lang}]; !ok { 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["title_html"] = render.EscapeAttr(title)
ctx["page_title"] = render.Escape(d.title) ctx["page_title"] = render.Escape(d.title)
ctx["description_attr"] = render.EscapeAttr(d.description) ctx["description_attr"] = render.EscapeAttr(d.description)
for _, k := range menuPages { for _, id := range b.pageIDs {
ctx["cur_"+k] = "" ctx["cur_"+id] = ""
if d.nav == k { if d.nav == id {
ctx["cur_"+k] = ` aria-current="page"` ctx["cur_"+id] = ` aria-current="page"`
} }
} }
ctx["lang_links"], ctx["alt_links"] = b.languageLinks(d) 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, lang: p.Lang, id: p.ID, lead: "back", path: p.Path, title: p.Title,
description: p.Description, switches: p.Switch, source: p.Source, 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" { if p.ID == "home" {
d.lead = "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) 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) 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
}