|
|
|
@ -10,6 +10,7 @@ import ( |
|
|
|
|
"github.com/gofiber/fiber/v2" |
|
|
|
|
"github.com/gofiber/template/html/v2" |
|
|
|
|
"zedshaw.games/ssgod/config" |
|
|
|
|
"github.com/yuin/goldmark" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
func Fail(err error, format string, v ...any) error { |
|
|
|
@ -34,9 +35,9 @@ func ProcessDirEntry(engine *html.Engine, path string, d fs.DirEntry, err error) |
|
|
|
|
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) |
|
|
|
|
template_name, found := strings.CutSuffix(source_name, ext) |
|
|
|
|
source_name, found := strings.CutSuffix(source_name, ext) |
|
|
|
|
|
|
|
|
|
if found && ext == ".html" && template_name != settings.Layout { |
|
|
|
|
if found && source_name != settings.Layout { |
|
|
|
|
prefixed_path := append([]string{settings.Target}, split_path...) |
|
|
|
|
|
|
|
|
|
target_path := filepath.Join(prefixed_path...) |
|
|
|
@ -48,19 +49,36 @@ func ProcessDirEntry(engine *html.Engine, path string, d fs.DirEntry, err error) |
|
|
|
|
os.MkdirAll(target_dir, 0750) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// TODO: compare time stamps and skip if not newer
|
|
|
|
|
// 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) |
|
|
|
|
if err != nil { return Fail(err, "writing file %s", target_path) } |
|
|
|
|
defer out.Close() |
|
|
|
|
|
|
|
|
|
// generate a data-testid for all pages based on template name
|
|
|
|
|
page_id := strings.ReplaceAll(template_name, "/", "-") + "-page" |
|
|
|
|
if err != nil { return Fail(err, "writing file %s", target_path) } |
|
|
|
|
|
|
|
|
|
err = engine.Render(out, template_name, fiber.Map{"PageId": page_id}, settings.Layout) |
|
|
|
|
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", template_name, target_path) |
|
|
|
|
out.Close() |
|
|
|
|
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) |
|
|
|
|
|
|
|
|
|
if err != nil { return Fail(err, "failed to render markdown %s", path) } |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -81,8 +99,5 @@ func RenderPages() { |
|
|
|
|
|
|
|
|
|
func main() { |
|
|
|
|
config.Load("ssgod.toml") |
|
|
|
|
log.Printf("views=%s, layouts=%s, target=%s", |
|
|
|
|
config.Settings.Views, config.Settings.Layout, config.Settings.Target) |
|
|
|
|
|
|
|
|
|
RenderPages() |
|
|
|
|
} |
|
|
|
|