Contains Unicode codepoints, loading as needed from YAML files
Returns string with its UTF-8 characters transliterated to ASCII ones
You're probably better off just using the added String#to_ascii
# File lib/stringex/unidecoder.rb, line 15 def decode(string) string.gsub(/[^\x00-\x7f]/) do |codepoint| if localized = local_codepoint(codepoint) localized else begin unpacked = codepoint.unpack("U")[0] CODEPOINTS[code_group(unpacked)][grouped_point(unpacked)] rescue # Hopefully this won't come up much # TODO: Make this note something to the user that is reportable to me perhaps "?" end end end end
Returns default locale for localized transliterations. NOTE: Will set @locale as well.
# File lib/stringex/unidecoder.rb, line 76 def default_locale @default_locale ||= "en" @locale = @default_locale end
Sets the default locale for localized transliterations. NOTE: Will set @locale as well.
# File lib/stringex/unidecoder.rb, line 82 def default_locale=(new_locale) @default_locale = new_locale # Seems logical that @locale should be the new default @locale = new_locale end
Returns character for the given Unicode codepoint
# File lib/stringex/unidecoder.rb, line 33 def encode(codepoint) ["0x#{codepoint}".to_i(16)].pack("U") end
Returns Unicode codepoint for the given character
# File lib/stringex/unidecoder.rb, line 38 def get_codepoint(character) "%04x" % character.unpack("U")[0] end
Returns string indicating which file (and line) contains the transliteration value for the character
# File lib/stringex/unidecoder.rb, line 44 def in_yaml_file(character) unpacked = character.unpack("U")[0] "#{code_group(unpacked)}.yml (line #{grouped_point(unpacked) + 2})" end
Returns the localized transliteration for a codepoint
# File lib/stringex/unidecoder.rb, line 89 def local_codepoint(codepoint) locale_hash = LOCAL_CODEPOINTS[locale] || LOCAL_CODEPOINTS[locale.is_a?(Symbol) ? locale.to_s : locale.to_sym] locale_hash && locale_hash[codepoint] end
Returns locale for localized transliterations
# File lib/stringex/unidecoder.rb, line 60 def locale if @locale @locale elsif defined?(I18n) I18n.locale else default_locale end end
Sets locale for localized transliterations
# File lib/stringex/unidecoder.rb, line 71 def locale=(new_locale) @locale = new_locale end
Adds localized transliterations to Unidecoder
# File lib/stringex/unidecoder.rb, line 50 def localize_from(hash_or_path_to_file) hash = if hash_or_path_to_file.is_a?(Hash) hash_or_path_to_file else YAML.load_file(hash_or_path_to_file) end verify_local_codepoints hash end
Runs a block with default locale
# File lib/stringex/unidecoder.rb, line 104 def with_default_locale(&block) with_locale default_locale, &block end
Runs a block with a temporary locale setting, returning the locale to the original state when complete
# File lib/stringex/unidecoder.rb, line 95 def with_locale(new_locale, &block) new_locale = default_locale if new_locale == :default original_locale = locale self.locale = new_locale block.call self.locale = original_locale end
Generated with the Darkfish Rdoc Generator 2.