MCPcopy Create free account
hub / github.com/Mnexa-AI/e2a / htmlToText

Function htmlToText

internal/mailparse/parse.go:215–251  ·  view source on GitHub ↗

htmlToText renders HTML to plain text: drops script/style, inserts newlines at block boundaries, and decodes entities (via the tokenizer).

(h string)

Source from the content-addressed store, hash-verified

213// htmlToText renders HTML to plain text: drops script/style, inserts newlines
214// at block boundaries, and decodes entities (via the tokenizer).
215func htmlToText(h string) string {
216 z := html.NewTokenizer(strings.NewReader(h))
217 var sb strings.Builder
218 skip := 0 // depth inside script/style
219 for {
220 tt := z.Next()
221 switch tt {
222 case html.ErrorToken:
223 return sb.String()
224 case html.StartTagToken, html.SelfClosingTagToken:
225 name, _ := z.TagName()
226 tag := string(name)
227 switch tag {
228 case "script", "style", "head":
229 if tt == html.StartTagToken {
230 skip++
231 }
232 case "br", "p", "div", "tr", "li", "h1", "h2", "h3", "h4", "h5", "h6", "blockquote":
233 sb.WriteByte('\n')
234 }
235 case html.EndTagToken:
236 name, _ := z.TagName()
237 switch string(name) {
238 case "script", "style", "head":
239 if skip > 0 {
240 skip--
241 }
242 case "p", "div", "tr", "li", "blockquote":
243 sb.WriteByte('\n')
244 }
245 case html.TextToken:
246 if skip == 0 {
247 sb.Write(z.Text())
248 }
249 }
250 }
251}
252
253// quoteMarker matches the start of a quoted reply / forwarded block. Anything
254// from the first match onward is dropped.

Callers 1

walkPartsFunction · 0.85

Calls 3

NextMethod · 0.80
StringMethod · 0.45
WriteMethod · 0.45

Tested by

no test coverage detected