Preventing Nokogiri from escaping characters?

I created node text and pasted into my document like this:

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

When I try to save a 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 turn off escaping and save my page with saved erb tags?

Thanks!

+7
ruby ruby-on-rails nokogiri
source share
2 answers

You must avoid some characters in text elements, for example:

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

If you want your text to use the CDATA section verbatim, since everything inside the 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 contain erb tags unchanged!

+7
source share

Perhaps you want to use the <<method to insert the source XML as follows:

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

All Articles