Skip to content

Commit

Permalink
feat(parser/renderer): support Asciidoc content in table cells (#1020)
Browse files Browse the repository at this point in the history
support other blocks such as images

Fixes #1015

Signed-off-by: Xavier Coulon <xcoulon@redhat.com>
  • Loading branch information
xcoulon authored May 15, 2022
1 parent e157d69 commit 8c2d04a
Show file tree
Hide file tree
Showing 21 changed files with 8,526 additions and 8,056 deletions.
32 changes: 24 additions & 8 deletions pkg/parser/cross_reference_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,15 +293,23 @@ some content`
Cells: []*types.TableCell{
{
Elements: []interface{}{
&types.StringElement{
Content: "A ",
&types.Paragraph{
Elements: []interface{}{
&types.StringElement{
Content: "A",
},
},
},
},
},
{
Elements: []interface{}{
&types.StringElement{
Content: "B",
&types.Paragraph{
Elements: []interface{}{
&types.StringElement{
Content: "B",
},
},
},
},
},
Expand Down Expand Up @@ -364,15 +372,23 @@ some content`
Cells: []*types.TableCell{
{
Elements: []interface{}{
&types.StringElement{
Content: "A ",
&types.Paragraph{
Elements: []interface{}{
&types.StringElement{
Content: "A",
},
},
},
},
},
{
Elements: []interface{}{
&types.StringElement{
Content: "B",
&types.Paragraph{
Elements: []interface{}{
&types.StringElement{
Content: "B",
},
},
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/parser/document_processing_aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func aggregate(ctx *ParseContext, fragmentStream <-chan types.DocumentFragment)
if err := lvls.appendElement(e); err != nil {
return nil, err
}
case *types.BlankLine, *types.SingleLineComment:
case *types.BlankLine, *types.SinglelineComment:
// ignore
case *types.Section:
if err := e.ResolveID(attrs.allAttributes(), refs); err != nil {
Expand Down
21 changes: 14 additions & 7 deletions pkg/parser/document_processing_apply_substitutions.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ func applySubstitutionsOnFragment(ctx *ParseContext, f types.DocumentFragment) t
}

func applySubstitutionsOnElement(ctx *ParseContext, element interface{}, opts ...Option) error {
if log.IsLevelEnabled(log.DebugLevel) {
log.Debugf("applying substitutions on element of type '%T'", element)
}
switch b := element.(type) {
case *types.FrontMatter:
ctx.attributes.setAll(b.Attributes)
Expand Down Expand Up @@ -124,12 +127,19 @@ func applySubstitutionsOnTable(ctx *ParseContext, t *types.Table, opts ...Option
if err != nil {
return err
}
t.Attributes[types.AttrCols] = values
if err := t.SetColumnDefinitions(values); err != nil {
return err
}
}
if t.Header != nil {
for _, c := range t.Header.Cells {
if err := applySubstitutionsOnWithElements(ctx, c, opts...); err != nil {
return err
// assume elements to process were wrapped in a paragraph at parse time
if len(c.Elements) == 1 {
if p, ok := c.Elements[0].(*types.Paragraph); ok {
if err := applySubstitutionsOnWithElements(ctx, p, opts...); err != nil {
return err
}
}
}
}
}
Expand Down Expand Up @@ -218,9 +228,6 @@ func applySubstitutionsOnWithElements(ctx *ParseContext, b types.WithElements, o
case *types.Paragraph:
b.Elements, err = processSubstitutions(ctx, b.Elements, subs, opts...)
return err
case *types.TableCell:
b.Elements, err = processSubstitutions(ctx, b.Elements, subs, opts...)
return err
default:
for _, e := range b.GetElements() {
if err := applySubstitutionsOnElement(ctx, e, opts...); err != nil {
Expand Down Expand Up @@ -539,7 +546,7 @@ func serialize(content []interface{}) ([]byte, *placeholders) {
result.WriteString(string(element))
case types.RawLine:
result.WriteString(string(element))
case *types.SingleLineComment:
case *types.SinglelineComment:
// replace with placeholder
p := placeholders.add(element)
result.WriteString(p.String())
Expand Down
87 changes: 56 additions & 31 deletions pkg/parser/document_processing_apply_substitutions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,12 @@ var _ = Describe("apply substitutions", func() {
Cells: []*types.TableCell{
{
Elements: []interface{}{
types.RawLine("[.{role1}]_yummy header!_"),
// element of header cells are wrapped in a paragraph until substitutions are applied
&types.Paragraph{
Elements: []interface{}{
types.RawLine("[.{role1}]_yummy header!_"),
},
},
},
},
},
Expand All @@ -433,7 +438,11 @@ var _ = Describe("apply substitutions", func() {
Cells: []*types.TableCell{
{
Elements: []interface{}{
types.RawLine("image:{cookie}.png[[.{role1}]_yummy row!_]"),
&types.Paragraph{
Elements: []interface{}{
types.RawLine("image:{cookie}.png[[.{role1}]_yummy row!_]"),
},
},
},
},
},
Expand All @@ -443,7 +452,11 @@ var _ = Describe("apply substitutions", func() {
Cells: []*types.TableCell{
{
Elements: []interface{}{
types.RawLine("[.{role1}]_yummy footer!_"),
&types.Paragraph{
Elements: []interface{}{
types.RawLine("[.{role1}]_yummy footer!_"),
},
},
},
},
},
Expand All @@ -464,14 +477,18 @@ var _ = Describe("apply substitutions", func() {
Cells: []*types.TableCell{
{
Elements: []interface{}{
&types.QuotedText{
Kind: types.SingleQuoteItalic,
Attributes: types.Attributes{
types.AttrRoles: types.Roles{"role_1"},
},
&types.Paragraph{
Elements: []interface{}{
&types.StringElement{
Content: "yummy header!",
&types.QuotedText{
Kind: types.SingleQuoteItalic,
Attributes: types.Attributes{
types.AttrRoles: types.Roles{"role_1"},
},
Elements: []interface{}{
&types.StringElement{
Content: "yummy header!",
},
},
},
},
},
Expand All @@ -484,25 +501,29 @@ var _ = Describe("apply substitutions", func() {
Cells: []*types.TableCell{
{
Elements: []interface{}{
&types.InlineImage{
Attributes: types.Attributes{
types.AttrImageAlt: []interface{}{
&types.QuotedText{
Kind: types.SingleQuoteItalic,
Attributes: types.Attributes{
types.AttrRoles: types.Roles{"role_1"},
},
Elements: []interface{}{
&types.StringElement{
Content: "yummy row!",
&types.Paragraph{
Elements: []interface{}{
&types.InlineImage{
Attributes: types.Attributes{
types.AttrImageAlt: []interface{}{
&types.QuotedText{
Kind: types.SingleQuoteItalic,
Attributes: types.Attributes{
types.AttrRoles: types.Roles{"role_1"},
},
Elements: []interface{}{
&types.StringElement{
Content: "yummy row!",
},
},
},
},
},
Location: &types.Location{
Path: "yummy.png",
},
},
},
Location: &types.Location{
Path: "yummy.png",
},
},
},
},
Expand All @@ -513,14 +534,18 @@ var _ = Describe("apply substitutions", func() {
Cells: []*types.TableCell{
{
Elements: []interface{}{
&types.QuotedText{
Kind: types.SingleQuoteItalic,
Attributes: types.Attributes{
types.AttrRoles: types.Roles{"role_1"},
},
&types.Paragraph{
Elements: []interface{}{
&types.StringElement{
Content: "yummy footer!",
&types.QuotedText{
Kind: types.SingleQuoteItalic,
Attributes: types.Attributes{
types.AttrRoles: types.Roles{"role_1"},
},
Elements: []interface{}{
&types.StringElement{
Content: "yummy footer!",
},
},
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/parser/document_processing_filter_elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ type filterMatcher func(element interface{}) bool

// singleLineCommentMatcher filters the element if it is a SingleLineComment
var singleLineCommentMatcher filterMatcher = func(element interface{}) bool {
_, ok := element.(*types.SingleLineComment)
_, ok := element.(*types.SinglelineComment)
return ok
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/parser/document_processing_filter_elements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ var _ = Describe("element filters", func() {
actual := []interface{}{
&types.DocumentHeader{
Elements: []interface{}{
&types.SingleLineComment{},
&types.SinglelineComment{},
&types.AttributeDeclaration{
Name: "cookie",
Value: "yummy",
},
&types.SingleLineComment{},
&types.SinglelineComment{},
},
},
}
Expand All @@ -142,7 +142,7 @@ var _ = Describe("element filters", func() {

It("should remove single line comment as a block", func() {
actual := []interface{}{
&types.SingleLineComment{},
&types.SinglelineComment{},
&types.Paragraph{
Elements: []interface{}{
&types.StringElement{},
Expand All @@ -164,7 +164,7 @@ var _ = Describe("element filters", func() {
&types.Paragraph{
Elements: []interface{}{
&types.StringElement{},
&types.SingleLineComment{},
&types.SinglelineComment{},
},
},
}
Expand All @@ -182,7 +182,7 @@ var _ = Describe("element filters", func() {
actual := []interface{}{
&types.Paragraph{
Elements: []interface{}{
&types.SingleLineComment{},
&types.SinglelineComment{},
},
},
}
Expand Down Expand Up @@ -210,7 +210,7 @@ var _ = Describe("element filters", func() {
&types.OrderedListElement{
Elements: []interface{}{
&types.StringElement{},
&types.SingleLineComment{},
&types.SinglelineComment{},
},
},
},
Expand Down
Loading

0 comments on commit 8c2d04a

Please sign in to comment.