开发者

Preventing Nokogiri from escaping characters?

I have created a text node and inserted into开发者_如何学Python my document like so:

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

When I try to save the document with this:

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

I get this in the actual document:

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

Is there a way to disable the escaping and save my page with my erb tags intact?

Thanks!


Perhaps you want to use the "<<" method to insert raw XML like this:

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


You are obliged to escape some characters in text elements like:

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

If you want your text verbatim use a CDATA section since everything inside a CDATA section is ignored by the parser.

Nokogiri example:

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

This should keep you erb tags intact!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