From fb67ef046a955d81717bde8b7f561dca9acce3a3 Mon Sep 17 00:00:00 2001 From: Daniel Fichtinger Date: Sun, 29 Dec 2024 20:06:57 -0500 Subject: [PATCH] added check for posts directory --- go.mod | 2 +- internal/builder/build_page.go | 3 ++- internal/util/path.go | 21 +++++++++++++++++++++ runtest.sh | 2 +- test/in.md | 2 +- test/posts/in.md | 10 ++++++++++ test/yamltest.md | 1 + 7 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 test/posts/in.md diff --git a/go.mod b/go.mod index d9c42d7..9b7a11c 100644 --- a/go.mod +++ b/go.mod @@ -8,4 +8,4 @@ require ( gopkg.in/yaml.v3 v3.0.1 ) -require golang.org/x/text v0.20.0 +require golang.org/x/text v0.21.0 diff --git a/internal/builder/build_page.go b/internal/builder/build_page.go index 783268f..b3bb817 100644 --- a/internal/builder/build_page.go +++ b/internal/builder/build_page.go @@ -89,7 +89,8 @@ func buildPageData(m Metadata, in string, out string, settings *Settings) *PageD p.FooterName = settings.FooterName p.Footer = settings.Footer } - if t, ok := m["type"].(string); ok && t == "article" || t == "post" { + // TODO: Don't hard code posts dir name + if t, ok := m["type"].(string); util.InDir(in, "posts") && !ok || (ok && t == "article" || t == "post") { p.Template = (settings.ArticleTemplate) } else { p.Template = (settings.DefaultTemplate) diff --git a/internal/util/path.go b/internal/util/path.go index 12fa15e..02b4b7b 100644 --- a/internal/util/path.go +++ b/internal/util/path.go @@ -40,6 +40,27 @@ func ReplaceRoot(inPath, outRoot string) string { return outPath } +// InDir checks whether checkPath is +// inside targDir. +func InDir(checkPath string, targDir string) bool { + // fmt.Println("checking dir..") + i := 0 + for i < 10 { + parent := filepath.Dir(checkPath) + fmted := filepath.Base(parent) + switch fmted { + case targDir: + // fmt.Printf("%s in %s\n", checkPath, targDir) + return true + case ".": + return false + } + checkPath = parent + i += 1 + } + return false +} + // FileExists returns a boolean indicating // whether something exists at the path. func FileExists(path string) bool { diff --git a/runtest.sh b/runtest.sh index d20c26e..1a75f4d 100755 --- a/runtest.sh +++ b/runtest.sh @@ -5,4 +5,4 @@ fi go run cmd/zona/main.go test -bat foobar/in.html +bat foobar/posts/in.html diff --git a/test/in.md b/test/in.md index 2fd2350..57171ba 100644 --- a/test/in.md +++ b/test/in.md @@ -1,5 +1,5 @@ --- -type: article +title: tiltetest --- # My amazing markdown file! diff --git a/test/posts/in.md b/test/posts/in.md new file mode 100644 index 0000000..57171ba --- /dev/null +++ b/test/posts/in.md @@ -0,0 +1,10 @@ +--- +title: tiltetest +--- + +# My amazing markdown file! + +I can _even_ do **this**! + +- Or, I could... +- [Link](page.md) to this file diff --git a/test/yamltest.md b/test/yamltest.md index 974dedb..ffa1fb3 100644 --- a/test/yamltest.md +++ b/test/yamltest.md @@ -1,5 +1,6 @@ --- title: Yaml testing file +type: article --- # My amazing markdown file!