All checks were successful
check / check (pull_request) Successful in 30s
The hand written fence scanner took a line that starts with a code span for a fence and missed fences in quotes and lists. The code blocks are now the ones goldmark finds. A backslash before a placeholder is turned into a marker that survives the Markdown and the list expansion, so it works in a code span and next to a block name, and two backslashes are a literal one. The em dash rule reports the line of the body, refuses padded entities and checks the header keys by name, the feed leaves protocol-relative links alone, base_url needs a host and a language file cannot set it.
186 lines
5.8 KiB
Go
186 lines
5.8 KiB
Go
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
// Package site loads the configuration of a site: site.json and the language
|
|
// files in lang/.
|
|
package site
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
// Config is the content of site.json.
|
|
type Config struct {
|
|
// Strings holds every string value of site.json, plus the derived
|
|
// contact_shown, security_shown and abuse_shown.
|
|
Strings map[string]string
|
|
// Languages lists the language codes; the first one is the default and
|
|
// lives at the root of the site.
|
|
Languages []string
|
|
// Assets maps the published name of a file to its source in the site
|
|
// directory; the files are copied as they are.
|
|
Assets map[string]string
|
|
}
|
|
|
|
// Lang is the content of lang/<code>.json: the strings of one language.
|
|
type Lang struct {
|
|
// Code is the language code, such as "en".
|
|
Code string
|
|
// Strings holds every string value of the file.
|
|
Strings map[string]string
|
|
// Nav maps a menu entry to its label in this language.
|
|
Nav map[string]string
|
|
}
|
|
|
|
// shownKeys are the address keys that get a harvest-proof "_shown" twin.
|
|
var shownKeys = []string{"contact", "security", "abuse"}
|
|
|
|
// Load reads site.json and the language file of every language it lists. All
|
|
// paths in error messages are relative to dir.
|
|
func Load(dir string) (Config, map[string]Lang, error) {
|
|
raw, err := readObject(dir, "site.json")
|
|
if err != nil {
|
|
return Config{}, nil, err
|
|
}
|
|
cfg := Config{Strings: stringValues(raw), Assets: map[string]string{}}
|
|
if err := checkBaseURL(cfg.Strings["base_url"]); err != nil {
|
|
return Config{}, nil, err
|
|
}
|
|
if err := decodeField(raw, "site.json", "languages", &cfg.Languages); err != nil {
|
|
return Config{}, nil, err
|
|
}
|
|
if err := checkLanguages(cfg.Languages); err != nil {
|
|
return Config{}, nil, err
|
|
}
|
|
if err := decodeField(raw, "site.json", "assets", &cfg.Assets); err != nil {
|
|
return Config{}, nil, err
|
|
}
|
|
if err := checkAssets(cfg.Assets); err != nil {
|
|
return Config{}, nil, err
|
|
}
|
|
for _, key := range shownKeys {
|
|
if value, ok := cfg.Strings[key]; ok {
|
|
cfg.Strings[key+"_shown"] = strings.ReplaceAll(value, "@", " (at) ")
|
|
}
|
|
}
|
|
|
|
langs := make(map[string]Lang, len(cfg.Languages))
|
|
for _, code := range cfg.Languages {
|
|
lang, err := loadLang(dir, code)
|
|
if err != nil {
|
|
return Config{}, nil, err
|
|
}
|
|
langs[code] = lang
|
|
}
|
|
return cfg, langs, nil
|
|
}
|
|
|
|
// checkBaseURL returns an error unless the address of the site is absolute and
|
|
// has no trailing slash: page addresses and the feed are built by adding to it.
|
|
func checkBaseURL(base string) error {
|
|
u, err := url.Parse(base)
|
|
if err != nil || (u.Scheme != "https" && u.Scheme != "http") || u.Host == "" || strings.HasSuffix(base, "/") {
|
|
return fmt.Errorf("site.json: base_url must be an absolute address with a host and no trailing slash, such as https://kmalloc.org")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// languageCode is the shape of a language code: "en", "pt-BR".
|
|
var languageCode = regexp.MustCompile(`^[a-z]{2,3}(-[A-Za-z0-9]+)?$`)
|
|
|
|
// checkLanguages returns an error when the list is empty, has a code of an
|
|
// unexpected shape (the code becomes part of addresses and folders) or has a
|
|
// code twice.
|
|
func checkLanguages(codes []string) error {
|
|
if len(codes) == 0 {
|
|
return fmt.Errorf("site.json: languages is empty")
|
|
}
|
|
seen := map[string]bool{}
|
|
for _, code := range codes {
|
|
if !languageCode.MatchString(code) {
|
|
return fmt.Errorf("site.json: the language code %q is not like \"en\" or \"pt-BR\"", code)
|
|
}
|
|
if seen[code] {
|
|
return fmt.Errorf("site.json: the language %q is listed twice", code)
|
|
}
|
|
seen[code] = true
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// checkAssets returns an error for an asset whose published name or source is
|
|
// not a path inside the output folder and the site folder.
|
|
func checkAssets(assets map[string]string) error {
|
|
for name, source := range assets {
|
|
if !filepath.IsLocal(name) || !filepath.IsLocal(source) {
|
|
return fmt.Errorf("site.json: asset %q (%s): the name and the source must be paths inside the site", name, source)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// loadLang reads lang/<code>.json and checks the keys every language needs.
|
|
func loadLang(dir, code string) (Lang, error) {
|
|
name := filepath.ToSlash(filepath.Join("lang", code+".json"))
|
|
raw, err := readObject(dir, name)
|
|
if err != nil {
|
|
return Lang{}, err
|
|
}
|
|
lang := Lang{Code: code, Strings: stringValues(raw), Nav: map[string]string{}}
|
|
if lang.Strings["lang_name"] == "" {
|
|
return Lang{}, fmt.Errorf("%s: missing lang_name", name)
|
|
}
|
|
if _, ok := lang.Strings["base_url"]; ok {
|
|
return Lang{}, fmt.Errorf("%s: base_url is set in site.json only", name)
|
|
}
|
|
if _, ok := raw["nav"]; !ok {
|
|
return Lang{}, fmt.Errorf("%s: missing nav", name)
|
|
}
|
|
if err := decodeField(raw, name, "nav", &lang.Nav); err != nil {
|
|
return Lang{}, err
|
|
}
|
|
return lang, nil
|
|
}
|
|
|
|
// readObject reads a JSON file that holds one object and returns its fields
|
|
// undecoded. Errors name the file.
|
|
func readObject(dir, name string) (map[string]json.RawMessage, error) {
|
|
data, err := os.ReadFile(filepath.Join(dir, filepath.FromSlash(name)))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read %s: %w", name, err)
|
|
}
|
|
var raw map[string]json.RawMessage
|
|
if err := json.Unmarshal(data, &raw); err != nil {
|
|
return nil, fmt.Errorf("%s: %w", name, err)
|
|
}
|
|
return raw, nil
|
|
}
|
|
|
|
// stringValues returns the fields of raw whose value is a JSON string.
|
|
func stringValues(raw map[string]json.RawMessage) map[string]string {
|
|
out := make(map[string]string)
|
|
for key, value := range raw {
|
|
var s string
|
|
if json.Unmarshal(value, &s) == nil {
|
|
out[key] = s
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// decodeField decodes raw[key] into target when the field exists.
|
|
func decodeField(raw map[string]json.RawMessage, name, key string, target any) error {
|
|
value, ok := raw[key]
|
|
if !ok {
|
|
return nil
|
|
}
|
|
if err := json.Unmarshal(value, target); err != nil {
|
|
return fmt.Errorf("%s: %s: %w", name, key, err)
|
|
}
|
|
return nil
|
|
}
|