aboutsummaryrefslogtreecommitdiff
path: root/converter.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2023-11-06 21:33:45 +0700
committerShulhan <ms@kilabit.info>2023-11-09 20:25:15 +0700
commitbad30877282ff2e584c49ea1a656bf0fa0f3b1fa (patch)
tree3750564f74822352e20d80acebbe5f2460d7df72 /converter.go
parent3ca487dff9ea91ef6d60e4a9e7a78ea8e177bff1 (diff)
downloadciigo-bad30877282ff2e584c49ea1a656bf0fa0f3b1fa.tar.xz
all: use modification time when deciding when to convert to HTML
Previously, in changes 46bd8b68dc8c we pass the force=true to Convert function. In this changes, we derive the decision based on modification time of HTML template and markup file. If the HTML template or markup file is newer that HTML file then the new HTML file will be generated.
Diffstat (limited to 'converter.go')
-rw-r--r--converter.go51
1 files changed, 41 insertions, 10 deletions
diff --git a/converter.go b/converter.go
index 316bfc5..2350b5d 100644
--- a/converter.go
+++ b/converter.go
@@ -9,6 +9,7 @@ import (
"log"
"os"
"path/filepath"
+ "time"
"git.sr.ht/~shulhan/asciidoctor-go"
"github.com/yuin/goldmark"
@@ -18,8 +19,12 @@ import (
// Converter a single, reusable AsciiDoc converter.
type Converter struct {
- tmpl *template.Template
- tmplSearch *template.Template
+ tmpl *template.Template
+ tmplSearch *template.Template
+
+ // htmlTemplateModtime modification time for HtmlTemplate.
+ htmlTemplateModtime time.Time
+
htmlTemplate string // Path to HTML template in storage.
}
@@ -30,7 +35,6 @@ func NewConverter(htmlTemplate string) (converter *Converter, err error) {
logp = `NewConverter`
tmplContent string
- bhtml []byte
)
converter = &Converter{}
@@ -42,12 +46,23 @@ func NewConverter(htmlTemplate string) (converter *Converter, err error) {
} else {
converter.htmlTemplate = filepath.Clean(htmlTemplate)
+ var fi os.FileInfo
+
+ fi, err = os.Stat(converter.htmlTemplate)
+ if err != nil {
+ return nil, fmt.Errorf(`%s: %w`, logp, err)
+ }
+
+ converter.htmlTemplateModtime = fi.ModTime()
+
+ var bhtml []byte
+
bhtml, err = os.ReadFile(converter.htmlTemplate)
if err != nil {
- tmplContent = templateIndexHTML
- } else {
- tmplContent = string(bhtml)
+ return nil, fmt.Errorf(`%s: %w`, logp, err)
}
+
+ tmplContent = string(bhtml)
}
converter.tmpl, err = converter.tmpl.Parse(tmplContent)
@@ -69,13 +84,29 @@ func (converter *Converter) convertFileMarkups(fileMarkups map[string]*FileMarku
var (
logp = `convertFileMarkups`
- fmarkup *FileMarkup
- err error
+ fmarkup *FileMarkup
+ htmlInfo os.FileInfo
+ htmlModtime time.Time
+ err error
+ skip bool
)
for _, fmarkup = range fileMarkups {
- if !fmarkup.isNewerThanHtml() {
- if !isForce {
+ skip = true
+ if !isForce {
+ htmlInfo, _ = os.Stat(fmarkup.pathHtml)
+ if htmlInfo == nil {
+ // HTML file may not exist.
+ skip = false
+ } else {
+ htmlModtime = htmlInfo.ModTime()
+ if converter.htmlTemplateModtime.After(htmlModtime) {
+ skip = false
+ } else if fmarkup.info.ModTime().After(htmlModtime) {
+ skip = false
+ }
+ }
+ if skip {
continue
}
}