awesomeprogrammer.com

Sharing ideas

Solving problems

Gathering solutions

Exchanging thoughts

Ruby On Rails

PHP

Postgres

Debian & Ubuntu
jQuery & CSS

Batch ERB to HAML Conversion

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:

1
2
3
gem 'haml' # obviously
gem 'hpricot'
gem 'ruby_parser'

Here’s the code:

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

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!

Comments