Created
April 22, 2012 12:02
-
-
Save zakgrant/2463838 to your computer and use it in GitHub Desktop.
Script for converting ERB to HAML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
require 'hpricot' | |
require 'ruby_parser' | |
require 'optparse' | |
class ToHaml | |
def initialize(path) | |
@path = path | |
end | |
def convert! | |
Dir["#{@path}/**/*.erb"].each do |file| | |
`html2haml -rx #{file} #{file.gsub(/\.erb$/, '.haml')}` | |
`rm -Rf #{file}` | |
end | |
end | |
end | |
option_parser = OptionParser.new do |opts| | |
executable_name = File.basename($PROGRAM_NAME) | |
opts.banner = "Usage: #{executable_name} template_directory" | |
executable_name = File.basename($PROGRAM_NAME) | |
opts.banner = "Convert ERB templates to HAML | |
Usage: #{executable_name} template_directory | |
" | |
end | |
begin | |
option_parser.parse! | |
if ARGV.empty? | |
puts "error: you must supply a template_directory" | |
puts | |
puts option_parser.help | |
exit 1 | |
else | |
input_path = ARGV[0].split File::SEPARATOR | |
path = File.expand_path(File.join(input_path)) | |
ToHaml.new(path).convert! | |
end | |
rescue OptionParser::InvalidArgument => ex | |
STDERR.puts ex.message | |
STDERR.puts option_parser | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment