Convert existing html.erb to Haml [closed]

2019-01-30 18:02发布

问题:

I have a rails project, the views only consist with HTML.ERB files, my client wants to convert ERB to HAML. I have too many views file. It's taking a huge amount of time to convert file by file. So that any simply way I can convert HTML to haml? I installed haml plugin under my project.

回答1:

There you go: http://html2haml.heroku.com/



回答2:

You can use from the command line html2haml

html2haml your_erb_file new_haml_file

If you want to convert all your files in one go, look at this article : http://shifteleven.com/articles/2008/06/08/converting-erb-to-haml-snippet



回答3:

http://www.htmltohaml.com

A more user-friendly alternative to the selected answer.



回答4:

David Leung provides this gem on github that installs two rake tasks.

With erb2haml, you can easily convert an entire project from erb to haml with either rake haml:convert_erbs or rake haml:replace_erbs.



回答5:

On the haml-rails git page, it provides the cli command to do convert all erb to haml right in your project.

add gem "haml-rails" to your Gemfile

run: rake haml:erb2haml



回答6:

very simple

in your Gemfile add

gem "erb2haml", :group => :development

then run bundle install

for Converting *.erb to *.haml keeping original files do:

rake haml:convert_erbs

for Converting *.erb to *.haml replacing original files do:

rake haml:replace_erbs

it'll search all the erb files in project and convert to haml.

For shorthand: use on-line converter

http://www.htmltohaml.com



回答7:

EDIT: html2haml does work as advertised, however you must use version obtained from the current master branch of the haml github repoistory.

The version of html2haml included with the haml gem currently available from rubygems is no good. This is the version you will get if you were to do gem install haml right now. Using the version supplied with the gem will result in invalid haml, as it cannot process ruby properly.



回答8:

html2haml is now in the html2haml gem, so you can use:

$ gem install html2haml
$ html2haml path/to/yourfile.html path/to/yourfile.haml


回答9:

Way late to the game here, but this post still flies high in the Google when searching for similar solutions.

Install the html2haml gem, pop into your app/views directory and give this a try:

find ./ -name '*.erb' -exec html2haml -e {} {}.haml \;
find ./ -name "*.erb.haml" -exec sh -c 'mv "$1" "${1%.erb.haml}.haml"' _ {} \;
find ./ -name '*.erb' -exec rm {} \;

The flaw in this solution is that it doesn't retain revision history from your old .erb files to your new .haml files. But at times where that revision history of those view files isn't a big deal, this solution has served me pretty well.

Also, be sure to watch for any errors in the html2haml line before you delete the old .erb files.

  • Credit due to this Ask Ubuntu post for the renaming line