From 85ae94f62b75372943a8ffdd705ce932a7849a8d Mon Sep 17 00:00:00 2001 From: Shulhan Date: Mon, 6 Jan 2025 02:54:52 +0700 Subject: 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. --- watcher.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) (limited to 'watcher.go') 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() -- cgit v1.3