Skip to content

Instantly share code, notes, and snippets.

@HusseinMorsy
Created July 6, 2022 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HusseinMorsy/d7fd01b48b6620d433ae315dc117d38f to your computer and use it in GitHub Desktop.
Save HusseinMorsy/d7fd01b48b6620d433ae315dc117d38f to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
## frozen_string_literal: true
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem "faraday", "~> 2.2"
end
class Converter
def initialize(filename)
@content = File.open(filename).read
end
attr_reader :content
def to_erb
conn = Faraday.new(url: "https://haml2erb.org", headers: {'Content-Type' => 'application/json'})
response = JSON.parse(conn.post("/api/convert", body.to_json).body)
raise response["error"] unless response["success"]
decode(response["erb"])
end
private
def body
{haml: encode(content)}
end
### convert UTF-8 to ASCII
def to_ascii(str)
str.encode("ASCII", :invalid => :replace, :undef => :replace, :replace => "???")
end
def encode(str)
result = ""
str.each_char do |c|
result = (c.ord > 127) ? result + "&#" + c.ord.to_s + ";" : result + c
end
result
end
def decode(str)
str = str.gsub(/&#(\d+);/) {|m| $1.to_i.chr(Encoding::UTF_8)}
replace_inline_ruby(str)
end
def replace_inline_ruby(str)
str.gsub(/#\{(.*?)}/, '<%= \1 %>')
end
end
if __FILE__ == $PROGRAM_NAME
if ARGV.empty?
puts "Usage: #{$PROGRAM_NAME} <hamlfile>"
puts "or: #{$PROGRAM_NAME} <view-directory>"
exit(1)
end
if File.directory?(ARGV[0])
Dir.glob(ARGV[0] + "/*.haml").each do |haml_file|
puts "Converting #{haml_file}..."
converter = Converter.new(haml_file)
File.open(haml_file.sub(/\.haml$/, ".erb"), "w") do |f|
f.write(converter.to_erb)
end
File.delete(haml_file)
end
else
haml_file = ARGV[0]
raise "File is not a haml file" if !haml_file.end_with?(".haml") || !File.exist?(haml_file)
erb_file = haml_file.sub(".haml", ".erb")
converter = Converter.new(haml_file)
File.open(erb_file, 'w') { |f| f.write(converter.to_erb) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment