-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml.go
More file actions
286 lines (265 loc) · 6.46 KB
/
html.go
File metadata and controls
286 lines (265 loc) · 6.46 KB
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
package parser
import (
"context"
"io"
"strings"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
// HTML parses HTML using the same heading-driven section model as the
// Markdown parser: each <h1>-<h6> opens a new section whose content is
// the rendered plain text of everything up to the next heading at the
// same or shallower level.
//
// Non-structural chrome (script, style, nav, header, footer, aside) is
// skipped entirely. The parser prefers <main> / <article> as the root
// when one is present so site-wide navigation doesn't pollute the tree.
type HTML struct{}
// NewHTML returns a new HTML parser.
func NewHTML() *HTML { return &HTML{} }
// Name implements Parser.
func (*HTML) Name() string { return "html" }
// Accepts implements Parser.
func (*HTML) Accepts(contentType, filename string) bool {
switch contentType {
case "text/html", "application/xhtml+xml":
return true
}
return HasExt(filename, ".html", ".htm", ".xhtml")
}
// Parse implements Parser.
func (*HTML) Parse(_ context.Context, r io.Reader) (*ParsedDoc, error) {
root, err := html.Parse(r)
if err != nil {
return nil, err
}
docTitle := findTitle(root)
content := findMainContent(root)
if content == nil {
content = root
}
type flat struct {
level int
title string
content strings.Builder
}
flats := []*flat{{level: 0, title: ""}} // preamble bucket
current := flats[0]
// Walk depth-first. On a heading, push a new bucket. On text, append
// to current. Skip chrome and containers of their own content (we
// recurse into them so we don't lose their text).
var walk func(n *html.Node)
walk = func(n *html.Node) {
if n == nil || isChrome(n) {
return
}
if lvl := headingLevel(n); lvl > 0 {
title := strings.TrimSpace(textContent(n))
current = &flat{level: lvl, title: title}
flats = append(flats, current)
return // don't re-emit the heading text into the body
}
if n.Type == html.TextNode {
t := strings.TrimSpace(n.Data)
if t != "" {
if current.content.Len() > 0 {
current.content.WriteByte(' ')
}
current.content.WriteString(t)
}
return
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
walk(c)
}
// Insert a blank line after block-level elements for readability.
if isBlock(n) {
b := current.content.String()
if !strings.HasSuffix(b, "\n\n") {
current.content.WriteString("\n\n")
}
}
}
walk(content)
// Drop empty preamble if we found at least one real heading.
if len(flats) > 1 && flats[0].level == 0 && strings.TrimSpace(flats[0].content.String()) == "" {
flats = flats[1:]
}
// Derive title: prefer <title>, then first H1, then first bucket.
title := docTitle
if title == "" {
for _, f := range flats {
if f.level == 1 {
title = f.title
break
}
}
}
if title == "" && len(flats) > 0 {
title = flats[0].title
}
// Build the hierarchy via a level stack (same algorithm as Markdown).
rootSec := &Section{Level: 0, Title: title}
stack := []*Section{rootSec}
for _, f := range flats {
sec := Section{
Level: f.level,
Title: f.title,
Content: cleanWhitespace(f.content.String()),
}
if f.level == 0 {
if sec.Content == "" {
continue
}
sec.Level = 1
sec.Title = "Introduction"
}
for len(stack) > 1 && stack[len(stack)-1].Level >= sec.Level {
stack = stack[:len(stack)-1]
}
parent := stack[len(stack)-1]
parent.Children = append(parent.Children, sec)
tail := &parent.Children[len(parent.Children)-1]
stack = append(stack, tail)
}
return &ParsedDoc{
Title: title,
Sections: rootSec.Children,
}, nil
}
// headingLevel returns 1-6 for <h1>..<h6>, 0 otherwise.
func headingLevel(n *html.Node) int {
if n.Type != html.ElementNode {
return 0
}
switch n.DataAtom {
case atom.H1:
return 1
case atom.H2:
return 2
case atom.H3:
return 3
case atom.H4:
return 4
case atom.H5:
return 5
case atom.H6:
return 6
}
return 0
}
// isChrome returns true for elements we never want to emit into the body.
func isChrome(n *html.Node) bool {
if n.Type != html.ElementNode {
return false
}
switch n.DataAtom {
case atom.Script, atom.Style, atom.Noscript, atom.Template,
atom.Nav, atom.Header, atom.Footer, atom.Aside:
return true
}
return false
}
// isBlock returns true for elements that should introduce a paragraph
// break when rendered to plain text.
func isBlock(n *html.Node) bool {
if n.Type != html.ElementNode {
return false
}
switch n.DataAtom {
case atom.P, atom.Div, atom.Section, atom.Article, atom.Li, atom.Ul,
atom.Ol, atom.Pre, atom.Blockquote, atom.Tr, atom.Td, atom.Th,
atom.Table, atom.Br:
return true
}
return false
}
// findTitle returns the text of <title> if present.
func findTitle(root *html.Node) string {
var title string
var walk func(n *html.Node)
walk = func(n *html.Node) {
if title != "" || n == nil {
return
}
if n.Type == html.ElementNode && n.DataAtom == atom.Title {
title = strings.TrimSpace(textContent(n))
return
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
walk(c)
}
}
walk(root)
return title
}
// findMainContent prefers <main>, then <article>, then <body>.
func findMainContent(root *html.Node) *html.Node {
var main, article, body *html.Node
var walk func(n *html.Node)
walk = func(n *html.Node) {
if n == nil {
return
}
if n.Type == html.ElementNode {
switch n.DataAtom {
case atom.Main:
if main == nil {
main = n
}
case atom.Article:
if article == nil {
article = n
}
case atom.Body:
if body == nil {
body = n
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
walk(c)
}
}
walk(root)
switch {
case main != nil:
return main
case article != nil:
return article
case body != nil:
return body
}
return nil
}
// textContent returns the concatenated text of n and its descendants.
func textContent(n *html.Node) string {
var b strings.Builder
var walk func(n *html.Node)
walk = func(n *html.Node) {
if n == nil {
return
}
if n.Type == html.TextNode {
b.WriteString(n.Data)
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
walk(c)
}
}
walk(n)
return b.String()
}
// cleanWhitespace collapses runs of internal whitespace while preserving
// paragraph breaks.
func cleanWhitespace(s string) string {
paras := strings.Split(s, "\n\n")
out := make([]string, 0, len(paras))
for _, p := range paras {
p = strings.TrimSpace(strings.Join(strings.Fields(p), " "))
if p != "" {
out = append(out, p)
}
}
return strings.Join(out, "\n\n")
}