Quick and useful code snippet that you may want to use eg. while upgrading some old Rails projects. That HAML awesomeness you should use, Luke!

Depending on your configuration (I’m using RVM for example) you may want to add those gems to your Gemfile:

gem 'haml' # obviously
gem 'hpricot'
gem 'ruby_parser'

Here’s the code:

# erb_to_haml.rb
Dir["#{File.join(File.dirname(__FILE__), 'app', 'views')}/**/*.erb"].each do |file|
  puts "Working on: #{file} ..."
  `html2haml -rx #{file} #{file.gsub(/\.erb$/, '.haml')}`

Description of html2haml options:

-e, --erb Parse ERb tags.

-x, --xhtml Parse the input using the more strict XHTML parser.

Drop this file into your rails root directory and just run it. You may want to review the code of you new HAML files and delete those old nasty erbs. Have fun!