-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
363 lines (328 loc) · 6.82 KB
/
utils.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package main
import (
"bytes"
"fmt"
"io"
"net/url"
"strconv"
"strings"
"unicode/utf8"
ghtml "html"
"golang.org/x/net/html"
)
func trimUnused(s string) string {
return strings.TrimRight(s, "¶")
}
func getTitle(n *html.Node) string {
title := getText(n)
idx := strings.IndexAny(title, "\n\r")
if idx > 0 {
title = title[:idx]
}
title = strings.TrimSpace(title)
title = trimUnused(title)
return title
}
func getText(n *html.Node) string {
if n.Type == html.TextNode {
return n.Data
}
data := ""
if n.FirstChild != nil {
for n = n.FirstChild; n != nil; n = n.NextSibling {
data += getText(n)
}
}
return data
}
func removeAttr(key string, n *html.Node) {
for i := 0; i < len(n.Attr); i++ {
if n.Attr[i].Key == key {
n.Attr = append(n.Attr[:i], n.Attr[i+1:]...)
return
}
}
return
}
func setAttr(key, val string, n *html.Node) {
attr := getAttr(key, n)
if attr != nil {
attr.Val = val
} else {
n.Attr = append(n.Attr, html.Attribute{Key: key, Val: val})
}
}
func fillUrl(head, s string) string {
u, _ := url.Parse(head)
if strings.HasPrefix(s, "//") {
return "http:" + s
}
if strings.HasPrefix(s, "/") {
return "http://" + u.Host + s
}
if idx := strings.Index(s, "://"); idx > 0 && idx < 10 {
return s
}
if strings.HasPrefix(s, "#") {
return s
}
return head + "/" + s
}
func getAttr(key string, n *html.Node) *html.Attribute {
for i := 0; i < len(n.Attr); i++ {
if n.Attr[i].Key == key {
return &n.Attr[i]
}
}
return nil
}
func walkPrint(w io.Writer, i int, n *html.Node) {
for ; n != nil; n = n.NextSibling {
if n.Type == html.TextNode && strings.TrimSpace(n.Data) == "" {
continue
}
d := getData(n)
isMostChild := getData(n.Parent).Child == n
if isMostChild {
w.Write([]byte(`<div style="background: rgba(0, 0, 100, 0.1)">`))
}
if d.Chosen || d.ChosenBy {
color := "rgb(40, 79, 40)"
if d.ChosenBy {
color = "rgba(90, 60, 30, 0.8)"
}
w.Write([]byte(`<div id="chosen" style="background: ` + color + `;color: #fff">`))
}
factor := 0
if d.Count > 0 {
factor = d.MaxChild * 100 / d.Count
}
if len([]rune(n.Data)) > 40 {
n.Data = string([]rune(n.Data)[:40])
}
if n.Type == html.ElementNode {
fmt.Fprintf(w, "%v<%v>", strings.Repeat("\t", i), n.Data)
fmt.Fprintf(w, " (%v/%v = <b>%v%%</b>) - %v\n",
d.MaxChild,
d.Count,
factor,
n.Attr,
)
} else {
fmt.Fprintf(w, "%v%v\n", strings.Repeat("\t", i), strconv.Quote(ghtml.EscapeString(n.Data)))
}
if n.FirstChild != nil {
walkPrint(w, i+1, n.FirstChild)
}
if isMostChild {
w.Write([]byte(`</div>`))
}
if d.Chosen || d.ChosenBy {
w.Write([]byte("</div>"))
}
}
}
func walk(n *html.Node) {
for ; n != nil; n = n.NextSibling {
if n.Type == html.TextNode && strings.TrimSpace(n.Data) == "" {
continue
}
getData(n.Parent).ChildSize += 1
if n.FirstChild != nil {
walk(n.FirstChild)
}
if n.Type == html.TextNode {
if n.Parent.Data != "script" {
getData(n.Parent).Count += len(strings.TrimSpace(n.Data))
}
}
getData(n.Parent).Count += getData(n).Count
if getData(n).Count > getData(n.Parent).MaxChild {
getData(n.Parent).MaxChild = getData(n).Count
getData(n.Parent).Child = n
}
}
}
func nodeFindData(data string, n *html.Node) *html.Node {
for ; n != nil; n = n.NextSibling {
if isElem(n, data) {
return n
}
if n.FirstChild != nil {
if n2 := nodeFindData(data, n.FirstChild); n2 != nil {
return n2
}
}
}
return nil
}
func nodeContain(n, target *html.Node) bool {
found := false
walkDo(n, func(n *html.Node) bool {
if n == target {
found = true
return false
}
return true
})
return found
}
func nodeFindBody(n *html.Node) *html.Node {
return nodeFindData("body", n)
}
func nodePrev(n *html.Node) *html.Node {
for n = n.PrevSibling; n != nil; n = n.PrevSibling {
if n.Type == html.TextNode {
continue
}
return n
}
return nil
}
func nodeNext(n *html.Node) *html.Node {
for n = n.NextSibling; n != nil; n = n.NextSibling {
if n.Type == html.TextNode {
continue
}
return n
}
return nil
}
func nodeJoin(n *html.Node, newNodes []*html.Node) *html.Node {
if len(newNodes) == 0 {
return n
}
p := &html.Node{
Parent: n.Parent,
PrevSibling: n.PrevSibling,
NextSibling: n.NextSibling,
Type: html.ElementNode,
Attr: []html.Attribute{
{Key: "class", Val: JOINED},
},
Namespace: JOINED,
Data: "div",
}
for _, nn := range append(newNodes, n) {
nn.PrevSibling = nil
nn.Parent = nil
nn.NextSibling = nil
}
for _, n := range newNodes {
p.AppendChild(n)
}
p.AppendChild(n)
return p
}
func calTextWidth(s string) int {
data := []byte(s)
size := 0
off := 0
for off < len(data) {
_, s := utf8.DecodeRune(data[off:])
if s > 1 {
size += 2
} else {
size += s
}
off += s
}
return size
}
var factor = 0.6
func nodeFindMax(n *html.Node) *html.Node {
first := n
for ; n != nil; n = n.NextSibling {
count := getData(n).Count
maxChild := getData(n).MaxChild
if count != 0 && maxChild*100/count < 60 {
nodes := make([]*html.Node, getData(n).ChildSize)
off := len(nodes) - 1
for prev := nodePrev(n); prev != nil; {
if nodeFindData("p", prev.FirstChild) != nil {
nodes[off] = prev
off--
} else {
break
}
prev = nodePrev(prev)
}
return nodeJoin(n, nodes[off+1:])
} else if getData(n).Child != nil {
return nodeFindMax(getData(n).Child)
}
}
for n = first; n != nil; n = n.NextSibling {
if n.FirstChild != nil {
if n2 := nodeFindMax(n.FirstChild); n2 != nil {
return n2
}
}
}
return nil
}
func walkDo(n *html.Node, f func(n *html.Node) bool) bool {
var next *html.Node
for ; n != nil; n = next {
next = n.NextSibling
if !f(n) {
return false
}
if n.FirstChild != nil {
if !walkDo(n.FirstChild, f) {
return false
}
}
}
return true
}
func isElem(n *html.Node, d ...string) bool {
if n.Type == html.ElementNode {
for _, dd := range d {
if n.Data == dd {
return true
}
}
}
return false
}
const (
CS_UTF8 = "utf-8"
CS_GBK = "gb2312"
CS_BIG5 = "big5"
)
func getCharset(n *html.Node) string {
head := nodeFindData("head", n)
if head == nil {
return CS_UTF8
}
meta := head.FirstChild
for ; meta != nil; meta = nodeFindData("meta", meta) {
if attr := getAttr("charset", meta); attr != nil {
if strings.Contains(attr.Val, CS_GBK) {
return CS_GBK
}
}
attr := getAttr("http-equiv", meta)
if attr == nil || attr.Val != "Content-Type" {
meta = meta.NextSibling
continue
}
attr = getAttr("content", meta)
if attr != nil {
switch {
case strings.Contains(attr.Val, CS_GBK):
return CS_GBK
case strings.Contains(attr.Val, CS_BIG5):
return CS_BIG5
}
}
meta = meta.NextSibling
}
return CS_UTF8
}
func printNodes(n *html.Node) {
buf := bytes.NewBuffer(nil)
html.Render(buf, n)
println(string(buf.Bytes()))
}