178 lines
5.2 KiB
Go
178 lines
5.2 KiB
Go
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package content
|
|
|
|
import (
|
|
"fmt"
|
|
"io/fs"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"git.kmalloc.org/squid/motorino/internal/header"
|
|
"git.kmalloc.org/squid/motorino/internal/mdx"
|
|
)
|
|
|
|
// Page is a page of the site in one language.
|
|
type Page struct {
|
|
// Lang is the language code, such as "en".
|
|
Lang string
|
|
// ID names the page; the translations of a page share it.
|
|
ID string
|
|
// Path is the address of the page, such as "/services.html".
|
|
Path string
|
|
// Title, Description and Note come from the header.
|
|
Title, Description, Note string
|
|
// Layout is "page", "bare" or "manifesto".
|
|
Layout string
|
|
// Switch overrides the address of the language links, by language code.
|
|
Switch map[string]string
|
|
// Source is the file the page was read from, relative to the site.
|
|
Source string
|
|
// Body is the Markdown after the header.
|
|
Body string
|
|
}
|
|
|
|
// pageKeys are the keys a page header may have.
|
|
var pageKeys = []string{"Title", "ID", "Description", "Layout", "Note", "Switch"}
|
|
|
|
// layouts are the values of the Layout key; the first is the default.
|
|
var layouts = []string{"page", "bare", "manifesto"}
|
|
|
|
// PagePath returns the address of the page file rel, given with slashes and
|
|
// relative to the folder of its language. An index.md is the address of its
|
|
// folder; the default language lives at the root and the others under
|
|
// /<lang>/.
|
|
func PagePath(defaultLang, lang, rel string) string {
|
|
rel = strings.TrimSuffix(rel, ".md")
|
|
var p string
|
|
switch {
|
|
case rel == "index":
|
|
p = "/"
|
|
case strings.HasSuffix(rel, "/index"):
|
|
p = "/" + strings.TrimSuffix(rel, "index")
|
|
default:
|
|
p = "/" + rel + ".html"
|
|
}
|
|
if lang != defaultLang {
|
|
p = "/" + lang + p
|
|
}
|
|
return p
|
|
}
|
|
|
|
// LoadPages reads every Markdown file under content/<lang>/ for each language.
|
|
// The first language is the default. The em dash rule applies to every page
|
|
// except those with the manifesto layout, which is rewritten when it is built.
|
|
func LoadPages(dir string, languages []string) ([]Page, error) {
|
|
if len(languages) == 0 {
|
|
return nil, fmt.Errorf("no languages")
|
|
}
|
|
var pages []Page
|
|
for _, lang := range languages {
|
|
folder := path.Join("content", lang)
|
|
if info, err := os.Stat(filepath.Join(dir, filepath.FromSlash(folder))); err != nil || !info.IsDir() {
|
|
return nil, fmt.Errorf("%s: the folder of language %q is missing", folder, lang)
|
|
}
|
|
found, err := loadLanguagePages(dir, languages[0], lang, folder)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
pages = append(pages, found...)
|
|
}
|
|
return pages, nil
|
|
}
|
|
|
|
// loadLanguagePages reads the pages of one language and rejects duplicate IDs.
|
|
func loadLanguagePages(dir, defaultLang, lang, folder string) ([]Page, error) {
|
|
var pages []Page
|
|
seen := map[string]string{}
|
|
root := filepath.Join(dir, filepath.FromSlash(folder))
|
|
err := filepath.WalkDir(root, func(file string, d fs.DirEntry, err error) error {
|
|
if err != nil || d.IsDir() || !strings.HasSuffix(file, ".md") {
|
|
return err
|
|
}
|
|
rel, err := filepath.Rel(root, file)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
rel = filepath.ToSlash(rel)
|
|
page, err := loadPage(file, path.Join(folder, rel), defaultLang, lang, rel)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if other, dup := seen[page.ID]; dup {
|
|
return fmt.Errorf("%s: the ID %q is also used by %s", page.Source, page.ID, other)
|
|
}
|
|
seen[page.ID] = page.Source
|
|
pages = append(pages, page)
|
|
return nil
|
|
})
|
|
return pages, err
|
|
}
|
|
|
|
// loadPage reads and checks one page file.
|
|
func loadPage(file, source, defaultLang, lang, rel string) (Page, error) {
|
|
data, err := os.ReadFile(file)
|
|
if err != nil {
|
|
return Page{}, fmt.Errorf("read %s: %w", source, err)
|
|
}
|
|
h, body, err := header.Parse(source, data)
|
|
if err != nil {
|
|
return Page{}, err
|
|
}
|
|
if err := h.Only(source, pageKeys...); err != nil {
|
|
return Page{}, err
|
|
}
|
|
if err := h.Require(source, "Title", "ID", "Description"); err != nil {
|
|
return Page{}, err
|
|
}
|
|
layout := h.Get("Layout")
|
|
if layout == "" {
|
|
layout = layouts[0]
|
|
}
|
|
if !contains(layouts, layout) {
|
|
return Page{}, fmt.Errorf("%s: unknown Layout %q (use %s)", source, layout, strings.Join(layouts, ", "))
|
|
}
|
|
if layout != "manifesto" {
|
|
if err := mdx.RejectEmDash(source, string(data)); err != nil {
|
|
return Page{}, err
|
|
}
|
|
}
|
|
switches, err := parseSwitch(source, h.Get("Switch"))
|
|
if err != nil {
|
|
return Page{}, err
|
|
}
|
|
return Page{
|
|
Lang: lang, ID: h.Get("ID"), Path: PagePath(defaultLang, lang, rel),
|
|
Title: h.Get("Title"), Description: h.Get("Description"), Note: h.Get("Note"),
|
|
Layout: layout, Switch: switches, Source: source, Body: body,
|
|
}, nil
|
|
}
|
|
|
|
// parseSwitch reads "en=/path, de=/path" into a map; an empty value is an
|
|
// empty map.
|
|
func parseSwitch(source, value string) (map[string]string, error) {
|
|
out := map[string]string{}
|
|
if strings.TrimSpace(value) == "" {
|
|
return out, nil
|
|
}
|
|
for _, entry := range strings.Split(value, ",") {
|
|
lang, target, ok := strings.Cut(strings.TrimSpace(entry), "=")
|
|
if !ok || lang == "" || target == "" {
|
|
return nil, fmt.Errorf("%s: Switch entry %q is not of the form lang=/path", source, strings.TrimSpace(entry))
|
|
}
|
|
out[strings.TrimSpace(lang)] = strings.TrimSpace(target)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// contains reports whether list has s.
|
|
func contains(list []string, s string) bool {
|
|
for _, item := range list {
|
|
if item == s {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|