How to convert with Ruby accented characters in HTML special entities
Asked Answered
E

1

7

How can I do this on Ruby?

puts some_method("ò")
# => "ò"

In other words convert an accented character like ò to his HTML version: ò

I tried like this:

# coding: utf-8
require 'rubygems'
require 'htmlentities'
require 'unicode'

coder = HTMLEntities.new
string = "Scròfina"
puts coder.encode(string, :named)

but what I get this (from: http://htmlentities.rubyforge.org/) :

/Library/Ruby/Gems/1.8/gems/htmlentities-4.2.0/lib/htmlentities/encoder.rb:85:in `unpack': malformed UTF-8 character (expected 2 bytes, given 1 bytes) (ArgumentError)
 from /Library/Ruby/Gems/1.8/gems/htmlentities-4.2.0/lib/htmlentities/encoder.rb:85:in `encode_decimal'
 from (eval):2:in `encode_extended'
 from /Library/Ruby/Gems/1.8/gems/htmlentities-4.2.0/lib/htmlentities/encoder.rb:18:in `encode'
 from /Library/Ruby/Gems/1.8/gems/htmlentities-4.2.0/lib/htmlentities/encoder.rb:18:in `gsub!'
 from /Library/Ruby/Gems/1.8/gems/htmlentities-4.2.0/lib/htmlentities/encoder.rb:18:in `encode'
 from /Library/Ruby/Gems/1.8/gems/htmlentities-4.2.0/lib/htmlentities.rb:74:in `encode'
 from unicode_pleasure.rb:8

Thank you for your time!

  • Leonardo
Excepting answered 30/10, 2009 at 13:31 Comment(1)
Just tested your example with htmlentities 4.0.0 and everything works fine. Not much help, I know. :(Earvin
V
12

I had explicitly set the $KCODE to make your example work. Also, make sure your source file is actually encoded as UTF-8!

# coding: utf-8
require 'rubygems'
require 'htmlentities'
require 'unicode'
$KCODE = 'UTF-8'
coder = HTMLEntities.new
string = "Scròfina"
puts coder.encode(string, :named)
Vitellus answered 30/10, 2009 at 14:5 Comment(2)
It Works! Thank You Jonathan! (I can't vote you up cause I have less then 15 reputation :-(Excepting
named parameter was what did the difference to me. Thanks.Grassland

© 2022 - 2024 — McMap. All rights reserved.