You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ascii-ify /asciify (transliterate from unicode (UTF-8) to ASCII (7-bit))
[stringex gem][github.com/rsl/stringex] - string extensions includes Unidecoder (aka asciify, that is from unicode to ascii); also includes acts_as_url (aka slugifier)
def decode(string)
string.chars.map{|char| decoded(char)}.join
end
def decoded(character)
localized(character) || from_yaml(character)
end
def localized(character)
Localization.translate(:transliterations, character)
end
# Contains Unicode codepoints, loading as needed from YAML files
CODEPOINTS = Hash.new{|h, k|
h[k] = ::YAML.load_file(File.join(File.expand_path(File.dirname(__FILE__)), "unidecoder_data", "#{k}.yml"))
} unless defined?(CODEPOINTS)
def from_yaml(character)
return character unless character.ord > 128
unpacked = character.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
# Returns the Unicode codepoint grouping for the given character
def code_group(unpacked_character)
"x%02x" % (unpacked_character >> 8)
end
# Returns the index of the given character in the YAML file for its codepoint group
def grouped_point(unpacked_character)
unpacked_character & 255
end