From a42cf66cee55a427347da5193fedde1cce0494ba Mon Sep 17 00:00:00 2001 From: Daniel Fichtinger Date: Sun, 24 Nov 2024 15:42:11 -0500 Subject: [PATCH] Implemented copying site directory and processing certain filetypes --- internal/convert/convert.go | 20 ++++++++++++++++++++ internal/util/path.go | 31 +++++++++++++++++++++++++------ 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/internal/convert/convert.go b/internal/convert/convert.go index f6222ff..fe8e8b3 100644 --- a/internal/convert/convert.go +++ b/internal/convert/convert.go @@ -3,6 +3,8 @@ package convert import ( "io" "os" + "path/filepath" + "strings" "github.com/gomarkdown/markdown" "github.com/gomarkdown/markdown/html" @@ -62,6 +64,20 @@ func ReadFile(p string) ([]byte, error) { return result, nil } +// CopyFile reads the file at the input path, and write +// it to the output path. +func CopyFile(inPath string, outPath string) error { + inB, err := ReadFile(inPath) + if err != nil { + return err + } + if err := WriteFile(inB, outPath); err != nil { + return err + } else { + return nil + } +} + func ConvertFile(in string, out string) error { md, err := ReadFile(in) if err != nil { @@ -74,3 +90,7 @@ func ConvertFile(in string, out string) error { err = WriteFile(html, out) return err } + +func ChangeExtension(in string, outExt string) string { + return strings.TrimSuffix(in, filepath.Ext(in)) + outExt +} diff --git a/internal/util/path.go b/internal/util/path.go index 7013734..2cacbf8 100644 --- a/internal/util/path.go +++ b/internal/util/path.go @@ -8,6 +8,8 @@ import ( "os" "path/filepath" "strings" + + "github.com/ficcdaf/zona/internal/convert" ) // CheckExtension checks if the file located at path (string) @@ -51,17 +53,15 @@ func replaceRoot(inPath, outRoot string) string { return outPath } -func createFileWithParents(path string) error { +func createParents(path string) error { dir := filepath.Dir(path) // Check if the parent directory already exists // before trying to create it if _, dirErr := os.Stat(dir); os.IsNotExist(dirErr) { // create directories - err := os.MkdirAll(dir, os.ModePerm) - if err != nil { + if err := os.MkdirAll(dir, os.ModePerm); err != nil { return err } - // TODO: write the file here } return nil } @@ -72,12 +72,31 @@ func processFile(inPath string, entry fs.DirEntry, err error, outRoot string) er } if !entry.IsDir() { ext := filepath.Ext(inPath) - fmt.Println("Root: ", replaceRoot(inPath, outRoot)) + outPath := replaceRoot(inPath, outRoot) + fmt.Println("NewRoot: ", outPath) switch ext { case ".md": fmt.Println("Processing markdown...") + outPath = convert.ChangeExtension(outPath, ".html") + if err := createParents(outPath); err != nil { + return err + } + if err := convert.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: - // All other file types, we copy! + if err := createParents(outPath); err != nil { + return err + } + if err := convert.CopyFile(inPath, outPath); err != nil { + return errors.Join(errors.New("Error processing file "+inPath), err) + } else { + return nil + } } } fmt.Printf("Visited: %s\n", inPath)