diff --git a/cmd/zona/config.go b/cmd/zona/config.go new file mode 100644 index 0000000..06ab7d0 --- /dev/null +++ b/cmd/zona/config.go @@ -0,0 +1 @@ +package main diff --git a/cmd/zona/main.go b/cmd/zona/main.go index e2d2a64..d925f7d 100644 --- a/cmd/zona/main.go +++ b/cmd/zona/main.go @@ -6,7 +6,7 @@ import ( "fmt" "os" - "github.com/ficcdaf/zona/internal/convert" + "github.com/ficcdaf/zona/internal/build" ) // // validateFile checks whether a given path @@ -41,7 +41,7 @@ func main() { } } - err := convert.Traverse(*rootPath, "foobar") + err := build.Traverse(*rootPath, "foobar") if err != nil { fmt.Printf("Error: %s\n", err.Error()) } diff --git a/internal/convert/build_page.go b/internal/build/build_page.go similarity index 77% rename from internal/convert/build_page.go rename to internal/build/build_page.go index 7505db9..bcb5a38 100644 --- a/internal/convert/build_page.go +++ b/internal/build/build_page.go @@ -1,4 +1,4 @@ -package convert +package build import ( "bytes" @@ -49,26 +49,32 @@ func pathToTitle(path string) string { return strings.ToTitle(replaced) } -func buildPageData(m Metadata) *PageData { +func buildPageData(m Metadata, path string) *PageData { p := &PageData{} if title, ok := m["title"].(string); ok { p.Title = title } else { - p.Title = pathToTitle(m["path"].(string)) + p.Title = pathToTitle(path) } if icon, ok := m["icon"].(string); ok { p.Icon = icon } else { - p.Icon = "" + p.Icon = DefaultIcon } if style, ok := m["style"].(string); ok { p.Stylesheet = style + } else { + p.Stylesheet = DefaultStylesheet } if header, ok := m["header"].(string); ok { p.Header = header + } else { + p.Header = DefaultHeader } if footer, ok := m["footer"].(string); ok { p.Footer = footer + } else { + p.Footer = DefaultFooter } return p } @@ -82,18 +88,25 @@ func ConvertFile(in string, out string) error { if err != nil { return err } - metadata["path"] = in - pd := buildPageData(metadata) - fmt.Println("Page title: ", pd.Title) - pd.Content = template.HTML(md) - - tmlp, err := template.New("webpage").Parse("placeholder") + pd := buildPageData(metadata, in) // build according to template here html, err := MdToHTML(md) if err != nil { return err } - err = WriteFile(html, out) + pd.Content = template.HTML(html) + + tmpl, err := template.New("webpage").Parse(DefaultTemplate) + if err != nil { + return err + } + + var output bytes.Buffer + if err := tmpl.Execute(&output, pd); err != nil { + return err + } + + err = WriteFile(output.Bytes(), out) return err } diff --git a/internal/build/config.go b/internal/build/config.go new file mode 100644 index 0000000..5618d93 --- /dev/null +++ b/internal/build/config.go @@ -0,0 +1,52 @@ +package build + +const ( + DefaultHeader = "" + DefaultFooter = "" + DefaultStylesheet = "/style/zonaDefault.css" + DefaultIcon = "" + DefaultTemplate = ` + + + {{ .Title }} + + + + + + +
+ +
+ {{ .Content }} + +
+ +
+ +` +) + +type Settings struct { + Header string + Footer string + Stylesheet string + Icon string +} + +func NewSettings(header string, footer string, style string, icon string) *Settings { + return &Settings{ + header, + footer, + style, + icon, + } +} diff --git a/internal/convert/convert.go b/internal/build/convert.go similarity index 70% rename from internal/convert/convert.go rename to internal/build/convert.go index 2cfdaf5..740e07c 100644 --- a/internal/convert/convert.go +++ b/internal/build/convert.go @@ -1,15 +1,12 @@ -package convert +package build import ( - "errors" "fmt" "io" - "io/fs" "os" "path/filepath" "strings" - "github.com/ficcdaf/zona/internal/util" "github.com/gomarkdown/markdown" "github.com/gomarkdown/markdown/ast" "github.com/gomarkdown/markdown/html" @@ -137,48 +134,3 @@ func CopyFile(inPath string, outPath string) error { func ChangeExtension(in string, outExt string) string { return strings.TrimSuffix(in, filepath.Ext(in)) + outExt } - -func processFile(inPath string, entry fs.DirEntry, err error, outRoot string) error { - if err != nil { - return err - } - if !entry.IsDir() { - ext := filepath.Ext(inPath) - outPath := util.ReplaceRoot(inPath, outRoot) - switch ext { - case ".md": - fmt.Println("Processing markdown...") - outPath = ChangeExtension(outPath, ".html") - if err := util.CreateParents(outPath); err != nil { - return err - } - if err := ConvertFile(inPath, outPath); err != nil { - return errors.Join(errors.New("Error processing file "+inPath), err) - } else { - return nil - } - // If it's not a file we need to process, - // we simply copy it to the destination path. - default: - if err := util.CreateParents(outPath); err != nil { - return err - } - if err := CopyFile(inPath, outPath); err != nil { - return errors.Join(errors.New("Error processing file "+inPath), err) - } else { - return nil - } - } - } - // fmt.Printf("Visited: %s\n", inPath) - return nil -} - -func Traverse(root string, outRoot string) error { - // err := filepath.WalkDir(root, func(path string, entry fs.DirEntry, err error) error { - walkFunc := func(path string, entry fs.DirEntry, err error) error { - return processFile(path, entry, err, outRoot) - } - err := filepath.WalkDir(root, walkFunc) - return err -} diff --git a/internal/convert/convert_test.go b/internal/build/convert_test.go similarity index 90% rename from internal/convert/convert_test.go rename to internal/build/convert_test.go index 68c1134..49a9691 100644 --- a/internal/convert/convert_test.go +++ b/internal/build/convert_test.go @@ -1,11 +1,11 @@ -package convert_test +package build_test import ( "os" "path/filepath" "testing" - "github.com/ficcdaf/zona/internal/convert" + "github.com/ficcdaf/zona/internal/build" "github.com/ficcdaf/zona/internal/util" ) @@ -13,7 +13,7 @@ func TestMdToHTML(t *testing.T) { md := []byte("# Hello World\n\nThis is a test.") expectedHTML := "

Hello World

\n

This is a test.

\n" nExpectedHTML := util.NormalizeContent(expectedHTML) - html, err := convert.MdToHTML(md) + html, err := build.MdToHTML(md) nHtml := util.NormalizeContent(string(html)) if err != nil { t.Fatalf("Expected no error, got %v", err) @@ -27,7 +27,7 @@ func TestWriteFile(t *testing.T) { path := filepath.Join(t.TempDir(), "test.txt") content := []byte("Hello, World!") - err := convert.WriteFile(content, path) + err := build.WriteFile(content, path) if err != nil { t.Fatalf("Expected no error, got %v", err) } @@ -51,7 +51,7 @@ func TestReadFile(t *testing.T) { t.Fatalf("Error writing file: %v", err) } - data, err := convert.ReadFile(path) + data, err := build.ReadFile(path) if err != nil { t.Fatalf("Expected no error, got %v", err) } @@ -70,7 +70,7 @@ func TestCopyFile(t *testing.T) { t.Fatalf("Error writing source file: %v", err) } - err = convert.CopyFile(src, dst) + err = build.CopyFile(src, dst) if err != nil { t.Fatalf("Expected no error, got %v", err) } @@ -96,7 +96,7 @@ func TestConvertFile(t *testing.T) { t.Fatalf("Error writing source Markdown file: %v", err) } - err = convert.ConvertFile(src, dst) + err = build.ConvertFile(src, dst) if err != nil { t.Fatalf("Expected no error, got %v", err) } @@ -113,7 +113,7 @@ func TestConvertFile(t *testing.T) { func TestChangeExtension(t *testing.T) { input := "test.md" - output := convert.ChangeExtension(input, ".html") + output := build.ChangeExtension(input, ".html") expected := "test.html" if output != expected { diff --git a/internal/build/traverse.go b/internal/build/traverse.go new file mode 100644 index 0000000..f0bc090 --- /dev/null +++ b/internal/build/traverse.go @@ -0,0 +1,55 @@ +package build + +import ( + "errors" + "fmt" + "io/fs" + "path/filepath" + + "github.com/ficcdaf/zona/internal/util" +) + +func processFile(inPath string, entry fs.DirEntry, err error, outRoot string) error { + if err != nil { + return err + } + if !entry.IsDir() { + ext := filepath.Ext(inPath) + outPath := util.ReplaceRoot(inPath, outRoot) + switch ext { + case ".md": + fmt.Println("Processing markdown...") + outPath = ChangeExtension(outPath, ".html") + if err := util.CreateParents(outPath); err != nil { + return err + } + if err := ConvertFile(inPath, outPath); err != nil { + return errors.Join(errors.New("Error processing file "+inPath), err) + } else { + return nil + } + // If it's not a file we need to process, + // we simply copy it to the destination path. + default: + if err := util.CreateParents(outPath); err != nil { + return err + } + if err := CopyFile(inPath, outPath); err != nil { + return errors.Join(errors.New("Error processing file "+inPath), err) + } else { + return nil + } + } + } + // fmt.Printf("Visited: %s\n", inPath) + return nil +} + +func Traverse(root string, outRoot string) error { + // err := filepath.WalkDir(root, func(path string, entry fs.DirEntry, err error) error { + walkFunc := func(path string, entry fs.DirEntry, err error) error { + return processFile(path, entry, err, outRoot) + } + err := filepath.WalkDir(root, walkFunc) + return err +}