diff options
| author | Shulhan <ms@kilabit.info> | 2021-10-10 21:31:00 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2021-10-10 21:31:00 +0700 |
| commit | 06d03f6afe37124c486a34c120fcb05a8ae86d5e (patch) | |
| tree | 98be3734ef6613b42878829bda7a19eed2e59576 | |
| parent | 88504a35592cf0c1a714d0a7454cdf95c7adec8b (diff) | |
| download | ciigo-06d03f6afe37124c486a34c120fcb05a8ae86d5e.tar.xz | |
all: check markup modification time before converting to HTML
Previously, when the Convert, Watch or Serve running it will convert
all markup files into HTML without checking if the adoc has been modified
or newer than HTML file.
This changes check the modification time of markup file first before
converting them, to minimize unnecessary operation.
| -rw-r--r-- | ciigo.go | 27 | ||||
| -rw-r--r-- | filehtml.go | 11 | ||||
| -rw-r--r-- | filemarkup.go | 11 | ||||
| -rw-r--r-- | htmlgenerator.go | 9 |
4 files changed, 36 insertions, 22 deletions
@@ -241,12 +241,16 @@ func listFileMarkups(dir string, excRE []*regexp.Regexp) ( for _, fi := range fis { name := fi.Name() + filePath := filepath.Join(dir, name) - if fi.IsDir() && name[0] != '.' { - newdir := filepath.Join(dir, fi.Name()) - fmarkups, err := listFileMarkups(newdir, excRE) + if fi.IsDir() { + if name[0] == '.' { + // Skip any directory start with '.'. + continue + } + fmarkups, err := listFileMarkups(filePath, excRE) if err != nil { - return nil, fmt.Errorf("%s: %w", logp, err) + return nil, fmt.Errorf("%s: %s: %w", logp, filePath, err) } for k, v := range fmarkups { fileMarkups[k] = v @@ -261,22 +265,13 @@ func listFileMarkups(dir string, excRE []*regexp.Regexp) ( if fi.Size() == 0 { continue } - - filePath := filepath.Join(dir, name) - if isExcluded(filePath, excRE) { continue } - - fmarkup := &fileMarkup{ - path: filePath, - info: fi, - basePath: strings.TrimSuffix(filePath, ext), - fhtml: &fileHTML{}, + fmarkup, err := newFileMarkup(filePath, fi) + if err != nil { + return nil, fmt.Errorf("%s: %s: %w", logp, filePath, err) } - - fmarkup.fhtml.path = fmarkup.basePath + ".html" - fileMarkups[filePath] = fmarkup } diff --git a/filehtml.go b/filehtml.go index 91a2c7d..541db91 100644 --- a/filehtml.go +++ b/filehtml.go @@ -6,6 +6,8 @@ package ciigo import ( "html/template" + "io/fs" + "os" "strings" "git.sr.ht/~shulhan/asciidoctor-go" @@ -22,9 +24,18 @@ type fileHTML struct { Metadata map[string]string path string + finfo fs.FileInfo rawBody strings.Builder } +func newFileHtml(path string) (fhtml *fileHTML) { + fhtml = &fileHTML{ + path: path, + } + fhtml.finfo, _ = os.Stat(path) + return fhtml +} + func (fhtml *fileHTML) unpackAdocMetadata(doc *asciidoctor.Document) { fhtml.Title = doc.Title.String() fhtml.Styles = fhtml.Styles[:0] diff --git a/filemarkup.go b/filemarkup.go index b53862c..b29483f 100644 --- a/filemarkup.go +++ b/filemarkup.go @@ -36,10 +36,17 @@ func newFileMarkup(filePath string, fi os.FileInfo) (fmarkup *fileMarkup, err er path: filePath, info: fi, basePath: strings.TrimSuffix(filePath, ext), - fhtml: &fileHTML{}, } - fmarkup.fhtml.path = fmarkup.basePath + ".html" + fmarkup.fhtml = newFileHtml(fmarkup.basePath + ".html") return fmarkup, nil } + +// isNewerThanHtml return true if the markup file is newer than HTML file. +func (fm *fileMarkup) isNewerThanHtml() bool { + if fm.fhtml.finfo == nil { + return true + } + return fm.info.ModTime().After(fm.fhtml.finfo.ModTime()) +} diff --git a/htmlgenerator.go b/htmlgenerator.go index ab90b94..51a263f 100644 --- a/htmlgenerator.go +++ b/htmlgenerator.go @@ -103,14 +103,15 @@ func (htmlg *htmlGenerator) convert(fmarkup *fileMarkup) (err error) { func (htmlg *htmlGenerator) convertFileMarkups(fileMarkups map[string]*fileMarkup) { logp := "convertFileMarkups" for _, fmarkup := range fileMarkups { - fmt.Printf("%s: converting %q to %q => ", logp, fmarkup.path, - fmarkup.fhtml.path) + if !fmarkup.isNewerThanHtml() { + continue + } err := htmlg.convert(fmarkup) if err != nil { - fmt.Println(err) + fmt.Printf("%s: %s", logp, err) } else { - fmt.Println("OK") + fmt.Printf("%s: converting %s", logp, fmarkup.path) } } } |
