Skip to content

Commit

Permalink
feature(parser): custom subs on paragraphs
Browse files Browse the repository at this point in the history
Support for one or more custom substitutions
Starting with Rwline elements which are parsed
and result in other elements. Subsequent substitutions
only apply on the StringElements.
At the end of the substitutions, the lines are preserved

Also, fix issues with single line comments which should not
start with spaces before the `//` marker

Fixes bytesparadise#597

Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon committed Jul 14, 2020
1 parent 8fbc228 commit 5d5a214
Show file tree
Hide file tree
Showing 9 changed files with 5,308 additions and 4,381 deletions.
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 AsciidocRawDocument,RawFile,TextDocument,DocumentRawBlock,FileLocation,IncludedFileLine,InlineLinks,LabeledListItemTerm,NormalBlockContent,NormalParagraphContent,VerseBlockContent,MarkdownQuoteBlockAttribution,InlineElements \
-alternate-entrypoints AsciidocRawDocument,RawFile,TextDocument,DocumentRawBlock,FileLocation,IncludedFileLine,InlineLinks,LabeledListItemTerm,DefaultParagraphContent,NormalBlockContentSubstitution,VerseBlockContentSubstitution,MarkdownQuoteBlockAttribution,InlineElements,QuotedTextSubstitution,NoneSubstitution,AttributesSubstitution \
-o ./pkg/parser/parser.go ./pkg/parser/parser.peg

.PHONY: build
Expand Down
165 changes: 104 additions & 61 deletions pkg/parser/comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,6 @@ var _ = Describe("comments", func() {
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})

It("single line comment with prefixing spaces alone", func() {
source := ` // A single-line comment.`
expected := types.DraftDocument{
Blocks: []interface{}{
types.SingleLineComment{
Content: " A single-line comment.",
},
},
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})

It("single line comment with prefixing tabs alone", func() {
source := "\t\t// A single-line comment."
expected := types.DraftDocument{
Blocks: []interface{}{
types.SingleLineComment{
Content: " A single-line comment.",
},
},
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})

It("single line comment at end of line", func() {
source := `foo // A single-line comment.`
expected := types.DraftDocument{
Expand Down Expand Up @@ -95,37 +71,78 @@ another line`
},
},
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
result, err := ParseDraftDocument(source)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(MatchDraftDocument(expected))
})

It("single line comment within a paragraph with tab", func() {
source := `a first line
Context("invalid", func() {

It("single line comment with prefixing spaces alone", func() {
source := ` // A single-line comment.`
expected := types.DraftDocument{
Blocks: []interface{}{
types.LiteralBlock{
Attributes: types.Attributes{
types.AttrKind: types.Literal,
types.AttrLiteralBlockType: types.LiteralBlockWithSpacesOnFirstLine,
},
Lines: []string{
" // A single-line comment.",
},
},
},
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})

It("single line comment with prefixing tabs alone", func() {
source := "\t\t// A single-line comment."
expected := types.DraftDocument{
Blocks: []interface{}{
types.LiteralBlock{
Attributes: types.Attributes{
types.AttrKind: types.Literal,
types.AttrLiteralBlockType: types.LiteralBlockWithSpacesOnFirstLine,
},
Lines: []string{
"\t\t// A single-line comment.",
},
},
},
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})

It("single line comment within a paragraph with tab", func() {
source := `a first line
// A single-line comment.
another line`
expected := types.DraftDocument{
Blocks: []interface{}{
types.Paragraph{
Lines: []interface{}{
[]interface{}{
types.StringElement{
Content: "a first line",
expected := types.DraftDocument{
Blocks: []interface{}{
types.Paragraph{
Lines: []interface{}{
[]interface{}{
types.StringElement{
Content: "a first line",
},
},
},
[]interface{}{
types.SingleLineComment{
Content: " A single-line comment.",
[]interface{}{
types.StringElement{
Content: "\t// A single-line comment.",
},
},
},
[]interface{}{
types.StringElement{
Content: "another line",
[]interface{}{
types.StringElement{
Content: "another line",
},
},
},
},
},
},
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})
})
})

Expand Down Expand Up @@ -216,15 +233,35 @@ a second paragraph`
It("single line comment with prefixing spaces alone", func() {
source := ` // A single-line comment.`
expected := types.Document{
Elements: []interface{}{},
Elements: []interface{}{
types.LiteralBlock{
Attributes: types.Attributes{
types.AttrKind: types.Literal,
types.AttrLiteralBlockType: types.LiteralBlockWithSpacesOnFirstLine,
},
Lines: []string{
" // A single-line comment.",
},
},
},
}
Expect(ParseDocument(source)).To(MatchDocument(expected))
})

It("single line comment with prefixing tabs alone", func() {
source := "\t\t// A single-line comment."
expected := types.Document{
Elements: []interface{}{},
Elements: []interface{}{
types.LiteralBlock{
Attributes: types.Attributes{
types.AttrKind: types.Literal,
types.AttrLiteralBlockType: types.LiteralBlockWithSpacesOnFirstLine,
},
Lines: []string{
"\t\t// A single-line comment.",
},
},
},
}
Expect(ParseDocument(source)).To(MatchDocument(expected))
})
Expand Down Expand Up @@ -266,25 +303,31 @@ another line`
Expect(ParseDocument(source)).To(MatchDocument(expected))
})

It("single line comment within a paragraph with tab", func() {
source := `a first line
Context("invalid", func() {

It("single line comment within a paragraph with tab", func() {
source := `a first line
// A single-line comment.
another line`
expected := types.Document{
Elements: []interface{}{
types.Paragraph{
Lines: []interface{}{
[]interface{}{
types.StringElement{Content: "a first line"},
},
[]interface{}{
types.StringElement{Content: "another line"},
expected := types.Document{
Elements: []interface{}{
types.Paragraph{
Lines: []interface{}{
[]interface{}{
types.StringElement{Content: "a first line"},
},
[]interface{}{
types.StringElement{Content: "\t// A single-line comment."},
},
[]interface{}{
types.StringElement{Content: "another line"},
},
},
},
},
},
}
Expect(ParseDocument(source)).To(MatchDocument(expected))
}
Expect(ParseDocument(source)).To(MatchDocument(expected))
})
})
})

Expand Down
37 changes: 37 additions & 0 deletions pkg/parser/delimited_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,39 @@ var _ = Describe("delimited blocks", func() {
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})

Context("with custom substitutions", func() {

It("should apply the 'none' substitution", func() {
source := "[subs=none]\n```" + "\n" +
"a http://website.com[]" + "\n" +
"and more text on the" + "\n" +
"next lines" + "\n" +
"```"
expected := types.DraftDocument{
Blocks: []interface{}{
types.DelimitedBlock{
Kind: types.Fenced,
Attributes: types.Attributes{
types.AttrSubstitutions: "none",
},
Elements: []interface{}{
types.VerbatimLine{
Content: "a http://website.com[]",
},
types.VerbatimLine{
Content: "and more text on the",
},
types.VerbatimLine{
Content: "next lines",
},
},
},
},
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})
})
})

Context("listing block", func() {
Expand Down Expand Up @@ -654,6 +687,10 @@ import <a>
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})

Context("with custom substitutions", func() {

})
})

Context("example block", func() {
Expand Down
Loading

0 comments on commit 5d5a214

Please sign in to comment.