Skip to content

Commit

Permalink
feat(parser/renderer): Support for Document Author and Revision, and …
Browse files Browse the repository at this point in the history
…Preamble (#36)

Also, refactor test by using contexts to group
Also, update dependencies, including Pigeon (see below)
Also, use new 'Entrypoint' option in Pigeon to reduce the structure
of the expected output when parsing
Also, refactor the options to render a document, using functions to
configure the rendering context

Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon authored Oct 24, 2017
1 parent 6ace088 commit 99b1fd9
Show file tree
Hide file tree
Showing 41 changed files with 4,188 additions and 2,614 deletions.
14 changes: 7 additions & 7 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 7 additions & 8 deletions libasciidoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"io"

asciidoc "github.com/bytesparadise/libasciidoc/context"
"github.com/bytesparadise/libasciidoc/parser"
"github.com/bytesparadise/libasciidoc/renderer"
htmlrenderer "github.com/bytesparadise/libasciidoc/renderer/html5"
Expand All @@ -16,15 +15,14 @@ import (
// ConvertToHTMLBody converts the content of the given reader `r` into an set of <DIV> elements for an HTML/BODY document.
// The conversion result is written in the given writer `w`, whereas the document metadata (title, etc.) (or an error if a problem occurred) is returned
// as the result of the function call.
func ConvertToHTMLBody(ctx context.Context, r io.Reader, w io.Writer) (*types.DocumentAttributes, error) {
func ConvertToHTMLBody(ctx context.Context, r io.Reader, w io.Writer) (types.DocumentAttributes, error) {
doc, err := parser.ParseReader("", r)
if err != nil {
return nil, errors.Wrapf(err, "error while parsing the document")
}
document := doc.(*types.Document)
options := renderer.Options{}
options[renderer.IncludeHeaderFooter] = false // force value
err = htmlrenderer.Render(asciidoc.Wrap(ctx, *document), w, options)
options := []renderer.Option{renderer.IncludeHeaderFooter(false)}
err = htmlrenderer.Render(renderer.Wrap(ctx, *document, options...), w)
if err != nil {
return nil, errors.Wrapf(err, "error while rendering the document")
}
Expand All @@ -34,14 +32,15 @@ func ConvertToHTMLBody(ctx context.Context, r io.Reader, w io.Writer) (*types.Do

// ConvertToHTML converts the content of the given reader `r` into a full HTML document, written in the given writer `w`.
// Returns an error if a problem occurred
func ConvertToHTML(ctx context.Context, r io.Reader, w io.Writer, options renderer.Options) error {
func ConvertToHTML(ctx context.Context, r io.Reader, w io.Writer, options ...renderer.Option) error {
doc, err := parser.ParseReader("", r)
if err != nil {
return errors.Wrapf(err, "error while parsing the document")
}
document := doc.(*types.Document)
options[renderer.IncludeHeaderFooter] = true // force value
err = htmlrenderer.Render(asciidoc.Wrap(ctx, *document), w, options)
// force/override value
options = append(options, renderer.IncludeHeaderFooter(true))
err = htmlrenderer.Render(renderer.Wrap(ctx, *document, options...), w)
if err != nil {
return errors.Wrapf(err, "error while rendering the document")
}
Expand Down
32 changes: 14 additions & 18 deletions libasciidoc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var _ = Describe("Rendering documents in HTML", func() {
verifyDocumentBody(GinkgoT(), &expectedTitle, expectedContent, source)
})

It("section levels 1 and 2", func() {
It("section levels 0 and 1", func() {
source := `= a document title
== Section A
Expand All @@ -50,7 +50,7 @@ a paragraph with *bold content*`
verifyDocumentBody(GinkgoT(), &expectedTitle, expectedContent, source)
})

It("section level 2", func() {
It("section level 1 with a paragraph", func() {
source := `== Section A
a paragraph with *bold content*`
Expand All @@ -65,14 +65,14 @@ a paragraph with *bold content*`
verifyDocumentBody(GinkgoT(), nil, expectedContent, source)
})

It("section levels 1, 2 and 3", func() {
It("section levels 0, 1 and 3", func() {
source := `= a document title
== Section A
a paragraph with *bold content*
=== Section A.a
==== Section A.a.a
a paragraph`
expectedTitle := "a document title"
Expand All @@ -82,8 +82,8 @@ a paragraph`
<div class="paragraph">
<p>a paragraph with <strong>bold content</strong></p>
</div>
<div class="sect2">
<h3 id="_section_a_a">Section A.a</h3>
<div class="sect3">
<h4 id="_section_a_a_a">Section A.a.a</h4>
<div class="paragraph">
<p>a paragraph</p>
</div>
Expand Down Expand Up @@ -136,10 +136,10 @@ a paragraph with _italic content_`

Context("Complete Document ", func() {

It("section levels 1 and 2", func() {
It("section levels 0 and 5", func() {
source := `= a document title
== Section A
====== Section A
a paragraph with *bold content*`
expectedContent := `<!DOCTYPE html>
Expand All @@ -155,15 +155,13 @@ a paragraph with *bold content*`
<h1>a document title</h1>
</div>
<div id="content">
<div class="sect1">
<h2 id="_section_a">Section A</h2>
<div class="sectionbody">
<div class="sect5">
<h6 id="_section_a">Section A</h6>
<div class="paragraph">
<p>a paragraph with <strong>bold content</strong></p>
</div>
</div>
</div>
</div>
<div id="footer">
<div id="footer-text">
Last updated {{.LastUpdated}}
Expand Down Expand Up @@ -201,16 +199,14 @@ func verifyCompleteDocument(t GinkgoTInterface, expectedContent, source string)
t.Logf("processing '%s'", source)
sourceReader := strings.NewReader(source)
resultWriter := bytes.NewBuffer(nil)
options := renderer.Options{}
options[renderer.LastUpdated] = time.Now()
err := ConvertToHTML(context.Background(), sourceReader, resultWriter, options)
lastUpdated := time.Now()
err := ConvertToHTML(context.Background(), sourceReader, resultWriter, renderer.LastUpdated(lastUpdated))
require.Nil(t, err, "Error found while parsing the document")
t.Log("Done processing document")
result := string(resultWriter.Bytes())
result := resultWriter.String()
t.Logf("** Actual output:\n`%s`\n", result)
lastUpdated, err := options.LastUpdated()
require.Nil(t, err)
expectedContent = strings.Replace(expectedContent, "{{.LastUpdated}}", *lastUpdated, 1)
expectedContent = strings.Replace(expectedContent, "{{.LastUpdated}}", lastUpdated.Format(renderer.LastUpdatedFormat), 1)
t.Logf("** expectedContent output:\n`%s`\n", expectedContent)
assert.Equal(t, expectedContent, result)
}
Loading

0 comments on commit 99b1fd9

Please sign in to comment.