-
-
Save lucascaton/7dad1b110efbc5b25a15ac7ae8b58f4f to your computer and use it in GitHub Desktop.
HAML to ERB converter with herbalizer
This file contains hidden or 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 | |
# 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