forked from feedbin/feedbin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content_formatter.rb
282 lines (243 loc) · 9.15 KB
/
content_formatter.rb
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
class ContentFormatter
ALLOWLIST_BASE = {}.tap do |hash|
hash[:elements] = %w[
h1 h2 h3 h4 h5 h6 h7 h8 br b i strong em a pre code img tt div ins del sup sub
p ol ul table thead tbody tfoot blockquote dl dt dd kbd q samp var hr ruby rt
rp li tr td th s strike summary details figure figcaption audio video source
small iframe
]
hash[:attributes] = {
"a" => ["href"],
"img" => ["src", "longdesc"],
"div" => ["itemscope", "itemtype"],
"blockquote" => ["cite"],
"del" => ["cite"],
"ins" => ["cite"],
"q" => ["cite"],
"source" => ["src"],
"video" => ["src", "poster", "playsinline", "loop", "muted", "controls", "preload"],
"audio" => ["src"],
"td" => ["align"],
"th" => ["align"],
"iframe" => ["src", "width", "height"],
:all => %w[
abbr accept accept-charset accesskey action alt axis border cellpadding
cellspacing char charoff charset checked clear cols colspan color compact
coords datetime dir disabled enctype for frame headers height hreflang hspace
ismap label lang maxlength media method multiple name nohref noshade nowrap
open prompt readonly rel rev rows rowspan rules scope selected shape size span
start summary tabindex target title type usemap valign value vspace width
itemprop id
]
}
hash[:protocols] = {
"a" => {
"href" => ["http", "https", "mailto", :relative]
},
"blockquote" => {
"cite" => ["http", "https", :relative]
},
"del" => {
"cite" => ["http", "https", :relative]
},
"ins" => {
"cite" => ["http", "https", :relative]
},
"q" => {
"cite" => ["http", "https", :relative]
},
"img" => {
"src" => ["http", "https", :relative, "data"],
"longdesc" => ["http", "https", :relative]
},
"video" => {
"src" => ["http", "https"],
"poster" => ["http", "https"]
},
"audio" => {
"src" => ["http", "https"]
}
}
hash[:remove_contents] = %w[script style iframe object embed]
end
ALLOWLIST_DEFAULT = ALLOWLIST_BASE.clone.tap do |hash|
transformers = Transformers.new
hash[:transformers] = [transformers.class_allowlist, transformers.table_elements, transformers.top_level_li, transformers.video, transformers.links]
end
ALLOWLIST_NEWSLETTER = ALLOWLIST_BASE.clone.tap do |hash|
hash[:elements] = hash[:elements] - %w[table thead tbody tfoot tr td]
end
ALLOWLIST_EVERNOTE = {
elements: %w[
a abbr acronym address area b bdo big blockquote br caption center cite code col colgroup dd
del dfn div dl dt em font h1 h2 h3 h4 h5 h6 hr i img ins kbd li map ol p pre q s samp small
strike strong sub sup table tbody td tfoot th thead tr tt u ul var xmp
],
remove_contents: ["script", "style", "iframe", "object", "embed", "title"],
attributes: {
"a" => ["href"],
"img" => ["src", "width", "height", "alt"],
"ol" => Sanitize::Config::RELAXED[:attributes]["ol"],
"ul" => Sanitize::Config::RELAXED[:attributes]["ul"],
"table" => Sanitize::Config::RELAXED[:attributes]["table"],
"td" => Sanitize::Config::RELAXED[:attributes]["td"],
"th" => Sanitize::Config::RELAXED[:attributes]["th"]
},
protocols: {
"a" => {"href" => ["http", "https", :relative]},
"img" => {"src" => ["http", "https", :relative]}
}
}
SANITIZE_BASIC = Sanitize::Config.merge(Sanitize::Config::BASIC, remove_contents: ["script", "style", "iframe", "object", "embed", "figure"])
def self.format!(*args)
new._format!(*args)
end
def _format!(content, entry = nil, image_proxy_enabled = true, base_url = nil)
context = {
scrub_mode: :default,
embed_url: Rails.application.routes.url_helpers.iframe_embeds_path,
embed_classes: "iframe-placeholder entry-callout system-content"
}
if entry && entry.feed.newsletter?
context[:scrub_mode] = :newsletter
end
filters = [HTML::Pipeline::SmileyFilter, ContentFilters::Scrub, ContentFilters::Attributes, HTML::Pipeline::SrcFixer, HTML::Pipeline::IframeFilter]
if ENV["CAMO_HOST"] && ENV["CAMO_KEY"] && image_proxy_enabled
context[:asset_proxy] = ENV["CAMO_HOST"]
context[:asset_proxy_secret_key] = ENV["CAMO_KEY"]
context[:asset_src_attribute] = "data-camo-src"
filters.push(HTML::Pipeline::CamoFilter)
end
if entry || base_url
filters.unshift(HTML::Pipeline::AbsoluteSourceFilter)
filters.unshift(HTML::Pipeline::AbsoluteHrefFilter)
context[:image_base_url] = base_url || entry.base_url
context[:image_subpage_url] = base_url || entry.fully_qualified_url || ""
context[:href_base_url] = base_url || entry.base_url
context[:href_subpage_url] = base_url || entry.fully_qualified_url || ""
end
filters.unshift(HTML::Pipeline::LazyLoadFilter)
pipeline = HTML::Pipeline.new filters, context
result = pipeline.call(content)
if entry&.archived_images?
result[:output] = ImageFallback.new(result[:output]).add_fallbacks
end
result[:output].to_s
end
def self.newsletter_format(*args)
new._newsletter_format(*args)
end
def _newsletter_format(content)
context = {
whitelist: Sanitize::Config::RELAXED
}
filters = [HTML::Pipeline::SanitizationFilter]
if ENV["CAMO_HOST"] && ENV["CAMO_KEY"]
context[:asset_proxy] = ENV["CAMO_HOST"]
context[:asset_proxy_secret_key] = ENV["CAMO_KEY"]
context[:asset_src_attribute] = "data-camo-src"
filters = filters << HTML::Pipeline::CamoFilter
end
pipeline = HTML::Pipeline.new filters, context
result = pipeline.call(content)
result[:output].to_s
end
def self.absolute_source(*args)
new._absolute_source(*args)
end
def _absolute_source(content, entry, base_url = nil)
filters = [HTML::Pipeline::AbsoluteSourceFilter, HTML::Pipeline::AbsoluteHrefFilter]
context = {
image_base_url: base_url || entry.base_url,
image_subpage_url: base_url || entry.fully_qualified_url || "",
href_base_url: base_url || entry.base_url,
href_subpage_url: base_url || entry.fully_qualified_url || ""
}
pipeline = HTML::Pipeline.new filters, context
result = pipeline.call(content)
result[:output].to_s
rescue
content
end
def self.api_format(*args)
new._api_format(*args)
end
def _api_format(content, entry)
filters = [HTML::Pipeline::AbsoluteSourceFilter, HTML::Pipeline::AbsoluteHrefFilter, HTML::Pipeline::ProtocolFilter, ContentFilters::Scrub, ContentFilters::Attributes]
context = {
scrub_mode: :default,
image_base_url: entry.base_url,
image_subpage_url: entry.fully_qualified_url || "",
href_base_url: entry.base_url,
href_subpage_url: entry.fully_qualified_url || ""
}
if entry.feed.newsletter?
context[:scrub_mode] = :newsletter
end
pipeline = HTML::Pipeline.new filters, context
result = pipeline.call(content)
result[:output].to_s
rescue
content
end
def self.app_format(*args)
new._app_format(*args)
end
def _app_format(content, entry)
filters = [HTML::Pipeline::AbsoluteSourceFilter, HTML::Pipeline::AbsoluteHrefFilter, HTML::Pipeline::ProtocolFilter, HTML::Pipeline::ImagePlaceholderFilter]
context = {
image_base_url: entry.base_url,
image_subpage_url: entry.fully_qualified_url || "",
href_base_url: entry.base_url,
href_subpage_url: entry.fully_qualified_url || "",
placeholder_url: "",
placeholder_attribute: "data-feedbin-src"
}
pipeline = HTML::Pipeline.new filters, context
result = pipeline.call(content)
result[:output].to_s
rescue
content
end
def self.evernote_format(*args)
new._evernote_format(*args)
end
def _evernote_format(content, entry)
filters = [HTML::Pipeline::SanitizationFilter, HTML::Pipeline::SrcFixer, HTML::Pipeline::AbsoluteSourceFilter, HTML::Pipeline::AbsoluteHrefFilter, HTML::Pipeline::ProtocolFilter]
context = {
whitelist: ALLOWLIST_EVERNOTE,
image_base_url: entry.base_url,
image_subpage_url: entry.fully_qualified_url || "",
href_base_url: entry.base_url,
href_subpage_url: entry.fully_qualified_url || ""
}
pipeline = HTML::Pipeline.new filters, context
result = pipeline.call(content)
result[:output].to_xml
rescue
content
end
def self.summary(*args)
new._summary(*args)
end
def _summary(content, length = nil)
return "" if content.nil?
content = HTML::Pipeline.new([ContentFilters::Scrub])
.call(content)[:output]
.to_text(encode_special_chars: false).gsub(/\s+/, " ").squish
content = content.truncate(length, separator: " ", omission: "") if length
content
rescue HTML::Pipeline::Filter::InvalidDocumentException
""
end
def self.text_email(*args)
new._text_email(*args)
end
def _text_email(content)
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML.new(hard_wrap: true), autolink: true)
content = markdown.render(content)
Sanitize.fragment(content, ALLOWLIST_DEFAULT).html_safe
rescue
content
end
end