Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(parser): custom subs on paragraphs and delimited blocks #725

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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