|
|
|
@ -19,6 +19,65 @@ func Fail(err error, format string, v ...any) error { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func RenderMarkdown(target_path string, ext string, path string) error { |
|
|
|
|
// need to strip the .md and replace with .html
|
|
|
|
|
html_name, _ := strings.CutSuffix(target_path, ext) |
|
|
|
|
html_name = fmt.Sprintf("%s.html", html_name) |
|
|
|
|
|
|
|
|
|
log.Printf("MARKDOWN: %s -> %s", path, html_name) |
|
|
|
|
|
|
|
|
|
out, err := os.OpenFile(html_name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) |
|
|
|
|
defer out.Close() |
|
|
|
|
|
|
|
|
|
if err != nil { return Fail(err, "writing file %s", target_path) } |
|
|
|
|
|
|
|
|
|
input_data, err := os.ReadFile(path) |
|
|
|
|
err = goldmark.Convert(input_data, out) |
|
|
|
|
|
|
|
|
|
return err; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func RenderHTML(engine *html.Engine, source_name string, target_path string, page_id string) error { |
|
|
|
|
log.Printf("RENDER: %s -> %s", source_name, target_path) |
|
|
|
|
|
|
|
|
|
out, err := os.OpenFile(target_path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) |
|
|
|
|
defer out.Close() |
|
|
|
|
|
|
|
|
|
if err != nil { return Fail(err, "writing file %s", target_path) } |
|
|
|
|
|
|
|
|
|
err = engine.Render(out, source_name, fiber.Map{"PageId": page_id}, config.Settings.Layout) |
|
|
|
|
return err; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func MkdirPath(target_path string) error { |
|
|
|
|
target_dir := filepath.Dir(target_path) |
|
|
|
|
_, err := os.Stat(target_dir) |
|
|
|
|
|
|
|
|
|
if os.IsNotExist(err) { |
|
|
|
|
log.Println("MAKING: ", target_dir) |
|
|
|
|
err = os.MkdirAll(target_dir, 0750) |
|
|
|
|
|
|
|
|
|
if err != nil { return Fail(err, "making path to %s", target_dir); } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return nil; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func SplitPathExt(path string) (string, string, bool) { |
|
|
|
|
split_path := strings.Split(path, string(os.PathSeparator))[1:] |
|
|
|
|
source_name := strings.Join(split_path, "/") // Render wants / even on windows
|
|
|
|
|
|
|
|
|
|
ext := filepath.Ext(source_name) |
|
|
|
|
source_name, found := strings.CutSuffix(source_name, ext) |
|
|
|
|
return source_name, ext, found |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func RePrefixPath(path string, new_prefix string) string { |
|
|
|
|
split_path := strings.Split(path, string(os.PathSeparator))[1:] |
|
|
|
|
|
|
|
|
|
prefixed_path := append([]string{new_prefix}, split_path...) |
|
|
|
|
return filepath.Join(prefixed_path...) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func ProcessDirEntry(engine *html.Engine, path string, d fs.DirEntry, err error) error { |
|
|
|
|
settings := config.Settings |
|
|
|
@ -26,56 +85,23 @@ func ProcessDirEntry(engine *html.Engine, path string, d fs.DirEntry, err error) |
|
|
|
|
if !d.IsDir() { |
|
|
|
|
if err != nil { return Fail(err, "path: %s", path); } |
|
|
|
|
|
|
|
|
|
dir := filepath.Dir(path) |
|
|
|
|
err = os.MkdirAll(dir, 0750) |
|
|
|
|
if err != nil { |
|
|
|
|
return Fail(err, "making dir %s", dir); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
split_path := strings.Split(path, string(os.PathSeparator))[1:] |
|
|
|
|
source_name := strings.Join(split_path, "/") // Render wants / even on windows
|
|
|
|
|
ext := filepath.Ext(source_name) |
|
|
|
|
source_name, found := strings.CutSuffix(source_name, ext) |
|
|
|
|
source_name, ext, found := SplitPathExt(path) |
|
|
|
|
|
|
|
|
|
if found && source_name != settings.Layout { |
|
|
|
|
prefixed_path := append([]string{settings.Target}, split_path...) |
|
|
|
|
target_path := RePrefixPath(path, settings.Target) |
|
|
|
|
|
|
|
|
|
target_path := filepath.Join(prefixed_path...) |
|
|
|
|
_, err := os.Stat(target_path) |
|
|
|
|
|
|
|
|
|
if os.IsNotExist(err) { |
|
|
|
|
target_dir := filepath.Dir(target_path) |
|
|
|
|
log.Println("MAKING: ", target_dir) |
|
|
|
|
os.MkdirAll(target_dir, 0750) |
|
|
|
|
} |
|
|
|
|
err = MkdirPath(target_path) |
|
|
|
|
if err != nil { return Fail(err, "making target path: %s", target_path) } |
|
|
|
|
|
|
|
|
|
// generate a data-testid for all pages based on template name
|
|
|
|
|
page_id := strings.ReplaceAll(source_name, "/", "-") + "-page" |
|
|
|
|
|
|
|
|
|
if ext == ".html" { |
|
|
|
|
out, err := os.OpenFile(target_path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) |
|
|
|
|
defer out.Close() |
|
|
|
|
err = RenderHTML(engine, source_name, target_path, page_id) |
|
|
|
|
|
|
|
|
|
if err != nil { return Fail(err, "writing file %s", target_path) } |
|
|
|
|
|
|
|
|
|
err = engine.Render(out, source_name, fiber.Map{"PageId": page_id}, settings.Layout) |
|
|
|
|
if err != nil { return Fail(err, "failed to render %s", path) } |
|
|
|
|
|
|
|
|
|
log.Printf("RENDER: %s -> %s", source_name, target_path) |
|
|
|
|
} else if ext == ".md" { |
|
|
|
|
// need to strip the .md and replace with .html
|
|
|
|
|
html_name, _ := strings.CutSuffix(target_path, ext) |
|
|
|
|
html_name = fmt.Sprintf("%s.html", html_name) |
|
|
|
|
|
|
|
|
|
log.Printf("MARKDOWN: %s -> %s", path, html_name) |
|
|
|
|
|
|
|
|
|
out, err := os.OpenFile(html_name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) |
|
|
|
|
defer out.Close() |
|
|
|
|
|
|
|
|
|
if err != nil { return Fail(err, "writing file %s", target_path) } |
|
|
|
|
|
|
|
|
|
input_data, err := os.ReadFile(path) |
|
|
|
|
err = goldmark.Convert(input_data, out) |
|
|
|
|
RenderMarkdown(target_path, ext, path) |
|
|
|
|
|
|
|
|
|
if err != nil { return Fail(err, "failed to render markdown %s", path) } |
|
|
|
|
} |
|
|
|
|