2015-12-17 37 views
2

我對ruby相當陌生,正致力於構建一個具有html片段的前端styleguide,我希望將haml渲染爲pre標籤。我爲中間人建立了一個幫手,並且已經想出瞭如何讀取HTML文件並輸出其內容。現在我想將html轉換爲haml並輸出。如何將html文件渲染爲haml

圍繞它看起來像html2haml寶石是我想使用的,雖然該寶石上的文檔似乎只包括在命令行中使用它,而我試圖將此功能添加到幫助程序。

這裏是我迄今爲止針對助手

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" 

回答

1

要回答你的問題,這是你可以做一個幫手在終端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這樣的託管服務就不會允許這樣做),但是如果你只是讓自己成爲這樣的開發助手,那麼這可能對你有幫助。

0

我結束了這方面的工作多一些,並結束了以下內容:

def render_html2haml(file) 
    templateSource = preserve(File.read("source/"+"#{file}")) 
    haml = Html2haml::HTML.new(templateSource, {:erb => nil}) 
    content_tag(:pre, content_tag(:code, haml.render)) 
end