-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
84 lines (75 loc) · 1.7 KB
/
util.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
package bkhtml
import (
"bytes"
"strings"
"golang.org/x/net/html"
)
// 目標 attr 是否存在 token 內
func AttrCompare(targetAttr html.Attribute, token *html.Token) bool {
for _, attr := range token.Attr {
if targetAttr == attr {
return true
}
}
return false
}
// 目標 attrs 是否全部都在目標內
func AttrsCompare(targetAttrs []html.Attribute, token *html.Token) bool {
for _, targetAttr := range targetAttrs {
if !AttrCompare(targetAttr, token) {
return false
}
}
return true
}
func IsTextEnd(text string) bool {
// 結束符號
if text == "\n" {
return true
} else { // 結束符號含其他格式符號
contant := strings.ReplaceAll(text, "\t", "")
contant = strings.ReplaceAll(contant, "\r", "")
contant = strings.ReplaceAll(contant, " ", "")
contant = strings.ReplaceAll(contant, "\n", "")
if contant == "" {
return true
}
}
return false
}
// 取得節點附加參數
//
// @params attrKey 附加參數關鍵字
//
// @params []html.Attribute 附加參數列表
//
// @return string 附加參數資料, PS: 預設為空字串
func GetAttr(attrKey string, attrs []html.Attribute) string {
for _, attr := range attrs {
if attr.Key == attrKey {
return attr.Val
}
}
return ""
}
// 取得所有子物件內文
//
// @params *TokenObjSub 起始解點
//
// @return string 內文
func GetContext(node *TokenObjSub) string {
res := bytes.Buffer{}
if node.Res.Type == html.TextToken {
res.WriteString(node.Res.Data)
return res.String()
}
for _, subnode := range node.SubRes {
if subnode.Res.Type == html.TextToken {
res.WriteString(subnode.Res.Data)
} else {
subContext := GetContext(subnode)
res.WriteString(subContext)
}
}
return res.String()
}