Class: Rack::Rewritten::Html

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/html.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Html

Returns a new instance of Html.



9
10
11
# File 'lib/rack/html.rb', line 9

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rack/html.rb', line 13

def call(env)
  req = Rack::Request.new(env)
  status, headers, response = @app.call(env)

  if status == 200
    new_response = []
    response.each do |line|
      links = line.scan(/href="([^"]+)"/).flatten.uniq
      res = line
      links.each do |link|
        t = ::Rewritten.get_current_translation(link)
        res.gsub!(/href="#{link}"/, %Q|href="#{t}"|) if t
      end
      new_response << res
    end
  else
    new_response = response
  end

  [status, headers, new_response]
end