-
-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/html2text' into develop
- Loading branch information
Showing
8 changed files
with
135 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Package html2text is a simple library to convert HTML to plain text | ||
package html2text | ||
|
||
import ( | ||
"bytes" | ||
"log" | ||
"regexp" | ||
"strings" | ||
|
||
"golang.org/x/net/html" | ||
) | ||
|
||
var ( | ||
re = regexp.MustCompile(`\s+`) | ||
spaceRe = regexp.MustCompile(`(?mi)<\/(div|p|td|th|h[1-6]|ul|ol|li|address|article|aside|blockquote|dl|dt|footer|header|hr|main|nav|pre|table|thead|tfoot|video)><`) | ||
brRe = regexp.MustCompile(`(?mi)<(br /|br)>`) | ||
imgRe = regexp.MustCompile(`(?mi)<(img)`) | ||
skip = make(map[string]bool) | ||
) | ||
|
||
func init() { | ||
skip["script"] = true | ||
skip["title"] = true | ||
skip["head"] = true | ||
skip["link"] = true | ||
skip["meta"] = true | ||
skip["style"] = true | ||
skip["noscript"] = true | ||
} | ||
|
||
// Strip will convert a HTML string to plain text | ||
func Strip(h string, includeLinks bool) string { | ||
h = spaceRe.ReplaceAllString(h, "</$1> <") | ||
h = brRe.ReplaceAllString(h, " ") | ||
h = imgRe.ReplaceAllString(h, " <$1") | ||
var buffer bytes.Buffer | ||
doc, err := html.Parse(strings.NewReader(h)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
extract(doc, &buffer, includeLinks) | ||
return clean(buffer.String()) | ||
} | ||
|
||
func extract(node *html.Node, buff *bytes.Buffer, includeLinks bool) { | ||
if node.Type == html.TextNode { | ||
data := node.Data | ||
if data != "" { | ||
buff.WriteString(data) | ||
} | ||
} | ||
for c := node.FirstChild; c != nil; c = c.NextSibling { | ||
if _, skip := skip[c.Data]; !skip { | ||
if includeLinks && c.Data == "a" { | ||
for _, a := range c.Attr { | ||
if a.Key == "href" && strings.HasPrefix(strings.ToLower(a.Val), "http") { | ||
buff.WriteString(" " + a.Val + " ") | ||
} | ||
} | ||
} | ||
extract(c, buff, includeLinks) | ||
} | ||
} | ||
} | ||
|
||
func clean(text string) string { | ||
// replace \uFEFF with space, see https://github.com/golang/go/issues/42274#issuecomment-1017258184 | ||
text = strings.ReplaceAll(text, string('\uFEFF'), " ") | ||
text = re.ReplaceAllString(text, " ") | ||
return strings.TrimSpace(text) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package html2text | ||
|
||
import "testing" | ||
|
||
func TestPlain(t *testing.T) { | ||
tests := map[string]string{} | ||
tests["this is a test"] = "this is a test" | ||
tests["thiS IS a Test"] = "thiS IS a Test" | ||
tests["thiS IS a Test :-)"] = "thiS IS a Test :-)" | ||
tests["<h1>This is a test.</h1> "] = "This is a test." | ||
tests["<p>Paragraph 1</p><p>Paragraph 2</p>"] = "Paragraph 1 Paragraph 2" | ||
tests["<h1>Heading</h1><p>Paragraph</p>"] = "Heading Paragraph" | ||
tests["<span>Alpha</span>bet <strong>chars</strong>"] = "Alphabet chars" | ||
tests["<span><b>A</b>lpha</span>bet chars."] = "Alphabet chars." | ||
tests["<table><tr><td>First</td><td>Second</td></table>"] = "First Second" | ||
tests[`<h1>Heading</h1> | ||
<p>Paragraph</p>`] = "Heading Paragraph" | ||
tests[`<h1>Heading</h1><p> <a href="https://github.com">linked text</a></p>`] = "Heading linked text" | ||
// broken html | ||
tests[`<h1>Heading</h3><p> <a href="https://github.com">linked text.`] = "Heading linked text." | ||
|
||
for str, expected := range tests { | ||
res := Strip(str, false) | ||
if res != expected { | ||
t.Log("error:", res, "!=", expected) | ||
t.Fail() | ||
} | ||
} | ||
} | ||
|
||
func TestWithLinks(t *testing.T) { | ||
tests := map[string]string{} | ||
tests["this is a test"] = "this is a test" | ||
tests["thiS IS a Test"] = "thiS IS a Test" | ||
tests["thiS IS a Test :-)"] = "thiS IS a Test :-)" | ||
tests["<h1>This is a test.</h1> "] = "This is a test." | ||
tests["<p>Paragraph 1</p><p>Paragraph 2</p>"] = "Paragraph 1 Paragraph 2" | ||
tests["<h1>Heading</h1><p>Paragraph</p>"] = "Heading Paragraph" | ||
tests["<span>Alpha</span>bet <strong>chars</strong>"] = "Alphabet chars" | ||
tests["<span><b>A</b>lpha</span>bet chars."] = "Alphabet chars." | ||
tests["<table><tr><td>First</td><td>Second</td></table>"] = "First Second" | ||
tests["<h1>Heading</h1><p>Paragraph</p>"] = "Heading Paragraph" | ||
tests[`<h1>Heading</h1> | ||
<p>Paragraph</p>`] = "Heading Paragraph" | ||
tests[`<h1>Heading</h1><p> <a href="https://github.com">linked text</a></p>`] = "Heading https://github.com linked text" | ||
// broken html | ||
tests[`<h1>Heading</h3><p> <a href="https://github.com">linked text.`] = "Heading https://github.com linked text." | ||
|
||
for str, expected := range tests { | ||
res := Strip(str, true) | ||
if res != expected { | ||
t.Log("error:", res, "!=", expected) | ||
t.Fail() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters