From 988d4ba42e3f3f3f9160c033104f7cd85794e304 Mon Sep 17 00:00:00 2001 From: Daniel Fichtinger Date: Fri, 4 Apr 2025 23:53:47 -0400 Subject: [PATCH] added proper output directory structure --- internal/builder/process.go | 4 +++- internal/util/path.go | 14 ++++++++++++++ internal/util/path_test.go | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 internal/util/path_test.go diff --git a/internal/builder/process.go b/internal/builder/process.go index b46cedb..7108668 100644 --- a/internal/builder/process.go +++ b/internal/builder/process.go @@ -57,15 +57,17 @@ func processFile(inPath string, entry fs.DirEntry, err error, outRoot string, se return nil } else { ext = filepath.Ext(inPath) - outPath = util.ReplaceRoot(inPath, outRoot) // NOTE: This could be an if statement, but keeping // the switch makes it easy to extend the logic here later switch ext { case ".md": toProcess = true + outPath = util.ReplaceRoot(inPath, outRoot) outPath = util.ChangeExtension(outPath, ".html") + outPath = util.Indexify(outPath) default: toProcess = false + outPath = util.ReplaceRoot(inPath, outRoot) } } diff --git a/internal/util/path.go b/internal/util/path.go index 02b4b7b..6c95cfc 100644 --- a/internal/util/path.go +++ b/internal/util/path.go @@ -22,6 +22,7 @@ func ChangeExtension(in string, outExt string) string { return strings.TrimSuffix(in, filepath.Ext(in)) + outExt } +// TODO: look for .zona.yml instead? func getRoot(path string) string { for { parent := filepath.Dir(path) @@ -40,6 +41,19 @@ func ReplaceRoot(inPath, outRoot string) string { return outPath } +// Indexify converts format path/file.ext +// into path/file/index.ext +func Indexify(in string) string { + ext := filepath.Ext(in) + trimmed := strings.TrimSuffix(in, ext) + filename := filepath.Base(trimmed) + if filename == "index" { + return in + } + prefix := strings.TrimSuffix(trimmed, filename) + return filepath.Join(prefix, filename, "index"+ext) +} + // InDir checks whether checkPath is // inside targDir. func InDir(checkPath string, targDir string) bool { diff --git a/internal/util/path_test.go b/internal/util/path_test.go new file mode 100644 index 0000000..ccac2fc --- /dev/null +++ b/internal/util/path_test.go @@ -0,0 +1,35 @@ +package util_test + +import ( + "testing" + + "github.com/ficcdaf/zona/internal/util" +) + +func TestIndexify(t *testing.T) { + tests := []struct { + name string // description of this test case + // Named input parameters for target function. + in string + want string + }{ + { + "Simple Path", + "foo/bar/name.html", + "foo/bar/name/index.html", + }, + { + "Index Name", + "foo/bar/index.md", + "foo/bar/index.md", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := util.Indexify(tt.in) + if got != tt.want { + t.Errorf("Indexify() = %v, want %v", got, tt.want) + } + }) + } +}