|
|
|
|
@ -1,20 +1,20 @@ |
|
|
|
|
package main |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"log" |
|
|
|
|
"fmt" |
|
|
|
|
"bytes" |
|
|
|
|
"strings" |
|
|
|
|
"io/fs" |
|
|
|
|
"flag" |
|
|
|
|
"fmt" |
|
|
|
|
"github.com/fsnotify/fsnotify" |
|
|
|
|
"github.com/yuin/goldmark" |
|
|
|
|
"io" |
|
|
|
|
"path/filepath" |
|
|
|
|
"io/fs" |
|
|
|
|
"lcthw.dev/go/ssgod/config" |
|
|
|
|
"log" |
|
|
|
|
"os" |
|
|
|
|
"flag" |
|
|
|
|
"time" |
|
|
|
|
"path/filepath" |
|
|
|
|
"strings" |
|
|
|
|
"text/template" |
|
|
|
|
"lcthw.dev/go/ssgod/config" |
|
|
|
|
"github.com/yuin/goldmark" |
|
|
|
|
"github.com/fsnotify/fsnotify" |
|
|
|
|
"time" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
var DEFAULT_CONFIG = ` |
|
|
|
|
@ -37,7 +37,7 @@ func Fail(err error, format string, v ...any) error { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func RenderTemplate(out io.Writer, embed string, variables any) (error) { |
|
|
|
|
func RenderTemplate(out io.Writer, embed string, variables any) error { |
|
|
|
|
layout_path := config.Settings.Layout |
|
|
|
|
|
|
|
|
|
layout_main, err := os.ReadFile(layout_path) |
|
|
|
|
@ -54,7 +54,9 @@ func RenderTemplate(out io.Writer, embed string, variables any) (error) { |
|
|
|
|
tmpl.Funcs(callbacks) |
|
|
|
|
|
|
|
|
|
tmpl, err = tmpl.Parse(string(layout_main)) |
|
|
|
|
if err != nil { return Fail(err, "can't parse %s", layout_path) } |
|
|
|
|
if err != nil { |
|
|
|
|
return Fail(err, "can't parse %s", layout_path) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err = tmpl.Execute(out, variables) |
|
|
|
|
|
|
|
|
|
@ -67,20 +69,26 @@ func RenderMarkdown(path string, target_path string, page_id string) error { |
|
|
|
|
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) } |
|
|
|
|
if err != nil { |
|
|
|
|
return Fail(err, "writing file %s", target_path) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
input_data, err := os.ReadFile(path) |
|
|
|
|
var md_out bytes.Buffer |
|
|
|
|
|
|
|
|
|
err = goldmark.Convert(input_data, &md_out) |
|
|
|
|
if err != nil { return Fail(err, "failed converting markdown %s", path) } |
|
|
|
|
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) } |
|
|
|
|
if err != nil { |
|
|
|
|
return Fail(err, "failed to render template %s->%s", path, target_path) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return err; |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func RenderHTML(source_path string, target_path string, page_id string) error { |
|
|
|
|
@ -90,14 +98,18 @@ func RenderHTML(source_path string, target_path string, page_id string) error { |
|
|
|
|
defer out.Close() |
|
|
|
|
|
|
|
|
|
html_content, err := os.ReadFile(source_path) |
|
|
|
|
if err != nil { return Fail(err, "cannot open html input %s", 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) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return err; |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func MkdirPath(target_path string) error { |
|
|
|
|
@ -108,10 +120,12 @@ func MkdirPath(target_path string) error { |
|
|
|
|
log.Println("MAKING: ", target_dir) |
|
|
|
|
err = os.MkdirAll(target_dir, 0750) |
|
|
|
|
|
|
|
|
|
if err != nil { return Fail(err, "making path to %s", target_dir); } |
|
|
|
|
if err != nil { |
|
|
|
|
return Fail(err, "making path to %s", target_dir) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return nil; |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func SplitPathExt(path string) (string, string, bool) { |
|
|
|
|
@ -138,7 +152,9 @@ func ProcessDirEntry(path string, d fs.DirEntry, err error) error { |
|
|
|
|
settings := config.Settings |
|
|
|
|
|
|
|
|
|
if !d.IsDir() && !SamePath(path, config.Settings.Layout) { |
|
|
|
|
if err != nil { return Fail(err, "path: %s", path); } |
|
|
|
|
if err != nil { |
|
|
|
|
return Fail(err, "path: %s", path) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
source_name, ext, found := SplitPathExt(path) |
|
|
|
|
|
|
|
|
|
@ -146,7 +162,9 @@ func ProcessDirEntry(path string, d fs.DirEntry, err error) error { |
|
|
|
|
target_path := RePrefixPath(path, settings.Target) |
|
|
|
|
|
|
|
|
|
err = MkdirPath(target_path) |
|
|
|
|
if err != nil { return Fail(err, "making target path: %s", 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" |
|
|
|
|
@ -154,7 +172,9 @@ func ProcessDirEntry(path string, d fs.DirEntry, err error) error { |
|
|
|
|
if ext == ".html" { |
|
|
|
|
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" { |
|
|
|
|
// need to strip the .md and replace with .html
|
|
|
|
|
html_name, _ := strings.CutSuffix(target_path, ext) |
|
|
|
|
@ -162,7 +182,9 @@ func ProcessDirEntry(path string, d fs.DirEntry, err error) error { |
|
|
|
|
|
|
|
|
|
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) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -173,7 +195,9 @@ func ProcessDirEntry(path string, d fs.DirEntry, err error) error { |
|
|
|
|
func SyncStaticDir() { |
|
|
|
|
target := config.Settings.Target |
|
|
|
|
sync_dir := config.Settings.SyncDir |
|
|
|
|
if sync_dir == "" { return } |
|
|
|
|
if sync_dir == "" { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
log.Printf("removing target directory: %s", target) |
|
|
|
|
|
|
|
|
|
@ -202,7 +226,9 @@ func RenderPages() { |
|
|
|
|
return ProcessDirEntry(path, d, err) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
if err != nil { log.Fatalf("can't walk content") } |
|
|
|
|
if err != nil { |
|
|
|
|
log.Fatalf("can't walk content") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func WatchMatches(name string) bool { |
|
|
|
|
@ -213,28 +239,36 @@ func WatchMatches(name string) bool { |
|
|
|
|
func AddWatchDir(watcher *fsnotify.Watcher, name string) error { |
|
|
|
|
return filepath.WalkDir(name, |
|
|
|
|
func(path string, d fs.DirEntry, err error) error { |
|
|
|
|
if err != nil { log.Fatalf("WATCH ERROR! path=%s: err=%v", path, err) } |
|
|
|
|
if err != nil { |
|
|
|
|
log.Printf("WATCH ERROR! walking=%s path=%s: err=%v", name, path, err) |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if d.IsDir() { |
|
|
|
|
log.Println("WATCHING: ", path) |
|
|
|
|
err = watcher.Add(path) |
|
|
|
|
if err != nil { return Fail(err, "failed to watch %s", path) } |
|
|
|
|
if err != nil { |
|
|
|
|
log.Printf("failed to watch %s", path) |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func WatchDir() { |
|
|
|
|
watcher, err := fsnotify.NewWatcher() |
|
|
|
|
if err != nil { log.Fatal(err) } |
|
|
|
|
if err != nil { |
|
|
|
|
log.Fatal(err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
defer watcher.Close() |
|
|
|
|
|
|
|
|
|
wait_time, err := time.ParseDuration(config.Settings.WatchDelay) |
|
|
|
|
if err != nil { log.Fatal(err) } |
|
|
|
|
if err != nil { |
|
|
|
|
log.Fatal(err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
doit := time.NewTimer(wait_time) |
|
|
|
|
doit.Stop() |
|
|
|
|
@ -243,8 +277,16 @@ func WatchDir() { |
|
|
|
|
for { |
|
|
|
|
select { |
|
|
|
|
case event, ok := <-watcher.Events: |
|
|
|
|
if !ok { return } |
|
|
|
|
if !ok { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if event.Has(fsnotify.Create) { |
|
|
|
|
log.Println("---> CREATE IS:", event.Name) |
|
|
|
|
AddWatchDir(watcher, event.Name) |
|
|
|
|
} else { |
|
|
|
|
log.Println("event: ", event) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if WatchMatches(event.Name) { |
|
|
|
|
log.Println("modified file: ", event.Name) |
|
|
|
|
@ -254,7 +296,9 @@ func WatchDir() { |
|
|
|
|
SyncStaticDir() |
|
|
|
|
RenderPages() |
|
|
|
|
case err, ok := <-watcher.Errors: |
|
|
|
|
if !ok { return } |
|
|
|
|
if !ok { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
log.Println("error: ", err) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -270,10 +314,12 @@ func WatchDir() { |
|
|
|
|
Fatal(err, "failed to watch %s", filepath.Dir(config.Settings.Layout)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if config.Settings.SyncDir != "" { |
|
|
|
|
err = AddWatchDir(watcher, config.Settings.SyncDir) |
|
|
|
|
if err != nil { |
|
|
|
|
Fatal(err, "failed to watch %s", config.Settings.SyncDir) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
<-make(chan struct{}) |
|
|
|
|
} |
|
|
|
|
@ -283,12 +329,14 @@ func InitConfig(config_file string) { |
|
|
|
|
|
|
|
|
|
if os.IsNotExist(err) { |
|
|
|
|
out, err := os.OpenFile(config_file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) |
|
|
|
|
if err != nil { log.Fatalf("error opening %s", config_file) } |
|
|
|
|
if err != nil { |
|
|
|
|
log.Fatalf("error opening %s", config_file) |
|
|
|
|
} |
|
|
|
|
defer out.Close() |
|
|
|
|
|
|
|
|
|
out.WriteString(DEFAULT_CONFIG) |
|
|
|
|
} else { |
|
|
|
|
log.Fatalf("there's already a %s file here", config_file); |
|
|
|
|
log.Fatalf("there's already a %s file here", config_file) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -302,14 +350,14 @@ func main() { |
|
|
|
|
switch command { |
|
|
|
|
case "watch": |
|
|
|
|
config.Load(config_file) |
|
|
|
|
SyncStaticDir(); |
|
|
|
|
RenderPages(); |
|
|
|
|
SyncStaticDir() |
|
|
|
|
RenderPages() |
|
|
|
|
WatchDir() |
|
|
|
|
case "init": |
|
|
|
|
InitConfig(config_file) |
|
|
|
|
default: |
|
|
|
|
config.Load(config_file) |
|
|
|
|
SyncStaticDir(); |
|
|
|
|
RenderPages(); |
|
|
|
|
SyncStaticDir() |
|
|
|
|
RenderPages() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|