我是 ruby 的新手,正在构建一个前端样式指南,其中包含我想作为 haml 呈现到 pre 标记中的 html 片段。我正在为中间人构建一个助手,并且已经弄清楚如何读取 HTML 文件并输出其内容。现在我想将 html 转换为 haml 并输出它。
环顾四周似乎是 html2haml gem 是我想要使用的,尽管该 gem 上的文档似乎只涵盖了在命令行上使用它,而我正在尝试将此功能添加到帮助程序。
这是目前为止我需要的助手
helpers do
def render_snippet(page)
p1 = ("<pre><code>").html_safe
p2 = File.read("source/"+"#{page}")
p3 = ("</code></pre>").html_safe
p0+p1+p2+p3
end
end
这是我如何使用助手
= render_snippet "partials/examples/typography/elements.html"
最佳答案
为了回答您的问题,您可以通过以下方式让助手在终端 shell 命令之外使用 html2haml gem
# some_view.html.erb
<%= render html_2_haml("home/my_partial.html") %>
# app/helpers/application_helper.rb
module ApplicationHelper
def html_2_haml(path)
file_name = path.split("/").last
path_with_underscore = path.gsub(file_name, "_#{file_name}")
system "html2haml app/views/#{path_with_underscore} app/views/#{path_with_underscore}.haml"
"#{path}.haml"
end
end
现在我想说这绝对不会在生产中工作(因为它动态创建一个新文件和像 Heroku 这样的托管服务不允许这样做)但是如果你只是让自己成为这个的开发助手 - 并且-那也许这对你有帮助。
关于html - 如何将 html 文件呈现为 haml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34334973/