3 # The XChar library is provided courtesy of Sam Ruby (See
4 # http://intertwingly.net/stories/2005/09/28/xchar.rb)
6 # --------------------------------------------------------------------
8 # If the Builder::XChar module is not currently defined, fail on any
9 # name clashes in standard library classes.
12 def self.check_for_name_collision(klass
, method_name
, defined_constant
=nil)
13 if klass
.instance_methods
.include?(method_name
.to_s
)
15 "Name Collision: Method '#{method_name}' is already defined in #{klass}"
20 if ! defined?(Builder
::XChar)
21 Builder
.check_for_name_collision(String
, "to_xs")
22 Builder
.check_for_name_collision(Fixnum
, "xchr")
25 ######################################################################
28 ####################################################################
29 # XML Character converter, from Sam Ruby:
30 # (see http://intertwingly.net/stories/2005/09/28/xchar.rb).
32 module XChar
# :nodoc:
35 # http://intertwingly.net/stories/2004/04/14/i18n.html#CleaningWindows
38 128 => 8364, # euro sign
39 130 => 8218, # single low-9 quotation mark
40 131 => 402, # latin small letter f with hook
41 132 => 8222, # double low-9 quotation mark
42 133 => 8230, # horizontal ellipsis
44 135 => 8225, # double dagger
45 136 => 710, # modifier letter circumflex accent
46 137 => 8240, # per mille sign
47 138 => 352, # latin capital letter s with caron
48 139 => 8249, # single left-pointing angle quotation mark
49 140 => 338, # latin capital ligature oe
50 142 => 381, # latin capital letter z with caron
51 145 => 8216, # left single quotation mark
52 146 => 8217, # right single quotation mark
53 147 => 8220, # left double quotation mark
54 148 => 8221, # right double quotation mark
56 150 => 8211, # en dash
57 151 => 8212, # em dash
58 152 => 732, # small tilde
59 153 => 8482, # trade mark sign
60 154 => 353, # latin small letter s with caron
61 155 => 8250, # single right-pointing angle quotation mark
62 156 => 339, # latin small ligature oe
63 158 => 382, # latin small letter z with caron
64 159 => 376, # latin capital letter y with diaeresis
67 # See http://www.w3.org/TR/REC-xml/#dt-chardata for details.
69 38 => '&', # ampersand
70 60 => '<', # left angle bracket
71 62 => '>', # right angle bracket
74 # See http://www.w3.org/TR/REC-xml/#charsets for details.
86 ######################################################################
87 # Enhance the Fixnum class with a XML escaped character conversion.
90 XChar
= Builder
::XChar if ! defined?(XChar
)
92 # XML escaped version of chr
94 n
= XChar
::CP1252[self] || self
95 case n
when *XChar
::VALID
96 XChar
::PREDEFINED[n
] or (n
<128 ? n
.chr
: "&##{n};")
104 ######################################################################
105 # Enhance the String class with a XML escaped character version of
109 # XML escaped version of to_s
111 unpack('U*').map
{|n
| n
.xchr
}.join
# ASCII, UTF-8
113 unpack('C*').map
{|n
| n
.xchr
}.join
# ISO-8859-1, WIN-1252