Sha256: 6343a84993afa659e1fc5c7e1bd23894f228e15caf5e958356478c0ca023b12b

Contents?: true

Size: 840 Bytes

Versions: 5

Compression:

Stored size: 840 Bytes

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|
              t = get_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

      private

      def get_translation(url)
        ::Rewritten.list_range("to:#{url}", -1, 1)
      end

    end

  end
end


Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rewritten-0.7.0 lib/rack/html.rb
rewritten-0.6.0 lib/rack/html.rb
rewritten-0.5.0 lib/rack/html.rb
rewritten-0.4.0 lib/rack/html.rb
rewritten-0.3.3 lib/rack/html.rb