Class: HtmlMockup::Release::Processors::UrlRelativizer

Inherits:
Base
  • Object
show all
Defined in:
lib/html_mockup/release/processors/url_relativizer.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ UrlRelativizer

Returns a new instance of UrlRelativizer.



6
7
8
9
10
11
12
13
14
# File 'lib/html_mockup/release/processors/url_relativizer.rb', line 6

def initialize(options={})
  @options = {
    :url_attributes => %w{src href action},
    :match => ["**/*.html"],
    :skip => []
  }
  
  @options.update(options) if options            
end

Instance Method Details

#call(release, options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/html_mockup/release/processors/url_relativizer.rb', line 16

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 = HtmlMockup::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