/
cleanup.go
155 lines (124 loc) · 3.04 KB
/
cleanup.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
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
package processor
import (
"net/url"
"regexp"
"strings"
"unicode"
"github.com/urandom/readeef/log"
"github.com/urandom/readeef/parser"
"github.com/urandom/readeef/pool"
"golang.org/x/net/html"
)
type Cleanup struct {
log log.Log
}
func NewCleanup(l log.Log) Cleanup {
return Cleanup{log: l}
}
var (
iframeFixer = regexp.MustCompile(`(<iframe [^>]+)/>`)
)
func (p Cleanup) ProcessFeed(f parser.Feed) parser.Feed {
p.log.Infof("Cleaning up feed '%s'\n", f.Title)
for i := range f.Articles {
f.Articles[i].Description = strings.TrimSpace(f.Articles[i].Description)
// html.Parse breaks on self-closing iframe tags
f.Articles[i].Description =
iframeFixer.ReplaceAllString(f.Articles[i].Description, "$1></iframe>")
if nodes, err := html.ParseFragment(strings.NewReader(f.Articles[i].Description), nil); err == nil {
if nodesCleanup(nodes) {
if len(nodes) == 0 {
break
}
buf := pool.Buffer.Get()
defer pool.Buffer.Put(buf)
for _, n := range nodes {
err = html.Render(buf, n)
if err != nil {
break
}
}
content := buf.String()
// net/http tries to provide valid html, adding html, head and body tags
content = content[strings.Index(content, "<body>")+6 : strings.LastIndex(content, "</body>")]
f.Articles[i].Description = content
}
}
u, err := url.Parse(f.Articles[i].Link)
if err != nil {
p.log.Infof("Error parsing article link %q: %v", f.Articles[i].Link, err)
}
u.Fragment = ""
f.Articles[i].Link = u.String()
}
return f
}
func nodesCleanup(nodes []*html.Node) bool {
changed := false
for _, n := range nodes {
if n.Type == html.ElementNode && n.Data == "script" || n.Type == html.CommentNode {
if n.Parent != nil {
n.Parent.RemoveChild(n)
changed = true
}
break
}
if n.Type == html.ElementNode {
// Remove all 'on*' attributes, xmlns attributes, and any that contain 'javascript:'
attrs := []html.Attribute{}
for _, a := range n.Attr {
if strings.HasPrefix(a.Key, "on") || a.Key == "xmlns" {
changed = true
break
}
i := strings.Index(a.Val, "javascript:")
if i != -1 {
onlySpace := true
if i > 0 {
for _, r := range a.Val[:i] {
if !unicode.IsSpace(r) {
onlySpace = false
break
}
}
}
if onlySpace {
changed = true
break
}
}
attrs = append(attrs, a)
}
n.Attr = attrs
if n.Data == "iframe" {
}
// Add a target attribute to the article links
if n.Data == "a" {
var attr *html.Attribute
for i, a := range n.Attr {
if a.Key == "target" {
attr = &n.Attr[i]
break
}
}
val := "feed-article"
if attr == nil {
n.Attr = append(n.Attr, html.Attribute{Key: "target", Val: val})
} else {
attr.Val = val
}
changed = true
}
}
children := []*html.Node{}
for c := n.FirstChild; c != nil; c = c.NextSibling {
children = append(children, c)
}
if len(children) > 0 {
if nodesCleanup(children) {
changed = true
}
}
}
return changed
}