Skip to content

Instantly share code, notes, and snippets.

@jzajpt
Created March 11, 2009 07:59
Show Gist options
  • Save jzajpt/77368 to your computer and use it in GitHub Desktop.
Save jzajpt/77368 to your computer and use it in GitHub Desktop.
Simple & stupid HAML to ERB convertor...
#!/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
@hron84
Copy link

hron84 commented Mar 6, 2011

For newer Haml versions, you must replace push_script definition to

def push_script(text, opts = {} )

because API was changed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment