Skip to content

Instantly share code, notes, and snippets.

@AlexanderPavlenko
Created May 8, 2012 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexanderPavlenko/2637518 to your computer and use it in GitHub Desktop.
Save AlexanderPavlenko/2637518 to your computer and use it in GitHub Desktop.
wrap erb template into erb template
#!/usr/bin/env ruby
filename = ARGV[0]
str = File.read filename
blocks = str.split('<%').map{|s| s.empty? ? s : s.split('%>') }.flatten
inside = false
blocks.map do |block|
if inside
block.gsub! '\\', '\\\\\\\\'
block.gsub! '"', '\"'
block.gsub! '#{', '#" %><%= "{" + "'
end
inside = !inside
end
result = []
blocks.each_slice(2) do |pair|
result.push pair.join('<%= "<%')
end
result = result.join('%" + ">" %>')
File.open("/tmp/#{filename.split('/').last}.erb", 'w') do |f|
f.write result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment