pagr

A 'static site generator', built using dati.
Log | Files | Refs | Atom

commit edbda75f2515581e80b2ae102704d87158ecbf7a
parent edd5aee33076a1489c78c27bfb93d56fba13d18c
Author: gearsix <gearsix@tuta.io>
Date:   Fri, 18 Mar 2022 01:16:33 +0000

tidyup: split LoadContentDir/2, minor renames, added minor doc

Diffstat:
Mcontent.go | 55++++++++++++++++++++++++++++++-------------------------
Mpage.go | 33++++++++++++++++++---------------
Mpagr.go | 2+-
3 files changed, 49 insertions(+), 41 deletions(-)

diff --git a/content.go b/content.go @@ -18,6 +18,7 @@ import ( "time" ) +// Content is the converted HTML string of a Content file type Content string var contentExts = [6]string{ @@ -65,14 +66,14 @@ func lastModFile(fpath string) (t time.Time) { // filetype found in `contentExts`, will be parsed into a string of HTML // and appended to the `.Content` of the `Page` generated for it's parent // directory. -func LoadContentsDir(dir string) (p []Page, e error) { +func LoadContentDir(dir string) (p []Page, e error) { if _, e = os.Stat(dir); e != nil { return } dir = filepath.Clean(dir) pages := make(map[string]Page) - dmetas := make(map[string]Meta) + dmeta := make(map[string]Meta) e = filepath.Walk(dir, func(fpath string, info os.FileInfo, err error) error { if err != nil { @@ -87,34 +88,13 @@ func LoadContentsDir(dir string) (p []Page, e error) { pages[path] = NewPage(path, lastModFile(fpath)) } else { path := pagePath(dir, filepath.Dir(fpath)) - page := pages[path] - - if suti.IsSupportedDataLang(filepath.Ext(fpath)) > -1 { - var m Meta - if err = suti.LoadDataFilepath(fpath, &m); err == nil { - if strings.Contains(filepath.Base(fpath), "defaults.") || - strings.Contains(filepath.Base(fpath), "default.") { - if meta, ok := dmetas[path]; ok { - m.MergeMeta(meta, false) - } - dmetas[path] = m - } else { - page.Meta.MergeMeta(m, true) - } - } - } else if isContentExt(filepath.Ext(fpath)) > -1 { - err = page.NewContentFromFile(fpath) - } else if suti.IsSupportedDataLang(filepath.Ext(fpath)) == -1 { - page.Assets = append(page.Assets, filepath.Join(path, filepath.Base(fpath))) - } - - pages[path] = page + pages[path], dmeta, err = loadContentFile(pages[path], dmeta, fpath, path) } return err }) for _, page := range pages { - page.applyDefaults(dmetas) + page.applyDefaults(dmeta) p = append(p, page) } @@ -132,6 +112,31 @@ func LoadContentsDir(dir string) (p []Page, e error) { return } +func loadContentFile(p Page, d map[string]Meta, fpath string, ppath string) ( +Page, map[string]Meta, error) { + var err error + if suti.IsSupportedDataLang(filepath.Ext(fpath)) > -1 { + var m Meta + if err = suti.LoadDataFilepath(fpath, &m); err == nil { + if strings.Contains(filepath.Base(fpath), "defaults.") || + strings.Contains(filepath.Base(fpath), "default.") { + if meta, ok := d[ppath]; ok { + m.MergeMeta(meta, false) + } + d[ppath] = m + } else { + p.Meta.MergeMeta(m, true) + } + } + } else if isContentExt(filepath.Ext(fpath)) > -1 { + err = p.NewContentFromFile(fpath) + } else if suti.IsSupportedDataLang(filepath.Ext(fpath)) == -1 { + a := filepath.Join(ppath, filepath.Base(fpath)) + p.Assets = append(p.Assets, a) + } + return p, d, err +} + // NewContentFromFile loads the file from `fpath` and converts it to HTML // from the language matching it's file extension (see below). // - ".txt" = plain-text diff --git a/page.go b/page.go @@ -99,21 +99,8 @@ func (p *Page) TemplateName() string { } } -func (page *Page) applyDefaults(defaultMetas map[string]Meta) { - for i, p := range page.Path { - if p != '/' { - continue - } - path := page.Path[:i] - if len(path) == 0 { - path = "/" - } - if meta, ok := defaultMetas[path]; ok { - page.Meta.MergeMeta(meta, false) - } - } -} - +// Build will run `t.Execute(p)` and write the result to +// `outDir/p.Path/index.html`. func (p *Page) Build(outDir string, t suti.Template) (out string, err error) { var buf bytes.Buffer if buf, err = t.Execute(p); err == nil { @@ -125,6 +112,7 @@ func (p *Page) Build(outDir string, t suti.Template) (out string, err error) { return out, err } +// call `NewContentFromFile` and append it to `p.Contents` func (p *Page) NewContentFromFile(fpath string) (err error) { var c Content if c, err = NewContentFromFile(fpath); err == nil { @@ -132,3 +120,18 @@ func (p *Page) NewContentFromFile(fpath string) (err error) { } return } + +func (page *Page) applyDefaults(defaultMetas map[string]Meta) { + for i, p := range page.Path { + if p != '/' { + continue + } + path := page.Path[:i] + if len(path) == 0 { + path = "/" + } + if meta, ok := defaultMetas[path]; ok { + page.Meta.MergeMeta(meta, false) + } + } +} diff --git a/pagr.go b/pagr.go @@ -52,7 +52,7 @@ func main() { var err error var content []Page - content, err = LoadContentsDir(config.Contents) + content, err = LoadContentDir(config.Contents) check(err) ilog.Printf("loaded %d content pages", len(content))