Skip to content

Commit

Permalink
fix edge case in spacing
Browse files Browse the repository at this point in the history
  • Loading branch information
hownowstephen committed Jul 31, 2023
1 parent 7a3d339 commit 8e28ceb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
12 changes: 6 additions & 6 deletions setup_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package textplain_test

import (
"reflect"
"testing"

"github.com/mailproto/textplain"
Expand All @@ -16,15 +17,14 @@ func runTestCases(t *testing.T, testCases []testCase) {
}
}

func runTestCase(t *testing.T, tc testCase) {
func runTestCase(t *testing.T, tc testCase, converters ...textplain.Converter) {

converters := map[string]textplain.Converter{
"regexp": textplain.NewRegexpConverter(),
"tree": textplain.NewTreeConverter(),
if len(converters) == 0 {
converters = []textplain.Converter{textplain.NewRegexpConverter(), textplain.NewTreeConverter()}
}

for name, converter := range converters {
t.Run(name, func(tt *testing.T) {
for _, converter := range converters {
t.Run(reflect.TypeOf(converter).Elem().Name(), func(tt *testing.T) {
result, err := converter.Convert(tc.body, textplain.DefaultLineLength)
assert.Nil(tt, err)
assert.Equal(tt, tc.expect, result)
Expand Down
8 changes: 8 additions & 0 deletions textplain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,3 +548,11 @@ func TestStrippingComments(t *testing.T) {
},
})
}

func TestFixSpacing(t *testing.T) {
runTestCase(t, testCase{
name: "ends in *",
body: "<p>hello</p>*",
expect: "hello\n\n*",
})
}
6 changes: 3 additions & 3 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func (t *TreeConverter) fixSpacing(text string) string {
var inList = (processed[0] == '*' && processed[1] == ' ')

tidyLoop:
for i := 2; i < len(text); i++ {
for i := 2; i < len(text)-1; i++ {

switch processed[idx] {
case '\n':
Expand All @@ -338,7 +338,7 @@ tidyLoop:
if text[j] == '\t' || text[j] == ' ' || text[j] == '\n' {
continue
}
if text[j] == '*' && j+1 < len(text) && text[j+1] == ' ' {
if text[j] == '*' && text[j+1] == ' ' {
continue tidyLoop
}
}
Expand All @@ -364,7 +364,7 @@ tidyLoop:
idx++
}

return string(processed)
return string(append(processed, text[len(text)-1]))
}

func getAttr(n *html.Node, name string) string {
Expand Down

0 comments on commit 8e28ceb

Please sign in to comment.