AlexanderNelzin
3/13/2016 - 2:54 PM

Convert erb to haml

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