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
|
# File 'lib/rack/relativize.rb', line 10
def call(env)
status, , enumerable_body = original_response = @app.call(env.dup)
if ["Content-Type"].to_s.match(/(ht|x)ml/) type = :html
elsif ["Content-Type"].to_s.match(/css/)
type = :css
else
return original_response
end
path = env['PATH_INFO']
raise "PATH_INFO is Empty" if path == ""
content = join_body(enumerable_body)
case type
when :html
processed_body = content.gsub(/(<[^>]+\s+(src|href))=(['"]?)(\/.*?)\3([ >])/) do
$1 + '=' + $3 + relative_path_to(path, $4) + $3 + $5
end
when :css
processed_body = content.gsub(/url\((['"]?)(\/.*?)\1\)/) do
'url(' + $1 + relative_path_to(path, $2) + $1 + ')'
end
else
raise RuntimeError.new(
"The relativize_paths needs to know the type of content to " +
"process. Pass :type => :html for HTML or :type => :css for CSS."
)
end
= .merge({
"Content-Length" => processed_body.size.to_s
})
[status, , [processed_body]]
end
|