Skip to content

Commit

Permalink
fix(parser): support comments in document header (#917)
Browse files Browse the repository at this point in the history
keeps the `toc` and other document attributes attached to the header,
even when there are single line comments and block comments, making them
available later during processing of Table of Contents, for example.

fixes #916

Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon authored Jan 30, 2022
1 parent c6590e2 commit 397035b
Show file tree
Hide file tree
Showing 10 changed files with 8,520 additions and 7,925 deletions.
3 changes: 2 additions & 1 deletion pkg/parser/document_header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,8 @@ John Foo Doe <[email protected]>; Jane the_Doe <[email protected]>`
expected := &types.Document{
Elements: []interface{}{
&types.DocumentHeader{
Title: Title,
Title: Title,
Elements: nil, // single comment is filtered out
},
},
}
Expand Down
21 changes: 11 additions & 10 deletions pkg/parser/document_processing_apply_substitutions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,20 @@ var _ = Describe("apply substitutions", func() {

var c chan types.DocumentFragment
done := make(<-chan interface{})
ctx := parser.NewParseContext(configuration.NewConfiguration(
configuration.WithAttributes(map[string]interface{}{
"role1": "role_1",
"role2": "my_role_2",
"title": "Title",
"option1": "option_1",
"option2": "option_2",
"cookie": "yummy",
}),
))
var ctx *parser.ParseContext

BeforeEach(func() {
c = make(chan types.DocumentFragment, 1)
ctx = parser.NewParseContext(configuration.NewConfiguration(
configuration.WithAttributes(map[string]interface{}{
"role1": "role_1",
"role2": "my_role_2",
"title": "Title",
"option1": "option_1",
"option2": "option_2",
"cookie": "yummy",
}),
))
})

AfterEach(func() {
Expand Down
8 changes: 1 addition & 7 deletions pkg/parser/document_processing_filter_elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,6 @@ elements:
}
// also, process the content of the element to retain
switch element := element.(type) {
case *types.Paragraph:
elmts, err := doFilterOut(element.Elements, matchers...)
if err != nil {
return nil, err
}
element.Elements = elmts
case *types.DelimitedBlock:
elmts, err := doFilterOut(element.Elements, singleLineCommentMatcher, commentBlockMatcher, blanklineMatcher) // keep blanklines are not retained in delimited blocks
if err != nil {
Expand All @@ -74,7 +68,7 @@ elements:
return nil, err
}
}
case types.ListElement:
case types.WithElements:
elmts, err := doFilterOut(element.GetElements(), matchers...)
if err != nil {
return nil, err
Expand Down
115 changes: 57 additions & 58 deletions pkg/parser/document_processing_filter_elements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,64 +63,7 @@ var _ = Describe("element filters", func() {
Expect(doFilterOut(actual, allMatchers...)).To(Equal(expected))
})

// It("should remove document attribute declaration", func() {
// actual := []interface{}{
// &types.AttributeDeclaration{},
// &types.Paragraph{
// Elements: []interface{}{
// &types.StringElement{},
// },
// },
// }
// expected := []interface{}{
// &types.Paragraph{
// Elements: []interface{}{
// &types.StringElement{},
// },
// },
// }
// Expect(filterComments(actual, allMatchers...)).To(Equal(expected))
// })

// It("should remove document attribute substitution", func() {
// actual := []interface{}{
// &types.AttributeSubstitution{},
// &types.Paragraph{
// Elements: []interface{}{
// &types.StringElement{},
// },
// },
// }
// expected := []interface{}{
// &types.Paragraph{
// Elements: []interface{}{
// &types.StringElement{},
// },
// },
// }
// Expect(filterComments(actual, allMatchers...)).To(Equal(expected))
// })

// It("should remove document attribute reset", func() {
// actual := []interface{}{
// &types.AttributeReset{},
// &types.Paragraph{
// Elements: []interface{}{
// &types.StringElement{},
// },
// },
// }
// expected := []interface{}{
// &types.Paragraph{
// Elements: []interface{}{
// &types.StringElement{},
// },
// },
// }
// Expect(filterComments(actual, allMatchers...)).To(Equal(expected))
// })

It("should remove comment block", func() {
It("should remove comment block at root", func() {
actual := []interface{}{
&types.DelimitedBlock{
Kind: types.Comment,
Expand All @@ -141,6 +84,62 @@ var _ = Describe("element filters", func() {
Expect(doFilterOut(actual, allMatchers...)).To(Equal(expected))
})

It("should remove comment blocks in document header", func() {
actual := []interface{}{
&types.DocumentHeader{
Elements: []interface{}{
&types.DelimitedBlock{
Kind: types.Comment,
},
&types.AttributeDeclaration{
Name: "cookie",
Value: "yummy",
},
&types.DelimitedBlock{
Kind: types.Comment,
},
},
},
}
expected := []interface{}{
&types.DocumentHeader{
Elements: []interface{}{
&types.AttributeDeclaration{
Name: "cookie",
Value: "yummy",
},
},
},
}
Expect(doFilterOut(actual, allMatchers...)).To(Equal(expected))
})

It("should remove single line comments in document header", func() {
actual := []interface{}{
&types.DocumentHeader{
Elements: []interface{}{
&types.SingleLineComment{},
&types.AttributeDeclaration{
Name: "cookie",
Value: "yummy",
},
&types.SingleLineComment{},
},
},
}
expected := []interface{}{
&types.DocumentHeader{
Elements: []interface{}{
&types.AttributeDeclaration{
Name: "cookie",
Value: "yummy",
},
},
},
}
Expect(doFilterOut(actual, allMatchers...)).To(Equal(expected))
})

It("should remove single line comment as a block", func() {
actual := []interface{}{
&types.SingleLineComment{},
Expand Down
Loading

0 comments on commit 397035b

Please sign in to comment.