Skip to content

Instantly share code, notes, and snippets.

@webgago
Created April 1, 2011 13:30
Show Gist options
  • Save webgago/898141 to your computer and use it in GitHub Desktop.
Save webgago/898141 to your computer and use it in GitHub Desktop.
fix russian chars in xml/rss builder
class Fixnum #:nodoc:
XChar = Builder::XChar unless defined?(XChar)
# XML escaped version of chr (inlines cyrillic unicode range)
def xchr
n = XChar::CP1252[self] || self
n = 42 unless XChar::VALID.find { |value| value.kind_of?(Range) ? value.include?(n) : (value == n) }
XChar::PREDEFINED[n] or case n
when 0...128
n.chr
when 0x400..0x4FF
[n].pack 'U'
else
"&##{n};"
end
end
end
class String
# XML escaped version of to_s
def to_xs
unpack('U*').map {|n| n.xchr}.join # ASCII, UTF-8
rescue
unpack('C*').map {|n| n.xchr}.join # ISO-8859-1, WIN-1252
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment