refactored some modules

This commit is contained in:
Daniel Fichtinger 2024-11-24 21:14:38 -05:00
parent 68d2ddb692
commit 11f724732d
4 changed files with 102 additions and 69 deletions

View file

@ -1,12 +1,15 @@
package convert
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"
@ -134,3 +137,48 @@ 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
}