Skip to content

Commit

Permalink
fix(parser): ignore special chars in passthrough blocks
Browse files Browse the repository at this point in the history
Fixes bytesparadise#746

Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon committed Oct 4, 2020
1 parent 11c69eb commit d0ab802
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 1 deletion.
29 changes: 29 additions & 0 deletions pkg/parser/delimited_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2649,6 +2649,35 @@ _foo_
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})

It("with special characters", func() {
source := `++++
<input>
<input>
++++`
expected := types.DraftDocument{
Blocks: []interface{}{
types.DelimitedBlock{
Kind: types.Passthrough,
Elements: []interface{}{
[]interface{}{
types.StringElement{
Content: "<input>",
},
},
[]interface{}{},
[]interface{}{
types.StringElement{
Content: "<input>",
},
},
},
},
},
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})
})

Context("passthrough open block", func() {
Expand Down
49 changes: 48 additions & 1 deletion pkg/parser/document_processing_apply_substitutions.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,10 @@ func applySubstitutionsOnDelimitedBlock(b types.DelimitedBlock, attrs types.Attr
switch b.Kind {
case types.Example, types.Quote, types.Sidebar:
return applyNormalBlockSubstitutions(b, attrs, options...)
case types.Fenced, types.Listing, types.Literal, types.Source, types.Passthrough:
case types.Fenced, types.Listing, types.Literal, types.Source:
return applyVerbatimBlockSubstitutions(b, attrs, options...)
case types.Passthrough:
return applyPassthroughBlockSubstitutions(b, attrs, options...)
case types.Verse:
return applyVerseBlockSubstitutions(b, attrs, options...)
case types.MarkdownQuote:
Expand Down Expand Up @@ -231,6 +233,51 @@ func applyVerbatimBlockSubstitutions(b types.DelimitedBlock, attrs types.Attribu
return b, nil
}

func applyPassthroughBlockSubstitutions(b types.DelimitedBlock, attrs types.AttributesWithOverrides, options ...Option) (types.DelimitedBlock, error) {
funcs := []elementsSubstitution{}
subs, _ := b.Attributes.GetAsString(types.AttrSubstitutions)
for _, s := range strings.Split(subs, ",") {
switch s {
case "":
funcs = append(funcs, substituteCallouts)
case "normal":
funcs = append(funcs,
substituteInlinePassthrough,
substituteSpecialCharacters,
substituteQuotedTexts,
substituteAttributes,
substituteReplacements,
substituteInlineMacros,
substitutePostReplacements)
case "specialcharacters", "specialchars":
funcs = append(funcs, substituteSpecialCharacters)
case "quotes":
funcs = append(funcs, substituteQuotedTexts)
case "attributes":
funcs = append(funcs, substituteAttributes)
case "macros":
funcs = append(funcs, substituteInlineMacros)
case "replacements":
funcs = append(funcs, substituteReplacements)
case "post_replacements":
funcs = append(funcs, substitutePostReplacements)
case "none":
funcs = append(funcs, substituteNone)
default:
return types.DelimitedBlock{}, fmt.Errorf("unsupported substitution: '%s", s)
}
}
funcs = append(funcs, splitLines)
// apply all the substitutions
var err error
for _, sub := range funcs {
if b.Elements, err = sub(b.Elements, attrs, options...); err != nil {
return types.DelimitedBlock{}, err
}
}
return b, nil
}

func applyVerseBlockSubstitutions(b types.DelimitedBlock, attrs types.AttributesWithOverrides, options ...Option) (types.DelimitedBlock, error) {
funcs := []elementsSubstitution{}
subs, _ := b.Attributes.GetAsString(types.AttrSubstitutions)
Expand Down
12 changes: 12 additions & 0 deletions pkg/renderer/sgml/html5/delimited_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,18 @@ _foo_
Expect(RenderHTML(source)).To(MatchHTML(expected))
})

It("with special characters", func() {
source := `++++
<input>
<input>
++++`
expected := `<input>
<input>
`
Expect(RenderHTML(source)).To(MatchHTML(expected))
})
})

Context("passthrough open block", func() {
Expand Down
13 changes: 13 additions & 0 deletions pkg/renderer/sgml/xhtml5/delimited_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,19 @@ _foo_
Expect(RenderXHTML(source)).To(MatchHTML(expected))
})

It("with special characters", func() {
source := `++++
<input>
<input>
++++`
expected := `<input>
<input>
`
Expect(RenderHTML(source)).To(MatchHTML(expected))
})

})

Context("passthrough open block", func() {
Expand Down

0 comments on commit d0ab802

Please sign in to comment.