0
我使用这个脚本,以便将它们转换成解析HTML标签<%= IMAGE_TAG ...%>转换IMG SRC标签IMAGE_TAG
我正则表达式和测试案例在这里:http://rubular.com/r/sBPAaWSdGQ
我想捕获src =“captureme.jpg”和所有其他属性,以便我可以将它们移植过来。
我得到的正则表达式开始它只是还没有到那里 - 我需要一些东西来捕捉所有的属性...即类,编号 以下是我到目前为止。
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
http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – 2011-06-16 23:33:59
不能使用nokogiri来解析.html.erb文件。刚刚尝试过。 – maletor 2011-06-16 23:37:11
使用http://hpricot.com/这就是HAML人员用来将ERB转换为HAML的用途。 – 2011-06-16 23:42:15