Created
March 11, 2009 07:59
-
-
Save jzajpt/77368 to your computer and use it in GitHub Desktop.
Simple & stupid HAML to ERB convertor...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# haml2erb | |
require 'rubygems' | |
require 'haml' | |
class ErbEngine < Haml::Engine | |
def push_script(text, preserve_script, in_tag = false, preserve_tag = false, | |
escape_html = false, nuke_inner_whitespace = false) | |
push_text "<%= #{text.strip} %>" | |
end | |
def push_silent(text, can_suppress = false) | |
push_text "<% #{text.strip} %>" | |
end | |
end | |
def fix_whitespaces(str) | |
str.gsub(/>([ \t]+)</, '><'). | |
gsub(/%>\n<\//, '%></') | |
end | |
if ARGV.size == 0 | |
puts "usage: haml2erb [files]" | |
exit | |
end | |
ARGV.each do |filename| | |
content = File.open(filename).read | |
erb_engine = ErbEngine.new(content) | |
erbized = fix_whitespaces(erb_engine.render) | |
new_filename = filename.gsub('haml', 'html.erb').gsub('html.html', 'html') | |
puts "writing to file #{new_filename}" | |
File.open(new_filename, "w").write(erbized) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For newer Haml versions, you must replace push_script definition to
because API was changed