Skip to content

Commit

Permalink
feat(parser): support substitutions on passthrough blocks
Browse files Browse the repository at this point in the history
supports default and custom substitutions

Fixes #772

Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon committed Oct 12, 2020
1 parent bd51a6d commit 12b1ffa
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 72 deletions.
130 changes: 123 additions & 7 deletions pkg/parser/delimited_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1747,7 +1747,9 @@ and <more text> on the +
expected := types.DraftDocument{
Elements: []interface{}{
types.FencedBlock{
Lines: [][]interface{}{},
Lines: [][]interface{}{
{},
},
},
},
}
Expand Down Expand Up @@ -1994,7 +1996,9 @@ some listing code
expected := types.DraftDocument{
Elements: []interface{}{
types.ListingBlock{
Lines: [][]interface{}{},
Lines: [][]interface{}{
{},
},
},
},
}
Expand Down Expand Up @@ -2618,6 +2622,66 @@ _foo_
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})

It("with inline link", func() {
source := `++++
http://example.com[]
++++`
expected := types.DraftDocument{
Elements: []interface{}{
types.PassthroughBlock{
Lines: [][]interface{}{
{
types.StringElement{
Content: "http://example.com[]",
},
},
},
},
},
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})

It("with inline pass", func() {
source := `++++
pass:[foo]
++++`
expected := types.DraftDocument{
Elements: []interface{}{
types.PassthroughBlock{
Lines: [][]interface{}{
{
types.StringElement{
Content: "pass:[foo]",
},
},
},
},
},
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})

It("with quoted text", func() {
source := `++++
*foo*
++++`
expected := types.DraftDocument{
Elements: []interface{}{
types.PassthroughBlock{
Lines: [][]interface{}{
{
types.StringElement{
Content: "*foo*",
},
},
},
},
},
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})
})

Context("passthrough open block", func() {
Expand Down Expand Up @@ -3576,6 +3640,51 @@ and <more text> on the +
}
Expect(ParseDraftDocument(s)).To(MatchDraftDocument(expected))
})

It("should apply the 'quotes' substitutions on a passthrough block", func() {
source := `[subs=quotes]
.a title
++++
_foo_
*bar*
++++`
expected := types.DraftDocument{
Elements: []interface{}{
types.PassthroughBlock{
Attributes: types.Attributes{
types.AttrSubstitutions: "quotes",
types.AttrTitle: "a title",
},
Lines: [][]interface{}{
{
types.QuotedText{
Kind: types.Italic,
Elements: []interface{}{
types.StringElement{
Content: "foo",
},
},
},
},
{},
{
types.QuotedText{
Kind: types.Bold,
Elements: []interface{}{
types.StringElement{
Content: "bar",
},
},
},
},
},
},
},
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))

})
})

Context("admonition block", func() {
Expand Down Expand Up @@ -3606,7 +3715,6 @@ foo
},
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))

})

