diff --git a/config/loader.go b/config/loader.go index 085df14..a614c1d 100644 --- a/config/loader.go +++ b/config/loader.go @@ -9,6 +9,7 @@ type config struct { Views string `toml:"views"` Layout string `toml:"layout"` Target string `toml:"target"` + WatchDelay string `toml:"watch_delay"` } var Settings config diff --git a/example/pages/test.md b/example/pages/test.md index 20da2c7..1a7a31c 100644 --- a/example/pages/test.md +++ b/example/pages/test.md @@ -11,4 +11,3 @@ Hi, I am a test file. * sdf * sa * fs -* a diff --git a/example/ssgod.toml b/example/ssgod.toml index e316621..875dd8e 100644 --- a/example/ssgod.toml +++ b/example/ssgod.toml @@ -1,3 +1,4 @@ views = "pages" layout = "pages/layouts/main.html" target = "public" +watch_delay = "1s" diff --git a/main.go b/main.go index 262cee8..5e1c59c 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( "path/filepath" "os" "flag" + "time" "text/template" "zedshaw.games/ssgod/config" "github.com/yuin/goldmark" @@ -160,21 +161,37 @@ func RenderPages() { if err != nil { log.Fatalf("can't walk content") } } +func WatchMatches(name string) bool { + return filepath.Ext(name) == ".html" || filepath.Ext(name) == ".md" +} + func WatchDir() { watcher, err := fsnotify.NewWatcher() if err != nil { log.Fatal(err) } + defer watcher.Close() + wait_time, err := time.ParseDuration(config.Settings.WatchDelay) + if err != nil { log.Fatal(err) } + + doit := time.NewTimer(wait_time) + doit.Stop() + + log.Println("WATCHING directory", config.Settings.Views) + go func() { for { select { case event, ok := <-watcher.Events: if !ok { return } log.Println("event: ", event) - if event.Has(fsnotify.Write) { + + if WatchMatches(event.Name) { log.Println("modified file: ", event.Name) - RenderPages() + doit.Reset(wait_time) } + case <-doit.C: + RenderPages() case err, ok := <-watcher.Errors: if !ok { return } log.Println("error: ", err)