Skip to content

Instantly share code, notes, and snippets.

@3zcurdia
Last active September 22, 2020 09:48
Show Gist options
  • Save 3zcurdia/78bb32cb433e799f676ac77fd04bec8d to your computer and use it in GitHub Desktop.
Save 3zcurdia/78bb32cb433e799f676ac77fd04bec8d to your computer and use it in GitHub Desktop.
Haml to erb converter with herbalizer
#!/usr/bin/env ruby
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__ == $0
if ARGV.length < 1
puts "Views path must be given"
exit(1)
end
# puts %x[find #{ARGV[0]} -mindepth 1 -type f -name '*.haml' -exec printf x \; | wc -c]
files = %x[find #{ARGV[0]} -mindepth 1 -type f -name "*.haml"].split("\n")
files.each do |file|
lines_count = %x[cat #{file} | wc -l].tr("\s", "").tr("\n", "").to_i + 1
next if lines_count > 5 # Max line threshold of 5 lines
puts "#{lines_count}\t=> #{file}"
begin
erb = Converter.new(file).erb
rescue => e
puts "Error while converting: #{$!}"
next
end
File.open(file.sub(".haml", ".erb"), 'w') do |f|
f.write(erb)
end
%x[rm #{file}]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment