From 3f502544038f8f1457e1ef62f16de36fe8f27690 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sat, 25 Oct 2025 22:43:50 -0400 Subject: [PATCH] Quick hack for a 'metafile' feature that is able to work on all of the file names processed. Usually for a sitemap or rss feed. NOT WORKING. --- config/loader.go | 1 + example/.ssgod.json | 5 ++- example/pages/sitemap.html | 9 +++++ main.go | 67 ++++++++++++++++++++++++++++++-------- 4 files changed, 68 insertions(+), 14 deletions(-) create mode 100644 example/pages/sitemap.html diff --git a/config/loader.go b/config/loader.go index cdf9968..4cea695 100644 --- a/config/loader.go +++ b/config/loader.go @@ -12,6 +12,7 @@ type config struct { Target string `json:"target"` WatchDelay string `json:"watch_delay"` SyncDir string `json:"sync_dir"` + MetaFiles []string `json:"meta_files"` } var Settings config diff --git a/example/.ssgod.json b/example/.ssgod.json index 9b6d393..dbb8eaf 100644 --- a/example/.ssgod.json +++ b/example/.ssgod.json @@ -3,5 +3,8 @@ "layout": "pages/layouts/main.html", "target": "public", "watch_delay": "500ms", - "sync_dir": "static" + "sync_dir": "static", + "meta_files": [ + "pages/sitemap.html" + ] } diff --git a/example/pages/sitemap.html b/example/pages/sitemap.html new file mode 100644 index 0000000..2238074 --- /dev/null +++ b/example/pages/sitemap.html @@ -0,0 +1,9 @@ +

Site Map

+ + diff --git a/main.go b/main.go index 853435d..e3b9a88 100644 --- a/main.go +++ b/main.go @@ -44,7 +44,13 @@ func RenderTemplate(out io.Writer, embed string, variables any) error { tmpl := template.New(layout_path) callbacks := template.FuncMap{ - "embed": func() string { return embed }, + "embed": func() string { + tmpl, err = tmpl.Parse(embed) + if err != nil { Fatal(err, "error in your template") } + out := bytes.NewBuffer(make([]byte, 0, 100)) + tmpl.Execute(out, variables) + return out.String() + }, } tmpl.Funcs(callbacks) @@ -58,7 +64,26 @@ func RenderTemplate(out io.Writer, embed string, variables any) error { return err } -func RenderMarkdown(path string, target_path string, page_id string) error { +func RenderMetaFiles(pages []string) { + fmt.Println("PAGES: ", pages) + + for _, path := range config.Settings.MetaFiles { + fmt.Println(">>> META >>>>", path) + + ext := filepath.Ext(path) + target_path := RePrefixPath(path, config.Settings.Target) + + if ext == ".md" { + RenderMarkdown(path, target_path, + map[string]any{"Pages": pages}) + } else { + RenderHTML(path, target_path, + map[string]any{"Pages": pages}) + } + } +} + +func RenderMarkdown(path string, target_path string, vars map[string]any) error { log.Printf("MARKDOWN: %s -> %s", path, target_path) out, err := os.Create(target_path) @@ -76,8 +101,7 @@ func RenderMarkdown(path string, target_path string, page_id string) error { return Fail(err, "failed converting markdown %s", path) } - err = RenderTemplate(out, md_out.String(), - map[string]string{"PageId": page_id}) + err = RenderTemplate(out, md_out.String(), vars) if err != nil { return Fail(err, "failed to render template %s->%s", path, target_path) @@ -86,19 +110,18 @@ func RenderMarkdown(path string, target_path string, page_id string) error { return err } -func RenderHTML(source_path string, target_path string, page_id string) error { +func RenderHTML(source_path string, target_path string, vars map[string]any) error { log.Printf("RENDER: %s -> %s", source_path, target_path) out, err := os.Create(target_path) defer out.Close() - html_content, err := os.ReadFile(source_path) + content, err := os.ReadFile(source_path) if err != nil { - return Fail(err, "cannot open html input %s", source_path) + return Fail(err, "cannot open input %s", source_path) } - err = RenderTemplate(out, string(html_content), - map[string]string{"PageId": page_id}) + err = RenderTemplate(out, string(content), vars) if err != nil { return Fail(err, "writing file %s", target_path) @@ -164,8 +187,10 @@ func ProcessDirEntry(path string, d fs.DirEntry, err error) error { // generate a data-testid for all pages based on template name page_id := strings.ReplaceAll(source_name, "/", "-") + "-page" - if ext == ".html" { - err = RenderHTML(path, target_path, page_id) + // BUG: make it so we can have a list of extensions + if ext == ".html" || ext == ".xml" || ext == ".rss" { + err = RenderHTML(path, target_path, + map[string]any{"PageId": page_id}) if err != nil { return Fail(err, "failed to render %s", path) @@ -175,7 +200,8 @@ func ProcessDirEntry(path string, d fs.DirEntry, err error) error { html_name, _ := strings.CutSuffix(target_path, ext) html_name = fmt.Sprintf("%s.html", html_name) - RenderMarkdown(path, html_name, page_id) + RenderMarkdown(path, html_name, + map[string]any{"PageId": page_id}) if err != nil { return Fail(err, "failed to render markdown %s", path) @@ -216,14 +242,29 @@ func SyncStaticDir() { } func RenderPages() { + pages := make([]string, 0, 100) + err := filepath.WalkDir(config.Settings.Views, func(path string, d fs.DirEntry, err error) error { - return ProcessDirEntry(path, d, err) + if err != nil { return err } + + create an exclusion/inclusion system that knows if a file + is a target, and only process it and add to pages if it matches + + err = ProcessDirEntry(path, d, err) + if err == nil { + // no errors, add to the list + pages = append(pages, path) + } + + return err }) if err != nil { Fatal(err, "can't walk content") } + + RenderMetaFiles(pages) } func WatchMatches(name string) bool {