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.
233 lines
6.5 KiB
Go
233 lines
6.5 KiB
Go
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package build
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"git.kmalloc.org/squid/motorino/internal/content"
|
|
"git.kmalloc.org/squid/motorino/internal/mdx"
|
|
"git.kmalloc.org/squid/motorino/internal/render"
|
|
)
|
|
|
|
// renderPages renders every page of every language.
|
|
func (b *builder) renderPages() error {
|
|
for _, p := range b.pages {
|
|
if err := b.renderPage(p); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// renderPage converts the body of a page to HTML, expands the generated lists,
|
|
// puts it in its layout and in the shared frame.
|
|
func (b *builder) renderPage(p content.Page) error {
|
|
d := document{
|
|
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
|
|
}
|
|
if p.ID == "home" {
|
|
d.lead = "home"
|
|
}
|
|
ctx := b.documentContext(d)
|
|
if p.ManifestoVersion != "" {
|
|
ctx["manifesto_version"] = render.Escape(p.ManifestoVersion)
|
|
}
|
|
if p.ManifestoDate != "" {
|
|
ctx["manifesto_date"] = render.Escape(p.ManifestoDate)
|
|
}
|
|
|
|
source := p.Body
|
|
if p.Layout == "manifesto" {
|
|
var err error
|
|
if source, err = mdx.NoEmDash(source); err != nil {
|
|
return fmt.Errorf("%s: %w", p.Source, err)
|
|
}
|
|
}
|
|
html, headings, err := b.markdown(p.Source, source, p.Lang, ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ctx["body"] = html
|
|
ctx["note_block"] = ""
|
|
if p.Note != "" {
|
|
ctx["note_block"] = "<p>\n" + render.Escape(p.Note) + "\n</p>\n\n"
|
|
}
|
|
var toc []string
|
|
for _, h := range headings {
|
|
if h.Level == 2 {
|
|
toc = append(toc, fmt.Sprintf(`<li><a href="#%s">%s</a></li>`, h.ID, render.Escape(h.Text)))
|
|
}
|
|
}
|
|
ctx["manifesto_toc"] = strings.Join(toc, "\n")
|
|
|
|
layout, err := b.template("pages/" + p.Layout)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
body, err := render.Substitute("templates/pages/"+p.Layout+".html", layout, ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return b.frame(d, ctx, body)
|
|
}
|
|
|
|
// markdown replaces the placeholders of a source, converts it to HTML and
|
|
// expands the generated lists. The name is used in error messages.
|
|
func (b *builder) markdown(name, source, lang string, ctx map[string]string) (string, []mdx.Heading, error) {
|
|
text, err := render.Substitute(name, source, ctx)
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
html, headings, err := mdx.Render(text)
|
|
if err != nil {
|
|
return "", nil, fmt.Errorf("%s: %w", name, err)
|
|
}
|
|
html, err = b.expandBlocks(name, html, lang, ctx)
|
|
return html, headings, err
|
|
}
|
|
|
|
// expandBlocks replaces each paragraph that holds only a block placeholder by
|
|
// the generated HTML. A block placeholder inside other text is an error.
|
|
func (b *builder) expandBlocks(name, html, lang string, ctx map[string]string) (string, error) {
|
|
for _, block := range blockNames {
|
|
marker := "<p>{{" + block + "}}</p>"
|
|
if strings.Contains(html, marker) {
|
|
generated, err := b.block(block, lang, ctx)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
html = strings.ReplaceAll(html, marker, generated)
|
|
}
|
|
if strings.Contains(html, "{{"+block+"}}") {
|
|
return "", fmt.Errorf("%s: {{%s}} must stand alone in a paragraph", name, block)
|
|
}
|
|
}
|
|
return html, nil
|
|
}
|
|
|
|
// block returns the HTML of a generated list.
|
|
func (b *builder) block(name, lang string, ctx map[string]string) (string, error) {
|
|
switch name {
|
|
case "news_list":
|
|
if list := newsList(b.posts, lang); list != "" {
|
|
return list, nil
|
|
}
|
|
return "<p>\n" + ctx["news_empty"] + "\n</p>", nil
|
|
case "latest_news":
|
|
if len(b.posts) == 0 {
|
|
return "", nil
|
|
}
|
|
tmpl, err := b.template("latest-news")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
with := copyContext(ctx)
|
|
with["news_items"] = latestNews(b.posts, lang, newsOnHome)
|
|
return render.Substitute("templates/latest-news.html", tmpl, with)
|
|
default:
|
|
list, _ := categoriesList(b.posts)
|
|
return list, nil
|
|
}
|
|
}
|
|
|
|
// renderPosts renders every news item and keeps its HTML for the feed.
|
|
func (b *builder) renderPosts() error {
|
|
tmpl, err := b.template("post")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, p := range b.posts {
|
|
d := document{
|
|
lang: b.defaultLang, nav: "news", lead: "news", path: p.Path, title: p.Title,
|
|
prefixed: true, description: p.Description, source: p.Source,
|
|
}
|
|
ctx := b.documentContext(d)
|
|
html, _, err := b.markdown(p.Source, p.Body, b.defaultLang, ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
day := p.Date.Format(dateOnly)
|
|
ctx["post_title"] = render.Escape(p.Title)
|
|
ctx["date"] = day
|
|
ctx["post_body"] = html
|
|
ctx["post_byline"] = byline(p)
|
|
body, err := render.Substitute("templates/post.html", tmpl, ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := b.frame(d, ctx, body); err != nil {
|
|
return err
|
|
}
|
|
b.rendered = append(b.rendered, renderedPost{Post: p, HTML: html})
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// byline returns the author and the category links of a post, each after a
|
|
// middle dot, or an empty string when the post has neither.
|
|
func byline(p content.Post) string {
|
|
var out strings.Builder
|
|
if p.Author != "" {
|
|
out.WriteString(" · " + render.Escape(p.Author))
|
|
}
|
|
if len(p.Categories) > 0 {
|
|
links := make([]string, len(p.Categories))
|
|
for i, c := range p.Categories {
|
|
links[i] = fmt.Sprintf(`<a href="%s">%s</a>`, categoryPath(c.Slug), render.Escape(c.Name))
|
|
}
|
|
out.WriteString(" · " + strings.Join(links, ", "))
|
|
}
|
|
return out.String()
|
|
}
|
|
|
|
// renderCategories renders the page of every category.
|
|
func (b *builder) renderCategories() error {
|
|
if len(b.categories) == 0 {
|
|
return nil
|
|
}
|
|
tmpl, err := b.template("category")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, c := range b.categories {
|
|
d := document{
|
|
lang: b.defaultLang, nav: "news", lead: "news", path: categoryPath(c.Slug), title: c.Name, prefixed: true,
|
|
description: b.langs[b.defaultLang].Strings["category_h"] + ": " + c.Name,
|
|
source: fmt.Sprintf("the page of the category %q", c.Name),
|
|
}
|
|
ctx := b.documentContext(d)
|
|
ctx["category_name"] = render.Escape(c.Name)
|
|
ctx["category_posts"] = newsList(c.posts, b.defaultLang)
|
|
body, err := render.Substitute("templates/category.html", tmpl, ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := b.frame(d, ctx, body); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// renderFeed writes the feed next to the news page.
|
|
func (b *builder) renderFeed() error {
|
|
newsPath := b.byKey[pageKey{"news", b.defaultLang}].Path
|
|
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
|
|
}
|