Convert Ruby string with ampersand-hash- char -semicolon characters to ascii or html-friendly string

Using Rails 3 I consume an XML stream created in drupal or something else. The tags that he gives me look like this:

<body><![CDATA[&#60;p&#62;This is a title&#60;br /&#62;A subheading&#60;/p&#62;]]></body>

So the intention is that it should look like this:

<p>This is a title<br />A subheading</p>

What can be shown in a view with <%= @mystring.html_safe %>or <%= raw @mystring %>or something else. The problem is that rendering the string in this way just converts the type substrings &#60;to a character <. I need something like double raw or double unencode for the first deal with chr, and then render the tags as safe html.

Does anyone know something like:

<%= @my_double_safed_string.html_safe.html_safe %>
+3
source share
1 answer

, XML - , cdata. , , nokogiri, :

require 'nokogiri'

xml = Nokogiri::XML.parse "<body><![CDATA[&#60;p&#62;This is a title&#60;br /&#62;A subheading&#60;/p&#62;]]></body>"
text = Nokogiri::XML.parse("<e>#{xml.text}</e>").text
#=> text = "<p>This is a title<br />A subheading</p>"

, drupal escape- xml, . ? . :

xml.text
#=> "&#60;p&#62;This is a title&#60;br /&#62;A subheading&#60;/p&#62;"
xml.text.gsub(/\&\#([0-9]+);/) { |i| $1.to_i.chr }
#=> "<p>This is a title<br />A subheading</p>"

, !

+5

All Articles