This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
forked from googlecodelabs/tools
/
html.go
268 lines (242 loc) · 6.4 KB
/
html.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
// Copyright 2016 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gdoc
import (
"bytes"
"strings"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
var (
// headerLevel maps HTML tags to their level in parser.HeaderNode.
headerLevel = map[atom.Atom]int{
atom.H2: 2,
atom.H3: 3,
atom.H4: 4,
atom.H5: 5,
atom.H6: 6,
}
)
// isHeader returns true if hn is one of secondary headers.
// Step header is not one of them.
func isHeader(hn *html.Node) bool {
_, ok := headerLevel[hn.DataAtom]
return ok
}
func isMeta(css cssStyle, hn *html.Node) bool {
return hasClassStyle(css, hn, "color", metaColor)
}
func isBold(css cssStyle, hn *html.Node) bool {
if hn.Type == html.TextNode {
hn = hn.Parent
}
return hn.DataAtom == atom.Strong ||
hn.DataAtom == atom.B ||
hasClassStyle(css, hn, "font-weight", "bold") ||
hasClassStyle(css, hn, "font-weight", "700")
}
func isItalic(css cssStyle, hn *html.Node) bool {
if hn.Type == html.TextNode {
hn = hn.Parent
}
return hn.DataAtom == atom.Em ||
hn.DataAtom == atom.I ||
hasClassStyle(css, hn, "font-style", "italic")
}
func isConsole(css cssStyle, hn *html.Node) bool {
if hn.Type == html.TextNode {
hn = hn.Parent
}
return hasClassStyle(css, hn, "font-family", fontConsole)
}
func isCode(css cssStyle, hn *html.Node) bool {
if hn.Type == html.TextNode {
hn = hn.Parent
}
return hasClassStyle(css, hn, "font-family", fontCode)
}
func isButton(css cssStyle, hn *html.Node) bool {
return hasClassStyle(css, hn, "background-color", buttonColor)
}
func isInfobox(css cssStyle, hn *html.Node) bool {
if hn.DataAtom != atom.Td {
return false
}
return hasClassStyle(css, hn, "background-color", ibPositiveColor) ||
isInfoboxNegative(css, hn)
}
func isInfoboxNegative(css cssStyle, hn *html.Node) bool {
if hn.DataAtom != atom.Td {
return false
}
return hasClassStyle(css, hn, "background-color", ibNegativeColor)
}
func isSurvey(css cssStyle, hn *html.Node) bool {
if hn.DataAtom != atom.Td {
return false
}
return hasClassStyle(css, hn, "background-color", surveyColor)
}
func isComment(css cssStyle, hn *html.Node) bool {
if hn.DataAtom != atom.Div {
return false
}
return hasClassStyle(css, hn, "border", "1px solid black")
}
func isTable(hn *html.Node) bool {
if hn.DataAtom != atom.Table {
return false
}
return countTwo(hn, atom.Tr) > 1 || countTwo(hn, atom.Td) > 1
}
func isList(hn *html.Node) bool {
return hn.DataAtom == atom.Ul || hn.DataAtom == atom.Ol
}
// countTwo starts counting the number of a Atom children in hn.
// It returns as soon as the count exceeds 1, so the returned value is inexact.
//
// The callers can test for > 1 to verify whether a node contains two
// or more children of the Atom a.
func countTwo(hn *html.Node, a atom.Atom) int {
var count int
for c := hn.FirstChild; c != nil; c = c.NextSibling {
if c.DataAtom == a {
count++
} else {
count += countTwo(c, a)
}
if count > 1 {
break
}
}
return count
}
// countDirect returns the number of immediate children of hn.
func countDirect(hn *html.Node) int {
var count int
for c := hn.FirstChild; c != nil; c = c.NextSibling {
count++
}
return count
}
// findAtom returns first child of root which matches a, nil otherwise.
// It returns root if it is the same Atom as a.
func findAtom(root *html.Node, a atom.Atom) *html.Node {
if root.DataAtom == a {
return root
}
for c := root.FirstChild; c != nil; c = c.NextSibling {
if v := findAtom(c, a); v != nil {
return v
}
}
return nil
}
func findChildAtoms(root *html.Node, a atom.Atom) []*html.Node {
var nodes []*html.Node
for hn := root.FirstChild; hn != nil; hn = hn.NextSibling {
if hn.DataAtom == a {
nodes = append(nodes, hn)
}
nodes = append(nodes, findChildAtoms(hn, a)...)
}
return nodes
}
// findParent is like findAtom but search is in the opposite direction.
// It is faster to look for parent than child lookup in findAtom.
func findParent(root *html.Node, a atom.Atom) *html.Node {
if root.DataAtom == a {
return root
}
for c := root.Parent; c != nil; c = c.Parent {
if c.DataAtom == a {
return c
}
}
return nil
}
var blockParents = map[atom.Atom]struct{}{
atom.H1: {},
atom.H2: {},
atom.H3: {},
atom.H4: {},
atom.H5: {},
atom.H6: {},
atom.Li: {},
atom.P: {},
atom.Div: {},
}
// findBlockParent looks up nearest block parent node of hn.
// For instance, block parent of "text" in <ul><li>text</li></ul> is <li>,
// while block parent of "text" in <p><span>text</span></p> is <p>.
func findBlockParent(hn *html.Node) *html.Node {
for p := hn.Parent; p != nil; p = p.Parent {
if _, ok := blockParents[p.DataAtom]; ok {
return p
}
}
return nil
}
// nodeAttr returns node attribute value of the key name.
// Attribute keys are case insensitive.
func nodeAttr(n *html.Node, name string) string {
name = strings.ToLower(name)
for _, a := range n.Attr {
if strings.ToLower(a.Key) == name {
return a.Val
}
}
return ""
}
// stringifyNode extracts and concatenates all text nodes starting with root.
// Line breaks are inserted at <br> and any non-<span> elements.
func stringifyNode(root *html.Node, trim bool) string {
if root.Type == html.TextNode {
s := textCleaner.Replace(root.Data)
if !trim {
return s
}
return strings.TrimSpace(s)
}
if root.DataAtom == atom.Br && !trim {
return "\n"
}
var buf bytes.Buffer
for c := root.FirstChild; c != nil; c = c.NextSibling {
if c.DataAtom == atom.A {
href := cleanURL(nodeAttr(c, "href"))
if strings.HasPrefix(href, commentPrefix) {
// skip doc comments
continue
}
}
if c.DataAtom == atom.Br {
buf.WriteRune('\n')
continue
}
if c.Type == html.TextNode {
buf.WriteString(c.Data)
continue
}
if c.DataAtom != atom.Span && c.DataAtom != atom.A {
buf.WriteRune('\n')
}
buf.WriteString(stringifyNode(c, false))
}
s := textCleaner.Replace(buf.String())
if !trim {
return s
}
return strings.TrimSpace(s)
}