diff options
| author | Gab Virebent <gabriel1@virebent.art> | 2026-07-07 16:24:28 +0200 |
|---|---|---|
| committer | Gab Virebent <gabriel1@virebent.art> | 2026-07-07 16:24:28 +0200 |
| commit | 37f156c30e2a2b01c840d39f0dd2bdbf811732d6 (patch) | |
| tree | e2ce84dc7f88ce0eb312899448f91b7bb93ca8a3 /internal/cms/section.go | |
| download | gemcms-37f156c30e2a2b01c840d39f0dd2bdbf811732d6.tar.gz gemcms-37f156c30e2a2b01c840d39f0dd2bdbf811732d6.tar.xz gemcms-37f156c30e2a2b01c840d39f0dd2bdbf811732d6.zip | |
Diffstat (limited to 'internal/cms/section.go')
| -rw-r--r-- | internal/cms/section.go | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/internal/cms/section.go b/internal/cms/section.go new file mode 100644 index 0000000..fbb9013 --- /dev/null +++ b/internal/cms/section.go @@ -0,0 +1,54 @@ +package cms + +import ( + "fmt" + "io/fs" + "os" + "path/filepath" +) + +func LoadSections(root string) (map[string]Section, error) { + dir := filepath.Join(root, "site/sections") + sections := map[string]Section{} + if _, err := os.Stat(dir); err != nil { + if os.IsNotExist(err) { + return sections, nil + } + return nil, err + } + err := filepath.WalkDir(dir, func(path string, entry fs.DirEntry, err error) error { + if err != nil { + return err + } + if entry.IsDir() || filepath.Ext(path) != ".toml" { + return nil + } + section, err := loadSection(path) + if err != nil { + return err + } + sections[section.Name] = section + return nil + }) + return sections, err +} + +func loadSection(path string) (Section, error) { + data, err := os.ReadFile(path) + if err != nil { + return Section{}, err + } + table, err := parseSimpleTable(string(data)) + if err != nil { + return Section{}, fmt.Errorf("%s: %w", path, err) + } + name := table.values["name"] + if name == "" { + name = slugify(filepath.Base(path)) + } + title := table.values["title"] + if title == "" { + title = titleize(name) + } + return Section{Name: name, Title: title}, nil +} |
