-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathutils.rb
262 lines (236 loc) · 7.33 KB
/
utils.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
require 'jsonpath'
require 'cgi'
require 'addressable/uri'
module Utils
def self.unindent(s)
s = s.gsub(/\t/, ' ').chomp
min = ((s.split("\n").find { |l| l !~ /^\s*$/ })[/^\s+/, 0] || '').length
if min > 0
s.gsub(/^#{" " * min}/, '')
else
s
end
end
def self.pretty_print(struct, indent = true)
output = JSON.pretty_generate(struct)
if indent
output.gsub(/\n/i, "\n ")
else
output
end
end
def self.normalize_uri(uri)
begin
URI(uri)
rescue URI::Error
begin
URI(uri.to_s.gsub(/[^\-_.!~*'()a-zA-Z\d;\/?:@&=+$,\[\]]+/) { |unsafe|
unsafe.bytes.each_with_object(String.new) { |uc, s|
s << sprintf('%%%02X', uc)
}
}.force_encoding(Encoding::US_ASCII))
rescue URI::Error => e
begin
auri = Addressable::URI.parse(uri.to_s)
rescue
# Do not leak Addressable::URI::InvalidURIError which
# callers might not expect.
raise e
else
# Addressable::URI#normalize! modifies the query and
# fragment components beyond escaping unsafe characters, so
# avoid using it. Otherwise `?a[]=%2F` would be normalized
# as `?a%5B%5D=/`, for example.
auri.site = auri.normalized_site
auri.path = auri.normalized_path
URI(auri.to_s)
end
end
end
end
def self.interpolate_jsonpaths(value, data, options = {})
if options[:leading_dollarsign_is_jsonpath] && value[0] == '$'
Utils.values_at(data, value).first.to_s
else
value.gsub(/<[^>]+>/).each { |jsonpath|
Utils.values_at(data, jsonpath[1..-2]).first.to_s
}
end
end
def self.recursively_interpolate_jsonpaths(struct, data, options = {})
case struct
when Hash
struct.inject({}) { |memo, (key, value)| memo[key] = recursively_interpolate_jsonpaths(value, data, options); memo }
when Array
struct.map { |elem| recursively_interpolate_jsonpaths(elem, data, options) }
when String
interpolate_jsonpaths(struct, data, options)
else
struct
end
end
def self.value_at(data, path)
values_at(data, path).first
end
def self.values_at(data, path)
if path =~ /\Aescape /
path.gsub!(/\Aescape /, '')
escape = true
else
escape = false
end
result = JsonPath.new(path).on(data.is_a?(String) ? data : data.to_json)
if escape
result.map { |r| CGI::escape r }
else
result
end
end
# Output JSON that is ready for inclusion into HTML. If you simply use to_json on an object, the
# presence of </script> in the valid JSON can break the page and allow XSS attacks.
# Optionally, pass `:skip_safe => true` to not call html_safe on the output.
def self.jsonify(thing, options = {})
json = thing.to_json.gsub('</', '<\/')
if !options[:skip_safe]
json.html_safe
else
json
end
end
def self.pretty_jsonify(thing)
JSON.pretty_generate(thing).gsub('</', '<\/')
end
class TupleSorter
class SortableTuple
attr_reader :array
# The <=> method will call orders[n] to determine if the nth element
# should be compared in descending order.
def initialize(array, orders = [])
@array = array
@orders = orders
end
def <=>(other)
other = other.array
@array.each_with_index do |e, i|
o = other[i]
case cmp = e <=> o || e.to_s <=> o.to_s
when 0
next
else
return @orders[i] ? -cmp : cmp
end
end
0
end
end
class << self
def sort!(array, orders = [])
array.sort_by! do |e|
SortableTuple.new(e, orders)
end
end
end
end
def self.sort_tuples!(array, orders = [])
TupleSorter.sort!(array, orders)
end
def self.parse_duration(string)
return nil if string.blank?
case string.strip
when /\A(\d+)\.(\w+)\z/
$1.to_i.send($2.to_s)
when /\A(\d+)\z/
$1.to_i
else
STDERR.puts "WARNING: Invalid duration format: '#{string.strip}'"
nil
end
end
def self.if_present(string, method)
if string.present?
string.send(method)
else
nil
end
end
module HTMLTransformer
SINGLE = 1
MULTIPLE = 2
COMMA_SEPARATED = 3
SRCSET = 4
URI_ATTRIBUTES = {
'a' => { 'href' => SINGLE },
'applet' => { 'archive' => COMMA_SEPARATED, 'codebase' => SINGLE },
'area' => { 'href' => SINGLE },
'audio' => { 'src' => SINGLE },
'base' => { 'href' => SINGLE },
'blockquote' => { 'cite' => SINGLE },
'body' => { 'background' => SINGLE },
'button' => { 'formaction' => SINGLE },
'command' => { 'icon' => SINGLE },
'del' => { 'cite' => SINGLE },
'embed' => { 'src' => SINGLE },
'form' => { 'action' => SINGLE },
'frame' => { 'longdesc' => SINGLE, 'src' => SINGLE },
'head' => { 'profile' => SINGLE },
'html' => { 'manifest' => SINGLE },
'iframe' => { 'longdesc' => SINGLE, 'src' => SINGLE },
'img' => { 'longdesc' => SINGLE, 'src' => SINGLE, 'srcset' => SRCSET, 'usemap' => SINGLE },
'input' => { 'formaction' => SINGLE, 'src' => SINGLE, 'usemap' => SINGLE },
'ins' => { 'cite' => SINGLE },
'link' => { 'href' => SINGLE },
'object' => { 'archive' => MULTIPLE, 'classid' => SINGLE, 'codebase' => SINGLE, 'data' => SINGLE, 'usemap' => SINGLE },
'q' => { 'cite' => SINGLE },
'script' => { 'src' => SINGLE },
'source' => { 'src' => SINGLE, 'srcset' => SRCSET },
'video' => { 'poster' => SINGLE, 'src' => SINGLE }
}
URI_ELEMENTS_XPATH = '//*[%s]' % URI_ATTRIBUTES.keys.map { |name| "name()='#{name}'" }.join(' or ')
module_function
def transform(html, &block)
block or raise ArgumentError, 'block must be given'
case html
when /\A\s*(?:<\?xml[\s?]|<!DOCTYPE\s)/i
doc = Nokogiri.parse(html)
yield doc
doc.to_s
when /\A\s*<(html|head|body)[\s>]/i
# Libxml2 automatically adds DOCTYPE and <html>, so we need to
# skip them.
element_name = $1
doc = Nokogiri::HTML::Document.parse(html)
yield doc
doc.at_xpath("//#{element_name}").xpath('self::node() | following-sibling::node()').to_s
else
doc = Nokogiri::HTML::Document.parse("<html><body>#{html}")
yield doc
doc.xpath('/html/body/node()').to_s
end
end
def replace_uris(html, &block)
block or raise ArgumentError, 'block must be given'
transform(html) { |doc|
doc.xpath(URI_ELEMENTS_XPATH).each { |element|
uri_attrs = URI_ATTRIBUTES[element.name] or next
uri_attrs.each { |name, format|
attr = element.attribute(name) or next
case format
when SINGLE
attr.value = block.call(attr.value.strip)
when MULTIPLE
attr.value = attr.value.gsub(/(\S+)/) { block.call($1) }
when COMMA_SEPARATED, SRCSET
attr.value = attr.value.gsub(/((?:\A|,)\s*)(\S+)/) { $1 + block.call($2) }
end
}
}
}
end
end
def self.rebase_hrefs(html, base_uri)
base_uri = normalize_uri(base_uri)
HTMLTransformer.replace_uris(html) { |url|
base_uri.merge(normalize_uri(url)).to_s
}
end
end