class RogerUrlRelativizer::UrlRelativizer

Public Instance Methods

default_options() click to toggle source
# File lib/roger_url_relativizer/processor.rb, line 10
def default_options
  {
    url_attributes: %w(src href action),
    match: ["**/*.html"],
    skip: []
  }
end
perform() click to toggle source
# File lib/roger_url_relativizer/processor.rb, line 18
def perform
  log_call

  @resolver = Roger::Resolver.new(release.build_path)

  relativize_attributes_in_files(
    release,
    options[:url_attributes],
    release.get_files(options[:match], options[:skip])
  )
end

Protected Instance Methods

log_call() click to toggle source
# File lib/roger_url_relativizer/processor.rb, line 32
def log_call
  log_message = "Relativizing all URLS in #{@options[:match].inspect}"
  log_message << "files in attributes #{@options[:url_attributes].inspect},"
  log_message << "skipping #{@options[:skip].any? ? @options[:skip].inspect : 'none'}"
  release.log(self, log_message)
end
relativize_attributes_in_file(release, attributes, file_path) click to toggle source
# File lib/roger_url_relativizer/processor.rb, line 47
def relativize_attributes_in_file(release, attributes, file_path)
  orig_source = File.read(file_path)
  File.open(file_path, "w") do |f|
    f.write(relativize_attributes_in_source(release, attributes, orig_source, file_path))
  end
end
relativize_attributes_in_files(release, attributes, files) click to toggle source
# File lib/roger_url_relativizer/processor.rb, line 39
def relativize_attributes_in_files(release, attributes, files)
  files.each do |file_path|
    release.debug(self, "Relativizing URLS in #{file_path}") do
      relativize_attributes_in_file(release, attributes, file_path)
    end
  end
end
relativize_attributes_in_source(release, attributes, source, file_path) click to toggle source
# File lib/roger_url_relativizer/processor.rb, line 54
def relativize_attributes_in_source(release, attributes, source, file_path)
  doc = Hpricot(source)
  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
  doc.to_original_html
end