Sha256: c252cb456d90a5a622153e5f751f54a6749c8705e739a0bc9adde3c1e883a917

Contents?: true

Size: 1.72 KB

Versions: 14

Compression:

Stored size: 1.72 KB

Contents

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

module Roger::Release::Processors
  class UrlRelativizer < Base
    
    def initialize(options={})
      @options = {
        :url_attributes => %w{src href action},
        :match => ["**/*.html"],
        :skip => []
      }
      
      @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}, skipping #{options[:skip].any? ? options[:skip].inspect : "none" }")
      
      @resolver = Roger::Resolver.new(release.build_path)
     release.get_files(options[:match], options[:skip]).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

Roger::Release::Processors.register(:url_relativizer, Roger::Release::Processors::UrlRelativizer)

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
roger-1.1.3 lib/roger/release/processors/url_relativizer.rb
roger-1.1.2 lib/roger/release/processors/url_relativizer.rb
roger-1.1.1 lib/roger/release/processors/url_relativizer.rb
roger-1.1.0 lib/roger/release/processors/url_relativizer.rb
roger-1.0.1 lib/roger/release/processors/url_relativizer.rb
roger-1.0.0 lib/roger/release/processors/url_relativizer.rb
roger-0.13.0 lib/roger/release/processors/url_relativizer.rb
roger-0.12.5 lib/roger/release/processors/url_relativizer.rb
roger-0.12.4 lib/roger/release/processors/url_relativizer.rb
roger-0.12.3 lib/roger/release/processors/url_relativizer.rb
roger-0.12.2 lib/roger/release/processors/url_relativizer.rb
roger-0.12.1 lib/roger/release/processors/url_relativizer.rb
roger-0.12.0 lib/roger/release/processors/url_relativizer.rb
roger-0.11.0 lib/roger/release/processors/url_relativizer.rb