Skip to content

Commit

Permalink
feat(parser/renderer): support callouts in verbatim blocks (#567)
Browse files Browse the repository at this point in the history
- support or or more callouts in a delimited block
with verbatim content (eg: `listing` block)
- render the "callout list" with the descriptions

Fixes #562 - Support callouts in verbatim blocks

Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon authored May 23, 2020
1 parent e697bac commit 9b5a26c
Show file tree
Hide file tree
Showing 17 changed files with 4,115 additions and 2,880 deletions.
579 changes: 558 additions & 21 deletions pkg/parser/delimited_block_test.go

Large diffs are not rendered by default.

26 changes: 25 additions & 1 deletion pkg/parser/document_processing_rearrange_lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ func rearrangeListItems(blocks []interface{}, withinDelimitedBlock bool) ([]inte
result = append(result, *list)
case *types.LabeledList:
result = append(result, *list)
case *types.CalloutList:
result = append(result, *list)
}
// reset the list for further usage while processing the rest of the document
lists = []types.List{}
}
result = append(result, block)
case types.OrderedListItem, types.UnorderedListItem, types.LabeledListItem:
case types.OrderedListItem, types.UnorderedListItem, types.LabeledListItem, types.CalloutListItem:
// there's a special case: if the next list item has attributes and was preceded by a
// blank line, then we need to start a new list
if blanklineCount > 0 && len(block.(types.DocumentElement).GetAttributes()) > 0 {
Expand Down Expand Up @@ -113,6 +115,8 @@ func appendListItem(lists []types.List, item interface{}) ([]types.List, error)
return appendUnorderedListItem(lists, &item)
case types.LabeledListItem:
return appendLabeledListItem(lists, item)
case types.CalloutListItem:
return appendCalloutListItem(lists, item)
}
return lists, nil
}
Expand Down Expand Up @@ -146,6 +150,26 @@ func appendOrderedListItem(lists []types.List, item *types.OrderedListItem) ([]t
return append(lists, list), nil
}

func appendCalloutListItem(lists []types.List, item types.CalloutListItem) ([]types.List, error) {
for i, list := range lists {
if list, ok := list.(*types.CalloutList); ok {
// assume we can't have empty lists
log.Debugf("found a matching callout list")
// prune items of "deeper/lower" level
lists = pruneLists(lists, i)
// apply the same level
list.AddItem(item)
// also, prune the pointers to the remaining sublists (in case there is any...)
return lists, nil
}
}
// no match found: create a new list and if needed, adjust the level of the item
log.Debugf("adding a new callout list")
list := types.NewCalloutList(item)
// also, force the current item level to (last seen level + 1)
return append(lists, list), nil
}

func appendUnorderedListItem(lists []types.List, item *types.UnorderedListItem) ([]types.List, error) {
maxLevel := 0
log.Debugf("looking-up list for unordered list item with level=%d and bullet style=%v", item.Level, item.BulletStyle)
Expand Down
72 changes: 72 additions & 0 deletions pkg/parser/document_processing_rearrange_lists_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,4 +532,76 @@ var _ = Describe("rearrange lists", func() {
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(expected))
})

It("callout list with rich terms", func() {
actual := []interface{}{
types.CalloutListItem{
Attributes: types.ElementAttributes{
types.AttrTitle: "callout title",
},
Elements: []interface{}{
types.Paragraph{
Attributes: types.ElementAttributes{},
Lines: [][]interface{}{
{
types.StringElement{Content: "description 1"},
},
},
},
},
},
types.CalloutListItem{
Attributes: types.ElementAttributes{},
Elements: []interface{}{
types.Paragraph{
Attributes: types.ElementAttributes{},
Lines: [][]interface{}{
{
types.StringElement{Content: "description 2"},
},
},
},
},
},
}
expected := []interface{}{
types.CalloutList{
Attributes: types.ElementAttributes{
types.AttrTitle: "callout title",
},
Items: []types.CalloutListItem{
{
Attributes: types.ElementAttributes{},
Elements: []interface{}{
types.Paragraph{
Attributes: types.ElementAttributes{},
Lines: [][]interface{}{
{
types.StringElement{Content: "description 1"},
},
},
},
},
},
{
Attributes: types.ElementAttributes{},
Elements: []interface{}{
types.Paragraph{
Attributes: types.ElementAttributes{},
Lines: [][]interface{}{
{
types.StringElement{Content: "description 2"},
},
},
},
},
},
},
},
}
result, err := rearrangeListItems(actual, false)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(expected))
})

})
4 changes: 1 addition & 3 deletions pkg/parser/labeled_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,9 +664,7 @@ TIP: tip`
},
},
}
result, err := ParseDraftDocument(source)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(MatchDraftDocument(expected))
Expect(ParseDraftDocument(source)).To(Equal(expected))
})

It("labeled list with nested unordered list - case 2", func() {
Expand Down
4 changes: 1 addition & 3 deletions pkg/parser/literal_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,7 @@ a normal paragraph.`
},
},
}
result, err := ParseDraftDocument(source)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(MatchDraftDocument(expected))
Expect(ParseDraftDocument(source)).To(Equal(expected))
})
})

Expand Down
Loading

0 comments on commit 9b5a26c

Please sign in to comment.