Skip to content

Commit

Permalink
refactor(renderer): Refactor paragraph renderer (#76)
Browse files Browse the repository at this point in the history
move call to rendering paragraph elements in the
template instead of a function.

also: use time.Now for the 'last updated' date to avoid comparison errors
when the rendering time and the comparison time are not in the same second.

Fixes #74

Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon authored Mar 24, 2018
1 parent 49b93ec commit 9932c28
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 36 deletions.
4 changes: 2 additions & 2 deletions renderer/html5/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ func renderElements(ctx *renderer.Context, elements []types.DocElement) ([]byte,
for _, element := range elements {
content, err := renderElement(ctx, element)
if err != nil {
return nil, errors.Wrapf(err, "failed to render the document")
return nil, errors.Wrapf(err, "failed to render the elements")
}
// if there's already some content, we need to insert a `\n` before writing
// the rendering output of the current element (if application, ie, not empty)
if hasContent && len(content) > 0 {
renderedElementsBuff.Write([]byte("\n"))
renderedElementsBuff.WriteString("\n")
}
// if the element was rendering into 'something' (ie, not enpty result)
if len(content) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion renderer/html5/document_details_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Last updated {{.LastUpdated}}
</div>
</body>
</html>`
verify(GinkgoT(), expectedResult, actualContent, renderer.IncludeHeaderFooter(true))
verify(GinkgoT(), expectedResult, actualContent, renderer.IncludeHeaderFooter(true), renderer.LastUpdated(time.Now()))
})

It("header with 2 authors and no revision", func() {
Expand Down
2 changes: 1 addition & 1 deletion renderer/html5/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Last updated {{.LastUpdated}}
</div>
</body>
</html>`
verify(GinkgoT(), expectedResult, actualContent, renderer.IncludeHeaderFooter(true))
verify(GinkgoT(), expectedResult, actualContent, renderer.IncludeHeaderFooter(true), renderer.LastUpdated(time.Now()))
})
})

Expand Down
56 changes: 24 additions & 32 deletions renderer/html5/paragraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,47 @@ package html5

import (
"bytes"
"html/template"
texttemplate "text/template"

"github.com/bytesparadise/libasciidoc/renderer"
"github.com/bytesparadise/libasciidoc/types"
"github.com/pkg/errors"
)

var paragraphTmpl template.Template
var paragraphTmpl texttemplate.Template

// initializes the template
func init() {
// TODO: use iterator and render func in the paragraph template
paragraphTmpl = newHTMLTemplate("paragraph",
`<div {{ if ne .ID "" }}id="{{.ID}}" {{ end }}class="paragraph">{{ if ne .Title "" }}
<div class="doctitle">{{.Title}}</div>{{ end }}
<p>{{.Lines}}</p>
</div>`)
paragraphTmpl = newTextTemplate("paragraph",
`{{ $ctx := .Context }}{{ with .Data }}{{ $renderedElements := renderElements $ctx .Lines | printf "%s" }}{{ if ne $renderedElements "" }}<div {{ if ne .ID "" }}id="{{ .ID }}" {{ end }}class="paragraph">{{ if ne .Title "" }}
<div class="doctitle">{{ .Title }}</div>{{ end }}
<p>{{ $renderedElements }}</p>
</div>{{ end }}{{ end }}`,
texttemplate.FuncMap{
"renderElements": renderInlineContents,
"notLastItem": notLastItem,
})
}

func renderParagraph(ctx *renderer.Context, p types.Paragraph) ([]byte, error) {
renderedLinesBuff := bytes.NewBuffer(nil)
for i, line := range p.Lines {
renderedLine, err := renderInlineContent(ctx, line)
if err != nil {
return nil, errors.Wrapf(err, "unable to render paragraph line")
}
renderedLinesBuff.Write(renderedLine)
if i < len(p.Lines)-1 {
renderedLinesBuff.WriteString("\n")
}

}
// skip rendering if there's no content in the paragraph (eg: empty passthough)
if renderedLinesBuff.Len() == 0 {
return []byte{}, nil
if len(p.Lines) == 0 {
return make([]byte, 0), nil
}
result := bytes.NewBuffer(nil)
err := paragraphTmpl.Execute(result, struct {
ID string
Title string
Lines template.HTML
}{
ID: p.ID.Value,
Title: p.Title.Value,
Lines: template.HTML(renderedLinesBuff.String()), // here we must preserve the HTML tags
err := paragraphTmpl.Execute(result, ContextualPipeline{
Context: ctx,
Data: struct {
ID string
Title string
Lines []types.InlineContent
}{
ID: p.ID.Value,
Title: p.Title.Value,
Lines: p.Lines,
},
})
if err != nil {
return nil, errors.Wrapf(err, "unable to render paragraph")
}
// log.Debugf("rendered paragraph: %s", result.Bytes())
return result.Bytes(), nil
}
16 changes: 16 additions & 0 deletions renderer/html5/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,22 @@ func renderPlainStringForInlineElements(ctx *renderer.Context, elements []types.
return buff.Bytes(), nil
}

// renderElements renders each element and includes an `\n` character until the last item
func renderInlineContents(ctx *renderer.Context, elements []types.InlineContent) ([]byte, error) {
buff := bytes.NewBuffer(nil)
for i, e := range elements {
renderedElement, err := renderElement(ctx, e)
if err != nil {
return nil, errors.Wrap(err, "unable to render element")
}
buff.Write(renderedElement)
if i < len(elements)-1 {
buff.WriteString("\n")
}
}
return buff.Bytes(), nil
}

// notLastItem returns true if the given index is NOT the last entry in the given description lines, false otherwise.
func notLastItem(index int, content interface{}) bool {
switch reflect.TypeOf(content).Kind() {
Expand Down

0 comments on commit 9932c28

Please sign in to comment.