Skip to content

Instantly share code, notes, and snippets.

@tkfm-yamaguchi
Created June 23, 2014 08:48
Show Gist options
  • Save tkfm-yamaguchi/178db832715ffe800d93 to your computer and use it in GitHub Desktop.
Save tkfm-yamaguchi/178db832715ffe800d93 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# coding: utf-8
require "fileutils"
require "pathname"
include FileUtils
class Pathname
def replace_extname(new_ext)
dirname.join(basename.to_s.sub(/#{Regexp.escape(extname)}$/, new_ext))
end
def append_underscore
if basename.to_s =~ /^_/
replaced_basename = "_#{basename.to_s}"
else
replaced_basename = "__#{basename.to_s}"
end
dirname.join(replaced_basename)
end
end
unless system("which html2haml > /dev/null 2>&1")
puts <<-MSG
[ERROR] Please install html2haml.
$ gem install html2haml
MSG
exit 1
end
eruby = ARGV[0]
if eruby.nil?
puts "[ERROR] Please specify the file name to convert."
exit 1
end
eruby = Pathname(eruby)
unless eruby.exist?
puts <<-MSG
[ERROR] Specified file does not exists.
specified file: #{eruby}
MSG
exit 1
end
unless eruby.extname == ".erb"
puts <<-MSG
[ERROR] Specified file is not looks like ERuby file.
specified file: #{eruby}
MSG
exit 1
end
haml = eruby.replace_extname(".haml")
if haml.exist?
puts <<-MSG
[ERROR] Haml file has already exists.
Please confirm and remove it if you want to continue the process.
REPLACED FILENAME: #{haml}
MSG
exit 1
end
underscored_eruby = eruby.append_underscore
if underscored_eruby.exist?
puts <<-MSG
[ERROR] Underscored file has already exists.
Please confirm and remove it if you want to continue the process.
UNDERSCORED FILENAME: #{underscored_eruby}
MSG
exit 1
end
# MAIN
`html2haml #{eruby} #{haml}`
`mv #{eruby} #{underscored_eruby}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment