-
Notifications
You must be signed in to change notification settings - Fork 0
/
feed_editor.go
167 lines (141 loc) · 4.23 KB
/
feed_editor.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
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"fmt"
"net/url"
"github.com/beevik/etree"
"github.com/microcosm-cc/bluemonday"
)
type FeedEditor interface {
// Add "(with feed-pruning-proxy)" notice to feed title
UpdateFeedTitle(doc *etree.Document)
// Remove entry contents
// Slack expands not only the page link but also links contained in these. Noisy!
RemoveEntryContent(doc *etree.Document)
// Remove links in entry contents
DietEntryContent(doc *etree.Document)
// Replace entry links with redirector
// The flood of feeds no one reading flushes out human conversations. Stop Robots Empire!
TapRedirector(doc *etree.Document, conf TransformConfig)
}
type RSSFeedEditor struct{}
func (e RSSFeedEditor) UpdateFeedTitle(doc *etree.Document) {
if i := doc.FindElement("/rss/channel/title"); i != nil {
i.SetText(addTitleNotice(i.Text()))
}
}
func (e RSSFeedEditor) RemoveEntryContent(doc *etree.Document) {
for _, i := range doc.FindElements("/rss/channel/item") {
if d := i.SelectElement("description"); d != nil {
i.RemoveChild(d)
}
if d := i.SelectElement("content:encoded"); d != nil {
i.RemoveChild(d)
}
}
}
func (e RSSFeedEditor) DietEntryContent(doc *etree.Document) {
for _, i := range doc.FindElements("/rss/channel/item") {
if d := i.SelectElement("description"); d != nil {
d.SetText(stripTags(d.Text()))
}
if d := i.SelectElement("content:encoded"); d != nil {
d.SetText(stripTags(d.Text()))
}
}
}
func (e RSSFeedEditor) TapRedirector(doc *etree.Document, conf TransformConfig) {
for _, i := range doc.FindElements("/rss/channel/item") {
if d := i.SelectElement("link"); d != nil {
d.SetText(tapRedirector(d.Text(), conf))
}
}
}
type AtomFeedEditor struct{}
func (e AtomFeedEditor) UpdateFeedTitle(doc *etree.Document) {
if i := doc.FindElement("/feed/title"); i != nil {
i.SetText(addTitleNotice(i.Text()))
}
}
func (e AtomFeedEditor) RemoveEntryContent(doc *etree.Document) {
for _, i := range doc.FindElements("/feed/entry") {
if d := i.SelectElement("summary"); d != nil {
i.RemoveChild(d)
}
if d := i.SelectElement("content"); d != nil {
i.RemoveChild(d)
}
}
}
func (e AtomFeedEditor) DietEntryContent(doc *etree.Document) {
for _, i := range doc.FindElements("/feed/entry") {
if d := i.SelectElement("summary"); d != nil {
d.SetText(stripTags(d.Text()))
}
if d := i.SelectElement("content"); d != nil {
d.SetText(stripTags(d.Text()))
}
}
}
func (e AtomFeedEditor) TapRedirector(doc *etree.Document, conf TransformConfig) {
for _, i := range doc.FindElements("/feed/entry") {
if d := i.SelectElement("link"); d != nil {
u := d.SelectAttrValue("href", "")
d.RemoveAttr("href")
d.CreateAttr("href", tapRedirector(u, conf))
}
}
}
type RDFFeedEditor struct{}
func (e RDFFeedEditor) UpdateFeedTitle(doc *etree.Document) {
if i := doc.FindElement("/rdf:RDF/channel[@rdf:about]/title"); i != nil {
i.SetText(addTitleNotice(i.Text()))
}
}
func (e RDFFeedEditor) RemoveEntryContent(doc *etree.Document) {
for _, i := range doc.FindElements("/rdf:RDF/item[@rdf:about]") {
if d := i.SelectElement("description"); d != nil {
i.RemoveChild(d)
}
if d := i.SelectElement("content:encoded"); d != nil {
i.RemoveChild(d)
}
}
}
func (e RDFFeedEditor) DietEntryContent(doc *etree.Document) {
for _, i := range doc.FindElements("/rdf:RDF/item[@rdf:about]") {
if d := i.SelectElement("description"); d != nil {
d.SetText(stripTags(d.Text()))
}
if d := i.SelectElement("content:encoded"); d != nil {
d.SetText(stripTags(d.Text()))
}
}
}
func (e RDFFeedEditor) TapRedirector(doc *etree.Document, conf TransformConfig) {
for _, i := range doc.FindElements("/rdf:RDF/item[@rdf:about]") {
if d := i.SelectElement("link"); d != nil {
d.SetText(tapRedirector(d.Text(), conf))
}
}
}
func addTitleNotice(title string) string {
return fmt.Sprintf("%s (with feed-pruning-proxy)", title)
}
func tapRedirector(link string, conf TransformConfig) string {
u, _ := url.Parse(conf.ProxyOrigin)
u.Path = "/r"
q := u.Query()
q.Set("url", link)
if conf.Org != "" {
q.Set("org", conf.Org)
}
if conf.Channel != "" {
q.Set("channel", conf.Channel)
}
u.RawQuery = q.Encode()
return u.String()
}
func stripTags(text string) string {
p := bluemonday.StrictPolicy()
return p.Sanitize(text)
}