I am using this script to parse HTML tags in order to convert them into <%= image_tag… %>
My regex and test cases are here: http://rubular.com/r/sBPAaWSdGQ
I would like to capture src=”captureme.jpg” and all the other attributes as well so that I can port them over.
I’ve got the regex started it’s just not quite there yet — I need something to capture all the attributes… i.e. class, id
Below is what I have so far.
class ToImageTag
def self.convert
Dir.glob("app/views/**/*").each do |filename|
next unless filename =~ /\.html\.erb$/
file = File.new(filename, "rw")
file.each_line do |line|
source = /(<\s*img\s*.*src=(.*?)(>|\/>))/.match(line)
source = src[1].split.first
image_tag = "<%= image_tag(\"#{source}\")"
line.gsub!(src[0], image_tag)
end
file.close
end
rescue => err
puts "Exception: #{err}"
end
end
Are you using javascript? If so, you can use jQuery to do what you want.
I haven’t tried this out, so it might not be bug free.
If you aren’t using javascript, there are plenty of DOM parsers out there that will simplify this task for you. I don’t think using regex is the best option.