Module: Forkforge::UnicodeOrgFileFormat
- Included in:
- SpecialCasing, UnicodeData
- Defined in:
- lib/forkforge/internal/unicode_org_file.rb
Constant Summary collapse
- HOST =
'www.unicode.org'- @@hashmap =
{}
Instance Method Summary collapse
- #__to_char(cp) ⇒ Object
- #__to_code_point(cp) ⇒ Object
- #i_grab(remote_folder, local_folder, file) ⇒ Object private
- #i_hash(remote_folder, local_folder, file, fields, arrayize = true) ⇒ Object private
- #i_load(remote_folder, local_folder, file) ⇒ Object private
Instance Method Details
#__to_char(cp) ⇒ Object
59 60 61 62 |
# File 'lib/forkforge/internal/unicode_org_file.rb', line 59 def __to_char cp cp = cp.to_s(16) if Integer === cp [cp.to_i(16)].pack('U') end |
#__to_code_point(cp) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/forkforge/internal/unicode_org_file.rb', line 51 def __to_code_point cp case cp when Integer then cp = cp.to_s(16) when Forkforge::CodePoint then cp = cp.code_point end '%04X' % cp.to_i(16) end |
#i_grab(remote_folder, local_folder, file) ⇒ Object (private)
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/forkforge/internal/unicode_org_file.rb', line 9 def i_grab remote_folder, local_folder, file require 'net/http' Net::HTTP.start(HOST) do |http| resp = http.get "/#{remote_folder}/#{file}" if !File.exist? local_folder require 'fileutils' FileUtils.mkpath local_folder end open("#{local_folder}/#{file}", "wb") do |file| file.write(resp.body.gsub(/^\s*#.*?$/, '').gsub(/\R+/, "\n").gsub(/\A\R+/, '')) end end end |
#i_hash(remote_folder, local_folder, file, fields, arrayize = true) ⇒ Object (private)
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/forkforge/internal/unicode_org_file.rb', line 30 def i_hash remote_folder, local_folder, file, fields, arrayize = true if @@hashmap[self.name].nil? @@hashmap[self.name] = {} i_load(remote_folder, local_folder, file).split(/\R/).each do |line| # comment is always last, while the amount of fields is subject to change comment = line.scan(/(?<=#).*?$/).first.strip line.gsub!(/;\s*#.*$/, '') unless comment.nil? values = line.split ';' key = values.first.strip value = (fields.map { |f| [ f, values.shift.strip ] } + [[ :comment, comment ]]).to_h arrayize ? \ (@@hashmap[self.name][key] ||= []) << value : \ @@hashmap[self.name][key] = value end end @@hashmap[self.name] end |
#i_load(remote_folder, local_folder, file) ⇒ Object (private)
24 25 26 27 |
# File 'lib/forkforge/internal/unicode_org_file.rb', line 24 def i_load remote_folder, local_folder, file i_grab(remote_folder, local_folder, file) unless File.exist? "#{local_folder}/#{file}" File.read "#{local_folder}/#{file}" end |