2010-07-15 82 views
7

我创建了一个文本节点并插入到我的文档,像这样:防止Nokogiri逃离角色?

#<Nokogiri::XML::Text:0x3fcce081481c "<%= stylesheet_link_tag 'style'%>">]> 

当我尝试将文档保存与此:

File.open('ng.html', 'w+'){|f| f << page.to_html} 

我实际的文件中得到这样的:

&lt;%= stylesheet_link_tag 'style'%&gt; 

有没有办法禁用转义和保存我的网页与我的erb标签完好?

谢谢!

回答

7

你有义务逃避文本元素一些字符,如:如果你想让你的文字逐字使用CDATA节,因为这里的一切都CDATA节被分析器忽略

" &quot; 
' &apos; 
< &lt; 
> &gt; 
& &amp; 

引入nokogiri例如:

builder = Nokogiri::HTML::Builder.new do |b| 
    b.html do 
    b.head do 
     b.cdata "<%= stylesheet_link_tag 'style'%>" 
    end 
    end 
end 
builder.to_html 

这应该让你的erb标签完好!

+0

甜!谢谢fotos! – mikewilliamson 2010-07-19 11:31:17

10

也许你想使用"<<" method插入原始XML这样的:

builder = Nokogiri::XML::Builder.new do |b| 
    b.html do 
    b.head do 
     b << stylesheet_link_tag 'style' 
    end 
    end 
end 
builder.to_xml 
+0

感谢我所需要的! – 2012-10-16 06:08:53