commit 59d536a260267d6cd4a2b82d7dd4628ba1f28532
parent b286bd6d6d2eb76a9f37153df6baa1a1ef447987
Author: gearsix <gearsix@tuta.io>
Date: Wed, 28 Jul 2021 14:30:50 +0100
tidyup & fixes in LoadContentDir (now LoadPageDir)
Diffstat:
M | page.go | | | 101 | ++++++++++++++++++++++++++++++++++++++++++------------------------------------- |
M | page_test.go | | | 20 | ++++++++++---------- |
M | pagr.go | | | 2 | +- |
3 files changed, 65 insertions(+), 58 deletions(-)
diff --git a/page.go b/page.go
@@ -79,7 +79,7 @@ func titleFromPath(path string) (title string) {
}
title = strings.TrimSuffix(title, filepath.Ext(title))
title = strings.ReplaceAll(title, "-", " ")
- title = strings.Title(title)
+ //title = strings.Title(title)
return
}
@@ -121,79 +121,63 @@ func isContentExt(ext string) int {
return -1
}
-// LoadContentDir parses all files/directories in `dir` into a `Content`.
+// LoadPagesDir parses all files/directories in `dir` into a `Content`.
// For each directory, a new `Page` element will be generated, any file with a
// 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 LoadContentDir(dir string) (p []Page, e error) {
+func LoadPagesDir(dir string) (p []Page, e error) {
if _, e = os.Stat(dir); e != nil {
return
}
+ dir = strings.TrimSuffix(dir, "/")
pages := make(map[string]Page)
- defaults := make(map[string]Meta)
- if dir[len(dir)-1] != '/' {
- dir += "/"
- }
+ dmetas := make(map[string]Meta)
+
e = filepath.Walk(dir, func(fpath string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
-
if strings.Contains(fpath, ".ignore") {
return nil
}
- var path string
if info.IsDir() {
- path = "/" + strings.TrimPrefix(fpath, dir)
- page := NewPage(path)
- for i, p := range path {
- if p != '/' {
- continue
- }
- dpath := path[:i]
- if len(dpath) == 0 {
- dpath = "/"
- }
- if _, ok := defaults[dpath]; ok {
- page.Meta.MergeMeta(defaults[dpath], true)
+ path := pagePath(dir, fpath)
+ pages[path] = NewPage(path)
+ } else {
+ path := pagePath(dir, filepath.Dir(fpath))
+ page := pages[path]
+
+ if suti.IsSupportedDataLang(filepath.Ext(fpath)) > -1 {
+ var m Meta
+ if err = suti.LoadDataFile(fpath, &m); err == nil {
+ if strings.Contains(filepath.Base(fpath), "defaults.") {
+ 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
- return nil
- }
-
- path, _ = filepath.Split(fpath)
- path = strings.TrimPrefix(path, dir)
- path = "/" + strings.TrimSuffix(path, "/")
- page := pages[path]
- if strings.Contains(fpath, "page.") || strings.Contains(fpath, "defaults.") {
- var m Meta
- if err = suti.LoadDataFile(fpath, &m); err != nil {
- return err
- }
- if strings.Contains(fpath, "page.") {
- page.Meta.MergeMeta(m, true)
- } else if strings.Contains(fpath, "defaults.") {
- page.Meta.MergeMeta(m, false)
- defaults[path] = m
- }
- } else if isContentExt(filepath.Ext(fpath)) > -1 {
- err = page.NewContentFromFile(fpath)
- } else {
- page.Assets = append(page.Assets, fpath)
+ pages[path] = page
}
-
- pages[path] = page
- return nil
+ return err
})
for _, page := range pages {
+ page.applyDefaults(dmetas)
p = append(p, page)
}
+
sort.SliceStable(p, func(i, j int) bool { return p[i].Updated.Before(p[j].Updated) })
p = BuildSitemap(p)
@@ -216,6 +200,14 @@ func (m Meta) MergeMeta(meta Meta, overwrite bool) {
}
}
+func pagePath(root, path string) string {
+ path = strings.TrimPrefix(path, root)
+ if len(path) == 0 {
+ path = "/"
+ }
+ return path
+}
+
// Page is the data structure loaded from Content files/folders that
// gets passed to templates for execution after Content has been loaded.
// This is the data structure to reference when writing a template!
@@ -315,6 +307,21 @@ func (p *Page) NewContentFromFile(fpath string) (err error) {
return err
}
+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)
+ }
+ }
+}
+
// convertTextToHTML parses textual data from `in` and line-by-line converts
// it to HTML. Conversion rules are as follows:
// - Blank lines (with escape characters trimmed) will close any opon tags
diff --git a/page_test.go b/page_test.go
@@ -7,7 +7,7 @@ import (
"testing"
)
-func TestLoadContentDir(t *testing.T) {
+func TestLoadPagesDir(t *testing.T) {
t.Parallel()
var err error
@@ -17,8 +17,8 @@ func TestLoadContentDir(t *testing.T) {
}
var p []Page
- if p, err = LoadContentDir(tdir); err != nil {
- t.Fatalf("LoadContentDir failed: %s", err)
+ if p, err = LoadPagesDir(tdir); err != nil {
+ t.Fatalf("LoadPagesDir failed: %s", err)
}
validateContents(t, p, err)
@@ -33,22 +33,22 @@ func validateContents(t *testing.T, pages []Page, e error) {
var last time.Time
for i, p := range pages {
if len(p.Title) == 0 {
- t.Fatal("empty Title for page:", p)
+ t.Error("empty Title for page:", p)
}
if len(p.Path) == 0 {
- t.Fatal("empty Path for page:", p)
+ t.Error("empty Path for page:", p)
}
if _, ok := p.Meta["page"]; !ok || len(p.Meta) == 0 {
- t.Fatal("missing page Meta key for page:", p.Path)
+ t.Error("missing page Meta key for page:", p.Path)
}
if _, ok := p.Meta["default"]; !ok || len(p.Meta) == 0 {
- t.Fatal("empty default Meta key for page:", p.Path)
+ t.Error("empty default Meta key for page:", p.Path)
}
if len(p.Contents) == 0 {
- t.Fatal("empty Contents for page:", p.Path)
+ t.Error("empty Contents for page:", p.Path)
}
if len(p.Assets) == 0 {
- t.Fatal("empty Assets for page:", p.Path)
+ t.Error("empty Assets for page:", p.Path)
}
if i == 0 {
@@ -57,7 +57,7 @@ func validateContents(t *testing.T, pages []Page, e error) {
for _, pp := range pages {
t.Log(pp.Updated)
}
- t.Fatal("Contents Pages returned in wrong order")
+ t.Error("Contents Pages returned in wrong order")
}
}
}
diff --git a/pagr.go b/pagr.go
@@ -54,7 +54,7 @@ func main() {
vlog("loaded config: %s\n", config)
var c []Page
- c, err = LoadContentDir(config.Pages)
+ c, err = LoadPagesDir(config.Pages)
check(err)
log.Printf("loaded %d content pages", len(c))