-
Notifications
You must be signed in to change notification settings - Fork 0
/
premailer.go
51 lines (43 loc) · 1.04 KB
/
premailer.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
package premailer
import (
"bytes"
"github.com/mailproto/textplain"
"golang.org/x/net/html"
)
type Premailer struct {
LineLength int
RemoveScripts bool
ResetContentEditable bool
RemoveIDs bool
RemoveClasses bool
RemoveComments bool
processed *html.Node
orig []byte
}
// New parses an HTML fragment []byte and returns the result
func New(body []byte) (*Premailer, error) {
return &Premailer{
orig: body,
LineLength: textplain.DefaultLineLength,
}, nil
}
// ToPlaintext converts the input document to the plaintext version
func (p Premailer) ToPlaintext() (string, error) {
if p.processed == nil {
_, err := p.ToInlineCSS()
if err != nil {
return "", err
}
}
baseElement := findElement(p.processed, "body")
if baseElement == nil {
baseElement = p.processed
}
var buf bytes.Buffer
err := html.Render(&buf, baseElement)
if err != nil {
return "", err
}
// XXX: add support for passing the html blob to textplain
return textplain.Convert(buf.String(), p.LineLength)
}