`motorino gemini [-site DIR] [-out DIR] [-lang CODE] [-drafts]` turns the pages and the news into gemtext with the new gemtext package. - the language served by default sits at the root (-lang, then gemini_language in site.json, then the default language) and the others under /<code>/ - the news exist once, under /news/: index, posts, categories, archives and a gemlog style feed (news/feed.gmi, a date before each title) - every page ends with the navigation of its language, the language links and, on translations, the machine translation note - .meta for Agate (--central-conf): two lines per language other than the root one and two for the news when they are in another language, never linked - addresses are mapped to the capsule, a fragment is dropped, a missing target stops the build, assets are copied only when a page points to them, gemini_web_paths keeps paths served by other means as links to the website - blocks of generated lists become gemtext with the strings of the language file - the block replacement of a page is shared with the HTML build Refs #10
227 lines
6.8 KiB
Go
227 lines
6.8 KiB
Go
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package build
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"git.kmalloc.org/squid/motorino/internal/render"
|
|
)
|
|
|
|
// 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", "news_blog", "news_archive"}
|
|
|
|
// sitePrefix goes before the title of a page that has no title of its own in the
|
|
// browser, such as a post.
|
|
const sitePrefix = "kmalloc.org: "
|
|
|
|
// 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 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 {
|
|
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 = sitePrefix + title
|
|
}
|
|
ctx["title_html"] = render.EscapeAttr(title)
|
|
ctx["page_title"] = render.Escape(d.title)
|
|
ctx["description_attr"] = render.EscapeAttr(d.description)
|
|
ctx["og_type"] = "website"
|
|
ctx["article_meta"] = ""
|
|
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)
|
|
return ctx
|
|
}
|
|
|
|
// langTarget is a page in another language that a page links to.
|
|
type langTarget struct{ lang, path string }
|
|
|
|
// languageTargets returns the pages in the other languages that a page links
|
|
// to: its translations, with the address a Switch key gives instead of the
|
|
// translation, or, when it has none, the home of the first other language that
|
|
// has one.
|
|
func (b *builder) languageTargets(d document) []langTarget {
|
|
var twins []langTarget
|
|
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, langTarget{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, langTarget{l, p.Path})
|
|
break
|
|
}
|
|
}
|
|
}
|
|
for i, t := range twins {
|
|
if override, ok := d.switches[t.lang]; ok {
|
|
twins[i].path = override
|
|
}
|
|
}
|
|
return twins
|
|
}
|
|
|
|
// languageLinks returns the links to the other languages for the menu and the
|
|
// alternate links for the head.
|
|
func (b *builder) languageLinks(d document) (links, alternates string) {
|
|
twins := b.languageTargets(d)
|
|
anchors := make([]string, len(twins))
|
|
for i, t := range twins {
|
|
anchors[i] = fmt.Sprintf(`<a href="%s" hreflang="%s" lang="%s">%s</a>`, render.EscapeAttr(t.path), 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"
|
|
}
|