Skip to content

Commit

Permalink
refactor(parser): default subs on paragraphs and delimited blocks (by…
Browse files Browse the repository at this point in the history
…tesparadise#688)

Splitting the parsing in 3 stages:
- the first parsing phase returns a `RawDocument` (new type)
which contains `RawLine` elements as the content of delimited blocks,
standalone paragraphs and paragraphs in continued list items (but
NOT in list items)
- the second phase applies all delimited block, paragraph and
attribute substitutions and produces a `DraftDocument` (which
now holds some attributes at its top-level)
- the final phase takes care about sections, lists, preamble
and table of contents (as before)

Also:
- using the new "MatchInlineElements" to check inline elements
- paragraphs lines are now `[]interface{}` instead of `[][]interface{}`,
which allows for containing a singular `types.RawLine` after the first
parsing phase.

Fixes bytesparadise#633

Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon authored Jul 5, 2020
1 parent e6437ac commit fe6293f
Show file tree
Hide file tree
Showing 82 changed files with 15,794 additions and 17,303 deletions.
16 changes: 9 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,23 @@ require (
github.com/davecgh/go-spew v1.1.1
github.com/golang/protobuf v1.3.2 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-isatty v0.0.8 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mna/pigeon v1.0.1-0.20200224192238-18953b277063
github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/onsi/ginkgo v1.12.0
github.com/onsi/gomega v1.9.0
github.com/pkg/errors v0.8.1
github.com/pkg/errors v0.9.1
github.com/sergi/go-diff v1.0.0
github.com/sirupsen/logrus v1.4.2
github.com/sirupsen/logrus v1.6.0
github.com/sozorogami/gover v0.0.0-20171022184752-b58185e213c5
github.com/spf13/cobra v0.0.5
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.5.1
github.com/stretchr/testify v1.6.1
golang.org/x/mod v0.3.0 // indirect
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e // indirect
golang.org/x/text v0.3.2 // indirect
golang.org/x/tools v0.0.0-20200502202811-ed308ab3e770 // indirect
golang.org/x/tools v0.0.0-20200702044944-0cc1aa72b347 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/yaml.v2 v2.2.8
gopkg.in/yaml.v2 v2.3.0
)
140 changes: 120 additions & 20 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion libasciidoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func Convert(r io.Reader, output io.Writer, config configuration.Configuration)
log.Debugf("rendered the output in %v", duration)
}()
log.Debugf("parsing the asciidoc source...")
doc, err := parser.ParseDocument(r, config) //, parser.Debug(true))
doc, err := parser.ParseDocument(r, config)
if err != nil {
return types.Metadata{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion make/go.mk
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ generate: install-pigeon
generate-optimized: install-pigeon
@echo "generating the parser (optimized)..."
@pigeon -optimize-parser \
-alternate-entrypoints AsciidocDocument,VerbatimDocument,TextDocument,DocumentBlock,FileLocation,IncludedFileLine,InlineLinks,LabeledListItemTerm,NormalBlockContent,VerseBlockContent,MarkdownQuoteBlockAttribution \
-alternate-entrypoints AsciidocRawDocument,RawFile,TextDocument,DocumentRawBlock,FileLocation,IncludedFileLine,InlineLinks,LabeledListItemTerm,NormalBlockContent,NormalParagraphContent,VerseBlockContent,MarkdownQuoteBlockAttribution,InlineElements \
-o ./pkg/parser/parser.go ./pkg/parser/parser.peg

.PHONY: build
Expand Down
12 changes: 11 additions & 1 deletion pkg/parser/attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ var _ = Describe("attributes", func() {
expected := types.DraftDocument{
Blocks: []interface{}{
types.ImageBlock{
Attributes: types.Attributes{
types.AttrImageAlt: "foo",
},
Location: types.Location{
Path: []interface{}{
types.StringElement{Content: "foo.png"},
Expand All @@ -33,6 +36,9 @@ var _ = Describe("attributes", func() {
expected := types.DraftDocument{
Blocks: []interface{}{
types.ImageBlock{
Attributes: types.Attributes{
types.AttrImageAlt: "foo",
},
Location: types.Location{
Path: []interface{}{
types.StringElement{Content: "foo.png"},
Expand All @@ -49,6 +55,9 @@ var _ = Describe("attributes", func() {
expected := types.DraftDocument{
Blocks: []interface{}{
types.ImageBlock{
Attributes: types.Attributes{
types.AttrImageAlt: "foo",
},
Location: types.Location{
Path: []interface{}{
types.StringElement{Content: "foo.png"},
Expand All @@ -66,7 +75,8 @@ var _ = Describe("attributes", func() {
Blocks: []interface{}{
types.ImageBlock{
Attributes: types.Attributes{
types.AttrWidth: "200",
types.AttrImageAlt: "foo",
types.AttrWidth: "200",
},
Location: types.Location{
Path: []interface{}{
Expand Down
140 changes: 103 additions & 37 deletions pkg/parser/blank_line_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,61 +8,127 @@ import (
. "github.com/onsi/gomega" //nolint golint
)

var _ = Describe("blank lines - draft", func() {
It("blank line between 2 paragraphs", func() {
source := `first paragraph
var _ = Describe("blank lines", func() {

Context("draft document", func() {

It("blank line between 2 paragraphs", func() {
source := `first paragraph
second paragraph`
expected := types.DraftDocument{
Blocks: []interface{}{
types.Paragraph{
Lines: [][]interface{}{
{
types.StringElement{Content: "first paragraph"},
expected := types.DraftDocument{
Blocks: []interface{}{
types.Paragraph{
Lines: []interface{}{
[]interface{}{
types.StringElement{
Content: "first paragraph",
},
},
},
},
types.BlankLine{},
types.Paragraph{
Lines: []interface{}{
[]interface{}{
types.StringElement{
Content: "second paragraph",
},
},
},
},
},
types.BlankLine{},
types.Paragraph{
Lines: [][]interface{}{
{
types.StringElement{Content: "second paragraph"},
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})
It("blank line with spaces and tabs between 2 paragraphs and after second paragraph", func() {
source := `first paragraph
second paragraph
`
expected := types.DraftDocument{
Blocks: []interface{}{
types.Paragraph{
Lines: []interface{}{
[]interface{}{
types.StringElement{
Content: "first paragraph",
},
},
},
},
types.BlankLine{},
types.BlankLine{},
types.BlankLine{},
types.Paragraph{
Lines: []interface{}{
[]interface{}{
types.StringElement{
Content: "second paragraph",
},
},
},
},
},
},
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})
})
It("blank line with spaces and tabs between 2 paragraphs and after second paragraph", func() {
source := `first paragraph

Context("final document", func() {

It("blank line between 2 paragraphs", func() {
source := `first paragraph
second paragraph`
expected := types.Document{
Elements: []interface{}{
types.Paragraph{
Lines: []interface{}{
[]interface{}{
types.StringElement{Content: "first paragraph"},
},
},
},
types.Paragraph{
Lines: []interface{}{
[]interface{}{
types.StringElement{Content: "second paragraph"},
},
},
},
},
}
Expect(ParseDocument(source)).To(MatchDocument(expected))
})
It("blank line with spaces and tabs between 2 paragraphs and after second paragraph", func() {
source := `first paragraph
second paragraph
`
expected := types.DraftDocument{
Blocks: []interface{}{
types.Paragraph{
Lines: [][]interface{}{
{
types.StringElement{Content: "first paragraph"},
expected := types.Document{
Elements: []interface{}{
types.Paragraph{
Lines: []interface{}{
[]interface{}{
types.StringElement{Content: "first paragraph"},
},
},
},
},
types.BlankLine{},
types.BlankLine{},
types.BlankLine{},
types.Paragraph{
Lines: [][]interface{}{
{
types.StringElement{Content: "second paragraph"},
types.Paragraph{
Lines: []interface{}{
[]interface{}{
types.StringElement{Content: "second paragraph"},
},
},
},
},
},
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
}
Expect(ParseDocument(source)).To(MatchDocument(expected))
})
})

})
Loading

0 comments on commit fe6293f

Please sign in to comment.