If you have a Ruby on Rails project and would like to convert all .erb templates to .haml across the project, you can follow these simple steps that will result in all .erb's files being parsed to new valid HAML and then the old ERB templates are deleted, in 3 simple steps.

1. Prerequisites

Install the following gems haml, hpricot, ruby_parser and html2haml. You do not necessarily need to have these in your Gemfile, just available on the system. Install in the following order:

gem install haml hpricot ruby_parser html2haml

2. Create new HAML files

Use the following bash script that will find all .erb files and then pipes them to Ruby so that it can run the html2haml command on each one.

To test what files will be generated without making changes:

find . -name '*erb' | xargs ruby -e 'ARGV.each { |i| puts "html2haml -r #{i} #{i.sub(/erb$/,"haml")}"}'

If you are happy with the output, simply pipe the command back into bash to execute the changes:

find . -name '*erb' | xargs ruby -e 'ARGV.each { |i| puts "html2haml -r #{i} #{i.sub(/erb$/,"haml")}"}' | bash

3. Remove old ERB files

Run this similar bash snippet that finds all files with a .erb extension and deletes them:

find . -name '*erb' | xargs rm -rf ARGV