Skip to content

Commit

Permalink
feat(renderer): attribute to disable header and footer (bytesparadise…
Browse files Browse the repository at this point in the history
…#516)

when the `noheader` or `nofooter` document attributes
are set (with an empty value), the header and footer
are NOT rendered (respectively)

Fixes bytesparadise#510

Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon authored Mar 22, 2020
1 parent a9da628 commit 3867576
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 23 deletions.
146 changes: 144 additions & 2 deletions pkg/renderer/html5/document_details_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

"github.com/bytesparadise/libasciidoc/pkg/configuration"
"github.com/bytesparadise/libasciidoc/pkg/types"
. "github.com/bytesparadise/libasciidoc/testsupport"

. "github.com/onsi/ginkgo"
Expand Down Expand Up @@ -52,7 +53,7 @@ Last updated {{.LastUpdated}}
</body>
</html>`
now := time.Now()
Expect(RenderHTML5Body(source, configuration.WithHeaderFooter(true), configuration.WithLastUpdated(time.Now()))).To(MatchHTML5Template(expected, now))
Expect(RenderHTML5Body(source, configuration.WithHeaderFooter(true), configuration.WithLastUpdated(now))).To(MatchHTML5Template(expected, now))
})

It("header with 2 authors and no revision", func() {
Expand Down Expand Up @@ -90,8 +91,149 @@ Last updated {{.LastUpdated}}
</body>
</html>`
now := time.Now()
Expect(RenderHTML5Body(source, configuration.WithHeaderFooter(true), configuration.WithLastUpdated(time.Now()))).
Expect(RenderHTML5Body(source, configuration.WithHeaderFooter(true), configuration.WithLastUpdated(now))).
To(MatchHTML5Template(expected, now))
})
})

