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

refactor(parser): merge 'LiteralParagraph' and 'Paragraph' rules #1032

Merged
Merged
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 pkg/parser/attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ var _ = Describe("attributes", func() {
&types.ImageBlock{
Attributes: types.Attributes{
types.AttrImageAlt: `Quoted, Here`,
types.AttrHeight: "100", // last one wins
types.AttrHeight: "100", // named attribute one wins
types.AttrWidth: "1",
},
Location: &types.Location{
Expand Down
10 changes: 4 additions & 6 deletions pkg/parser/comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ var _ = Describe("comments", func() {
Elements: []interface{}{
&types.Paragraph{
Attributes: types.Attributes{
types.AttrStyle: types.Literal,
types.AttrLiteralBlockType: types.LiteralBlockWithSpacesOnFirstLine,
types.AttrStyle: types.LiteralParagraph,
},
Elements: []interface{}{
&types.StringElement{
Content: " // A single-line comment.",
Content: " // A single-line comment.", // spaces on first line of literal paragraphs are NOT trimmed by parser
},
},
},
Expand All @@ -46,12 +45,11 @@ var _ = Describe("comments", func() {
Elements: []interface{}{
&types.Paragraph{
Attributes: types.Attributes{
types.AttrStyle: types.Literal,
types.AttrLiteralBlockType: types.LiteralBlockWithSpacesOnFirstLine,
types.AttrStyle: types.LiteralParagraph,
},
Elements: []interface{}{
&types.StringElement{
Content: "\t\t// A single-line comment.",
Content: "\t\t// A single-line comment.", // spaces on first line of literal paragraphs are NOT trimmed by parser
},
},
},
Expand Down
66 changes: 39 additions & 27 deletions pkg/parser/delimited_block_literal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ var _ = Describe("literal blocks", func() {
Elements: []interface{}{
&types.Paragraph{
Attributes: types.Attributes{
types.AttrStyle: types.Literal,
types.AttrLiteralBlockType: types.LiteralBlockWithSpacesOnFirstLine,
types.AttrStyle: types.LiteralParagraph,
},
Elements: []interface{}{
&types.StringElement{
Content: " some literal content",
Content: " some literal content", // spaces on first line of literal paragraphs are NOT trimmed by parser
},
},
},
Expand All @@ -42,12 +41,11 @@ lines.`
Elements: []interface{}{
&types.Paragraph{
Attributes: types.Attributes{
types.AttrStyle: types.Literal,
types.AttrLiteralBlockType: types.LiteralBlockWithSpacesOnFirstLine,
types.AttrStyle: types.LiteralParagraph,
},
Elements: []interface{}{
&types.StringElement{
Content: " some literal content\non 3\nlines.",
Content: " some literal content\non 3\nlines.", // spaces on first line of literal paragraphs are NOT trimmed by parser
},
},
},
Expand All @@ -65,12 +63,11 @@ lines.`
Elements: []interface{}{
&types.Paragraph{
Attributes: types.Attributes{
types.AttrStyle: types.Literal,
types.AttrLiteralBlockType: types.LiteralBlockWithSpacesOnFirstLine,
types.AttrStyle: types.LiteralParagraph,
},
Elements: []interface{}{
&types.StringElement{
Content: " some literal content\n on 3\n lines.",
Content: " some literal content\n on 3\n lines.", // spaces on first line of literal paragraphs are NOT trimmed by parser
},
},
},
Expand All @@ -89,14 +86,13 @@ a normal paragraph.`
Elements: []interface{}{
&types.Paragraph{
Attributes: types.Attributes{
types.AttrStyle: types.Literal,
types.AttrLiteralBlockType: types.LiteralBlockWithSpacesOnFirstLine,
types.AttrID: "ID",
types.AttrTitle: "title",
types.AttrStyle: types.LiteralParagraph,
types.AttrID: "ID",
types.AttrTitle: "title",
},
Elements: []interface{}{
&types.StringElement{
Content: " some literal content",
Content: " some literal content", // spaces on first line of literal paragraphs are NOT trimmed by parser
},
},
},
Expand Down Expand Up @@ -286,8 +282,7 @@ a normal paragraph.`
Elements: []interface{}{
&types.Paragraph{
Attributes: types.Attributes{
types.AttrStyle: types.Literal,
types.AttrLiteralBlockType: types.LiteralBlockWithAttribute,
types.AttrStyle: types.Literal,
},
Elements: []interface{}{
&types.StringElement{
Expand Down Expand Up @@ -319,10 +314,9 @@ a normal paragraph.`
Elements: []interface{}{
&types.Paragraph{
Attributes: types.Attributes{
types.AttrStyle: types.Literal,
types.AttrID: "ID",
types.AttrTitle: "title",
types.AttrLiteralBlockType: types.LiteralBlockWithAttribute,
types.AttrStyle: types.Literal,
types.AttrID: "ID",
types.AttrTitle: "title",
},
Elements: []interface{}{
&types.StringElement{
Expand All @@ -344,6 +338,26 @@ a normal paragraph.`
}
Expect(ParseDocument(source)).To(MatchDocument(expected))
})

It("not an admonition", func() {
source := `[literal]
TIP: not an admonition`
expected := &types.Document{
Elements: []interface{}{
&types.Paragraph{
Attributes: types.Attributes{
types.AttrStyle: types.Literal,
},
Elements: []interface{}{
&types.StringElement{
Content: "TIP: not an admonition",
},
},
},
},
}
Expect(ParseDocument(source)).To(MatchDocument(expected))
})
})

Context("with custom substitutions", func() {
Expand Down Expand Up @@ -633,13 +647,12 @@ and <more text> on the +
},
&types.Paragraph{
Attributes: types.Attributes{
types.AttrStyle: types.Literal,
types.AttrLiteralBlockType: types.LiteralBlockWithSpacesOnFirstLine,
types.AttrSubstitutions: "quotes,macros",
types.AttrStyle: types.LiteralParagraph,
types.AttrSubstitutions: "quotes,macros",
},
Elements: []interface{}{
&types.StringElement{
Content: " a link to ",
Content: " a link to ", // spaces on first line of literal paragraphs are NOT trimmed by parser
},
&types.InlineLink{
Location: &types.Location{
Expand Down Expand Up @@ -708,9 +721,8 @@ and <more text> on the +
},
&types.Paragraph{
Attributes: types.Attributes{
types.AttrStyle: types.Literal,
types.AttrLiteralBlockType: types.LiteralBlockWithAttribute,
types.AttrSubstitutions: "quotes,macros",
types.AttrStyle: types.Literal,
types.AttrSubstitutions: "quotes,macros",
},
Elements: []interface{}{
&types.StringElement{
Expand Down
2 changes: 1 addition & 1 deletion pkg/parser/delimited_block_passthrough_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ another paragraph`
Elements: []interface{}{
&types.Paragraph{
Attributes: types.Attributes{
types.AttrStyle: "pass",
types.AttrStyle: types.Passthrough,
},
Elements: []interface{}{
types.RawLine("_foo_\n"),
Expand Down
2 changes: 1 addition & 1 deletion pkg/parser/document_preprocessing.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func preprocess(ctx *ParseContext, source io.Reader) (string, error) {
}
b.WriteString(f)
case *types.BlockDelimiter:
t.push(types.BlockDelimiterKind(e.Kind), e.Length)
t.push(e.Kind, e.Length)
ctx.opts = append(ctx.opts, withinDelimitedBlock(t.withinDelimitedBlock()))
b.WriteString(e.RawText())
case types.ConditionalInclusion:
Expand Down
2 changes: 1 addition & 1 deletion pkg/parser/document_processing_apply_substitutions.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func applySubstitutionsOnWithElements(ctx *ParseContext, b types.WithElements, o
if err := applySubstitutionsOnAttributes(ctx, b, headerSubstitutions()); err != nil {
return err
}
if s := b.GetAttributes().GetAsStringWithDefault(types.AttrStyle, ""); s == types.Passthrough {
if style, found := b.GetAttributes()[types.AttrStyle]; found && style == types.Passthrough {
log.Debugf("skipping substitutions on passthrough block of type '%T'", b)
content, _ := serialize(b.GetElements())
return b.SetElements([]interface{}{
Expand Down
18 changes: 18 additions & 0 deletions pkg/parser/document_processing_parse_fragments.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,21 @@ func (c *current) isDocumentHeaderAllowed() bool {
func (c *current) disableDocumentHeaderRule() {
c.globalStore[documentHeaderKey] = false
}

const blockAttributesKey = "block_attributes"

func (c *current) storeBlockAttributes(attributes types.Attributes) {
if log.IsLevelEnabled(log.DebugLevel) {
log.Debugf("storing block attributes in global store: %s", spew.Sdump(attributes))
}
c.globalStore[blockAttributesKey] = attributes
}

func (c *current) isWithinLiteralParagraph() bool {
if attrs, ok := c.globalStore[blockAttributesKey].(types.Attributes); ok {
log.Debugf("within literal paragraph: %t", attrs[types.AttrStyle] == types.Literal)
return attrs[types.AttrPositional1] == types.Literal || attrs[types.AttrStyle] == types.Literal
}
log.Debug("not within literal paragraph")
return false
}
2 changes: 1 addition & 1 deletion pkg/parser/mixed_lists_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ ii) ordered 1.2.ii
Elements: []interface{}{
&types.Paragraph{
Elements: []interface{}{
&types.StringElement{Content: "unordered 2.1.1\nwith some\nextra lines."}, // leading tabs are trimmed
&types.StringElement{Content: "unordered 2.1.1\n\twith some\n\textra lines."},
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/parser/ordered_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ var _ = Describe("ordered lists", func() {
&types.Paragraph{
Elements: []interface{}{
&types.StringElement{
Content: "element\non\nmultiple\nlines", // spaces are trimmed
Content: "element\n\ton\n\tmultiple\n\tlines",
},
},
},
Expand Down
Loading