-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml_convert.go
More file actions
289 lines (277 loc) · 6.84 KB
/
html_convert.go
File metadata and controls
289 lines (277 loc) · 6.84 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
287
288
289
package main
import (
"bytes"
"fmt"
"html"
"strings"
xhtml "golang.org/x/net/html"
)
// htmlToPlain converts HTML to plain text with lightweight markdown-ish markup.
// It preserves paragraphs, line breaks, headings, lists, bold/italic, code/pre, and links.
// It intentionally skips <img> src embedding by default.
func htmlToPlain(htmlSrc string) (string, error) {
doc, err := xhtml.Parse(strings.NewReader(htmlSrc))
if err != nil {
return "", err
}
var buf bytes.Buffer
var listStack []string // "ul" or "ol"
var olCounters []int
var walk func(node *xhtml.Node)
walk = func(n *xhtml.Node) {
switch n.Type {
case xhtml.TextNode:
// collapse whitespace but keep newlines produced by blocks
text := html.UnescapeString(n.Data)
// trim leading/trailing spaces unless inside <pre> or code block
if parentIsPre(n) {
buf.WriteString(text)
} else {
// collapse internal whitespace to single space
space := false
for _, r := range text {
if r == ' ' || r == '\n' || r == '\t' || r == '\r' {
space = true
} else {
if space {
buf.WriteByte(' ')
space = false
}
buf.WriteRune(r)
}
}
}
case xhtml.ElementNode:
tag := strings.ToLower(n.Data)
switch tag {
case "br":
buf.WriteString("\n")
case "p":
// ensure blank line before paragraph unless at very start
ensureTwoNewlines(&buf)
for c := n.FirstChild; c != nil; c = c.NextSibling {
walk(c)
}
ensureTwoNewlines(&buf)
return
case "div":
// treat like paragraph-ish block
ensureTwoNewlines(&buf)
for c := n.FirstChild; c != nil; c = c.NextSibling {
walk(c)
}
ensureTwoNewlines(&buf)
return
case "h1", "h2", "h3", "h4", "h5", "h6":
ensureTwoNewlines(&buf)
// heading -> prefix with #s
level := 1
if len(tag) > 1 {
fmt.Sscanf(tag[1:], "%d", &level)
}
buf.WriteString(strings.Repeat("#", level) + " ")
for c := n.FirstChild; c != nil; c = c.NextSibling {
walk(c)
}
ensureTwoNewlines(&buf)
return
case "strong", "b":
buf.WriteString(" **")
for c := n.FirstChild; c != nil; c = c.NextSibling {
walk(c)
}
buf.WriteString("**")
return
case "em", "i":
buf.WriteString(" *")
for c := n.FirstChild; c != nil; c = c.NextSibling {
walk(c)
}
buf.WriteString("*")
return
case "a":
// collect inner text and href
var inner bytes.Buffer
buf.WriteString(" ")
for c := n.FirstChild; c != nil; c = c.NextSibling {
collectText(&inner, c)
}
href := ""
for _, attr := range n.Attr {
if strings.ToLower(attr.Key) == "href" {
href = strings.TrimSpace(attr.Val)
break
}
}
plainText := strings.TrimSpace(inner.String())
if href == "" || href == plainText {
buf.WriteString(plainText)
} else {
// format: text (url)
buf.WriteString(plainText)
buf.WriteString(" (")
buf.WriteString(href)
buf.WriteString(")")
}
return
case "ul":
// unordered list
listStack = append(listStack, "ul")
for c := n.FirstChild; c != nil; c = c.NextSibling {
walk(c)
}
listStack = listStack[:len(listStack)-1]
ensureTwoNewlines(&buf)
return
case "ol":
listStack = append(listStack, "ol")
olCounters = append(olCounters, 1)
for c := n.FirstChild; c != nil; c = c.NextSibling {
walk(c)
}
if len(olCounters) > 0 {
olCounters = olCounters[:len(olCounters)-1]
}
listStack = listStack[:len(listStack)-1]
ensureTwoNewlines(&buf)
return
case "li":
// prefix depending on list type
prefix := "- "
if len(listStack) > 0 && listStack[len(listStack)-1] == "ol" {
// use top ol counter
if len(olCounters) > 0 {
i := len(olCounters) - 1
prefix = fmt.Sprintf("%d. ", olCounters[i])
olCounters[i]++
}
}
// indent based on depth
indent := strings.Repeat(" ", len(listStack)-1)
buf.WriteString(indent + prefix)
for c := n.FirstChild; c != nil; c = c.NextSibling {
walk(c)
}
buf.WriteString("\n")
return
case "pre":
ensureTwoNewlines(&buf)
buf.WriteString("```\n")
// dump raw text nodes inside pre
raw := gatherInnerText(n)
buf.WriteString(raw)
if !strings.HasSuffix(raw, "\n") {
buf.WriteString("\n")
}
buf.WriteString("```\n")
ensureTwoNewlines(&buf)
return
case "code":
// inline code: wrap in backticks unless parent is pre
if parentIsPre(n) {
// handled by pre
for c := n.FirstChild; c != nil; c = c.NextSibling {
walk(c)
}
return
}
buf.WriteString(" `")
for c := n.FirstChild; c != nil; c = c.NextSibling {
walk(c)
}
buf.WriteString("`")
return
case "img":
// skip images by default; optionally include alt text
alt := ""
src := ""
for _, a := range n.Attr {
k := strings.ToLower(a.Key)
if k == "alt" {
alt = a.Val
} else if k == "src" {
src = a.Val
}
}
if alt != "" {
buf.WriteString(" ")
}
return
default:
// generic: descend
for c := n.FirstChild; c != nil; c = c.NextSibling {
walk(c)
}
return
}
default:
// descend
for c := n.FirstChild; c != nil; c = c.NextSibling {
walk(c)
}
}
}
walk(doc)
out := strings.TrimSpace(buf.String())
// Normalize multi-blank lines to two newlines
out = normalizeBlankLines(out)
return out, nil
}
// helper: write two newlines if buffer doesn't already end with one
func ensureTwoNewlines(buf *bytes.Buffer) {
s := buf.String()
if strings.HasSuffix(s, "\n\n") {
return
}
if strings.HasSuffix(s, "\n") {
buf.WriteString("\n")
return
}
buf.WriteString("\n\n")
}
// helper: collect text nodes into a buffer (used for anchors)
func collectText(buf *bytes.Buffer, n *xhtml.Node) {
if n == nil {
return
}
if n.Type == xhtml.TextNode {
buf.WriteString(html.UnescapeString(n.Data))
return
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
collectText(buf, c)
}
}
// gatherInnerText returns the concatenated text inside a node (used for <pre>)
func gatherInnerText(n *xhtml.Node) string {
var b bytes.Buffer
var g func(*xhtml.Node)
g = func(x *xhtml.Node) {
if x.Type == xhtml.TextNode {
b.WriteString(x.Data)
return
}
for c := x.FirstChild; c != nil; c = c.NextSibling {
g(c)
}
}
g(n)
return b.String()
}
// parentIsPre detects if any ancestor is <pre>
func parentIsPre(n *xhtml.Node) bool {
for p := n.Parent; p != nil; p = p.Parent {
if p.Type == xhtml.ElementNode && strings.ToLower(p.Data) == "pre" {
return true
}
}
return false
}
// normalizeBlankLines collapse >2 blank-lines into exactly 2
func normalizeBlankLines(s string) string {
// replace 3+ newlines with exactly 2
for strings.Contains(s, "\n\n\n") {
s = strings.ReplaceAll(s, "\n\n\n", "\n\n")
}
return s
}