|
|
@ -3,12 +3,13 @@ package main |
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"log" |
|
|
|
"log" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
|
|
|
|
"bytes" |
|
|
|
"strings" |
|
|
|
"strings" |
|
|
|
"io/fs" |
|
|
|
"io/fs" |
|
|
|
|
|
|
|
"io" |
|
|
|
"path/filepath" |
|
|
|
"path/filepath" |
|
|
|
"os" |
|
|
|
"os" |
|
|
|
"github.com/gofiber/fiber/v2" |
|
|
|
"text/template" |
|
|
|
"github.com/gofiber/template/html/v2" |
|
|
|
|
|
|
|
"zedshaw.games/ssgod/config" |
|
|
|
"zedshaw.games/ssgod/config" |
|
|
|
"github.com/yuin/goldmark" |
|
|
|
"github.com/yuin/goldmark" |
|
|
|
) |
|
|
|
) |
|
|
@ -19,33 +20,66 @@ func Fail(err error, format string, v ...any) error { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func RenderMarkdown(target_path string, ext string, path string) error { |
|
|
|
func RenderTemplate(out io.Writer, embed string, variables any) (error) { |
|
|
|
// need to strip the .md and replace with .html
|
|
|
|
layout_path := config.Settings.Layout |
|
|
|
html_name, _ := strings.CutSuffix(target_path, ext) |
|
|
|
|
|
|
|
html_name = fmt.Sprintf("%s.html", html_name) |
|
|
|
layout_main, err := os.ReadFile(layout_path) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return Fail(err, "can't read your layout file: %s", layout_path) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tmpl := template.New(layout_path) |
|
|
|
|
|
|
|
|
|
|
|
log.Printf("MARKDOWN: %s -> %s", path, html_name) |
|
|
|
callbacks := template.FuncMap{ |
|
|
|
|
|
|
|
"embed": func() string { return embed }, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
tmpl.Funcs(callbacks) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tmpl, err = tmpl.Parse(string(layout_main)) |
|
|
|
|
|
|
|
if err != nil { return Fail(err, "can't parse %s", layout_path) } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = tmpl.Execute(out, variables) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
out, err := os.OpenFile(html_name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) |
|
|
|
func RenderMarkdown(path string, target_path string, page_id string) error { |
|
|
|
|
|
|
|
log.Printf("MARKDOWN: %s -> %s", path, target_path) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
out, err := os.OpenFile(target_path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) |
|
|
|
defer out.Close() |
|
|
|
defer out.Close() |
|
|
|
|
|
|
|
|
|
|
|
if err != nil { return Fail(err, "writing file %s", target_path) } |
|
|
|
if err != nil { return Fail(err, "writing file %s", target_path) } |
|
|
|
|
|
|
|
|
|
|
|
input_data, err := os.ReadFile(path) |
|
|
|
input_data, err := os.ReadFile(path) |
|
|
|
err = goldmark.Convert(input_data, out) |
|
|
|
var md_out bytes.Buffer |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = goldmark.Convert(input_data, &md_out) |
|
|
|
|
|
|
|
if err != nil { return Fail(err, "failed converting markdown %s", path) } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = RenderTemplate(out, md_out.String(), |
|
|
|
|
|
|
|
map[string]string{"PageId": page_id}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if err != nil { return Fail(err, "failed to render template %s->%s", path, target_path) } |
|
|
|
|
|
|
|
|
|
|
|
return err; |
|
|
|
return err; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func RenderHTML(engine *html.Engine, source_name string, target_path string, page_id string) error { |
|
|
|
func RenderHTML(source_path string, target_path string, page_id string) error { |
|
|
|
log.Printf("RENDER: %s -> %s", source_name, target_path) |
|
|
|
log.Printf("RENDER: %s -> %s", source_path, target_path) |
|
|
|
|
|
|
|
|
|
|
|
out, err := os.OpenFile(target_path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) |
|
|
|
out, err := os.OpenFile(target_path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) |
|
|
|
defer out.Close() |
|
|
|
defer out.Close() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
html_content, err := os.ReadFile(source_path) |
|
|
|
|
|
|
|
if err != nil { return Fail(err, "cannot open html input %s", source_path) } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = RenderTemplate(out, string(html_content), |
|
|
|
|
|
|
|
map[string]string{"PageId": page_id}) |
|
|
|
|
|
|
|
|
|
|
|
if err != nil { return Fail(err, "writing file %s", target_path) } |
|
|
|
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; |
|
|
|
return err; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -79,7 +113,7 @@ func RePrefixPath(path string, new_prefix string) string { |
|
|
|
return filepath.Join(prefixed_path...) |
|
|
|
return filepath.Join(prefixed_path...) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func ProcessDirEntry(engine *html.Engine, path string, d fs.DirEntry, err error) error { |
|
|
|
func ProcessDirEntry(path string, d fs.DirEntry, err error) error { |
|
|
|
settings := config.Settings |
|
|
|
settings := config.Settings |
|
|
|
|
|
|
|
|
|
|
|
if !d.IsDir() { |
|
|
|
if !d.IsDir() { |
|
|
@ -87,7 +121,7 @@ func ProcessDirEntry(engine *html.Engine, path string, d fs.DirEntry, err error) |
|
|
|
|
|
|
|
|
|
|
|
source_name, ext, found := SplitPathExt(path) |
|
|
|
source_name, ext, found := SplitPathExt(path) |
|
|
|
|
|
|
|
|
|
|
|
if found && source_name != settings.Layout { |
|
|
|
if found && path != settings.Layout { |
|
|
|
target_path := RePrefixPath(path, settings.Target) |
|
|
|
target_path := RePrefixPath(path, settings.Target) |
|
|
|
|
|
|
|
|
|
|
|
err = MkdirPath(target_path) |
|
|
|
err = MkdirPath(target_path) |
|
|
@ -97,11 +131,15 @@ func ProcessDirEntry(engine *html.Engine, path string, d fs.DirEntry, err error) |
|
|
|
page_id := strings.ReplaceAll(source_name, "/", "-") + "-page" |
|
|
|
page_id := strings.ReplaceAll(source_name, "/", "-") + "-page" |
|
|
|
|
|
|
|
|
|
|
|
if ext == ".html" { |
|
|
|
if ext == ".html" { |
|
|
|
err = RenderHTML(engine, source_name, target_path, page_id) |
|
|
|
err = RenderHTML(path, target_path, page_id) |
|
|
|
|
|
|
|
|
|
|
|
if err != nil { return Fail(err, "failed to render %s", path) } |
|
|
|
if err != nil { return Fail(err, "failed to render %s", path) } |
|
|
|
} else if ext == ".md" { |
|
|
|
} else if ext == ".md" { |
|
|
|
RenderMarkdown(target_path, ext, path) |
|
|
|
// need to strip the .md and replace with .html
|
|
|
|
|
|
|
|
html_name, _ := strings.CutSuffix(target_path, ext) |
|
|
|
|
|
|
|
html_name = fmt.Sprintf("%s.html", html_name) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RenderMarkdown(path, html_name, page_id) |
|
|
|
|
|
|
|
|
|
|
|
if err != nil { return Fail(err, "failed to render markdown %s", path) } |
|
|
|
if err != nil { return Fail(err, "failed to render markdown %s", path) } |
|
|
|
} |
|
|
|
} |
|
|
@ -112,12 +150,9 @@ func ProcessDirEntry(engine *html.Engine, path string, d fs.DirEntry, err error) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func RenderPages() { |
|
|
|
func RenderPages() { |
|
|
|
engine := html.New(config.Settings.Views, ".html") |
|
|
|
|
|
|
|
engine.Load() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err := filepath.WalkDir(config.Settings.Views, |
|
|
|
err := filepath.WalkDir(config.Settings.Views, |
|
|
|
func (path string, d fs.DirEntry, err error) error { |
|
|
|
func (path string, d fs.DirEntry, err error) error { |
|
|
|
return ProcessDirEntry(engine, path, d, err) |
|
|
|
return ProcessDirEntry(path, d, err) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
if err != nil { log.Fatalf("can't walk content") } |
|
|
|
if err != nil { log.Fatalf("can't walk content") } |
|
|
|