Class: Asciify::Mapping
- Inherits:
-
Object
- Object
- Asciify::Mapping
- Defined in:
- lib/asciify.rb
Direct Known Subclasses
Instance Method Summary collapse
- #[](codepoint) ⇒ Object
-
#from_utf8(str) ⇒ Object
converts an UTF-8 string to an array of unicode codepoints.
-
#initialize(language = :default, replacement = "?") ⇒ Mapping
constructor
define a mapping from Unicode codepoints to ASCII chars
language
can be a path to a yaml file which contains the mappings as a Hash.
Constructor Details
#initialize(language = :default, replacement = "?") ⇒ Mapping
define a mapping from Unicode codepoints to ASCII chars language
can be a path to a yaml file which contains the mappings as a Hash
If language
is a symbol, it refers to a builtin mapping
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/asciify.rb', line 26 def initialize(language = :default, replacement = "?") if Symbol === language path = "#{File.dirname(__FILE__)}/mappings/#{language}.yaml" else path = language end h = YAML.load_file(path) i = Iconv.new("UCS-4","UTF-8") # use the default replacement if the hash @map = Hash.new( i.iconv(replacement).unpack(PackFormat) ) # the mappings file is UTF-8, recode to UCS-4 h.each { |k,v| @map[*i.iconv(k).unpack(PackFormat)] = i.iconv(v).unpack(PackFormat) } @map end |
Instance Method Details
#[](codepoint) ⇒ Object
48 49 50 |
# File 'lib/asciify.rb', line 48 def [](codepoint) @map[codepoint] end |
#from_utf8(str) ⇒ Object
converts an UTF-8 string to an array of unicode codepoints
16 17 18 |
# File 'lib/asciify.rb', line 16 def from_utf8(str) Iconv.new(Intermediate,"UTF-8").iconv(str).unpack(PackFormat) end |