¿Cómo puedo codificar / decodificar entidades HTML en Ruby?


Estoy tratando de decodificar algunas entidades HTML, como '&amp;lt;' convirtiéndose en '<'.

Tengo una gema vieja ( html_helpers) pero parece haber sido abandonada dos veces.

Alguna recomendación? Necesitaré usarlo en un modelo.

 174
Author: wehal3001, 2009-10-21

7 answers

HTMLEntities puede hacerlo:

: jmglov@laurana; sudo gem install htmlentities
Successfully installed htmlentities-4.2.4
: jmglov@laurana;  irb
irb(main):001:0> require 'htmlentities'
=> []
irb(main):002:0> HTMLEntities.new.decode "&iexcl;I&#39;m highly&nbsp;annoyed with character references!"
=> "¡I'm highly annoyed with character references!"
 134
Author: Ivailo Bardarov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-07-16 14:31:19

Para codificar los caracteres, puede usar CGI.escapeHTML:

string = CGI.escapeHTML('test "escaping" <characters>')

Para decodificarlos, hay CGI.unescapeHTML:

CGI.unescapeHTML("test &quot;unescaping&quot; &lt;characters&gt;")

Por supuesto, antes de eso necesitas incluir la biblioteca CGI:

require 'cgi'

Y si estás en Rails, no necesitas usar CGI para codificar la cadena. Está el método h.

<%= h 'escaping <html>' %>
 264
Author: Damien MATHIEU,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-11-29 22:55:32

Para decodificar caracteres en Rails use:

<%= raw '<html>' %>

Así que,

<%= raw '&lt;br&gt;' %>

Produciría

<br>
 34
Author: memonk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-08-02 13:27:33

Creo que Nokogiri gem también es una buena opción. Es muy estable y tiene una gran comunidad que contribuye.

Muestras:

a = Nokogiri::HTML.parse "foo&nbsp;b&auml;r"    
a.text 
=> "foo bär"

O

a = Nokogiri::HTML.parse "&iexcl;I&#39;m highly&nbsp;annoyed with character references!"
a.text
=> "¡I'm highly annoyed with character references!"
 30
Author: Hoang Le,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-07-18 01:35:20

Si no desea agregar una nueva dependencia solo para hacer esto (como HTMLEntities) y ya está usando Hpricot, puede escapar y no escapar para usted. Maneja mucho más que CGI:

Hpricot.uxs "foo&nbsp;b&auml;r"
=> "foo bär"
 8
Author: Jason L Perry,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-12-06 18:13:37

Puedes usar htmlascii gema:

Htmlascii.convert string
 0
Author: kartouch,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-02-10 15:16:33
<% str="<h1> Test </h1>" %>

result: &lt; h1 &gt; Test &lt; /h1 &gt;

<%= CGI.unescapeHTML(str).html_safe %>
 -5
Author: Usman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-01-01 11:47:00