/
redact.go
46 lines (43 loc) · 895 Bytes
/
redact.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
package redactomat
import (
"bytes"
"bufio"
"io"
"code.google.com/p/go.net/html"
)
func Redact(r io.Reader) (string, error) {
doc, err := html.Parse(r)
if err != nil {
return "", err
}
var f func(*html.Node)
f = func(n *html.Node) {
for c := n.FirstChild; c != nil; c = c.NextSibling {
if c.Type == html.ElementNode {
switch c.Data {
case "style", "script", "head", "meta":
n.RemoveChild(c)
return
case "img":
for i, attr := range c.Attr {
if attr.Key == "src" {
c.Attr[i].Key = "data-redacted-src"
}
}
}
} else if c.Type == html.CommentNode {
n.RemoveChild(c)
return
}
f(c)
}
}
f(doc)
buf := bytes.NewBufferString("")
err = html.Render(buf, doc)
return buf.String(), err
}
func RedactString(input string) (string, error) {
r := bufio.NewReader(bytes.NewBufferString(input))
return Redact(r)
}