-
Notifications
You must be signed in to change notification settings - Fork 7
/
node.go
192 lines (172 loc) · 3.8 KB
/
node.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
package url2epub
import (
"iter"
"strings"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
// Node is typedef'd html.Node with helper functions attached.
type Node html.Node
// FromNode casts *html.Node into *Node.
func FromNode(n *html.Node) *Node {
if n == nil {
return nil
}
ret := Node(*n)
return &ret
}
// AsNode casts n back to *html.Node
func (n Node) AsNode() html.Node {
return html.Node(n)
}
// ForEachChild calls f on each of n's children.
//
// If f returns false, ForEachChild stops the iteration.
func (n Node) ForEachChild(f func(child *Node) bool) {
for c := n.AsNode().FirstChild; c != nil; c = c.NextSibling {
if !f(FromNode(c)) {
return
}
}
}
// Children returns an iterator for all n's children.
func (n Node) Children() iter.Seq[*Node] {
return func(yield func(*Node) bool) {
n.ForEachChild(yield)
}
}
// FindFirstAtomNode returns n itself or the first node in its descendants,
// with Type == html.ElementNode and DataAtom == a, using depth first search.
//
// If none of n's descendants matches, nil will be returned.
func (n *Node) FindFirstAtomNode(a atom.Atom) *Node {
if n == nil {
return nil
}
if node := n.AsNode(); node.Type == html.ElementNode && node.DataAtom == a {
return n
}
var found *Node
for c := range n.Children() {
if ret := c.FindFirstAtomNode(a); ret != nil {
found = ret
break
}
}
return found
}
// IsAMP returns true if root is an AMP html document.
func (n *Node) IsAMP() bool {
n = n.FindFirstAtomNode(atom.Html)
if n == nil {
return false
}
for _, attr := range n.Attr {
if attr.Key == "amp" || attr.Key == "⚡" {
return true
}
}
return false
}
// GetLang returns the lang attribute of html node, if any.
func (n *Node) GetLang() string {
n = n.FindFirstAtomNode(atom.Html)
if n == nil {
return ""
}
for _, attr := range n.Attr {
if attr.Key == langKey {
return attr.Val
}
}
return ""
}
// GetAMPurl returns the amp URL of the document, if any.
func (n *Node) GetAMPurl() string {
head := n.FindFirstAtomNode(atom.Head)
if head == nil {
return ""
}
var found string
for cc := range head.Children() {
c := cc.AsNode()
if c.Type != html.ElementNode || c.DataAtom != atom.Link {
continue
}
m := buildAttrMap(&c)
if m["rel"] == "amphtml" {
found = m["href"]
break
}
}
return found
}
// GetTitle returns the title of the document, if any.
//
// Note that if og:title exists in the meta header, it's preferred over title.
func (n *Node) GetTitle() (title string) {
defer func() {
title = html.UnescapeString(title)
}()
head := n.FindFirstAtomNode(atom.Head)
if head == nil {
return ""
}
// Try to find og:title.
for cc := range head.Children() {
c := cc.AsNode()
if c.Type != html.ElementNode || c.DataAtom != atom.Meta {
continue
}
m := buildAttrMap(&c)
if m["property"] == "og:title" {
title = m["content"]
break
}
}
if title != "" {
return title
}
titleNode := head.FindFirstAtomNode(atom.Title)
if titleNode == nil {
return ""
}
for cc := range titleNode.Children() {
c := cc.AsNode()
if c.Type == html.TextNode {
return strings.TrimSpace(c.Data)
}
}
return ""
}
// GetAuthor returns the author of the document, if any.
func (n *Node) GetAuthor() (author string) {
defer func() {
author = html.UnescapeString(author)
}()
head := n.FindFirstAtomNode(atom.Head)
if head == nil {
return ""
}
for cc := range head.Children() {
c := cc.AsNode()
if c.Type != html.ElementNode || c.DataAtom != atom.Meta {
continue
}
m := buildAttrMap(&c)
if m["name"] == "author" {
return m["content"]
}
if m["property"] == "author" {
return m["content"]
}
}
return ""
}
func buildAttrMap(node *html.Node) map[string]string {
m := make(map[string]string, len(node.Attr))
for _, attr := range node.Attr {
m[attr.Key] = attr.Val
}
return m
}