Phase 6 part 5, step 1: a shared site theme
internal/theme embeds a Hugo theme from federated-website's look and the 13 section types both sites share, with the header, footer, colors, font and logo set per site in hugo.toml, and a README of each type's fields. The catalogue reader reads themes' sections and READMEs after the site's own; lead forms know a theme site needs only the Lead Capture address. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+36
-13
@@ -82,25 +82,36 @@ var (
|
||||
partialRef = regexp.MustCompile(`partial\s+"sections/([\w-]+)\.html"\s*(?:(\$s|\.)(?:\s|-?\}\}|\)))?`)
|
||||
dollarS = regexp.MustCompile(`\$s\.([a-z]\w*)`)
|
||||
bareDot = regexp.MustCompile(`(?:^|[\s(])\.([a-z]\w*)`)
|
||||
typeTest = regexp.MustCompile(`eq \.type "([\w-]+)"`)
|
||||
readmeList = regexp.MustCompile("^\\s*[-*]\\s+`([\\w-]+)`\\s+[—–-]+\\s+(.+)$")
|
||||
readmeRow = regexp.MustCompile("^\\|\\s*`([\\w-]+)`\\s*\\|\\s*(.+?)\\s*\\|\\s*$")
|
||||
)
|
||||
|
||||
// Read reads the catalogue from a working copy of the site.
|
||||
func Read(dir string, pathOf PathFunc) (*Catalog, error) {
|
||||
partDir := filepath.Join(dir, "layouts", "partials", "sections")
|
||||
entries, err := os.ReadDir(partDir)
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return nil, err
|
||||
// The site's own layouts, then its themes' (the site's win, as in Hugo).
|
||||
roots := []string{dir}
|
||||
if themes, err := filepath.Glob(filepath.Join(dir, "themes", "*")); err == nil {
|
||||
sort.Strings(themes)
|
||||
roots = append(roots, themes...)
|
||||
}
|
||||
partials := map[string]string{}
|
||||
for _, e := range entries {
|
||||
if !e.IsDir() && strings.HasSuffix(e.Name(), ".html") {
|
||||
for _, root := range roots {
|
||||
partDir := filepath.Join(root, "layouts", "partials", "sections")
|
||||
entries, err := os.ReadDir(partDir)
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return nil, err
|
||||
}
|
||||
for _, e := range entries {
|
||||
name := strings.TrimSuffix(e.Name(), ".html")
|
||||
if e.IsDir() || !strings.HasSuffix(e.Name(), ".html") || partials[name] != "" {
|
||||
continue
|
||||
}
|
||||
raw, err := os.ReadFile(filepath.Join(partDir, e.Name()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
partials[strings.TrimSuffix(e.Name(), ".html")] = string(raw)
|
||||
partials[name] = string(raw)
|
||||
}
|
||||
}
|
||||
// Partials named by another partial, or by render-sections, are
|
||||
@@ -115,9 +126,16 @@ func Read(dir string, pathOf PathFunc) (*Catalog, error) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if raw, err := os.ReadFile(filepath.Join(dir, "layouts", "partials", "render-sections.html")); err == nil {
|
||||
for _, m := range partialRef.FindAllStringSubmatch(string(raw), -1) {
|
||||
referenced[m[1]] = true
|
||||
dispatched := map[string]bool{} // types render-sections picks by name
|
||||
for _, root := range roots {
|
||||
if raw, err := os.ReadFile(filepath.Join(root, "layouts", "partials", "render-sections.html")); err == nil {
|
||||
for _, m := range partialRef.FindAllStringSubmatch(string(raw), -1) {
|
||||
referenced[m[1]] = true
|
||||
}
|
||||
for _, m := range typeTest.FindAllStringSubmatch(string(raw), -1) {
|
||||
dispatched[m[1]] = true
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,7 +153,7 @@ func Read(dir string, pathOf PathFunc) (*Catalog, error) {
|
||||
}
|
||||
|
||||
root := filepath.Join(dir, "content")
|
||||
err = filepath.WalkDir(root, func(p string, d fs.DirEntry, err error) error {
|
||||
err := filepath.WalkDir(root, func(p string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) && p == root {
|
||||
return fs.SkipDir
|
||||
@@ -208,13 +226,18 @@ func Read(dir string, pathOf PathFunc) (*Catalog, error) {
|
||||
}
|
||||
|
||||
for name := range partials {
|
||||
if referenced[name] && uses[name] == nil {
|
||||
if referenced[name] && uses[name] == nil && !dispatched[name] {
|
||||
c.Helpers = append(c.Helpers, name)
|
||||
continue
|
||||
}
|
||||
get(name)
|
||||
}
|
||||
docs := readmeDocs(dir)
|
||||
docs := map[string]string{}
|
||||
for i := len(roots) - 1; i >= 0; i-- { // the site's README wins
|
||||
for k, v := range readmeDocs(roots[i]) {
|
||||
docs[k] = v
|
||||
}
|
||||
}
|
||||
for name, t := range uses {
|
||||
_, has := partials[name]
|
||||
t.Missing = !has
|
||||
|
||||
Reference in New Issue
Block a user