Skip to content
This repository was archived by the owner on Aug 19, 2020. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions lib/unidecoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

# Utilities for transliterating UTF-8 strings to ASCII.
module Unidecoder

# Contains Unicode codepoints, loading as needed from YAML files.
CODEPOINTS = Hash.new { |h, k|
h[k] = YAML::load_file(File.expand_path("../unidecoder/data/#{k}.yml", __FILE__))
h[k] = YAML.safe_load(File.read(File.expand_path("../unidecoder/data/#{k}.yml", __FILE__)))
} unless defined?(CODEPOINTS)

module StringExtensions
Expand Down Expand Up @@ -60,19 +59,10 @@ def in_yaml_file(character)
"#{code_group(unpacked)}.yml (line #{grouped_point(unpacked) + 2})"
end

def define_normalize(library = nil, &block)
return if method_defined? :normalize
begin
require library if library
define_method(:normalize, &block)
rescue LoadError
end
def normalize(str)
str.unicode_normalize
end

define_normalize("unicode") {|str| Unicode.normalize_C(str)}
define_normalize("active_support") {|str| ActiveSupport::Multibyte::Chars.new(str).normalize(:c).to_s}
define_normalize {|str| str}

def decode_char(char)
unpacked = char.unpack("U")[0]
CODEPOINTS[code_group(unpacked)][grouped_point(unpacked)]
Expand Down