-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml.go
111 lines (103 loc) · 2.38 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package bbcode
import (
"github.com/sirclo-solution/bbcode/bbnode"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
"io"
"strings"
)
const (
newLine = "\n"
)
func HTMLToBBCode(r io.Reader) (res string, err error) {
bb, err := HTMLToBBNode(r)
return bb.String(), err
}
func trimHTML(data string) string {
tmp := data
tmp = strings.Replace(tmp, "\r", "", -1)
tmp = strings.Replace(tmp, "\n", "", -1)
tmp = strings.Replace(tmp, " ", "", -1)
return tmp
}
func HTMLToBBNode(r io.Reader) (res bbnode.Interface, err error) {
doc, err := html.Parse(r)
if err == nil {
res = HTMLNodeToBBNode(doc, nil)
}
return res, err
}
func HTMLNodeToBBNode(node *html.Node, parent bbnode.Interface) (res bbnode.Interface) {
if node != nil {
switch node.Type {
case html.ElementNode:
switch node.DataAtom {
case atom.Html, atom.Tbody:
//ignore tag
res = new(bbnode.Node)
case atom.Head, atom.Script, atom.Style:
//ignore all
res = nil
case atom.Body:
res = new(bbnode.Node)
case atom.Br:
res = bbnode.NewBrNode()
case atom.P:
res = bbnode.NewPNode()
case atom.B, atom.Strong:
res = bbnode.NewBBNode("b")
case atom.I, atom.Em:
res = bbnode.NewBBNode("i")
case atom.U, atom.Ins:
res = bbnode.NewBBNode("u")
case atom.S, atom.Del:
res = bbnode.NewBBNode("s")
case atom.Pre:
res = bbnode.NewBlockNode("code")
case atom.Blockquote:
res = bbnode.NewBlockNode("quote")
case atom.Img:
var url string
for _, a := range node.Attr {
if a.Key == "src" {
url = a.Val
break
}
}
res = bbnode.NewBBNode("img")
urlNode := new(bbnode.Node)
urlNode.Value = url
res.AddChild(urlNode)
case atom.A:
var url string
for _, a := range node.Attr {
if a.Key == "href" {
url = a.Val
break
}
}
res = bbnode.NewBBNode("url", url)
case atom.Ul, atom.Ol:
res = bbnode.NewBlockNode("list")
case atom.Li:
res = bbnode.NewLiNode()
case atom.Table, atom.Tr:
res = bbnode.NewBlockNode(node.Data)
case atom.Td:
res = bbnode.NewLineNode(node.Data)
default:
res = new(bbnode.Node)
}
default:
res = bbnode.NewNode(trimHTML(node.Data))
}
if res != nil {
res.SetParent(parent)
for cHTML := node.FirstChild; cHTML != nil; cHTML = cHTML.NextSibling {
cBB := HTMLNodeToBBNode(cHTML, res)
res.AddChild(cBB)
}
}
}
return res
}