-
Notifications
You must be signed in to change notification settings - Fork 9
/
html.go
49 lines (40 loc) · 860 Bytes
/
html.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package toolkit
import (
"fmt"
"golang.org/x/net/html"
"strings"
)
func Html2text(source string) (string, error) {
// ========= Parse the HTML
doc, err := html.Parse(strings.NewReader(source))
if err != nil {
return "", err
}
res := ""
var f func(n *html.Node)
f = func(n *html.Node) {
switch n.Type {
case html.ElementNode:
switch n.Data {
case "li", "br", "p", "div", "hr":
res = fmt.Sprintf("%s\n", res)
}
case html.TextNode:
res = fmt.Sprintf("%s%s ", res, n.Data)
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
f(c)
}
}
f(doc)
// ========= Replace double spaces
resFinal := ""
for _, each := range strings.Split(res, "\n") {
each := strings.TrimSpace(each)
if each == "" {
continue
}
resFinal = fmt.Sprintf("%s\n%s", resFinal, each)
}
return strings.TrimSpace(resFinal), nil
}