From 03da3b37704663682d5d4c1cf7354693687bbb82 Mon Sep 17 00:00:00 2001 From: facundoolano Date: Mon, 12 Feb 2024 15:38:26 -0300 Subject: [PATCH] replace method with field --- commands/commands.go | 9 ++++++--- commands/site.go | 10 +--------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/commands/commands.go b/commands/commands.go index b0bc7a9..3fa8e06 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -35,7 +35,8 @@ func Build() error { } site := Site{ - layouts: make(map[string]templates.Template), + layouts: make(map[string]templates.Template), + templateIndex: make(map[string]*templates.Template), } // FIXME these sound like they should be site methods too @@ -100,6 +101,9 @@ func loadTemplates(site *Site) error { } else { site.pages = append(site.pages, *templ) } + site.templateIndex[path] = templ + + // TODO load tags } return nil }) @@ -111,7 +115,6 @@ func writeTarget(site *Site) error { os.Mkdir(TARGET_DIR, FILE_RW_MODE) // walk the source directory, creating directories and files at the target dir - templIndex := site.templateIndex() return filepath.WalkDir(SRC_DIR, func(path string, entry fs.DirEntry, err error) error { subpath, _ := filepath.Rel(SRC_DIR, path) targetPath := filepath.Join(TARGET_DIR, subpath) @@ -120,7 +123,7 @@ func writeTarget(site *Site) error { os.MkdirAll(targetPath, FILE_RW_MODE) } else { - if templ, ok := templIndex[path]; ok { + if templ, ok := site.templateIndex[path]; ok { // if a template was found at source, render it content, err := site.render(templ) if err != nil { diff --git a/commands/site.go b/commands/site.go index 41af917..f4e0542 100644 --- a/commands/site.go +++ b/commands/site.go @@ -15,7 +15,7 @@ type Site struct { pages []templates.Template tags map[string]*templates.Template - renderCache map[string]string + templateIndex map[string]*templates.Template } func (site Site) render(templ *templates.Template) (string, error) { @@ -42,14 +42,6 @@ func (site Site) render(templ *templates.Template) (string, error) { return content, err } -func (site Site) templateIndex() map[string]*templates.Template { - templIndex := make(map[string]*templates.Template) - for _, templ := range append(site.posts, site.pages...) { - templIndex[templ.SrcPath] = &templ - } - return templIndex -} - func (site Site) baseContext() map[string]interface{} { return map[string]interface{}{ "config": site.config,