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.



6
7
8
# File 'lib/rack/html.rb', line 6

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



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
# File 'lib/rack/html.rb', line 10

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|
        if ::Rewritten.exist_translation_for?(link)
          t = ::Rewritten.get_current_translation(link)
          res.gsub!(%(href="#{link}"), %(href="#{t}")) if t
          res.gsub!(%(href='#{link}'), %(href='#{t}')) if t
          res.gsub!(%(href="#{link}?), %(href="#{t}?)) if t
          res.gsub!(%(href='#{link}?), %(href='#{t}?)) if t
        end
      end
      new_response << res
    end
  else
    new_response = response
  end

  [status, headers, new_response]
end