Sha256: e1289352e3153b3003edd640b03e534ac1a7a84d2960e61a94eac633e2f978e7

Contents?: true

Size: 769 Bytes

Versions: 4

Compression:

Stored size: 769 Bytes

Contents

require 'rack'

module Rack

  module Rewritten

    class Html

      def initialize(app)
        @app = app
      end

      def call(env)
        puts "-> Rack::Rewritten::Html"
        req = Rack::Request.new(env)
        status, headers, response = @app.call(env)

        new_response = []

        response.each do |line|
          links = line.scan(/href="([^"]+)"/).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

        [status, headers, new_response]
      end

      private

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

    end

  end
end


Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rewritten-0.3.2 lib/rack/html.rb
rewritten-0.3.1 lib/rack/html.rb
rewritten-0.3.0 lib/rack/html.rb
rewritten-0.2.2 lib/rack/html.rb