aboutsummaryrefslogtreecommitdiff
path: root/watcher.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2025-01-06 02:54:52 +0700
committerShulhan <ms@kilabit.info>2025-01-07 00:13:55 +0700
commit85ae94f62b75372943a8ffdd705ce932a7849a8d (patch)
treeff55363d9e47729ce5d3c0be9fafa54d2cbd2e30 /watcher.go
parent6593f4d2069790c73595f14b3312a8d83e61760e (diff)
downloadciigo-85ae94f62b75372943a8ffdd705ce932a7849a8d.tar.xz
all: auto convert markup when HTTP client request GET to HTML file
In development mode, where [ServeOptions.IsDevelopment] is set to true or when running "ciigo serve", the ciigo HTTP server will check if the new markup file is newer than HTML file when user press refresh or reload on the browser. If its newer, it will convert the markup file and return the new content of HTML file.
Diffstat (limited to 'watcher.go')
-rw-r--r--watcher.go59
1 files changed, 59 insertions, 0 deletions
diff --git a/watcher.go b/watcher.go
index 4bb0a9c..3e04292 100644
--- a/watcher.go
+++ b/watcher.go
@@ -7,6 +7,7 @@ import (
"log"
"os"
"path/filepath"
+ "strings"
"time"
"git.sr.ht/~shulhan/pakakeh.go/lib/clise"
@@ -79,6 +80,64 @@ func newWatcher(
return w, nil
}
+// getFileMarkupByHTML get the file markup based on the HTML file name.
+func (w *watcher) getFileMarkupByHTML(fileHTML string) (
+ fmarkup *FileMarkup, isNew bool,
+) {
+ // Use file extension to handle insensitive cases of '.html' suffix.
+ var ext = filepath.Ext(fileHTML)
+ if strings.ToLower(ext) != `.html` {
+ return nil, false
+ }
+
+ var (
+ pathMarkup string
+ ok bool
+ )
+ pathMarkup, ok = strings.CutSuffix(fileHTML, ext)
+ if !ok {
+ return nil, false
+ }
+ pathMarkup = filepath.Join(w.opts.Root, pathMarkup)
+
+ var pathMarkupAdoc = pathMarkup + `.adoc`
+ fmarkup = w.fileMarkups[pathMarkupAdoc]
+ if fmarkup != nil {
+ return fmarkup, false
+ }
+
+ var pathMarkupMd = pathMarkup + `.md`
+ fmarkup = w.fileMarkups[pathMarkupMd]
+ if fmarkup != nil {
+ return fmarkup, false
+ }
+
+ // Directly check on the file system.
+
+ var fi os.FileInfo
+ var err error
+ fi, err = os.Stat(pathMarkupAdoc)
+ if err == nil {
+ fmarkup, err = NewFileMarkup(pathMarkupAdoc, fi)
+ if err != nil {
+ return nil, false
+ }
+ w.fileMarkups[pathMarkupAdoc] = fmarkup
+ return fmarkup, true
+ }
+
+ fi, err = os.Stat(pathMarkupMd)
+ if err == nil {
+ fmarkup, err = NewFileMarkup(pathMarkupMd, fi)
+ if err != nil {
+ return nil, false
+ }
+ w.fileMarkups[pathMarkupMd] = fmarkup
+ return fmarkup, true
+ }
+ return nil, false
+}
+
func (w *watcher) scanFileMarkup() {
w.fileMarkups = make(map[string]*FileMarkup)
var files = w.watchDir.Files()