How can I use nokogiri to replace all img tags with image tags? This is to utilize Rails’ ability to plugin the correct asset server automatically?
require 'nokogiri'
class ToImageTag
def self.convert
Dir.glob("app/views/**/*").each do |filename|
doc = Nokogiri::HTML(File.open(filename))
doc.xpath("//img").each |img_tags|
# grab the src and all the attributes and move them to ERB
end
# rewrite the file
end
rescue => err
puts "Exception: #{err}"
end
end
Somewhat inspired by maerics’ response, I’ve created a script that does this. It doesn’t have an issue with HTML entities because it only uses the nokogiri output as a guide for replacement. The actual replacement is done by using String#gsub!
https://gist.github.com/1254319