Method: Deface::Parser.erb_markup!
- Defined in:
- lib/deface/parser.rb
.erb_markup!(source) ⇒ Object
converts erb to markup
8 9 10 11 12 13 14 15 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 44 45 46 47 48 |
# File 'lib/deface/parser.rb', line 8 def self.erb_markup!(source) #all opening html tags that contain <% %> blocks source.scan(/<\w+[^<>]+(?:<%.*?%>[^<>]*)+/m).each do |line| #regexs to catch <% %> inside attributes id="<% something %>" - with double, single or no quotes erb_attrs_regexs = [/([\w-]+)(\s?=\s?)(")([^"]*<%.*?%>[^"]*)/m, /([\w-]+)(\s?=\s?)(')([^']*<%.*?%>[^']*)'/m, /([\w-]+)(\s?=\s?)()(<%.*?%>)(?:\s|>|\z)/m] replace_line = erb_attrs_regexs.inject(line.clone) do |replace_line, regex| replace_line = line.scan(regex).inject(replace_line) do |replace_line, match| replace_line.sub("#{match[0]}#{match[1]}#{match[2]}#{match[3]}#{match[2]}") { |m| m = " data-erb-#{match[0]}=\"#{CGI.escapeHTML(match[3])}\"" } end replace_line end i = -1 #catch all <% %> inside tags id <p <%= test %>> , not inside attrs replace_line.scan(/(<%.*?%>)/m).each do |match| replace_line.sub!(match[0]) { |m| m = " data-erb-#{i += 1}=\"#{CGI.escapeHTML(match[0])}\"" } end source.sub!(line) { |m| m = replace_line } end #replaces all <% %> not inside opening html tags replacements = [ {"<%=" => "<erb loud>"}, {"<%" => "<erb silent>"}, {"%>" => "</erb>"} ] replacements.each{ |h| h.each { |replace, with| source.gsub! replace, with } } source.scan(/(<erb.*?>)((?:(?!<\/erb>)[\s\S])*)(<\/erb>)/).each do |match| source.sub!("#{match[0]}#{match[1]}#{match[2]}") { |m| m = "#{match[0]}#{CGI.escapeHTML(match[1])}#{match[2]}" } end source end |