Context("custom header and footer", func() {

now := time.Now()

It("with header and footer", func() {
source := `= Document Title
a paragraph`
expected := `<!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>Document Title</title>
</head>
<body class="article">
<div id="header">
<h1>Document Title</h1>
</div>
<div id="content">
<div class="paragraph">
<p>a paragraph</p>
</div>
</div>
<div id="footer">
<div id="footer-text">
Last updated {{.LastUpdated}}
</div>
</div>
</body>
</html>`
Expect(RenderHTML5Body(source,
configuration.WithHeaderFooter(true),
configuration.WithLastUpdated(now),
configuration.WithAttributes(map[string]string{}),
)).To(MatchHTML5Template(expected, now))
})

It("with header and without footer", func() {
source := `= Document Title
a paragraph`
expected := `<!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>Document Title</title>
</head>
<body class="article">
<div id="header">
<h1>Document Title</h1>
</div>
<div id="content">
<div class="paragraph">
<p>a paragraph</p>
</div>
</div>
</body>
</html>`
Expect(RenderHTML5Body(source,
configuration.WithHeaderFooter(true),
configuration.WithLastUpdated(now),
configuration.WithAttributes(map[string]string{
types.AttrNoFooter: "",
}),
)).To(MatchHTML5Template(expected, now))
})

It("without header and with footer", func() {
source := `= Document Title
a paragraph`
expected := `<!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>Document Title</title>
</head>
<body class="article">
<div id="content">
<div class="paragraph">
<p>a paragraph</p>
</div>
</div>
<div id="footer">
<div id="footer-text">
Last updated {{.LastUpdated}}
</div>
</div>
</body>
</html>`
Expect(RenderHTML5Body(source,
configuration.WithHeaderFooter(true),
configuration.WithLastUpdated(now),
configuration.WithAttributes(map[string]string{
types.AttrNoHeader: "",
}),
)).To(MatchHTML5Template(expected, now))
})

It("without header and without footer", func() {
source := `= Document Title
a paragraph`
expected := `<!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>Document Title</title>
</head>
<body class="article">
<div id="content">
<div class="paragraph">
<p>a paragraph</p>
</div>
</div>
</body>
</html>`
Expect(RenderHTML5Body(source,
configuration.WithHeaderFooter(true),
configuration.WithLastUpdated(now),
configuration.WithAttributes(map[string]string{
types.AttrNoHeader: "",
types.AttrNoFooter: "",
}),
)).To(MatchHTML5Template(expected, now))
})

})
})
44 changes: 24 additions & 20 deletions pkg/renderer/html5/html5.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ func init() {
<link type="text/css" rel="stylesheet" href="{{ .CSS }}">{{ end }}
<title>{{ escape .Title }}</title>
</head>
<body class="article">
<body class="article">{{ if .IncludeHeader }}
<div id="header">
<h1>{{ .Header }}</h1>{{ if .Details }}
{{ .Details }}{{ end }}
</div>
</div>{{ end }}
<div id="content">
{{ .Content }}
</div>
</div>{{ if .IncludeFooter }}
<div id="footer">
<div id="footer-text">{{ if .RevNumber }}
Version {{ .RevNumber }}<br>{{ end }}
Last updated {{ .LastUpdated }}
</div>
</div>
</div>{{ end }}
</body>
</html>`,
texttemplate.FuncMap{
Expand Down Expand Up @@ -76,23 +76,27 @@ func Render(ctx renderer.Context, output io.Writer) (types.Metadata, error) {
}
revNumber, _ := ctx.Document.Attributes.GetAsString("revnumber")
err = documentTmpl.Execute(output, struct {
Generator string
Title string
Header string
Content htmltemplate.HTML
RevNumber string
LastUpdated string
CSS string
Details *htmltemplate.HTML
Generator string
Title string
Header string
Content htmltemplate.HTML
RevNumber string
LastUpdated string
CSS string
Details *htmltemplate.HTML
IncludeHeader bool
IncludeFooter bool
}{
Generator: "libasciidoc", // TODO: externalize this value and include the lib version ?
Title: string(renderedTitle),
Header: string(renderedHeader),
Content: htmltemplate.HTML(string(renderedElements)), //nolint: gosec
RevNumber: revNumber,
LastUpdated: ctx.Config.LastUpdated.Format(configuration.LastUpdatedFormat),
CSS: ctx.Config.CSS,
Details: documentDetails,
Generator: "libasciidoc", // TODO: externalize this value and include the lib version ?
Title: string(renderedTitle),
Header: string(renderedHeader),
Content: htmltemplate.HTML(string(renderedElements)), //nolint: gosec
RevNumber: revNumber,
LastUpdated: ctx.Config.LastUpdated.Format(configuration.LastUpdatedFormat),
CSS: ctx.Config.CSS,
Details: documentDetails,
IncludeHeader: !ctx.Document.Attributes.Has(types.AttrNoHeader),
IncludeFooter: !ctx.Document.Attributes.Has(types.AttrNoFooter),
})
if err != nil {
return types.Metadata{}, errors.Wrapf(err, "unable to render full document")
Expand Down
10 changes: 9 additions & 1 deletion testsupport/html5_template_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"time"

"github.com/bytesparadise/libasciidoc/pkg/configuration"
. "github.com/onsi/ginkgo"
gomegatypes "github.com/onsi/gomega/types"
"github.com/pkg/errors"
"github.com/sergi/go-diff/diffmatchpatch"
)

// --------------------
Expand Down Expand Up @@ -36,7 +38,13 @@ func (m *html5TemplateMatcher) Match(actual interface{}) (success bool, err erro
return false, errors.Errorf("MatchHTML5Template matcher expects a string (actual: %T)", actual)
}
m.expected = strings.Replace(m.expected, "{{.LastUpdated}}", m.lastUpdated.Format(configuration.LastUpdatedFormat), 1)
return m.expected == m.actual, nil
if m.expected != m.actual {
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(m.actual, m.expected, true)
GinkgoT().Logf("diff:\n%s", dmp.DiffPrettyText(diffs))
return false, nil
}
return true, nil
}

func (m *html5TemplateMatcher) FailureMessage(_ interface{}) (message string) {
Expand Down

0 comments on commit 3867576

Please sign in to comment.