Skip to content

Commit

Permalink
fix(renderer): use file mtime as 'Last updated' (#467)
Browse files Browse the repository at this point in the history
use file's mtime instead of `time.Now()`
also, use the same datetime format as asciidoctor.

Fixes #461

Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon authored Jan 5, 2020
1 parent 6b58ad2 commit f539c75
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 33 deletions.
12 changes: 10 additions & 2 deletions libasciidoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ func ConvertFileToHTML(ctx context.Context, filename string, output io.Writer, o
return nil, errors.Wrapf(err, "error opening %s", filename)
}
defer file.Close()
// use the file mtime as the `last updated` value
stat, err := os.Stat(filename)
if err != nil {
return nil, errors.Wrapf(err, "error opening %s", filename)
}
options = append(options, renderer.LastUpdated(stat.ModTime()))
return ConvertToHTML(ctx, filename, file, output, options...)
}

Expand All @@ -51,6 +57,10 @@ func ConvertToHTML(ctx context.Context, filename string, r io.Reader, output io.

func convertToHTML(ctx context.Context, doc types.Document, output io.Writer, options ...renderer.Option) (map[string]interface{}, error) {
start := time.Now()
defer func() {
duration := time.Since(start)
log.Debugf("rendered the HTML output in %v", duration)
}()
rendererCtx := renderer.Wrap(ctx, doc, options...)
// insert tables of contents, preamble and process file inclusions
err := renderer.Prerender(rendererCtx)
Expand All @@ -62,7 +72,5 @@ func convertToHTML(ctx context.Context, doc types.Document, output io.Writer, op
return nil, errors.Wrapf(err, "error while rendering the document")
}
log.Debugf("Done processing document")
duration := time.Since(start)
log.Debugf("rendered the HTML output in %v", duration)
return metadata, nil
}
19 changes: 6 additions & 13 deletions libasciidoc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,31 +183,23 @@ a paragraph with _italic content_`

Context("complete Document ", func() {

It("section levels 0 and 5", func() {
source := `= a document title
====== Section A
a paragraph with *bold content*`
It("using existing file", func() {
expectedContent := `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]-->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="libasciidoc">
<title>a document title</title>
<title>Chapter A</title>
</head>
<body class="article">
<div id="header">
<h1>a document title</h1>
<h1>Chapter A</h1>
</div>
<div id="content">
<div class="sect5">
<h6 id="_section_a">Section A</h6>
<div class="paragraph">
<p>a paragraph with <strong>bold content</strong></p>
</div>
<p>content</p>
</div>
</div>
<div id="footer">
Expand All @@ -217,8 +209,9 @@ Last updated {{.LastUpdated}}
</div>
</body>
</html>`
Expect(source).To(RenderHTML5Document(expectedContent))
Expect("test/includes/chapter-a.adoc").To(RenderHTML5Document(expectedContent))
})

})

})
2 changes: 1 addition & 1 deletion pkg/renderer/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (
// keyEntrypoint a bool value to indicate if the entrypoint to start with when parsing the document
keyEntrypoint string = "Entrypoint"
// LastUpdatedFormat the time format for the `last updated` document attribute
LastUpdatedFormat string = "2006/01/02 15:04:05 MST"
LastUpdatedFormat string = "2006-01-02 15:04:05 -0700"
)

// LastUpdated function to set the `last updated` option in the renderer context (default is `time.Now()`)
Expand Down
25 changes: 8 additions & 17 deletions testsupport/html5_rendering_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"bytes"
"context"
"fmt"
"os"
"strings"
"time"

"github.com/bytesparadise/libasciidoc"
"github.com/bytesparadise/libasciidoc/pkg/renderer"
Expand Down Expand Up @@ -149,39 +149,30 @@ func (m *html5TitleMatcher) NegatedFailureMessage(_ interface{}) (message string
func RenderHTML5Document(expected string, options ...interface{}) gomegatypes.GomegaMatcher {
m := &html5DocumentMatcher{
expected: expected,
filename: "test.adoc",
}
for _, o := range options {
if configure, ok := o.(FilenameOption); ok {
configure(m)
}
}
return m
}

func (m *html5DocumentMatcher) setFilename(f string) {
m.filename = f
}

type html5DocumentMatcher struct {
filename string
expected string
actual string
}

func (m *html5DocumentMatcher) Match(actual interface{}) (success bool, err error) {
content, ok := actual.(string)
filename, ok := actual.(string)
if !ok {
return false, errors.Errorf("RenderHTML5Body matcher expects a string (actual: %T)", actual)
}
contentReader := strings.NewReader(content)
resultWriter := bytes.NewBuffer(nil)
lastUpdated := time.Now()
_, err = libasciidoc.ConvertToHTML(context.Background(), m.filename, contentReader, resultWriter, renderer.IncludeHeaderFooter(true))
_, err = libasciidoc.ConvertFileToHTML(context.Background(), filename, resultWriter, renderer.IncludeHeaderFooter(true))
if err != nil {
return false, err
}
m.expected = strings.Replace(m.expected, "{{.LastUpdated}}", lastUpdated.Format(renderer.LastUpdatedFormat), 1)
stat, err := os.Stat(filename)
if err != nil {
return false, errors.Wrapf(err, "unable to get stats for file '%s'", filename)
}
m.expected = strings.Replace(m.expected, "{{.LastUpdated}}", stat.ModTime().Format("2006-01-02 15:04:05 -0700"), 1)
m.actual = resultWriter.String()
return m.expected == m.actual, nil
}
Expand Down

0 comments on commit f539c75

Please sign in to comment.