File: active_support/vendor/builder-2.1.2/builder/xchar.rb

Overview
Module Structure
Class Hierarchy
Code

Overview

Module Structure

  module: <Toplevel Module>
  module: <Built-in Module>
  module: Builder#11, #26
has properties
module method: check_for_name_collision / 3 #12
  module: XChar#32
has properties
constant: CP1252 #37
constant: PREDEFINED #68
constant: VALID #75

Class Hierarchy

Object ( Builtin-Module )
  String    #108
  Fixnum    #89

Code

   1  #!/usr/bin/env ruby
   2 
   3  # The XChar library is provided courtesy of Sam Ruby (See
   4  # http://intertwingly.net/stories/2005/09/28/xchar.rb)
   5 
   6  # --------------------------------------------------------------------
   7 
   8  # If the Builder::XChar module is not currently defined, fail on any
   9  # name clashes in standard library classes.
  10 
  11  module Builder
  12    def self.check_for_name_collision(klass, method_name, defined_constant=nil)
  13      if klass.instance_methods.include?(method_name.to_s)
  14        fail RuntimeError,
  15          "Name Collision: Method '#{method_name}' is already defined in #{klass}"
  16      end
  17    end
  18  end
  19 
  20  if ! defined?(Builder::XChar)
  21    Builder.check_for_name_collision(String, "to_xs")
  22    Builder.check_for_name_collision(Fixnum, "xchr")
  23  end
  24 
  25  ######################################################################
  26  module Builder
  27 
  28    ####################################################################
  29    # XML Character converter, from Sam Ruby:
  30    # (see http://intertwingly.net/stories/2005/09/28/xchar.rb).
  31    #
  32    module XChar # :nodoc:
  33 
  34      # See
  35      # http://intertwingly.net/stories/2004/04/14/i18n.html#CleaningWindows
  36      # for details.
  37      CP1252 = {                  # :nodoc:
  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
  43        134 => 8224,              # dagger
  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
  55        149 => 8226,              # bullet
  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
  65      }
  66 
  67      # See http://www.w3.org/TR/REC-xml/#dt-chardata for details.
  68      PREDEFINED = {
  69        38 => '&amp;',            # ampersand
  70        60 => '&lt;',             # left angle bracket
  71        62 => '&gt;',             # right angle bracket
  72      }
  73 
  74      # See http://www.w3.org/TR/REC-xml/#charsets for details.
  75      VALID = [
  76        0x9, 0xA, 0xD,
  77        (0x20..0xD7FF),
  78        (0xE000..0xFFFD),
  79        (0x10000..0x10FFFF)
  80      ]
  81    end
  82 
  83  end
  84 
  85 
  86  ######################################################################
  87  # Enhance the Fixnum class with a XML escaped character conversion.
  88  #
  89  class Fixnum
  90    XChar = Builder::XChar if ! defined?(XChar)
  91 
  92    # XML escaped version of chr
  93    def xchr
  94      n = XChar::CP1252[self] || self
  95      case n when *XChar::VALID
  96        XChar::PREDEFINED[n] or (n<128 ? n.chr : "&##{n};")
  97      else
  98        '*'
  99      end
 100    end
 101  end
 102 
 103 
 104  ######################################################################
 105  # Enhance the String class with a XML escaped character version of
 106  # to_s.
 107  #
 108  class String
 109    # XML escaped version of to_s
 110    def to_xs
 111      unpack('U*').map {|n| n.xchr}.join # ASCII, UTF-8
 112    rescue
 113      unpack('C*').map {|n| n.xchr}.join # ISO-8859-1, WIN-1252
 114    end
 115  end