lib/elsmore/rewriter.rb in elsmore-0.1.7 vs lib/elsmore/rewriter.rb in elsmore-0.2.0
- old
+ new
@@ -1,8 +1,8 @@
module Elsmore
class Rewriter
- attr_accessor :resource
+ attr_accessor :resource, :emitter
def initialize resource
self.resource = resource
end
@@ -18,11 +18,11 @@
write_js
write_images
end
def write_css
- resource.doc.xpath('//link[rel=stylesheet]').each do |element|
+ resource.doc.xpath('//link[@rel="stylesheet"]').each do |element|
write_element(element, 'href')
end
end
def write_images
@@ -39,19 +39,29 @@
def write_element element, key
return unless element.attribute(key)
url = element.attribute(key).value
_resource = Elsmore::Resource.new(url, resource.url)
+ _resource.emitter = emitter
_resource.write!
+
+ emitter.log("# Rewriting #{url} => #{_resource.filename}") if url != _resource.filename
element.attribute(key).value = _resource.filename
end
def rewrite_links
resource.doc.xpath('//a').each do |element|
return unless element.attribute('href')
href = element.attribute('href').value
+
url = Elsmore::Url.new(href, resource.url)
- element.attribute('href').value = url.absolute_path_or_external_url
+ if url.valid
+ new_url = url.absolute_path_or_external_url
+ else
+ new_url = href
+ end
+ emitter.log("# Rewriting #{href} => #{new_url}") if href != new_url
+ element.attribute('href').value = new_url
end
end
end
end