Created
January 19, 2022 17:49
-
-
Save javan/7ddfe6d8b5276383922678cce23d7209 to your computer and use it in GitHub Desktop.
HAML → ERB (context: https://twitter.com/javan/status/1483610684281835523)
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
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 |
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
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
Thanks for posting this! I'm converting a lot of HAML -> ERB and this is very helpful. There are a lot of
data: {}
andaria: {}
hash attributes in the HAML I'm converting and I figured I'd share one insight regarding your commented out regex replacements.In this case there is an interpolated ruby value within a string inside a nested data attribute, i.e.:
The current gsub-ing would output
color.id}
. If I adjust the regex to use[^"]*
instead of.*
it works in this case: