Skip to content

Commit

Permalink
fix(parser/renderer): trim trailing spaces in section title (#1054)
Browse files Browse the repository at this point in the history
Fixes #1053

Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon authored Jun 23, 2022
1 parent 2375844 commit 53ab4a5
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 63 deletions.
42 changes: 39 additions & 3 deletions pkg/parser/section_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ var _ = Describe("sections", func() {
Expect(ParseDocument(source)).To(MatchDocument(expected))
})

It("header with many spaces around content", func() {
source := "= a header "
It("header with trailing spaces", func() {
source := "= a header "
expected := &types.Document{
Elements: []interface{}{
&types.DocumentHeader{
Title: []interface{}{
&types.StringElement{
Content: "a header ",
Content: "a header",
},
},
},
Expand All @@ -46,6 +46,42 @@ var _ = Describe("sections", func() {
Expect(ParseDocument(source)).To(MatchDocument(expected))
})

It("section with trailing spaces", func() {
source := "== a section "
expected := &types.Document{
Elements: []interface{}{
&types.Section{
Level: 1,
Attributes: types.Attributes{
types.AttrID: "_a_section",
},
Title: []interface{}{
&types.StringElement{
Content: "a section",
},
},
},
},
ElementReferences: types.ElementReferences{
"_a_section": []interface{}{
&types.StringElement{
Content: "a section",
},
},
},
TableOfContents: &types.TableOfContents{
MaxDepth: 2,
Sections: []*types.ToCSection{
{
ID: "_a_section",
Level: 1,
},
},
},
}
Expect(ParseDocument(source)).To(MatchDocument(expected))
})

It("header and paragraph", func() {
source := `= a header
Expand Down
4 changes: 2 additions & 2 deletions pkg/renderer/sgml/html5/cross_reference_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ with some content linked to <<thewrongtitle>>!`
Expect(RenderHTML(source)).To(MatchHTML(expected))
})

It("natural ref to section with plaintext title", func() {
It("natural ref to section with plaintext title with trailing spaces", func() {
source := `see <<Section 1>>.
== Section 1`
== Section 1 ` // trailing spaces in the title
expected := `<div class="paragraph">
<p>see <a href="#_section_1">Section 1</a>.</p>
</div>
Expand Down
4 changes: 2 additions & 2 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func NewDocumentHeader(info interface{}, extraAttrs []interface{}) (*DocumentHea
header := &DocumentHeader{}
elements := make([]interface{}, 0, 2+len(extraAttrs)) // estimated max capacity
if info, ok := info.(*DocumentInformation); ok {
header.Title = info.Title
header.Title = TrimTrailingSpaces(info.Title)
if len(info.Authors) > 0 {
// header.Authors = info.Authors
elements = append(elements, &AttributeDeclaration{
Expand Down Expand Up @@ -2492,7 +2492,7 @@ func (s *Section) SetTitle(title []interface{}) {
})
title = title[:len(title)-1]
}
s.Title = title
s.Title = TrimTrailingSpaces(title)
}

// GetAttributes returns this section's attributes
Expand Down
15 changes: 6 additions & 9 deletions pkg/types/types_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,12 @@ func stringify(element interface{}) string {
}
}

// TrimLeft returns a slice of elements where the
// `strings.TrimLeft` func was applied on the content of the first entry
// if it is a `*StringElement`
func TrimLeft(elements []interface{}, cutset string) []interface{} {
if len(elements) == 0 {
return elements
}
if first, ok := elements[0].(*StringElement); ok {
first.Content = strings.TrimLeft(first.Content, cutset)
// TrimTrailingSpaces trims trailing spaces on the last element (if applicable)
func TrimTrailingSpaces(elements []interface{}) []interface{} {
if len(elements) > 0 {
if s, ok := elements[len(elements)-1].(*StringElement); ok {
s.Content = strings.TrimRight(s.Content, " ")
}
}
return elements
}
Expand Down
117 changes: 70 additions & 47 deletions pkg/types/types_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,57 +31,80 @@ var _ = Describe("convert to inline elements", func() {
})
})

// var _ = DescribeTable("TrimLeft",
var _ = DescribeTable("TrimTrailingSpaces",

// func(source, expected []interface{}) {
func(source, expected []interface{}) {
Expect(types.TrimTrailingSpaces(source)).To(Equal(expected))
},
Entry("empty slice",
[]interface{}{},
[]interface{}{}),

Entry("single element with trailing spaces",
[]interface{}{
&types.StringElement{
Content: "pasta ", // trailing spaces
},
},
[]interface{}{
&types.StringElement{
Content: "pasta", // timmed
},
}),

// },
// Entry("empty slice",
// []interface{}{},
// []interface{}{}),
Entry("multiple elements with trailing spaces",
[]interface{}{
&types.StringElement{
Content: "cookies",
},
&types.InlineLink{},
&types.StringElement{
Content: "pasta ", // trailing spaces
},
},
[]interface{}{
&types.StringElement{
Content: "cookies",
},
&types.InlineLink{},
&types.StringElement{
Content: "pasta", // timmed
},
}),

// Entry("valid slice",
// []interface{}{
// &types.StringElement{
// Content: " cookies",
// },
// &types.StringElement{
// Content: " pasta",
// },
// },
// []interface{}{
// &types.StringElement{
// Content: "cookies", // trimmed
// },
// &types.StringElement{
// Content: " pasta",
// },
// }),
Entry("multiple elements without trailing spaces",
[]interface{}{
&types.StringElement{
Content: "cookies",
},
&types.InlineLink{},
&types.StringElement{
Content: "pasta", // no trailing spaces
},
},
[]interface{}{&types.StringElement{
Content: "cookies",
},
&types.InlineLink{},
&types.StringElement{
Content: "pasta", // no change
},
}),

// Entry("noop slice",
// []interface{}{
// &types.SpecialCharacter{
// Name: ">",
// },
// &types.StringElement{
// Content: " cookies",
// },
// &types.StringElement{
// Content: " pasta",
// },
// },
// []interface{}{
// &types.SpecialCharacter{
// Name: ">",
// },
// &types.StringElement{
// Content: " cookies", // not trimmed
// },
// &types.StringElement{
// Content: " pasta",
// },
// }),
// )
Entry("noop",
[]interface{}{
&types.StringElement{
Content: "cookies",
},
&types.InlineLink{}, // not a StringElement
},
[]interface{}{
&types.StringElement{
Content: "cookies",
},
&types.InlineLink{},
}),
)

var _ = DescribeTable("split elements per line",
func(elements []interface{}, expected [][]interface{}) {
Expand Down

0 comments on commit 53ab4a5

Please sign in to comment.