A new package, with no new dependency, that reads the HTML a page renders to (the lenient mode of encoding/xml with the HTML void elements and entities) and writes gemtext: three heading levels, one line paragraphs, the links of a paragraph, a list, a quote or a table on lines of their own after it, bullets (a list of only links is link lines), quotes (a note is a quote), preformatted blocks, tables as aligned preformatted blocks, images as link lines, no emphasis, and lines that look like gemtext are protected. Options.Link maps or drops an address and its error stops the conversion; Options.Block expands the placeholders of generated blocks. The language files also keep the order of the menu now (Lang.NavOrder), which the Gemini output needs. Refs #10
214 lines
6.5 KiB
Go
214 lines
6.5 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
|
|
// NavOrder lists the menu entries in the order the file writes them.
|
|
NavOrder []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
|
|
}
|
|
order, err := keyOrder(raw["nav"])
|
|
if err != nil {
|
|
return Lang{}, fmt.Errorf("%s: nav: %w", name, err)
|
|
}
|
|
lang.NavOrder = order
|
|
return lang, nil
|
|
}
|
|
|
|
// keyOrder returns the keys of a JSON object in the order they are written.
|
|
func keyOrder(object json.RawMessage) ([]string, error) {
|
|
decoder := json.NewDecoder(strings.NewReader(string(object)))
|
|
if _, err := decoder.Token(); err != nil {
|
|
return nil, err
|
|
}
|
|
var keys []string
|
|
for decoder.More() {
|
|
key, err := decoder.Token()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
keys = append(keys, key.(string))
|
|
var skipped json.RawMessage
|
|
if err := decoder.Decode(&skipped); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return keys, 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
|
|
}
|