The menu entries were three lists in Go (the menu, the linked pages and the required pages), so a new menu entry meant a new motorino release. Now every page ID gets url_<id> and cur_<id> for the templates, and header.html alone decides what is in the menu and in which order. Only the home page and the news section stay required, since motorino builds them itself. Output of the real kmalloc.org site is byte for byte the same as 1.1.0.
290 lines
8.7 KiB
Go
290 lines
8.7 KiB
Go
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package build
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
|
|
"git.kmalloc.org/squid/motorino/internal/content"
|
|
"git.kmalloc.org/squid/motorino/internal/site"
|
|
)
|
|
|
|
// Options tell Build where the site is, where to write it and which version
|
|
// of motorino to put in the generator meta.
|
|
type Options struct {
|
|
// SiteDir is the folder of the site: site.json, lang/, content/, news/,
|
|
// templates/ and static/.
|
|
SiteDir string
|
|
// OutDir is the folder the HTML is written to; it is created when missing.
|
|
OutDir string
|
|
// Version is the version of motorino, shown in the generator meta.
|
|
Version string
|
|
}
|
|
|
|
// requiredPages are the IDs every site must have in its default language,
|
|
// because motorino builds the home page and the news section itself. The
|
|
// other pages, and the menu that links to them, are the site's own: a template
|
|
// can link to any page with url_<id> and mark it with cur_<id>.
|
|
var requiredPages = []string{"home", "news"}
|
|
|
|
// newsOnHome is how many news the home page lists; feedLimit is how many the feed has.
|
|
const (
|
|
newsOnHome = 3
|
|
feedLimit = 20
|
|
)
|
|
|
|
// pageKey identifies a page by its ID and language.
|
|
type pageKey struct{ id, lang string }
|
|
|
|
// builder holds everything loaded for one build and the files it produced.
|
|
type builder struct {
|
|
opts Options
|
|
cfg site.Config
|
|
langs map[string]site.Lang
|
|
defaultLang string
|
|
pages []content.Page
|
|
posts []content.Post
|
|
categories []categoryEntry
|
|
templates map[string]string
|
|
css string
|
|
byKey map[pageKey]content.Page
|
|
// pageIDs lists the ID of every page, once, whatever the language.
|
|
pageIDs []string
|
|
// files holds the rendered output: relative path to content.
|
|
files map[string]string
|
|
// owners names, for each output file, what produces it.
|
|
owners map[string]string
|
|
// indexable holds the addresses listed in robots.txt.
|
|
indexable []string
|
|
// rendered holds the posts with their HTML, for the feed.
|
|
rendered []renderedPost
|
|
// langCtx caches the language level placeholders.
|
|
langCtx map[string]map[string]string
|
|
}
|
|
|
|
// categoryEntry is a category with the posts that have it.
|
|
type categoryEntry struct {
|
|
content.Category
|
|
posts []content.Post
|
|
}
|
|
|
|
// Result summarises a build.
|
|
type Result struct {
|
|
// Pages is the number of HTML pages written: pages, news and categories.
|
|
Pages int
|
|
}
|
|
|
|
// Build reads the site in opts.SiteDir and writes it to opts.OutDir. It renders
|
|
// everything in memory first, so a failure in the content leaves the output
|
|
// folder untouched. Writing the files is not atomic, and files of an earlier
|
|
// build that are no longer produced are not removed: build into a fresh folder
|
|
// when that matters, as the deploy does.
|
|
func Build(opts Options) (Result, error) {
|
|
b, err := load(opts)
|
|
if err != nil {
|
|
return Result{}, err
|
|
}
|
|
if err := b.renderPages(); err != nil {
|
|
return Result{}, err
|
|
}
|
|
if err := b.renderPosts(); err != nil {
|
|
return Result{}, err
|
|
}
|
|
if err := b.renderCategories(); err != nil {
|
|
return Result{}, err
|
|
}
|
|
if err := b.renderFeed(); err != nil {
|
|
return Result{}, err
|
|
}
|
|
if err := b.publish("robots.txt", "robots.txt", robots(b.indexable)); err != nil {
|
|
return Result{}, err
|
|
}
|
|
assets, err := b.readAssets()
|
|
if err != nil {
|
|
return Result{}, err
|
|
}
|
|
if err := b.write(assets); err != nil {
|
|
return Result{}, err
|
|
}
|
|
return Result{Pages: len(b.indexable)}, nil
|
|
}
|
|
|
|
// load reads the configuration, the content, the templates and the style.
|
|
func load(opts Options) (*builder, error) {
|
|
cfg, langs, err := site.Load(opts.SiteDir)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
b := &builder{
|
|
opts: opts, cfg: cfg, langs: langs, defaultLang: cfg.Languages[0],
|
|
byKey: map[pageKey]content.Page{}, files: map[string]string{}, owners: map[string]string{},
|
|
langCtx: map[string]map[string]string{},
|
|
}
|
|
if b.pages, err = content.LoadPages(opts.SiteDir, cfg.Languages); err != nil {
|
|
return nil, err
|
|
}
|
|
if b.posts, err = content.LoadPosts(opts.SiteDir); err != nil {
|
|
return nil, err
|
|
}
|
|
seenID := map[string]bool{}
|
|
for _, p := range b.pages {
|
|
b.byKey[pageKey{p.ID, p.Lang}] = p
|
|
if !seenID[p.ID] {
|
|
seenID[p.ID] = true
|
|
b.pageIDs = append(b.pageIDs, p.ID)
|
|
}
|
|
for lang := range p.Switch {
|
|
if _, known := langs[lang]; !known {
|
|
return nil, fmt.Errorf("%s: Switch names the language %q, which is not in site.json", p.Source, lang)
|
|
}
|
|
}
|
|
}
|
|
for _, id := range requiredPages {
|
|
if _, ok := b.byKey[pageKey{id, b.defaultLang}]; !ok {
|
|
return nil, fmt.Errorf("content/%s: there is no page with ID %q", b.defaultLang, id)
|
|
}
|
|
}
|
|
if news := b.byKey[pageKey{"news", b.defaultLang}]; !strings.HasSuffix(news.Path, "/") {
|
|
return nil, fmt.Errorf("%s: the news page must be an index (news/index.md), so that the feed has an address next to it", news.Source)
|
|
}
|
|
if b.templates, err = readTemplates(opts.SiteDir); err != nil {
|
|
return nil, err
|
|
}
|
|
css, err := os.ReadFile(filepath.Join(opts.SiteDir, "static", "style.css"))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read static/style.css: %w", err)
|
|
}
|
|
b.css = strings.Trim(string(css), "\n")
|
|
b.categories = groupCategories(b.posts)
|
|
return b, nil
|
|
}
|
|
|
|
// groupCategories lists the categories of the posts, sorted by name, each with
|
|
// its posts.
|
|
func groupCategories(posts []content.Post) []categoryEntry {
|
|
_, cats := categoriesList(posts)
|
|
out := make([]categoryEntry, len(cats))
|
|
for i, c := range cats {
|
|
out[i] = categoryEntry{Category: c, posts: postsIn(posts, c.Slug)}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// readTemplates reads every templates/**/*.html; the key is the path under
|
|
// templates/ without the extension, such as "pages/manifesto".
|
|
func readTemplates(dir string) (map[string]string, error) {
|
|
root := filepath.Join(dir, "templates")
|
|
out := map[string]string{}
|
|
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil || info.IsDir() || !strings.HasSuffix(path, ".html") {
|
|
return err
|
|
}
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
rel, err := filepath.Rel(root, path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
out[strings.TrimSuffix(filepath.ToSlash(rel), ".html")] = string(data)
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read templates: %w", err)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// template returns a template by key or an error naming its file.
|
|
func (b *builder) template(key string) (string, error) {
|
|
t, ok := b.templates[key]
|
|
if !ok {
|
|
return "", fmt.Errorf("templates/%s.html: not found", key)
|
|
}
|
|
return t, nil
|
|
}
|
|
|
|
// claim records that source produces the output file rel, and returns an error
|
|
// naming both sources when another one already does.
|
|
func (b *builder) claim(rel, source string) error {
|
|
if other, taken := b.owners[rel]; taken {
|
|
return fmt.Errorf("%s and %s are both published as %s", other, source, rel)
|
|
}
|
|
b.owners[rel] = source
|
|
return nil
|
|
}
|
|
|
|
// publish stores a rendered file; source names what produced it.
|
|
func (b *builder) publish(rel, source, text string) error {
|
|
if err := b.claim(rel, source); err != nil {
|
|
return err
|
|
}
|
|
b.files[rel] = text
|
|
return nil
|
|
}
|
|
|
|
// readAssets reads the files listed in site.json; a missing source is an
|
|
// error, and so is an asset that has the address of a page or of another asset.
|
|
func (b *builder) readAssets() (map[string][]byte, error) {
|
|
names := make([]string, 0, len(b.cfg.Assets))
|
|
for name := range b.cfg.Assets {
|
|
names = append(names, name)
|
|
}
|
|
sort.Strings(names)
|
|
out := map[string][]byte{}
|
|
for _, name := range names {
|
|
source := b.cfg.Assets[name]
|
|
data, err := os.ReadFile(filepath.Join(b.opts.SiteDir, filepath.FromSlash(source)))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("asset %q: %w", name, err)
|
|
}
|
|
rel := path.Clean(name)
|
|
if err := b.claim(rel, fmt.Sprintf("the asset %q (%s)", name, source)); err != nil {
|
|
return nil, err
|
|
}
|
|
out[rel] = data
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// write creates the output folder and writes the pages and the assets.
|
|
func (b *builder) write(assets map[string][]byte) error {
|
|
for rel, text := range b.files {
|
|
if err := writeFile(b.opts.OutDir, rel, []byte(text)); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
for rel, data := range assets {
|
|
if err := writeFile(b.opts.OutDir, rel, data); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// writeFile writes data to rel under dir, creating the folders.
|
|
func writeFile(dir, rel string, data []byte) error {
|
|
path := filepath.Join(dir, filepath.FromSlash(rel))
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
|
return fmt.Errorf("create %s: %w", filepath.Dir(path), err)
|
|
}
|
|
if err := os.WriteFile(path, data, 0o644); err != nil {
|
|
return fmt.Errorf("write %s: %w", path, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// outputPath returns the file of an address: a folder address gets index.html.
|
|
func outputPath(address string) string {
|
|
rel := strings.TrimPrefix(address, "/")
|
|
if rel == "" || strings.HasSuffix(rel, "/") {
|
|
rel += "index.html"
|
|
}
|
|
return rel
|
|
}
|