Skip to content

Instantly share code, notes, and snippets.

@lucascaton
Forked from 3zcurdia/haml2erb.rb
Last active September 24, 2020 07:42
Show Gist options
  • Save lucascaton/7dad1b110efbc5b25a15ac7ae8b58f4f to your computer and use it in GitHub Desktop.
Save lucascaton/7dad1b110efbc5b25a15ac7ae8b58f4f to your computer and use it in GitHub Desktop.
HAML to ERB converter with herbalizer
#!/usr/bin/env ruby
# frozen_string_literal: true
require "httparty"
class Converter
def initialize(filename)
@content = File.open(filename).read
end
attr_reader :content
def erb
raise request["error"] unless request["success"]
request["erb"]
end
private
def request
@request ||= HTTParty.post("https://haml2erb.org/api/convert", multipart: true, body: body)
end
def body
{ converter: "herbalizer", haml: content }
end
end
if __FILE__ == $PROGRAM_NAME
if ARGV.empty?
puts "View(s) path must be given"
exit(1)
end
path = ARGV[0].chomp("/")
if File.directory?(path)
files = `find #{path} -mindepth 1 -type f -name "*.haml"`.split("\n")
else
files = [path]
end
files.each do |file|
new_file_path = file.sub(".haml", ".erb")
erb = Converter.new(file).erb
File.open(new_file_path, 'w') { |f| f.write(erb) }
`rm #{file}`
`$EDITOR #{new_file_path}`
rescue StandardError => e
puts "Error while converting \"#{file}\" #{e}"
next
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment