aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2022-08-01 02:11:00 +0700
committerShulhan <ms@kilabit.info>2022-08-01 02:11:51 +0700
commit3f36d9d317263f6c65f7ec4ebb4b704d7dafe00b (patch)
tree6c78ccda08183dc1b94003ccbd9a6815d7ff2aad
parent8d347992ab2753dc878f56439883ec868e8d038d (diff)
downloadciigo-3f36d9d317263f6c65f7ec4ebb4b704d7dafe00b.tar.xz
all: export convert method in Converter as ToHtmlFile
This is the first public API provided for Converter, as promised in the previous commit.
-rw-r--r--.reuse/dep56
-rw-r--r--converter.go69
-rw-r--r--filehtml.go19
-rw-r--r--filemarkup.go24
-rw-r--r--testdata/html.tmpl4
-rw-r--r--watcher.go6
-rw-r--r--watcher_test.go62
7 files changed, 114 insertions, 76 deletions
diff --git a/.reuse/dep5 b/.reuse/dep5
index 3f866a7..8accd3c 100644
--- a/.reuse/dep5
+++ b/.reuse/dep5
@@ -15,10 +15,6 @@ Files: README.md
Copyright: 2022 Shulhan <ms@kilabit.info>
License: GPL-3.0-or-later
-Files: testdata/ex/clu/de/file.adoc
-Copyright: 2022 Shulhan <ms@kilabit.info>
-License: GPL-3.0-or-later
-
-Files: testdata/in/clu/de/file.adoc
+Files: testdata/*
Copyright: 2022 Shulhan <ms@kilabit.info>
License: GPL-3.0-or-later
diff --git a/converter.go b/converter.go
index 58afad3..0f819cf 100644
--- a/converter.go
+++ b/converter.go
@@ -6,6 +6,7 @@ package ciigo
import (
"fmt"
"html/template"
+ "log"
"os"
"path/filepath"
@@ -60,37 +61,25 @@ func NewConverter(htmlTemplate string) (converter *Converter, err error) {
return converter, nil
}
-// convert the markup into HTML.
-func (converter *Converter) convert(fmarkup *fileMarkup) (err error) {
- doc, err := asciidoctor.Open(fmarkup.path)
- if err != nil {
- return err
- }
-
- fmarkup.fhtml.rawBody.Reset()
- err = doc.ToHTMLBody(&fmarkup.fhtml.rawBody)
- if err != nil {
- return err
- }
-
- fmarkup.fhtml.unpackAdocMetadata(doc)
-
- return converter.write(fmarkup.fhtml)
-}
-
// convertFileMarkups convert markup files into HTML.
func (converter *Converter) convertFileMarkups(fileMarkups map[string]*fileMarkup, isForce bool) {
- logp := "convertFileMarkups"
- for _, fmarkup := range fileMarkups {
+ var (
+ logp = "convertFileMarkups"
+
+ fmarkup *fileMarkup
+ err error
+ )
+
+ for _, fmarkup = range fileMarkups {
if !fmarkup.isNewerThanHtml() {
if !isForce {
continue
}
}
- err := converter.convert(fmarkup)
+ err = converter.ToHtmlFile(fmarkup.path, fmarkup.pathHtml)
if err != nil {
- fmt.Printf("%s: %s\n", logp, err)
+ log.Printf("%s: %s", logp, err)
} else {
fmt.Printf("%s: converting %s\n", logp, fmarkup.path)
}
@@ -113,21 +102,45 @@ func (converter *Converter) htmlTemplateUseInternal() (err error) {
return nil
}
-// write the HTML file.
-func (converter *Converter) write(fhtml *fileHtml) (err error) {
- f, err := os.Create(fhtml.path)
+// ToHtmlFile convert the AsciiDoc file to HTML.
+func (converter *Converter) ToHtmlFile(pathAdoc, pathHtml string) (err error) {
+ var (
+ logp = "ToHtmlFile"
+ fhtml = newFileHtml()
+
+ htmlBody string
+ doc *asciidoctor.Document
+ f *os.File
+ )
+
+ doc, err = asciidoctor.Open(pathAdoc)
if err != nil {
- return err
+ return fmt.Errorf("%s: %w", logp, err)
+ }
+
+ err = doc.ToHTMLBody(&fhtml.rawBody)
+ if err != nil {
+ return fmt.Errorf("%s: %w", logp, err)
+ }
+
+ fhtml.unpackAdocMetadata(doc)
+
+ htmlBody = fhtml.rawBody.String()
+ fhtml.Body = template.HTML(htmlBody)
+
+ f, err = os.Create(pathHtml)
+ if err != nil {
+ return fmt.Errorf("%s: %w", logp, err)
}
err = converter.tmpl.Execute(f, fhtml)
if err != nil {
- return err
+ return fmt.Errorf("%s: %w", logp, err)
}
err = f.Close()
if err != nil {
- return err
+ return fmt.Errorf("%s: %w", logp, err)
}
return nil
diff --git a/filehtml.go b/filehtml.go
index 202c605..5d889a0 100644
--- a/filehtml.go
+++ b/filehtml.go
@@ -5,8 +5,6 @@ package ciigo
import (
"html/template"
- "io/fs"
- "os"
"strings"
"git.sr.ht/~shulhan/asciidoctor-go"
@@ -24,25 +22,26 @@ type fileHtml struct {
Body template.HTML
Metadata map[string]string
- path string
- finfo fs.FileInfo
rawBody strings.Builder
}
-func newFileHtml(path string) (fhtml *fileHtml) {
+func newFileHtml() (fhtml *fileHtml) {
fhtml = &fileHtml{
- path: path,
+ Metadata: map[string]string{},
}
- fhtml.finfo, _ = os.Stat(path)
return fhtml
}
func (fhtml *fileHtml) unpackAdocMetadata(doc *asciidoctor.Document) {
+ var (
+ k string
+ v string
+ )
+
fhtml.Title = doc.Title.String()
fhtml.Styles = fhtml.Styles[:0]
- fhtml.Metadata = make(map[string]string, len(doc.Attributes))
- for k, v := range doc.Attributes {
+ for k, v = range doc.Attributes {
switch k {
case metadataStylesheet:
fhtml.Styles = append(fhtml.Styles, v)
@@ -58,6 +57,4 @@ func (fhtml *fileHtml) unpackAdocMetadata(doc *asciidoctor.Document) {
if len(fhtml.Styles) == 0 {
fhtml.EmbeddedCSS = embeddedCSS()
}
-
- fhtml.Body = template.HTML(fhtml.rawBody.String())
}
diff --git a/filemarkup.go b/filemarkup.go
index d4567d8..4defea3 100644
--- a/filemarkup.go
+++ b/filemarkup.go
@@ -11,16 +11,20 @@ import (
)
type fileMarkup struct {
- fhtml *fileHtml // The HTML output of this markup.
- info os.FileInfo // info contains FileInfo of markup file.
+ info os.FileInfo // info contains FileInfo of markup file.
basePath string // basePath contains full path to file without markup extension.
path string // path contains full path to markup file.
-
+ pathHtml string // path to HTML file.
}
func newFileMarkup(filePath string, fi os.FileInfo) (fmarkup *fileMarkup, err error) {
- logp := "newFileMarkup"
+ var (
+ logp = "newFileMarkup"
+
+ ext string
+ )
+
if len(filePath) == 0 {
return nil, fmt.Errorf("%s: empty path", logp)
}
@@ -31,7 +35,7 @@ func newFileMarkup(filePath string, fi os.FileInfo) (fmarkup *fileMarkup, err er
}
}
- ext := strings.ToLower(filepath.Ext(filePath))
+ ext = strings.ToLower(filepath.Ext(filePath))
fmarkup = &fileMarkup{
path: filePath,
@@ -39,15 +43,19 @@ func newFileMarkup(filePath string, fi os.FileInfo) (fmarkup *fileMarkup, err er
basePath: strings.TrimSuffix(filePath, ext),
}
- fmarkup.fhtml = newFileHtml(fmarkup.basePath + ".html")
+ fmarkup.pathHtml = 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 {
+ var (
+ fi os.FileInfo
+ )
+ fi, _ = os.Stat(fm.pathHtml)
+ if fi == nil {
return true
}
- return fm.info.ModTime().After(fm.fhtml.finfo.ModTime())
+ return fm.info.ModTime().After(fi.ModTime())
}
diff --git a/testdata/html.tmpl b/testdata/html.tmpl
index 3f440a0..967be5e 100644
--- a/testdata/html.tmpl
+++ b/testdata/html.tmpl
@@ -1,7 +1,3 @@
-<!--
-// SPDX-FileCopyrightText: 2021 Shulhan <ms@kilabit.info>
-// SPDX-License-Identifier: GPL-3.0-or-later
---!>
<!DOCTYPE>
<html>
<head><title>{{.Title}}</title></head>
diff --git a/watcher.go b/watcher.go
index 1df157d..25bb12a 100644
--- a/watcher.go
+++ b/watcher.go
@@ -38,7 +38,7 @@ type watcher struct {
//
// watcher
// |
-// +-- watchFileMarkup --> UPDATE --> Converter.convert()
+// +-- watchFileMarkup --> UPDATE --> Converter.ToHtmlFile()
// |
// +-- watchHtmlTemplate +--> DELETE --> Converter.htmlTemplateUseInternal()
// |
@@ -156,9 +156,9 @@ func (w *watcher) watchFileMarkup() {
}
}
- err = w.converter.convert(fmarkup)
+ err = w.converter.ToHtmlFile(fmarkup.path, fmarkup.pathHtml)
if err != nil {
- log.Printf("%s: %s\n", logp, err)
+ log.Printf("%s: %s", logp, err)
}
w.changes.Push(fmarkup)
diff --git a/watcher_test.go b/watcher_test.go
index 5cc2079..a2e7f43 100644
--- a/watcher_test.go
+++ b/watcher_test.go
@@ -4,9 +4,9 @@
package ciigo
import (
+ "bytes"
"os"
"path/filepath"
- "strings"
"testing"
"time"
@@ -71,7 +71,8 @@ func TestWatcher(t *testing.T) {
func testCreate(t *testing.T) {
var (
- err error
+ err error
+ gotBody []byte
)
testFileAdoc = filepath.Join(testWatcher.dir, "index.adoc")
@@ -84,7 +85,10 @@ func testCreate(t *testing.T) {
test.Assert(t, "New adoc file created", testFileAdoc, got.path)
- expBody := `
+ expBody := `<!DOCTYPE>
+<html>
+<head><title></title></head>
+<body>
<div id="header">
<div class="details">
</div>
@@ -95,12 +99,23 @@ func testCreate(t *testing.T) {
</div>
</div>
</div>`
- gotBody := removeFooter(string(got.fhtml.Body))
- test.Assert(t, "HTML body", expBody, gotBody)
+
+ gotBody, err = os.ReadFile(got.pathHtml)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ gotBody = removeFooter(gotBody)
+ test.Assert(t, "HTML body", expBody, string(gotBody))
}
func testUpdate(t *testing.T) {
- _, err := testAdocFile.WriteString("= Hello")
+ var (
+ err error
+ gotBody []byte
+ )
+
+ _, err = testAdocFile.WriteString("= Hello")
if err != nil {
t.Fatal(err)
}
@@ -112,7 +127,10 @@ func testUpdate(t *testing.T) {
got := waitChanges()
test.Assert(t, "adoc file updated", testFileAdoc, got.path)
- expBody := `
+ expBody := `<!DOCTYPE>
+<html>
+<head><title>Hello</title></head>
+<body>
<div id="header">
<h1>Hello</h1>
<div class="details">
@@ -124,8 +142,15 @@ func testUpdate(t *testing.T) {
</div>
</div>
</div>`
- gotBody := removeFooter(string(got.fhtml.Body))
- test.Assert(t, "HTML body", expBody, gotBody)
+
+ gotBody, err = os.ReadFile(got.pathHtml)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ gotBody = removeFooter(gotBody)
+
+ test.Assert(t, "HTML body", expBody, string(gotBody))
}
func testDelete(t *testing.T) {
@@ -146,15 +171,18 @@ func testDelete(t *testing.T) {
test.Assert(t, "adoc file deleted", false, gotIsExist)
}
-// removeFooter remove the footer from generated HTML. The footer is 4 lines
-// at the bottom.
-func removeFooter(in string) string {
- lines := strings.Split(in, "\n")
- n := len(lines)
- if n > 5 {
- lines = lines[:n-5]
+// removeFooter remove the footer from generated HTML since its contains date
+// and time that changes during test.
+func removeFooter(in []byte) (out []byte) {
+ var (
+ lines = bytes.Split(in, []byte("\n"))
+ n = len(lines)
+ )
+ if n > 7 {
+ lines = lines[:n-7]
}
- return strings.Join(lines, "\n")
+ out = bytes.Join(lines, []byte("\n"))
+ return out
}
func waitChanges() (fmarkup *fileMarkup) {