A quote in a title broke the og:title attribute (a regression from build.py). Two sources that give the same output address now stop the build and name both, instead of one replacing the other. Asset names and sources must be paths inside the site, language codes must look like codes, and the Switch key is escaped, must point to a path and may name only listed languages.
214 lines
6.5 KiB
Go
214 lines
6.5 KiB
Go
// 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
|
|
// source names what produces the page, for error messages.
|
|
source 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.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"`
|
|
}
|
|
}
|
|
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>`, render.EscapeAttr(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
|
|
}
|
|
if err := b.publish(outputPath(d.path), d.source, html); err != nil {
|
|
return err
|
|
}
|
|
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"
|
|
}
|