Skip to content

Commit

Permalink
fix(parser): verify configuration attributes (bytesparadise#513)
Browse files Browse the repository at this point in the history
include all document attributes, including overrides
in the returned "parsed document" after processing the
draft document returned by the parser.
Those attributes can then be consumed by the renderer,
when applicable,

also: refactor predefined attributes (moved to pkg/types)

Fixes bytesparadise#509
Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon authored Mar 21, 2020
1 parent c3439bd commit a9da628
Show file tree
Hide file tree
Showing 17 changed files with 281 additions and 57 deletions.
61 changes: 61 additions & 0 deletions libasciidoc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,67 @@ a paragraph with _italic content_`
</div>`
Expect(RenderHTML5Body(source, configuration.WithFilename("tmp/foo.adoc"))).To(Equal(expectedContent))
})

It("document with custom icon attributes", func() {
// given
attrs := map[string]string{
"icons": "font",
"source-highlighter": "pygments",
}
source := `[source]
----
foo
----
NOTE: a note`
expected := `<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code>foo</code></pre>
</div>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
a note
</td>
</tr>
</table>
</div>`
Expect(RenderHTML5Body(source, configuration.WithAttributes(attrs))).To(Equal(expected))
})

It("document without custom icon attributes", func() {
// given
attrs := map[string]string{}
source := `[source]
----
foo
----
NOTE: a note`
expected := `<div class="listingblock">
<div class="content">
<pre class="highlight"><code>foo</code></pre>
</div>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<div class="title">Note</div>
</td>
<td class="content">
a note
</td>
</tr>
</table>
</div>`
Expect(RenderHTML5Body(source, configuration.WithAttributes(attrs))).To(Equal(expected))
})
})

