Sha256: 150423d8e74c265cbfdd1281b8fffb6b894d4f24844df4c94a1d0c76d70f3e38

Contents?: true

Size: 1.53 KB

Versions: 3

Compression:

Stored size: 1.53 KB

Contents

require File.dirname(__FILE__) + '../../../resolver'

module HtmlMockup::Release::Processors
  class UrlRelativizer < Base
    
    def initialize(options={})
      @options = {
        :url_attributes => %w{src href action},
        :match => ["**/*.html"]
      }
      
      @options.update(options) if options            
    end

    def call(release, options={})
      options = {}.update(@options).update(options)
      
      release.log(self, "Relativizing all URLS in #{options[:match].inspect} files in attributes #{options[:url_attributes].inspect}")
      
      @resolver = HtmlMockup::Resolver.new(release.build_path)
      release.get_files(options[:match]).each do |file_path|
        release.debug(self, "Relativizing URLS in #{file_path}") do
          orig_source = File.read(file_path)
          File.open(file_path,"w") do |f| 
            doc = Hpricot(orig_source)
            options[:url_attributes].each do |attribute|
              (doc/"*[@#{attribute}]").each do |tag|
                converted_url = @resolver.url_to_relative_url(tag[attribute], file_path)
                release.debug(self, "Converting '#{tag[attribute]}' to '#{converted_url}'")
                case converted_url
                when String
                  tag[attribute] = converted_url
                when nil
                  release.log(self, "Could not resolve link #{tag[attribute]} in #{file_path}")
                end
              end
            end
            f.write(doc.to_original_html) 
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
html_mockup-0.8.2 lib/html_mockup/release/processors/url_relativizer.rb
html_mockup-0.8.1 lib/html_mockup/release/processors/url_relativizer.rb
html_mockup-0.8.0 lib/html_mockup/release/processors/url_relativizer.rb