284 lines
8.4 KiB
Go
284 lines
8.4 KiB
Go
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package build
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"strconv"
|
|
"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
|
|
}
|
|
|
|
// blogBlock matches a paragraph that holds the blog list placeholder.
|
|
var blogBlock = regexp.MustCompile(`(?m)^\{\{news_blog\}\}\s*$`)
|
|
|
|
// renderPage renders a page. A page with the blog list is rendered once for
|
|
// every page of the list: the first at its own address, the others under it, at
|
|
// page/2/, page/3/ and so on.
|
|
func (b *builder) renderPage(p content.Page) error {
|
|
total := 1
|
|
if blogBlock.MatchString(p.Body) {
|
|
if !strings.HasSuffix(p.Path, "/") {
|
|
return fmt.Errorf("%s: {{news_blog}} must be on an index page (an index.md), so that its pages have addresses under it", p.Source)
|
|
}
|
|
total = pageCount(len(b.posts))
|
|
}
|
|
for n := 1; n <= total; n++ {
|
|
if err := b.renderPageNumber(p, n, total); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// renderPageNumber converts the body of a page to HTML, expands the generated
|
|
// lists, puts it in its layout and in the shared frame. n is the number of the
|
|
// page of the blog list, out of total; both are 1 for any other page.
|
|
func (b *builder) renderPageNumber(p content.Page, n, total int) error {
|
|
d := document{
|
|
lang: p.Lang, id: p.ID, lead: "back", path: pagePath(p.Path, n), title: p.Title,
|
|
description: p.Description, switches: p.Switch, source: p.Source,
|
|
}
|
|
d.nav = p.ID
|
|
if p.ID == "home" {
|
|
d.lead = "home"
|
|
}
|
|
if n > 1 {
|
|
label, err := b.pageLabel(p.Lang, n, total)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
d.id, d.switches = "", nil
|
|
d.title = p.Title + ", " + label
|
|
d.source = fmt.Sprintf("%s (page %d of the list)", p.Source, n)
|
|
}
|
|
ctx := b.documentContext(d)
|
|
ctx["news_page_n"] = strconv.Itoa(n)
|
|
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>`, render.EscapeAttr(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 := mdx.SubstituteMarkdown(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)
|
|
}
|
|
if html, err = b.expandBlocks(name, html, lang, ctx); err != nil {
|
|
return "", nil, err
|
|
}
|
|
for i := range headings {
|
|
headings[i].Text = render.Unescape(headings[i].Text)
|
|
}
|
|
return render.Unescape(html), headings, nil
|
|
}
|
|
|
|
// codeElement matches a code element, where a placeholder is shown as written.
|
|
var codeElement = regexp.MustCompile(`(?s)<code[^>]*>.*?</code>`)
|
|
|
|
// replaceBlocks replaces each paragraph that holds only a block placeholder by
|
|
// what replace returns for the name of the block. A block placeholder inside
|
|
// other text is an error. All blocks are checked before any is replaced, so the
|
|
// text of a generated list (a news title may say {{latest_news}}) is never
|
|
// taken for a placeholder.
|
|
func replaceBlocks(name, html string, replace func(block string) (string, error)) (string, error) {
|
|
outside := codeElement.ReplaceAllString(html, "")
|
|
for _, block := range blockNames {
|
|
token := "{{" + block + "}}"
|
|
if strings.Count(outside, token) != strings.Count(outside, "<p>"+token+"</p>") {
|
|
return "", fmt.Errorf("%s: {{%s}} must stand alone in a paragraph", name, block)
|
|
}
|
|
}
|
|
for _, block := range blockNames {
|
|
marker := "<p>{{" + block + "}}</p>"
|
|
if !strings.Contains(html, marker) {
|
|
continue
|
|
}
|
|
generated, err := replace(block)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
html = strings.ReplaceAll(html, marker, generated)
|
|
}
|
|
return html, nil
|
|
}
|
|
|
|
// expandBlocks replaces the block placeholders of a page by the generated HTML.
|
|
func (b *builder) expandBlocks(name, html, lang string, ctx map[string]string) (string, error) {
|
|
return replaceBlocks(name, html, func(block string) (string, error) { return b.block(block, lang, ctx) })
|
|
}
|
|
|
|
// 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 "news_blog":
|
|
n, _ := strconv.Atoi(ctx["news_page_n"])
|
|
return b.newsBlog(lang, n, ctx)
|
|
case "news_archive":
|
|
return b.newsArchive(lang, ctx)
|
|
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 i, p := range b.posts {
|
|
d := document{
|
|
lang: p.Language, 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, p.Language, 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)
|
|
if err := b.postFields(i, ctx); err != nil {
|
|
return err
|
|
}
|
|
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 links := categoryLinks(p); links != "" {
|
|
out.WriteString(" · " + 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.newsLang, nav: "news", lead: "news", path: categoryPath(c.Slug), title: c.Name, prefixed: true,
|
|
description: b.langs[b.newsLang].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)
|
|
panels, err := b.newsPanels(c.posts, b.newsLang)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ctx["category_posts"] = panels
|
|
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 {
|
|
text := feed(b.cfg.Strings["base_url"], b.langs[b.newsLang], b.newsPath, b.rendered, feedLimit)
|
|
return b.publish(outputPath(b.newsPath+"feed.xml"), "the feed", text)
|
|
}
|