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.

master
Zed A. Shaw 3 days ago
parent 364e203898
commit 3f50254403
  1. 1
      config/loader.go
  2. 5
      example/.ssgod.json
  3. 9
      example/pages/sitemap.html
  4. 67
      main.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

@ -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"
]
}

@ -0,0 +1,9 @@
<h1>Site Map</h1>
<ul>
{{range $index, $page := .Pages}}
<li><b>{{$index}}:</b> {{$page}}</li>
{{ else }}
Nothing Here
{{end}}
</ul>

@ -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 {

Loading…
Cancel
Save