Context("complete Document ", func() {
Expand Down
4 changes: 3 additions & 1 deletion pkg/parser/cross_reference_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,9 @@ with some content linked to <<thetitle,a label to the title>>!`
some content linked to xref:{foo}[another_doc()]!`
expected := types.Document{
Attributes: types.DocumentAttributes{},
Attributes: types.DocumentAttributes{
"foo": "another-doc.adoc",
},
ElementReferences: types.ElementReferences{},
Footnotes: types.Footnotes{},
FootnoteReferences: types.FootnoteReferences{},
Expand Down
47 changes: 42 additions & 5 deletions pkg/parser/document_attributes_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package parser_test

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

Expand Down Expand Up @@ -1021,10 +1022,10 @@ a paragraph`
a paragraph`
expected := types.Document{
Attributes: types.DocumentAttributes{
"toc": "",
"date": "2017-01-01",
// "author": "Xavier",
// "hardbreaks": "",
"toc": "",
"date": "2017-01-01",
"author": "Xavier",
"hardbreaks": "",
},
ElementReferences: types.ElementReferences{},
Footnotes: types.Footnotes{},
Expand All @@ -1050,7 +1051,11 @@ a paragraph`
:date: 2017-01-01
:author: Xavier`
expected := types.Document{
Attributes: types.DocumentAttributes{},
Attributes: types.DocumentAttributes{
"toc": "",
"date": "2017-01-01",
"author": "Xavier",
},
ElementReferences: types.ElementReferences{},
Footnotes: types.Footnotes{},
FootnoteReferences: types.FootnoteReferences{},
Expand Down Expand Up @@ -1272,4 +1277,36 @@ a paragraph written by {author}.`
Expect(ParseDocument(source)).To(Equal(expected))
})
})

Context("document with attribute overrides", func() {

It("custom icon attribute", func() {
// given
attrs := map[string]string{
"icons": "font",
"source-highlighter": "pygments",
}
source := `{icons}`
expected := types.Document{
Attributes: types.DocumentAttributes{
"icons": "font",
"source-highlighter": "pygments",
},
ElementReferences: types.ElementReferences{},
Footnotes: types.Footnotes{},
FootnoteReferences: types.FootnoteReferences{},
Elements: []interface{}{
types.Paragraph{
Attributes: types.ElementAttributes{},
Lines: [][]interface{}{
{
types.StringElement{Content: "font"},
},
},
},
},
}
Expect(ParseDocument(source, configuration.WithAttributes(attrs))).To(Equal(expected))
})
})
})
4 changes: 1 addition & 3 deletions pkg/parser/document_processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ func ParseDocument(r io.Reader, config configuration.Configuration) (types.Docum
Content: types.DocumentAttributes{},
Overrides: config.AttributeOverrides,
}
// add all predefined attributes
attrs.AddAll(Predefined)
// also, add all front-matter key/values
attrs.AddAll(draftDoc.FrontMatter.Content)
// also, add all DocumentAttributeDeclaration at the top of the document
Expand Down Expand Up @@ -48,6 +46,6 @@ func ParseDocument(r io.Reader, config configuration.Configuration) (types.Docum
doc.Attributes[k] = v
}
// and add all remaining attributes, too
doc.Attributes.AddAll(draftDoc.DocumentAttributes())
doc.Attributes.AddAll(attrs.All())
return doc, nil
}
51 changes: 51 additions & 0 deletions pkg/parser/document_processing_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package parser_test

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

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("document processing", func() {

It("should retain attributes passed in configuration", func() {
source := `[source]
----
foo
----`
expected := types.Document{
Attributes: types.DocumentAttributes{
types.AttrSyntaxHighlighter: "pygments",
},
ElementReferences: types.ElementReferences{},
Footnotes: types.Footnotes{},
FootnoteReferences: types.FootnoteReferences{},
Elements: []interface{}{
types.DelimitedBlock{
Attributes: types.ElementAttributes{
types.AttrKind: types.Source,
},
Kind: types.Source,
Elements: []interface{}{
types.Paragraph{
Attributes: types.ElementAttributes{},
Lines: [][]interface{}{
{
types.StringElement{
Content: "foo",
},
},
},
},
},
},
},
}
Expect(ParseDocument(source, configuration.WithAttributes(map[string]string{
types.AttrSyntaxHighlighter: "pygments",
}))).To(Equal(expected))
})
})
28 changes: 21 additions & 7 deletions pkg/parser/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,9 @@ image::images/bar.png[]`
image::foo.png[]`
expected := types.Document{
Attributes: types.DocumentAttributes{},
Attributes: types.DocumentAttributes{
"imagesdir": "./path/to/images",
},
ElementReferences: types.ElementReferences{},
Footnotes: types.Footnotes{},
FootnoteReferences: types.FootnoteReferences{},
Expand All @@ -237,7 +239,9 @@ image::foo.png[]`
image::{dir}/foo.png[]`
expected := types.Document{
Attributes: types.DocumentAttributes{},
Attributes: types.DocumentAttributes{
"dir": "./path/to/images",
},
ElementReferences: types.ElementReferences{},
Footnotes: types.Footnotes{},
FootnoteReferences: types.FootnoteReferences{},
Expand All @@ -263,7 +267,9 @@ image::{dir}/foo.png[]`
image::foo.png[]`
expected := types.Document{
Attributes: types.DocumentAttributes{},
Attributes: types.DocumentAttributes{
"imagesdir": "./path/to/images",
},
ElementReferences: types.ElementReferences{},
Footnotes: types.Footnotes{},
FootnoteReferences: types.FootnoteReferences{},
Expand All @@ -289,7 +295,9 @@ image::foo.png[]`
image::{imagesdir}/foo.png[]`
expected := types.Document{
Attributes: types.DocumentAttributes{},
Attributes: types.DocumentAttributes{
"imagesdir": "./path/to/images",
},
ElementReferences: types.ElementReferences{},
Footnotes: types.Footnotes{},
FootnoteReferences: types.FootnoteReferences{},
Expand Down Expand Up @@ -757,7 +765,9 @@ image::{imagesdir}/foo.png[]`
an image:{dir}/foo.png[].`
expected := types.Document{
Attributes: types.DocumentAttributes{},
Attributes: types.DocumentAttributes{
"dir": "./path/to/images",
},
ElementReferences: types.ElementReferences{},
Footnotes: types.Footnotes{},
FootnoteReferences: types.FootnoteReferences{},
Expand Down Expand Up @@ -792,7 +802,9 @@ an image:{dir}/foo.png[].`
an image:foo.png[].`
expected := types.Document{
Attributes: types.DocumentAttributes{},
Attributes: types.DocumentAttributes{
"imagesdir": "./path/to/images",
},
ElementReferences: types.ElementReferences{},
Footnotes: types.Footnotes{},
FootnoteReferences: types.FootnoteReferences{},
Expand Down Expand Up @@ -827,7 +839,9 @@ an image:foo.png[].`
an image:{imagesdir}/foo.png[].`
expected := types.Document{
Attributes: types.DocumentAttributes{},
Attributes: types.DocumentAttributes{
"imagesdir": "./path/to/images",
},
ElementReferences: types.ElementReferences{},
Footnotes: types.Footnotes{},
FootnoteReferences: types.FootnoteReferences{},
Expand Down
22 changes: 17 additions & 5 deletions pkg/parser/link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,9 @@ Test 2: link:/test/a%20b[with encoded space]`
a link to {scheme}:{path}[] and https://foo.baz`

expected := types.Document{
Attributes: types.DocumentAttributes{},
Attributes: types.DocumentAttributes{
"scheme": "link",
},
ElementReferences: types.ElementReferences{},
Footnotes: types.Footnotes{},
FootnoteReferences: types.FootnoteReferences{},
Expand Down Expand Up @@ -1189,7 +1191,9 @@ a link to *{scheme}://{path}[] and https://foo.baz[]*`
a link to {url}`

expected := types.Document{
Attributes: types.DocumentAttributes{},
Attributes: types.DocumentAttributes{
"url": "https://foo2.bar", // overriden by second declaration
},
ElementReferences: types.ElementReferences{},
Footnotes: types.Footnotes{},
FootnoteReferences: types.FootnoteReferences{},
Expand Down Expand Up @@ -1225,7 +1229,10 @@ a link to {url}`
a link to {scheme}://{path} and https://foo.baz`

expected := types.Document{
Attributes: types.DocumentAttributes{},
Attributes: types.DocumentAttributes{
"scheme": "https",
"path": "foo.bar",
},
ElementReferences: types.ElementReferences{},
Footnotes: types.Footnotes{},
FootnoteReferences: types.FootnoteReferences{},
Expand Down Expand Up @@ -1272,7 +1279,10 @@ a link to {scheme}://{path} and https://foo.baz`
a link to *{scheme}://{path}[] and https://foo.baz[]*`

expected := types.Document{
Attributes: types.DocumentAttributes{},
Attributes: types.DocumentAttributes{
"scheme": "https",
"path": "foo.bar",
},
ElementReferences: types.ElementReferences{},
Footnotes: types.Footnotes{},
FootnoteReferences: types.FootnoteReferences{},
Expand Down Expand Up @@ -1326,7 +1336,9 @@ a link to *{scheme}://{path}[] and https://foo.baz[]*`
a link to {scheme}://{path} and https://foo.baz`

expected := types.Document{
Attributes: types.DocumentAttributes{},
Attributes: types.DocumentAttributes{
"scheme": "https",
},
ElementReferences: types.ElementReferences{},
Footnotes: types.Footnotes{},
FootnoteReferences: types.FootnoteReferences{},
Expand Down
6 changes: 4 additions & 2 deletions pkg/parser/section_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1557,7 +1557,7 @@ a paragraph`
}
expected := types.Document{
Attributes: types.DocumentAttributes{
types.AttrIDPrefix: "custom1a_",
types.AttrIDPrefix: "custom1b_", // overriden by second occurrence of attribute declaration
},
ElementReferences: types.ElementReferences{
"custom1a_a_header": doctitle,
Expand Down Expand Up @@ -1617,7 +1617,9 @@ a paragraph`
types.StringElement{Content: "section 1b"},
}
expected := types.Document{
Attributes: types.DocumentAttributes{},
Attributes: types.DocumentAttributes{
"idprefix": "custom1b_", // overridden by second attribute declaration
},
ElementReferences: types.ElementReferences{
"_a_header": doctitle,
"custom1a_section_1a": section1aTitle,
Expand Down
2 changes: 1 addition & 1 deletion pkg/renderer/html5/delimited_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func renderSourceBlock(ctx renderer.Context, b types.DelimitedBlock) ([]byte, er
content := contentBuf.String()
language := b.Attributes.GetAsString(types.AttrLanguage)

hightligher, _ := ctx.Document.Attributes.GetAsString("source-highlighter")
hightligher, _ := ctx.Document.Attributes.GetAsString(types.AttrSyntaxHighlighter)
if language != "" && hightligher == "pygments" {
// using github.com/alecthomas/chroma to highlight the content
contentBuf = bytes.NewBuffer(nil)
Expand Down
Loading

0 comments on commit a9da628

Please sign in to comment.