forked from yizenghui/wxarticle2md
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
349 lines (315 loc) · 7.01 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
//COPYRIGHT https://github.com/golang/tools/blob/master/cmd/html2article/conv.go
package wxarticle2md
import (
"bytes"
"fmt"
"strconv"
"strings"
"unicode"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
type selector func(*html.Node) bool
type Style string
// get Text and transform the charset
func getText(n *html.Node, filter ...selector) string {
return Compress(strings.TrimSpace(text(n, filter...)))
}
//压缩字符串
//将多个空格字符压缩为一个空格
func Compress(str string) string {
buf := make([]byte, 0, len(str)/2)
buffer := bytes.NewBuffer(buf)
flag := false // 标识当前是否已经有一个空格
for _, r := range str {
if unicode.IsSpace(r) {
if flag {
continue
} else {
flag = true
r = ' '
}
} else {
flag = false
}
buffer.WriteRune(r)
}
return buffer.String()
}
func text(n *html.Node, filter ...selector) string {
if isTag(atom.Style)(n) || isTag(atom.Script)(n) || isTag(atom.Image)(n) || isTag(atom.Img)(n) || isTag(atom.Textarea)(n) || isTag(atom.Input)(n) || isTag(atom.Noscript)(n) {
return ""
}
var buf bytes.Buffer
switch n.Type {
case html.TextNode:
buf.WriteString(n.Data)
case html.ElementNode:
for c := n.FirstChild; c != nil; c = c.NextSibling {
ok := true
for _, f := range filter {
if !f(c) {
ok = false
break
}
}
if ok {
fmt.Fprint(&buf, text(c, filter...))
}
}
}
return buf.String()
}
func getHtml(n *html.Node) (str string, err error) {
var buf bytes.Buffer
err = html.Render(&buf, n)
str = buf.String()
return
}
func getImages(node *html.Node) []string {
res := []string{}
mp := make(map[string]bool)
walk(node, func(n *html.Node) bool {
if isTag(atom.Img)(n) {
if width, err := strconv.Atoi(attr(n, "width")); err == nil {
if width != 0 && width < 30 {
return false
}
}
if height, err := strconv.Atoi(attr(n, "height")); err == nil {
if height != 0 && height < 30 {
return false
}
}
// 不抓取默认不展示图片
if display := attr(n.Parent, "style"); len(display) > 0 && strings.Contains(display, "display: none") {
return false
}
if display := attr(n, "style"); len(display) > 0 && strings.Contains(display, "display: none") {
return false
}
atts := []string{"data-original", "data-echo", "data-src"}
src := ""
for _, a := range atts {
src = attr(n, a)
if len(src) > 0 {
setAttr(n, "src", src)
removeAttr(n, a)
break
}
}
if len(src) == 0 {
src = attr(n, "src")
}
excludeStrs := []string{
"w16_h16.png",
"logo.png",
"icon.png",
}
if len(src) > 0 {
for _, exc := range excludeStrs {
if strings.Contains(src, exc) {
return false
}
}
}
if _, ok := mp[src]; !ok && len(src) > 0 {
mp[src] = true
res = append(res, src)
}
return false
} else {
for c := node.FirstChild; c != nil; c = c.NextSibling {
res = append(res, getImages(c)...)
}
return false
}
})
return res
}
func isTag(a atom.Atom) selector {
return func(n *html.Node) bool {
return n.DataAtom == a
}
}
func hasAttr(key, val string) selector {
return func(n *html.Node) bool {
for _, a := range n.Attr {
if a.Key == key && a.Val == val {
return true
}
}
return false
}
}
func attr(node *html.Node, key string) (value string) {
for _, attr := range node.Attr {
if attr.Key == key {
return attr.Val
}
}
return ""
}
func findAll(node *html.Node, fn selector) (nodes []*html.Node) {
walk(node, func(n *html.Node) bool {
if fn(n) {
nodes = append(nodes, n)
}
return true
})
return
}
func find(n *html.Node, fn selector) *html.Node {
var result *html.Node
walk(n, func(n *html.Node) bool {
if result != nil {
return false
}
if fn(n) {
result = n
return false
}
return true
})
return result
}
func walk(n *html.Node, fn selector) {
if fn(n) {
for c := n.FirstChild; c != nil; c = c.NextSibling {
walk(c, fn)
}
}
}
func walkRemove(n *html.Node, fn selector, del selector) {
if del(n) {
travesRemove(n)
return
}
if fn(n) {
for c := n.FirstChild; c != nil; c = c.NextSibling {
walkRemove(c, fn, del)
}
}
}
//remove node n when using ` c := node.FirstChild; c != nil; c = c.NextSibling` traves
func travesRemove(n *html.Node) {
next := n.NextSibling
if n.Parent != nil {
n.Parent.RemoveChild(n)
}
n.NextSibling = next
}
//remove br node
func brRemove(n *html.Node) {
next := n.NextSibling
if n.Parent != nil {
if n.Parent.NextSibling != nil && n.Parent.Type == html.TextNode && n.Parent.NextSibling.Type == html.TextNode {
n.Parent.Data += fmt.Sprintf("<%s>", n.Data)
n.Parent.Data += n.Parent.NextSibling.Data
travesRemove(n.Parent.NextSibling)
}
}
n.NextSibling = next
}
func removeAttr(n *html.Node, attrName string) {
for i, a := range n.Attr {
if a.Key == attrName {
n.Attr[i], n.Attr[len(n.Attr)-1], n.Attr =
n.Attr[len(n.Attr)-1], html.Attribute{}, n.Attr[:len(n.Attr)-1]
return
}
}
}
func setAttr(n *html.Node, attrName, value string) {
for i, a := range n.Attr {
if a.Key == attrName {
n.Attr[i].Val = value
return
}
}
if len(n.Attr) == 0 {
n.Attr = make([]html.Attribute, 1)
}
n.Attr = append(n.Attr, html.Attribute{
Key: attrName,
Val: value,
})
}
func diffString(a, b string) int {
aa := []rune(a)
bb := []rune(b)
return diffRune(aa, bb)
}
func diffRune(a, b []rune) int {
var tmp []rune
if len(a) < len(b) {
tmp = a
a = b
b = tmp
}
//a should be bigger
mp := make(map[rune]bool, len(a))
mpb := make(map[rune]bool, len(b))
for _, r := range a {
mp[r] = true
}
for _, r := range b {
mpb[r] = true
}
for k, _ := range mpb {
if _, ok := mp[k]; ok {
delete(mp, k)
delete(mpb, k)
}
}
return len(mpb) + len(mp)
}
func distanceExit(a, b string, maxValue int) (dis int, ok bool) {
aa := []rune(a)
bb := []rune(b)
return distanceRuneExit(aa, bb, len(aa), len(bb), maxValue)
}
func distanceRuneExit(a, b []rune, s1 int, s2 int, maxValue int) (dis int, ok bool) {
if maxValue < 0 {
return
}
if s1 == 0 || s2 == 0 {
dis = max(s1, s2)
if dis > maxValue {
return
}
ok = true
return
}
if a[0] == b[0] {
return distanceRuneExit(a[1:], b[1:], s1-1, s2-1, maxValue)
}
var v = (1 << 31) - 1
if vv, ok := distanceRuneExit(a[1:], b[1:], s1-1, s2-1, maxValue-1); ok && vv+1 < v {
v = vv + 1
}
if vv, ok := distanceRuneExit(a, b[1:], s1, s2-1, maxValue-1); ok && vv+1 < v {
v = vv + 1
}
if vv, ok := distanceRuneExit(a[1:], b, s1-1, s2, maxValue-1); ok && vv+1 < v {
v = vv + 1
}
if v == (1<<31)-1 {
return
}
dis = v
ok = true
return
}
func max(a, b int) int {
if a < b {
return b
}
return a
}
func min(a, b int) int {
if a < b {
return a
}
return b
}