Skip to content
This repository was archived by the owner on Jan 18, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ original ERB templates or not.
| Yes | `rake haml:convert_erbs` |
| No | `rake haml:replace_erbs` |

By default, erb2haml looks for ERB templates inside `app/views` and additional `test/dummy/app/views` in case of an engine. If you have ERB templates inside another non-standard direcotry(e.g. `spec/dummy/app/views`) just pass the path to the rake task as an environment variable called `ERB_PATH`:

```
rake haml:convert_erbs ERB_PATH=spec/dummy/app/views
```

## License

Copyright (c) 2011-2013 David Leung and [contributors](https://github.com/dhl/erb2haml/contributors). See LICENSE for further details.
88 changes: 47 additions & 41 deletions lib/erb2haml/railties/erb2haml.rake
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,25 @@ def color(text, begin_text_style)
begin_text_style + text + END_TEXT_STYLE
end

namespace :haml do
desc "Perform bulk conversion of all html.erb files to Haml in views folder."
task :convert_erbs do

if `which html2haml`.empty?
puts "#{color "ERROR: ", RED_FG} Could not find " +
"#{color "html2haml", GREEN_FG} in your PATH. Aborting."
exit(false)
end
def erb_paths
if ENV.has_key? 'ERB_PATH'
[ENV['ERB_PATH']]
else
['app/views/', 'test/dummy/app/views']
end
end

puts "Looking for #{color "ERB", GREEN_FG} files to convert to " +
"#{color("Haml", RED_FG)}..."
def check_html2haml
if `which html2haml`.empty?
puts "#{color "ERROR: ", RED_FG} Could not find " +
"#{color "html2haml", GREEN_FG} in your PATH. Aborting."
exit(false)
end
end

Find.find("app/views/") do |path|
def erb2haml(path, options={remove: false})
begin
Find.find(path) do |path|
if FileTest.file?(path) and path.downcase.match(/\.erb$/i)
haml_path = path.slice(0...-3)+"haml"

Expand All @@ -31,50 +36,51 @@ namespace :haml do

if system("html2haml", path, haml_path)
puts color("Done!", GREEN_FG)
if options[:remove]
print "Removing: #{path}... "
if system("rm", path)
puts color("Removed!", GREEN_FG)
else
puts color("Removal failed!", RED_FG)
end
end
else
puts color("Failed!", RED_FG)
end

end

end
end
end #End rake task

desc "Perform bulk conversion of all html.erb files to Haml in views folder, then remove the converted html.erb files."
task :replace_erbs do
if `which html2haml`.empty?
puts "#{color "ERROR: ", RED_FG} Could not find " +
"#{color "html2haml", GREEN_FG} in your PATH. Aborting."
exit(false)
rescue
if ENV['ERB_PATH'] && ENV['ERB_PATH'] == path
puts color("Path #{path} not found!", RED_FG)
end
end
end

namespace :haml do
desc "Perform bulk conversion of all html.erb files to Haml in views folder."
task :convert_erbs do

check_html2haml

puts "Looking for #{color "ERB", GREEN_FG} files to convert to " +
"#{color("Haml", RED_FG)}..."

Find.find("app/views/") do |path|
if FileTest.file?(path) and path.downcase.match(/\.erb$/i)
haml_path = path.slice(0...-3)+"haml"

unless FileTest.exists?(haml_path)
print "Converting: #{path}... "
erb_paths.each do |path|
erb2haml path
end
end #End rake task

if system("html2haml", path, haml_path)
puts color("Done!", GREEN_FG)
print "Removing: #{path}... "
if system("rm", path)
puts color("Removed!", GREEN_FG)
else
puts color("Failed!", RED_FG)
end
else
puts color("Failed!", RED_FG)
end
desc "Perform bulk conversion of all html.erb files to Haml in views folder, then remove the converted html.erb files."
task :replace_erbs do

end
check_html2haml

end
puts "Looking for #{color "ERB", GREEN_FG} files to convert to " +
"#{color("Haml", RED_FG)}..."

erb_paths.each do |path|
erb2haml path, remove: true
end
end #End rake task

Expand Down