Sha256: 4797129bd0f0b8aa1600564d9618383bb36590631e54c59578579ebf07005962

Contents?: true

Size: 1.02 KB

Versions: 14

Compression:

Stored size: 1.02 KB

Contents

require 'rack'

module Rack

  module Rewritten

    class Html

      def initialize(app)
        @app = app
      end

      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!(%Q|href="#{link}"|, %Q|href="#{t}"|) if t
                res.gsub!(%Q|href='#{link}'|, %Q|href='#{t}'|) if t
                res.gsub!(%Q|href="#{link}?|, %Q|href="#{t}?|) if t
                res.gsub!(%Q|href='#{link}?|, %Q|href='#{t}?|) if t
              end

            end
            new_response << res
          end
        else
          new_response = response
        end

        [status, headers, new_response]
      end

    end

  end
end


Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
rewritten-0.15.2 lib/rack/html.rb
rewritten-0.15.1 lib/rack/html.rb
rewritten-0.15.0 lib/rack/html.rb
rewritten-0.14.2 lib/rack/html.rb
rewritten-0.14.1 lib/rack/html.rb
rewritten-0.14.0 lib/rack/html.rb
rewritten-0.13.1 lib/rack/html.rb
rewritten-0.13.0 lib/rack/html.rb
rewritten-0.12.1 lib/rack/html.rb
rewritten-0.12.0 lib/rack/html.rb
rewritten-0.11.1 lib/rack/html.rb
rewritten-0.11.0 lib/rack/html.rb
rewritten-0.10.0 lib/rack/html.rb
rewritten-0.9.1 lib/rack/html.rb