It("as admonition", func() {
Expand Down Expand Up @@ -4017,7 +4125,9 @@ ____`
Attributes: types.Attributes{
types.AttrKind: types.Verse,
},
Lines: [][]interface{}{},
Lines: [][]interface{}{
{},
},
},
},
}
Expand Down Expand Up @@ -5710,7 +5820,9 @@ bar
expected := types.Document{
Elements: []interface{}{
types.FencedBlock{
Lines: [][]interface{}{},
Lines: [][]interface{}{
{},
},
},
},
}
Expand Down Expand Up @@ -5954,7 +6066,9 @@ some listing code
expected := types.Document{
Elements: []interface{}{
types.ListingBlock{
Lines: [][]interface{}{},
Lines: [][]interface{}{
{},
},
},
},
}
Expand Down Expand Up @@ -7030,7 +7144,9 @@ ____`
Attributes: types.Attributes{
types.AttrKind: types.Verse,
},
Lines: [][]interface{}{},
Lines: [][]interface{}{
{},
},
},
},
}
Expand Down
40 changes: 14 additions & 26 deletions pkg/parser/document_processing_apply_substitutions.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func applySubstitutions(elements []interface{}, attrs types.AttributesWithOverri
// Delimited Block substitutions
// ----------------------------------------------------------------------------

// blocks of blocks
var defaultSubstitutionsForBlockElements = []elementsSubstitution{
substituteInlinePassthrough,
substituteSpecialCharacters,
Expand All @@ -128,35 +129,21 @@ var defaultSubstitutionsForBlockElements = []elementsSubstitution{
var defaultExampleBlockSubstitutions = defaultSubstitutionsForBlockElements
var defaultQuoteBlockSubstitutions = defaultSubstitutionsForBlockElements
var defaultSidebarBlockSubstitutions = defaultSubstitutionsForBlockElements
var defaultVerseBlockSubstitutions = defaultSubstitutionsForBlockElements // even though it's a block of lines, not a block of blocks
var defaultParagraphSubstitutions = defaultSubstitutionsForBlockElements // even though it's a block of lines, not a block of blocks

// blocks of lines
var defaultSubstitutionsForBlockLines = []elementsSubstitution{
substituteCallouts,
substituteSpecialCharacters,
}
var defaultFencedBlockSubstitutions = defaultSubstitutionsForBlockLines
var defaultListingBlockSubstitutions = defaultSubstitutionsForBlockLines

// other blocks
var defaultPassthroughBlockSubstitutions = []elementsSubstitution{}
var defaultCommentBlockSubstitutions = []elementsSubstitution{substituteNone}

var defaultVerseBlockSubstitutions = []elementsSubstitution{
substituteInlinePassthrough,
substituteSpecialCharacters,
substituteQuotedTexts,
substituteAttributes,
substituteReplacements,
substituteInlineMacros, // substituteVerseMacros
substitutePostReplacements,
}
var defaultParagraphSubstitutions = []elementsSubstitution{
substituteInlinePassthrough,
substituteSpecialCharacters,
substituteQuotedTexts,
substituteAttributes,
substituteReplacements,
substituteInlineMacros,
substitutePostReplacements,
}

func applySubstitutionsOnMarkdownQuoteBlock(b types.MarkdownQuoteBlock, attrs types.AttributesWithOverrides) (types.MarkdownQuoteBlock, error) {
funcs := []elementsSubstitution{
substituteInlinePassthrough,
Expand Down Expand Up @@ -265,8 +252,8 @@ func defaultSubstitutionsFor(block interface{}) ([]elementsSubstitution, error)
return defaultListingBlockSubstitutions, nil
case types.VerseBlock:
return defaultVerseBlockSubstitutions, nil
// case types.MarkdownQuoteBlock:
// return defaultMarkdownQuoteSubstitutions, nil
case types.PassthroughBlock:
return defaultPassthroughBlockSubstitutions, nil
case types.CommentBlock:
return defaultCommentBlockSubstitutions, nil
case types.Paragraph:
Expand Down Expand Up @@ -594,8 +581,8 @@ func splitLines(lines [][]interface{}, _ types.AttributesWithOverrides) ([][]int
spew.Fdump(log.StandardLogger().Out, lines)
}
result := [][]interface{}{}
pendingLine := []interface{}{}
for _, line := range lines {
pendingLine := []interface{}{}
for _, element := range line {
switch element := element.(type) {
case types.StringElement:
Expand All @@ -607,19 +594,19 @@ func splitLines(lines [][]interface{}, _ types.AttributesWithOverrides) ([][]int
}
if i < len(split)-1 {
result = append(result, pendingLine)
pendingLine = []interface{}{} // reset for the next line, except for the last item
pendingLine = []interface{}{} // reset for the next line
}
}
default:
pendingLine = append(pendingLine, element)
}
}
}
if len(pendingLine) > 0 { // don't forget the last line (if applicable)
// don't forget the last line (if applicable)
result = append(result, pendingLine)
pendingLine = []interface{}{} // reset for the next line
}
if log.IsLevelEnabled(log.DebugLevel) {
log.Debug("result")
log.Debug("splitted lines")
spew.Fdump(log.StandardLogger().Out, result)
}
return result, nil
Expand Down Expand Up @@ -685,6 +672,7 @@ func applyAttributeSubstitutionsOnElement(element interface{}, attrs types.Attri
case types.ContinuedListItemElement:
e.Element, err = applyAttributeSubstitutionsOnElement(e.Element, attrs)
return e, err

case types.ExampleBlock:
e.Elements, err = applyAttributeSubstitutionsOnElements(e.Elements, attrs)
return e, err
Expand Down
36 changes: 1 addition & 35 deletions pkg/renderer/sgml/delimited_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,6 @@ import (
log "github.com/sirupsen/logrus"
)

// func (r *sgmlRenderer) renderNormalDelimitedBlock(ctx *renderer.Context, b types.NormalDelimitedBlock) (string, error) {
// log.Debugf("rendering delimited block of kind '%v'", b.Kind)
// switch b.Kind {
// case types.Example:
// return r.renderExampleBlock(ctx, b)
// case types.Quote:
// return r.renderQuoteBlock(ctx, b)
// case types.Sidebar:
// return r.renderSidebarBlock(ctx, b)
// default:
// return "", fmt.Errorf("unable to render normal delimited block of kind '%v'", b.Kind)
// }
// }

// func (r *sgmlRenderer) renderVerbatimDelimitedBlock(ctx *renderer.Context, b types.VerbatimDelimitedBlock) (string, error) {
// log.Debugf("rendering delimited block of kind '%v'", b.Kind)
// switch b.Kind {
// case types.Fenced:
// return r.renderFencedBlock(ctx, b)
// case types.Listing:
// return r.renderListingBlock(ctx, b)
// case types.Source:
// return r.renderSourceBlock(ctx, b)
// case types.Passthrough:
// return r.renderPassthrough(ctx, b)
// case types.MarkdownQuote:
// return r.renderMarkdownQuoteBlock(ctx, b)
// case types.Verse:
// return r.renderVerseBlock(ctx, b)
// default:
// return "", fmt.Errorf("unable to render verbatim delimited block of kind '%v'", b.Kind)
// }
// }

func (r *sgmlRenderer) renderFencedBlock(ctx *renderer.Context, b types.FencedBlock) (string, error) {
previousWithinDelimitedBlock := ctx.WithinDelimitedBlock
previousIncludeBlankLine := ctx.IncludeBlankLine
Expand Down Expand Up @@ -428,7 +394,7 @@ func (r *sgmlRenderer) renderVerseBlock(ctx *renderer.Context, b types.VerseBloc
ctx.WithinDelimitedBlock = previousWithinDelimitedBlock
}()
ctx.WithinDelimitedBlock = true
content, err := r.renderLines(ctx, b.Lines)
content, err := r.renderLines(ctx, discardEmptyLines(b.Lines))
if err != nil {
return "", errors.Wrap(err, "unable to render verse block")
}
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 @@ -811,6 +811,18 @@ next lines</code></pre>

Context("listing blocks", func() {

It("with no line", func() {
source := `----
----`
expected := `<div class="listingblock">
<div class="content">
<pre></pre>
</div>
</div>
`
Expect(RenderHTML(source)).To(MatchHTML(expected))
})

It("with multiple lines", func() {
source := `----
some source code
Expand Down
6 changes: 4 additions & 2 deletions pkg/renderer/sgml/html5/file_inclusion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,13 @@ include::../../../../test/includes/chapter-a.adoc[]
})

It("should include adoc file within passthrough block", func() {
Skip("missing support for passthrough blocks")
source := `++++
include::../../../../test/includes/chapter-a.adoc[]
++++`
expected := ``
expected := `= Chapter A
content
`
Expect(RenderHTML(source)).To(MatchHTML(expected))
})
})
Expand Down
6 changes: 4 additions & 2 deletions pkg/renderer/sgml/xhtml5/file_inclusion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,11 +448,13 @@ include::../../../../test/includes/chapter-a.adoc[]
})

It("should include adoc file within passthrough block", func() {
Skip("missing support for passthrough blocks")
source := `++++
include::../../../../test/includes/chapter-a.adoc[]
++++`
expected := ``
expected := `= Chapter A
content
`
Expect(RenderXHTML(source)).To(MatchHTML(expected))
})
})
Expand Down
Loading

0 comments on commit 12b1ffa

Please sign in to comment.