The theme carries a MANIFEST of its files' SHA-256s. A site's Theme card shows its copy's version against Supermarket's and any files changed by hand; Update the theme opens a pull request that writes new and changed files, removes retired ones, rebuilds, and lists what it replaces. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
102 lines
2.5 KiB
Go
102 lines
2.5 KiB
Go
// Package theme is the Hugo theme new sites are made with: federated-
|
|
// website's look and sections, made generic, with each site's brand as
|
|
// settings. Supermarket copies it into a site as themes/supermarket/.
|
|
package theme
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"embed"
|
|
"fmt"
|
|
"io/fs"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// Version is the theme's version, written into the site's copy
|
|
// (themes/supermarket/VERSION) so an older copy can be updated. Raise it
|
|
// whenever the files change.
|
|
const Version = 1
|
|
|
|
// Dir is where the theme goes in a site.
|
|
const Dir = "themes/supermarket"
|
|
|
|
// Name is the theme's name in hugo.toml.
|
|
const Name = "supermarket"
|
|
|
|
//go:embed all:files
|
|
var files embed.FS
|
|
|
|
// Files are the theme's files: path (inside the theme) → content,
|
|
// VERSION and MANIFEST included.
|
|
func Files() map[string]string {
|
|
out := files0()
|
|
out["MANIFEST"] = Manifest(out)
|
|
return out
|
|
}
|
|
|
|
// Manifest lists each file's SHA-256 (VERSION and MANIFEST aside), so a
|
|
// site's copy can be checked for files changed by hand.
|
|
func Manifest(files map[string]string) string {
|
|
var b strings.Builder
|
|
for _, p := range sortedKeys(files) {
|
|
if p == "VERSION" || p == "MANIFEST" {
|
|
continue
|
|
}
|
|
fmt.Fprintf(&b, "%x %s\n", sha256.Sum256([]byte(files[p])), p)
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
// ParseManifest reads a MANIFEST: path → SHA-256 (hex).
|
|
func ParseManifest(m string) map[string]string {
|
|
out := map[string]string{}
|
|
for _, l := range strings.Split(m, "\n") {
|
|
if sum, p, ok := strings.Cut(l, " "); ok && p != "" {
|
|
out[p] = sum
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// Sum is a file's SHA-256, as in the manifest.
|
|
func Sum(content []byte) string { return fmt.Sprintf("%x", sha256.Sum256(content)) }
|
|
|
|
func sortedKeys(m map[string]string) []string {
|
|
var ks []string
|
|
for k := range m {
|
|
ks = append(ks, k)
|
|
}
|
|
sort.Strings(ks)
|
|
return ks
|
|
}
|
|
|
|
func files0() map[string]string {
|
|
out := map[string]string{"VERSION": strconv.Itoa(Version) + "\n"}
|
|
_ = fs.WalkDir(files, "files", func(p string, d fs.DirEntry, err error) error {
|
|
if err != nil || d.IsDir() {
|
|
return err
|
|
}
|
|
raw, err := files.ReadFile(p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
out[strings.TrimPrefix(p, "files/")] = string(raw)
|
|
return nil
|
|
})
|
|
return out
|
|
}
|
|
|
|
// Paths are the theme's file paths, sorted.
|
|
func Paths() []string {
|
|
var ps []string
|
|
for p := range Files() {
|
|
ps = append(ps, p)
|
|
}
|
|
sort.Strings(ps)
|
|
return ps
|
|
}
|
|
|
|
// LeadScript is the lead forms' script (static/js/supermarket-leads.js).
|
|
func LeadScript() string { return Files()["static/js/supermarket-leads.js"] }
|