Ruby-on-rails 将现有的 html.erb 转换为 Haml

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2242699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:16:53  来源:igfitidea点击:

Convert existing html.erb to Haml

ruby-on-railshamlerb

提问by palani

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.

我有一个 rails 项目,视图只包含 HTML.ERB 文件,我的客户想将 ERB 转换为 HAML。我有太多的意见文件。逐个文件转换需要花费大量时间。那么我可以将HTML转换为haml的任何简单方法吗?我在我的项目下安装了 haml 插件。

采纳答案by mikewilliamson

回答by Mike

You can use from the command line html2haml

您可以从命令行使用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

如果您想一次性转换所有文件,请查看这篇文章:http: //shifteleven.com/articles/2008/06/08/converting-erb-to-haml-snippet

回答by bevanb

http://www.htmltohaml.com

http://www.htmltohaml.com

A more user-friendly alternative to the selected answer.

所选答案的更用户友好的替代方案。

回答by IAmNaN

David Leung provides this gemon github that installs two rake tasks.

David Leung在 github 上提供了这个 gem,它安装了两个 rake 任务。

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

使用 erb2haml,您可以使用rake haml:convert_erbs或将整个项目从 erb 轻松转换为 haml rake haml:replace_erbs

回答by aviemet

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

haml-rails git 页面上,它提供了 cli 命令来在您的项目中将所有 erb 转换为 haml。

add gem "haml-rails"to your Gemfile

添加gem "haml-rails"到您的 Gemfile

run: rake haml:erb2haml

跑: rake haml:erb2haml

回答by Kaleem Ullah

very simple

很简单

in your Gemfileadd

在您Gemfile添加

gem "erb2haml", :group => :development

then run bundle install

然后运行 bundle install

for Converting *.erbto *.hamlkeeping original files do:

要转换*.erb*.haml保留原始文件,请执行以下操作:

rake haml:convert_erbs

for Converting *.erbto *.hamlreplacing original files do:

要转换*.erb*.haml替换原始文件,请执行以下操作:

rake haml:replace_erbs

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

它将搜索erb项目中的所有文件并转换为haml.

For shorthand: use on-line converter

简写:使用在线转换器

http://www.htmltohaml.com

http://www.htmltohaml.com

回答by EmFi

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

编辑:html2haml 确实像宣传的那样工作,但是您必须使用从haml github 存储库的当前主分支获得的版本

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 hamlright now. Using the version supplied with the gem will result in invalid haml, as it cannot process ruby properly.

当前可从 ruby​​gems 获得的 haml gem 中包含的 html2haml 版本不好。如果您gem install haml现在这样做,这就是您将获得的版本。使用 gem 提供的版本将导致无效的 haml,因为它无法正确处理 ruby​​。

回答by joealba

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

在这里玩游戏已经很晚了,但是在搜索类似的解决方案时,这篇文章仍然在谷歌中飞得很高。

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

安装html2hamlgem,进入你的 app/views 目录并尝试一下:

find ./ -name '*.erb' -exec html2haml -e {} {}.haml \;
find ./ -name "*.erb.haml" -exec sh -c 'mv "" "${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.

此解决方案的缺陷在于它不会保留从旧 .erb 文件到新 .haml 文件的修订历史记录。但是有时这些视图文件的修订历史不是什么大问题,这个解决方案对我很有帮助。

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

此外,在删除旧的 .erb 文件之前,请务必注意 html2haml 行中的任何错误。

回答by JobJob

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

html2haml 现在位于 html2haml gem 中,因此您可以使用:

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