Skip to content

Commit ab70b34

Browse files
authored
Merge pull request #2 from TheBookPeople/unordered-lists
Add new line for unordered lists
2 parents ff40519 + 26fd74f commit ab70b34

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

html2text.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,12 @@ func HTML2Text(html string) string {
161161
shouldOutput = true
162162
tagName := strings.ToLower(html[tagStart:i])
163163

164-
if headersRE.MatchString(tagName) {
165-
if canPrintNewline {
164+
if tagName == "/ul" {
165+
outBuf.WriteString("\r\n")
166+
} else if tagName == "li" || tagName == "li/" {
167+
outBuf.WriteString("\r\n")
168+
} else if headersRE.MatchString(tagName) {
169+
if canPrintNewline {
166170
outBuf.WriteString("\r\n\r\n")
167171
}
168172
canPrintNewline = false

html2text_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package html2text
22

3-
import "testing"
4-
import . "github.com/smartystreets/goconvey/convey"
3+
import (
4+
"testing"
5+
6+
. "github.com/smartystreets/goconvey/convey"
7+
)
58

69
func TestHTML2Text(t *testing.T) {
710
Convey("HTML2Text should work", t, func() {
@@ -49,6 +52,7 @@ func TestHTML2Text(t *testing.T) {
4952
ShouldEqual, "would you pay in ¢, £, ¥ or €?")
5053
So(HTML2Text(`Tom & Jerry is not an entity`), ShouldEqual, "Tom & Jerry is not an entity")
5154
So(HTML2Text(`this &neither; as you see`), ShouldEqual, "this &neither; as you see")
55+
So(HTML2Text(`list of items<ul><li>One</li><li>Two</li><li>Three</li></ul>`), ShouldEqual, "list of items\r\nOne\r\nTwo\r\nThree\r\n")
5256
So(HTML2Text(`fish &amp; chips`), ShouldEqual, "fish & chips")
5357
So(HTML2Text(`&quot;I'm sorry, Dave. I'm afraid I can't do that.&quot; – HAL, 2001: A Space Odyssey`), ShouldEqual, "\"I'm sorry, Dave. I'm afraid I can't do that.\" – HAL, 2001: A Space Odyssey")
5458
So(HTML2Text(`Google &reg;`), ShouldEqual, "Google ®")

0 commit comments

Comments
 (0)