Skip to content

Commit

Permalink
fix(renderer): do not always render preamble withing wrapper (#299)
Browse files Browse the repository at this point in the history
depends if the document has a section level 0 (ie, a title)

fixes #298

Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon authored Feb 23, 2019
1 parent 17c30c4 commit 76ea3f7
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,4 @@ coverage.txt
*.html
*.test
!test/**/*.html
*libasciidoc.html
14 changes: 11 additions & 3 deletions pkg/renderer/html5/section.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ var otherSectionContentTmpl texttemplate.Template
// initializes the templates
func init() {
preambleTmpl = newTextTemplate("preamble",
`<div id="preamble">
`{{ $ctx := .Context }}{{ with .Data }}{{ if .Wrapper }}<div id="preamble">
<div class="sectionbody">
{{ $ctx := .Context }}{{ with .Data }}{{ renderElements $ctx .Elements | printf "%s" }}{{ end }}
{{ end }}{{ renderElements $ctx .Elements | printf "%s" }}{{ if .Wrapper }}
</div>
</div>`,
</div>{{ end }}{{ end }}`,
texttemplate.FuncMap{
"renderElements": renderElements,
})
Expand Down Expand Up @@ -54,11 +54,19 @@ func init() {
func renderPreamble(ctx *renderer.Context, p types.Preamble) ([]byte, error) {
log.Debugf("Rendering preamble...")
result := bytes.NewBuffer(nil)
// the <div id="preamble"> wrapper is only necessary
// if the document has a section 0
wrapper := false
if ctx.Document.Attributes.HasTitle() {
wrapper = true
}
err := preambleTmpl.Execute(result, ContextualPipeline{
Context: ctx,
Data: struct {
Wrapper bool
Elements []interface{}
}{
Wrapper: wrapper,
Elements: p.Elements,
},
})
Expand Down
53 changes: 53 additions & 0 deletions pkg/renderer/html5/section_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,59 @@ Listing block content is commonly used to preserve code input.</pre>
</div>`
verify(GinkgoT(), expectedResult, actualContent)
})
})

Context("preambles", func() {

It("should include preamble wrapper", func() {
actualContent := `= Title
preamble
here
== section 1
content here`
expectedResult := `<div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p>preamble
here</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_section_1">section 1</h2>
<div class="sectionbody">
<div class="paragraph">
<p>content here</p>
</div>
</div>
</div>`
verify(GinkgoT(), expectedResult, actualContent)
})

It("should not include preamble wrapper", func() {
actualContent := `preamble
here
== section 1
content here
`
expectedResult := `<div class="paragraph">
<p>preamble
here</p>
</div>
<div class="sect1">
<h2 id="_section_1">section 1</h2>
<div class="sectionbody">
<div class="paragraph">
<p>content here</p>
</div>
</div>
</div>`
verify(GinkgoT(), expectedResult, actualContent)
})
})
})
6 changes: 6 additions & 0 deletions pkg/types/document_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ func (a DocumentAttributes) HasAuthors() bool {
return exists
}

// HasTitle returns `true` if the document has a title, ie, a section with level = 0
func (a DocumentAttributes) HasTitle() bool {
_, found := a[title]
return found
}

// GetTitle retrieves the document title in its metadata, or returns nil if the title was not specified
func (a DocumentAttributes) GetTitle() (SectionTitle, error) {
if t, found := a[title]; found {
Expand Down

0 comments on commit 76ea3f7

Please sign in to comment.