// SPDX-License-Identifier: GPL-3.0-or-later package build import ( "fmt" "os" "path" "path/filepath" "sort" "strings" "time" "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 // Now is the moment the site is built at: a post dated later is not // published yet. The zero value means the current time. Now time.Time // Drafts publishes the drafts and the posts dated in the future too, for a // preview of the site. Drafts bool } // requiredPages are the IDs every site must have, because motorino builds the // home page and the news section itself: the home in the default language and // the news page in the language of the news. The other pages, and the menu // that links to them, are the site's own: a template can link to any page with // url_ and mark it with cur_. 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 // newsLang is the language the news are written in, and so the language of // the news pages: the posts, the index, the archives, the categories and // the feed exist once, in this language. A post can say another one. newsLang string // newsPath is the address of the news index, such as "/news/". newsPath string // now is the moment the site is built at. now time.Time 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], now: opts.Now, byKey: map[pageKey]content.Page{}, files: map[string]string{}, owners: map[string]string{}, langCtx: map[string]map[string]string{}, } if b.now.IsZero() { b.now = time.Now() } if b.newsLang, err = newsLanguage(cfg); err != nil { return nil, err } if b.pages, err = content.LoadPages(opts.SiteDir, cfg.Languages); err != nil { return nil, err } all, err := content.LoadPosts(opts.SiteDir) if err != nil { return nil, err } if b.posts, err = b.publishedPosts(all); 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 { lang := b.defaultLang if id == "news" { lang = b.newsLang } if _, ok := b.byKey[pageKey{id, lang}]; !ok { return nil, fmt.Errorf("content/%s: there is no page with ID %q", lang, id) } } news := b.byKey[pageKey{"news", b.newsLang}] if !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) } b.newsPath = news.Path 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 } // newsLanguage returns the language of the news: news_language in site.json, // else English when the site has it, else the default language. func newsLanguage(cfg site.Config) (string, error) { listed := func(code string) bool { for _, l := range cfg.Languages { if l == code { return true } } return false } switch code := cfg.Strings["news_language"]; { case code == "" && listed("en"): return "en", nil case code == "": return cfg.Languages[0], nil case !listed(code): return "", fmt.Errorf("site.json: news_language %q is not one of the languages of the site", code) default: return code, nil } } // publishedPosts gives every post the language of the news when it names none, // checks that the language is one of the site, and keeps the posts that are // published at the moment of the build: not a draft and not dated in the // future, unless the build is a preview with drafts. func (b *builder) publishedPosts(all []content.Post) ([]content.Post, error) { var out []content.Post for _, p := range all { if p.Language == "" { p.Language = b.newsLang } if _, ok := b.langs[p.Language]; !ok { return nil, fmt.Errorf("%s: Language %q is not one of the languages of site.json", p.Source, p.Language) } if b.opts.Drafts || p.Visible(b.now) { out = append(out, p) } } return out, 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 }