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

bug(parser): failure to convert nested elements #740

Merged
merged 2 commits into from
Aug 2, 2020
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
34 changes: 25 additions & 9 deletions pkg/parser/document_processing_apply_substitutions.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,17 +499,33 @@ func normalParagraph(_ ...Option) paragraphSubstitution {
}
}

func serializeParagraph(lines []interface{}) (io.Reader, error) {
buf := strings.Builder{}
for i, line := range lines {
if r, ok := line.(types.RawLine); ok {
buf.WriteString(r.Content)
if i < len(lines)-1 {
buf.WriteString("\n")
func serializeRawLines(w *strings.Builder, line interface{}) error {
if line == nil {
return nil
}
switch line := line.(type) {
case types.RawLine:
if w.Len() > 0 {
w.WriteString("\n")
}
w.WriteString(line.Content)
case []interface{}:
for _, line := range line {
if err := serializeRawLines(w, line); err != nil {
return err
}
} else {
return nil, fmt.Errorf("unexpected type of element while serializing a paragraph: '%T'", line)
}
default:
return fmt.Errorf("unexpected element type serializing raw lines: '%T'", line)
}
return nil
}

func serializeParagraph(lines []interface{}) (io.Reader, error) {
buf := &strings.Builder{}
err := serializeRawLines(buf, lines)
if err != nil {
return nil, err
}
return strings.NewReader(buf.String()), nil
}
86 changes: 86 additions & 0 deletions pkg/parser/paragraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,5 +856,91 @@ a foo image:foo.png[]`
Expect(ParseDocument(source)).To(MatchDocument(expected))
})
})

Context("list paragraphs", func() {

It("inline image within a quote", func() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll rename that test in #726, its title does not reflect the content of source

source := `item::
This is the first line of the first paragraph.
This is the second line of the first paragraph.
+
This is the first line of the continuation paragraph.
This is the second line of the continuation paragraph.
+
This is the next continuation paragraph.
+
TIP: We can embed admonitions too!
`
expected := types.Document{
Elements: []interface{}{
types.LabeledList{
Items: []types.LabeledListItem{
{
Level: 1,
Term: []interface{}{
types.StringElement{
Content: "item",
},
},
Elements: []interface{}{
types.Paragraph{
Lines: []interface{}{
[]interface{}{
types.StringElement{
Content: "This is the first line of the first paragraph.",
},
},
[]interface{}{
types.StringElement{
Content: "This is the second line of the first paragraph.",
},
},
},
},
types.Paragraph{
Lines: []interface{}{
[]interface{}{
types.StringElement{
Content: "This is the first line of the continuation paragraph.",
},
},
[]interface{}{
types.StringElement{
Content: "This is the second line of the continuation paragraph.",
},
},
},
},
types.Paragraph{
Lines: []interface{}{
[]interface{}{
types.StringElement{
Content: "This is the next continuation paragraph.",
},
},
},
},
types.Paragraph{
Attributes: types.Attributes{
types.AttrAdmonitionKind: types.Tip,
},
Lines: []interface{}{
[]interface{}{
types.StringElement{
Content: "We can embed admonitions too!",
},
},
},
},
},
},
},
},
},
}
Expect(ParseDocument(source)).To(MatchDocument(expected))
})
})

})
})