/
cleaner.go
193 lines (164 loc) · 3.51 KB
/
cleaner.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package rule
import (
"bytes"
"fmt"
"strings"
"sync"
"golang.org/x/net/html"
)
var (
mutex sync.Mutex
)
var validTag = map[string]string{
"blockquote": "",
"h1": "",
"h2": "",
"h3": "",
"h4": "",
"p": "",
"strong": "",
"span": "",
"em": "",
"li": "",
"ol": "",
"ul": "",
"code": "",
"pre": "",
}
// CleanConvertor return the title and the content of the html in cleaner way
func CleanConvertor(tr *html.Tokenizer, rules ...string) (string, bytes.Buffer) {
var title string
couldGetContent := false
titleFound := false
ruleEnable := false
var ruleTag string
var ruleValue string
ruleAttr := "class"
if len(rules) == 2 {
ruleTag = rules[0]
ruleValue = rules[1]
} else if len(rules) == 3 {
ruleTag = rules[0]
ruleAttr = rules[1]
ruleValue = rules[2]
}
for {
tt := tr.Next()
t := tr.Token()
if t.Data == "title" && tt == html.StartTagToken {
tr.Next()
title = tr.Token().Data
break
}
}
var b bytes.Buffer
tStack := NewTokenStack()
for {
tokenType := tr.Next()
if tokenType == html.ErrorToken {
break
}
token := tr.Token()
if isInvalidTag(token.Data) {
if tokenType == html.StartTagToken {
tokenType = tr.Next()
token = tr.Token()
for !isInvalidTag(token.Data) {
tokenType = tr.Next()
token = tr.Token()
}
}
}
if !titleFound && isTitleTag(token.Data) && tokenType == html.EndTagToken {
b.Reset()
titleFound = true
continue
}
if ruleTag != "" && token.Data == ruleTag {
if tokenType == html.StartTagToken {
if ruleEnable {
tStack.push(NewTagToken(ruleTag))
continue
}
for _, a := range token.Attr {
if a.Key == ruleAttr {
vals := strings.Split(a.Val, " ")
for _, val := range vals {
if val == ruleValue {
tStack.push(NewTagToken(ruleTag))
ruleEnable = true
b.Reset()
break
}
}
continue
}
}
} else if tokenType == html.EndTagToken {
if ruleEnable {
tStack.pop()
if tStack.isEmpty() {
break
}
continue
}
}
}
if token.Data == "article" {
if tokenType == html.StartTagToken {
b.Reset()
continue
} else if tokenType == html.EndTagToken {
break
}
}
if token.Data == "img" {
for _, a := range token.Attr {
if a.Key == "src" {
b.WriteString(fmt.Sprintf("<img src=\"%s\">", a.Val))
continue
}
}
}
if token.Data == "hr" || token.Data == "br" {
b.WriteString(fmt.Sprintf("<%s>", token.Data))
continue
}
if tokenType == html.StartTagToken {
mutex.Lock()
_, contains := validTag[token.Data]
mutex.Unlock()
if contains {
couldGetContent = true
b.WriteString(createStartTag(token.Data))
} else {
couldGetContent = false
}
} else if tokenType == html.TextToken && couldGetContent {
es := html.EscapeString(token.Data)
if strings.TrimSpace(es) != "" {
b.WriteString(es)
}
} else if tokenType == html.EndTagToken {
mutex.Lock()
_, has := validTag[token.Data]
mutex.Unlock()
if has {
b.WriteString(createEndTag(token.Data))
}
}
}
return title, b
}
func createStartTag(tagName string) string {
return fmt.Sprintf("<%s>", tagName)
}
func createEndTag(tagName string) string {
return fmt.Sprintf("</%s>", tagName)
}
func isInvalidTag(name string) bool {
return name == "script" || name == "style"
}
func isTitleTag(tagName string) bool {
return tagName == "h1" || tagName == "h2"
}