Implemented copying site directory and processing certain filetypes
This commit is contained in:
parent
e68611afb1
commit
a42cf66cee
2 changed files with 45 additions and 6 deletions
|
@ -3,6 +3,8 @@ package convert
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/gomarkdown/markdown"
|
"github.com/gomarkdown/markdown"
|
||||||
"github.com/gomarkdown/markdown/html"
|
"github.com/gomarkdown/markdown/html"
|
||||||
|
@ -62,6 +64,20 @@ func ReadFile(p string) ([]byte, error) {
|
||||||
return result, nil
|
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 {
|
func ConvertFile(in string, out string) error {
|
||||||
md, err := ReadFile(in)
|
md, err := ReadFile(in)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -74,3 +90,7 @@ func ConvertFile(in string, out string) error {
|
||||||
err = WriteFile(html, out)
|
err = WriteFile(html, out)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ChangeExtension(in string, outExt string) string {
|
||||||
|
return strings.TrimSuffix(in, filepath.Ext(in)) + outExt
|
||||||
|
}
|
||||||
|
|
|
@ -8,6 +8,8 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/ficcdaf/zona/internal/convert"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CheckExtension checks if the file located at path (string)
|
// CheckExtension checks if the file located at path (string)
|
||||||
|
@ -51,17 +53,15 @@ func replaceRoot(inPath, outRoot string) string {
|
||||||
return outPath
|
return outPath
|
||||||
}
|
}
|
||||||
|
|
||||||
func createFileWithParents(path string) error {
|
func createParents(path string) error {
|
||||||
dir := filepath.Dir(path)
|
dir := filepath.Dir(path)
|
||||||
// Check if the parent directory already exists
|
// Check if the parent directory already exists
|
||||||
// before trying to create it
|
// before trying to create it
|
||||||
if _, dirErr := os.Stat(dir); os.IsNotExist(dirErr) {
|
if _, dirErr := os.Stat(dir); os.IsNotExist(dirErr) {
|
||||||
// create directories
|
// create directories
|
||||||
err := os.MkdirAll(dir, os.ModePerm)
|
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// TODO: write the file here
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -72,12 +72,31 @@ func processFile(inPath string, entry fs.DirEntry, err error, outRoot string) er
|
||||||
}
|
}
|
||||||
if !entry.IsDir() {
|
if !entry.IsDir() {
|
||||||
ext := filepath.Ext(inPath)
|
ext := filepath.Ext(inPath)
|
||||||
fmt.Println("Root: ", replaceRoot(inPath, outRoot))
|
outPath := replaceRoot(inPath, outRoot)
|
||||||
|
fmt.Println("NewRoot: ", outPath)
|
||||||
switch ext {
|
switch ext {
|
||||||
case ".md":
|
case ".md":
|
||||||
fmt.Println("Processing markdown...")
|
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:
|
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)
|
fmt.Printf("Visited: %s\n", inPath)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue