Class: HtmlMockup::Release::Processors::UrlRelativizer
- Defined in:
- lib/html_mockup/release/processors/url_relativizer.rb
Instance Method Summary collapse
- #call(release, options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ UrlRelativizer
constructor
A new instance of UrlRelativizer.
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 = { :url_attributes => %w{src href action}, :match => ["**/*.html"], :skip => [] } @options.update() if 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, ={}) = {}.update(@options).update() release.log(self, "Relativizing all URLS in #{[:match].inspect} files in attributes #{[:url_attributes].inspect}, skipping #{[:skip].any? ? [:skip].inspect : "none" }") @resolver = HtmlMockup::Resolver.new(release.build_path) release.get_files([:match], [: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) [: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 |