Skip to content

Commit

Permalink
refactor(types): RawLine as custom struct (#1087)
Browse files Browse the repository at this point in the history
Signed-off-by: Xavier Coulon <xcoulon@redhat.com>
  • Loading branch information
xcoulon authored Sep 25, 2022
1 parent dd9e1d1 commit d7fb02e
Show file tree
Hide file tree
Showing 15 changed files with 5,169 additions and 4,989 deletions.
4 changes: 3 additions & 1 deletion pkg/parser/attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,9 @@ var _ = Describe("attributes", func() {
Elements: []interface{}{
&types.Paragraph{
Elements: []interface{}{
types.RawLine(`image::foo.png[ "This \Backslash 2Spaced End Space " ]`),
&types.RawLine{
Content: `image::foo.png[ "This \Backslash 2Spaced End Space " ]`,
},
},
},
},
Expand Down
16 changes: 12 additions & 4 deletions pkg/parser/blank_line_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ second paragraph`
Elements: []interface{}{
&types.Paragraph{
Elements: []interface{}{
types.RawLine("first paragraph"),
&types.RawLine{
Content: "first paragraph",
},
},
},
},
Expand All @@ -47,7 +49,9 @@ second paragraph`
Elements: []interface{}{
&types.Paragraph{
Elements: []interface{}{
types.RawLine("second paragraph"),
&types.RawLine{
Content: "second paragraph",
},
},
},
},
Expand All @@ -72,7 +76,9 @@ second paragraph
Elements: []interface{}{
&types.Paragraph{
Elements: []interface{}{
types.RawLine("first paragraph"),
&types.RawLine{
Content: "first paragraph",
},
},
},
},
Expand Down Expand Up @@ -112,7 +118,9 @@ second paragraph
Elements: []interface{}{
&types.Paragraph{
Elements: []interface{}{
types.RawLine("second paragraph"),
&types.RawLine{
Content: "second paragraph",
},
},
},
},
Expand Down
12 changes: 9 additions & 3 deletions pkg/parser/delimited_block_passthrough_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ another paragraph`
types.AttrStyle: types.Passthrough,
},
Elements: []interface{}{
types.RawLine("_foo_\n"),
types.RawLine("*bar*"),
&types.RawLine{
Content: "_foo_\n",
},
&types.RawLine{
Content: "*bar*",
},
},
},
},
Expand All @@ -55,7 +59,9 @@ another paragraph`
Elements: []interface{}{
&types.Paragraph{
Elements: []interface{}{
types.RawLine("another paragraph"),
&types.RawLine{
Content: "another paragraph",
},
},
},
},
Expand Down
12 changes: 6 additions & 6 deletions pkg/parser/document_processing_apply_substitutions.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,8 @@ func extractMarkdownQuoteAttribution(elements []interface{}) ([]interface{}, str
return elements, ""
}
log.Debugf("attempting to extract markdown-style quote block author")
if l, ok := elements[len(elements)-1].(types.RawLine); ok {
a, err := ParseReader("", strings.NewReader(string(l)), Entrypoint("MarkdownQuoteAttribution"))
if l, ok := elements[len(elements)-1].(*types.RawLine); ok {
a, err := ParseReader("", strings.NewReader(l.Content), Entrypoint("MarkdownQuoteAttribution"))
// assume that the last line is not an author attribution if an error occurred
if err != nil {
log.Debugf("failed to extract markdown-style quote block author: %v", err)
Expand Down Expand Up @@ -663,8 +663,8 @@ func serialize(content interface{}) ([]byte, *placeholders, error) {
result := bytes.NewBuffer(nil)
for _, element := range content {
switch element := element.(type) {
case types.RawLine:
result.WriteString(string(element))
case *types.RawLine:
result.WriteString(element.Content)
case string:
result.WriteString(string(element))
case *types.StringElement:
Expand Down Expand Up @@ -703,8 +703,8 @@ func serializePlainText(content interface{}) (string, error) {
return "", err
}
result.WriteString(s)
case types.RawLine:
result.WriteString(string(element))
case *types.RawLine:
result.WriteString(element.Content)
case *types.StringElement:
result.WriteString(element.Content)
case *types.SpecialCharacter:
Expand Down
32 changes: 24 additions & 8 deletions pkg/parser/document_processing_apply_substitutions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,12 @@ var _ = Describe("apply substitutions", func() {
types.AttrTitle: "The Title",
},
Elements: []interface{}{
types.RawLine("a paragraph called\n"),
types.RawLine("the {title}."),
&types.RawLine{
Content: "a paragraph called\n",
},
&types.RawLine{
Content: "the {title}.",
},
},
},
},
Expand Down Expand Up @@ -245,7 +249,9 @@ var _ = Describe("apply substitutions", func() {
},
},
Elements: []interface{}{
types.RawLine("a line"),
&types.RawLine{
Content: "a line",
},
},
},
},
Expand Down Expand Up @@ -283,7 +289,9 @@ var _ = Describe("apply substitutions", func() {
Elements: []interface{}{
&types.Paragraph{
Elements: []interface{}{
types.RawLine("quoted text [.{role1}]*here*."),
&types.RawLine{
Content: "quoted text [.{role1}]*here*.",
},
},
},
},
Expand Down Expand Up @@ -326,7 +334,9 @@ var _ = Describe("apply substitutions", func() {
Elements: []interface{}{
&types.Paragraph{
Elements: []interface{}{
types.RawLine("image:{cookie}.png[[.{role1}]_yummy!_]"),
&types.RawLine{
Content: "image:{cookie}.png[[.{role1}]_yummy!_]",
},
},
},
},
Expand Down Expand Up @@ -421,7 +431,9 @@ var _ = Describe("apply substitutions", func() {
// element of header cells are wrapped in a paragraph until substitutions are applied
&types.Paragraph{
Elements: []interface{}{
types.RawLine("[.{role1}]_yummy header!_"),
&types.RawLine{
Content: "[.{role1}]_yummy header!_",
},
},
},
},
Expand All @@ -435,7 +447,9 @@ var _ = Describe("apply substitutions", func() {
Elements: []interface{}{
&types.Paragraph{
Elements: []interface{}{
types.RawLine("image:{cookie}.png[[.{role1}]_yummy row!_]"),
&types.RawLine{
Content: "image:{cookie}.png[[.{role1}]_yummy row!_]",
},
},
},
},
Expand All @@ -449,7 +463,9 @@ var _ = Describe("apply substitutions", func() {
Elements: []interface{}{
&types.Paragraph{
Elements: []interface{}{
types.RawLine("[.{role1}]_yummy footer!_"),
&types.RawLine{
Content: "[.{role1}]_yummy footer!_",
},
},
},
},
Expand Down
6 changes: 3 additions & 3 deletions pkg/parser/document_processing_arrange_lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ func arrangeLists(f types.DocumentFragment) types.DocumentFragment {
if err != nil {
return types.NewErrorFragment(f.Position, err)
}
result := types.NewDocumentFragment(f.Position, elements...)
// result := types.NewDocumentFragment(f.Position, elements...)
// if log.IsLevelEnabled(log.DebugLevel) {
// log.WithField("pipeline_task", "arrange_lists").Debugf("arranged lists: %s", spew.Sdump(result))
// }

return result
f.Elements = elements
return f
}

func arrangeListElements(elements []interface{}) ([]interface{}, error) {
Expand Down
Loading

0 comments on commit d7fb02e

Please sign in to comment.