Reputation: 2986
Using Rails 3 I am consuming an XML feed generated in drupal or something. The tags it gives me look like:
<body><![CDATA[<p>This is a title<br />A subheading</p>]]></body>
So the intention is that this should really look like:
<p>This is a title<br />A subheading</p>
Which could then be rendered in a view using <%= @mystring.html_safe %>
or <%= raw @mystring %>
or something. The trouble is that rendering the string in this way will simply convert substrings like <
into the <
character. I need a sort of double raw or double unencode to first deal with the chr and then render the tags as html safe.
Anyone know of anything like:
<%= @my_double_safed_string.html_safe.html_safe %>
Upvotes: 4
Views: 1648
Reputation: 706
I don't think this is valid XML - they've sort of escaped the text twice in two different ways, by using entities and cdata. Still, you can parse it using nokogiri for example:
require 'nokogiri'
xml = Nokogiri::XML.parse "<body><![CDATA[<p>This is a title<br />A subheading</p>]]></body>"
text = Nokogiri::XML.parse("<e>#{xml.text}</e>").text
#=> text = "<p>This is a title<br />A subheading</p>"
Seeing as this drupal site is spewing crazy double escaped xml, I'd be inclined to even use a regexp. Hacks to solve a problem hacks created? IDK. Regardless:
xml.text
#=> "<p>This is a title<br />A subheading</p>"
xml.text.gsub(/\&\#([0-9]+);/) { |i| $1.to_i.chr }
#=> "<p>This is a title<br />A subheading</p>"
Hope this helps!
Upvotes: 6