module DYI::Formatter::XmlChar

@since 0.0.0

Constants

ATTR_PREDEFINED

See www.w3.org/TR/REC-xml/#dt-chardata for details.

CP1252

See intertwingly.net/stories/2004/04/14/i18n.html#CleaningWindows

PREDEFINED

See www.w3.org/TR/REC-xml/#dt-chardata for details.

VALID

See www.w3.org/TR/REC-xml/#charsets for details.

Private Instance Methods

attr_escape(s) click to toggle source
# File lib/dyi/formatter/base.rb, line 153
def attr_escape(s)
  s.to_s.unpack('U*').map {|n| code_to_char(n, true)}.join # ASCII, UTF-8
rescue
  s.to_s.unpack('C*').map {|n| code_to_char(n, true)}.join # ISO-8859-1, WIN-1252
end
code_to_char(code, is_attr=false) click to toggle source
# File lib/dyi/formatter/base.rb, line 159
def code_to_char(code, is_attr=false)
  code = CP1252[code] || code
  case code when *VALID
    (is_attr ? ATTR_PREDEFINED : PREDEFINED)[code] || (code<128 ? code.chr : "&##{code};")
  else
    '*'
  end
end
escape(s) click to toggle source
# File lib/dyi/formatter/base.rb, line 147
def escape(s)
  s.to_s.unpack('U*').map {|n| code_to_char(n)}.join # ASCII, UTF-8
rescue
  s.to_s.unpack('C*').map {|n| code_to_char(n)}.join # ISO-8859-1, WIN-1252
end