¿Cómo puedo codificar / decodificar entidades HTML en Ruby?
Estoy tratando de decodificar algunas entidades HTML, como '<'
convirtiéndose en '<'
.
Tengo una gema vieja ( html_helpers) pero parece haber sido abandonada dos veces.
Alguna recomendación? Necesitaré usarlo en un modelo.
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 "¡I'm highly annoyed with character references!"
=> "¡I'm highly annoyed with character references!"
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 "unescaping" <characters>")
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>' %>
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 '<br>' %>
Produciría
<br>
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 bär"
a.text
=> "foo bär"
O
a = Nokogiri::HTML.parse "¡I'm highly annoyed with character references!"
a.text
=> "¡I'm highly annoyed with character references!"
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 bär"
=> "foo bär"
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
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: < h1 > Test < /h1 >
<%= CGI.unescapeHTML(str).html_safe %>
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