Skip to content

Instantly share code, notes, and snippets.

@sc0ttman
Forked from javan/haml.rake
Created January 19, 2022 19:43
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 sc0ttman/4b1b634c37a658a8e5130edc90368052 to your computer and use it in GitHub Desktop.
Save sc0ttman/4b1b634c37a658a8e5130edc90368052 to your computer and use it in GitHub Desktop.
namespace :haml do
desc "Convert HAML templates to ERB"
task :convert_to_erb do
# Assumes you have faraday in your Gemfile
conn = Faraday.new(url: "https://haml2erb.org") do |f|
f.request :json
f.response :json
end
haml_filenames = Dir["app/views/**/*.haml"]
failures = []
puts "Converting #{haml_filenames.size} .haml templates…"
haml_filenames.each do |haml_filename|
haml_file = Pathname.new(haml_filename)
haml = ""
haml_file.read.strip.each_line(chomp: true) do |line|
# Replace {"foo": "bar"} with {"foo" => "bar"}
line.gsub!(/": /, '" => ') if line.match? /{.*".+":.*}/
# WARNING: These lines are potentially buggy / destructive. Uncomment to live dangerously.
#
# # Replace {data: {foo: "bar"}} with {"data-foo" => "bar"}
# line.gsub!(/data:.*{(.+)}/) { $1.gsub(/(\w+):/) { "\"data-#{$1.dasherize}\" =>" } } if line.match? /{.*data: {.+}.*}/
# # Replace {aria: {foo: "bar"}} with {"aria-foo" => "bar"}
# line.gsub!(/aria:.*{(.+)}/) { $1.gsub(/(\w+):/) { "\"aria-#{$1.dasherize}\" =>" } } if line.match? /{.*aria: {.+}.*}/
#
haml << "#{line}\n" if line.present?
end
response = conn.post("/api/convert", { haml: haml })
erb = response.body["erb"] if response.success? && response.body["success"]
if erb.present? && !erb.match?(/\(line \d+, column \d+\)/) # haml2erb encountered an error, but returned "success" 🤷‍♂️
erb = response.body["erb"]
# Replace <%= # … %> with <%# … %>
erb.gsub!("<%= #", "<%#")
erb_filename = haml_filename.sub(/\.haml$/, ".erb")
erb_file = Pathname.new(erb_filename)
erb_file.write(erb)
haml_file.unlink
puts "✅ #{haml_filename} → #{erb_filename}"
else
failures << haml_filename
error = response.body["error"].presence || erb.presence || ""
puts "❌ #{haml_filename}: #{error.squish}"
end
sleep 0.3
end
puts "Done! Converted #{haml_filenames.size - failures.size} of #{haml_filenames.size} templates."
if failures.any?
puts "Failures:"
failures.each { puts " #{_1}" }
end
end
end
The MIT License (MIT)
Copyright (c) 2022 Javan Makhmali
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment