// SPDX-License-Identifier: GPL-3.0-or-later package build import ( "fmt" "regexp" "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, } 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"] = "
\n" + render.Escape(p.Note) + "\n
\n\n" } var toc []string for _, h := range headings { if h.Level == 2 { toc = append(toc, fmt.Sprintf(`]*>.*?`)
// expandBlocks replaces each paragraph that holds only a block placeholder by
// the generated HTML. A block placeholder inside other text is an error. All
// blocks are checked before any is expanded, so the text of a generated list
// (a news title may say {{latest_news}}) is never taken for a placeholder.
func (b *builder) expandBlocks(name, html, lang string, ctx map[string]string) (string, error) {
outside := codeElement.ReplaceAllString(html, "")
for _, block := range blockNames {
token := "{{" + block + "}}"
if strings.Count(outside, token) != strings.Count(outside, ""+token+"
") { return "", fmt.Errorf("%s: {{%s}} must stand alone in a paragraph", name, block) } } for _, block := range blockNames { marker := "{{" + block + "}}
" if !strings.Contains(html, marker) { continue } generated, err := b.block(block, lang, ctx) if err != nil { return "", err } html = strings.ReplaceAll(html, marker, generated) } 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 "\n" + ctx["news_empty"] + "\n
", 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: 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) 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(`%s`, 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.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) ctx["category_posts"] = newsList(c.posts, b.newsLang) 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) }