Class: Unicoder::Builder::Emoji

Inherits:
Object
  • Object
show all
Includes:
Unicoder::Builder
Defined in:
lib/unicoder/builders/emoji.rb

Constant Summary collapse

REVERSE_PROPERTY_NAMES =
{
  "Emoji" => :E,
  "Emoji_Modifier_Base" => :B,
  "Emoji_Modifier" => :M,
  "Emoji_Component" => :C,
  "Emoji_Presentation" => :P,
  "Extended_Pictographic" => :X,
}

Instance Attribute Summary

Attributes included from Unicoder::Builder

#formats, #index, #option

Instance Method Summary collapse

Methods included from Unicoder::Builder

#assign, #assign_codepoint, build, #export, #initialize, #meta, #parse_file

Instance Method Details

#initialize_indexObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/unicoder/builders/emoji.rb', line 15

def initialize_index
  @index = {
    PROPERTIES: {},
    FLAGS: [],
    TAGS: [],
    KEYCAPS: [],
    ZWJ: [],
    SD: [],
    LIST: {},
  }
end

#parse!Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/unicoder/builders/emoji.rb', line 27

def parse!
  parse_file :emoji_data, :line, regex: /^(?<codepoints>\S+?) +; (?<property>\S+) *#/ do |line|
    if line["codepoints"]['..']
      codepoints = Range.new(*line["codepoints"].split('..').map{ |codepoint|
        codepoint.to_i(16)
      })
    else
      codepoints = [line["codepoints"].to_i(16)]
    end

    codepoints.each{ |codepoint|
      @index[:PROPERTIES][codepoint] ||= []
      @index[:PROPERTIES][codepoint] << (REVERSE_PROPERTY_NAMES[line["property"]] || line["property"])
    }
  end

  parse_file :emoji_sequences, :line, regex: /^(?<codepoints>.+?)\s*; RGI_Emoji_Flag_Sequence/ do |line|
    codepoints = line["codepoints"].split
    @index[:FLAGS] << codepoints.map{|e| e.to_i(16)}
  end

  parse_file :emoji_sequences, :line, regex: /^(?<codepoints>.+?)\s*; RGI_Emoji_Tag_Sequence/ do |line|
    codepoints = line["codepoints"].split
    @index[:TAGS] << codepoints.map{|e| e.to_i(16)}
  end

  parse_file :emoji_sequences, :line, regex: /^(?<codepoints>.+?)\s*; Emoji_Keycap_Sequence/ do |line|
    @index[:KEYCAPS] << line["codepoints"].split[0].to_i(16)
  end

  parse_file :emoji_zwj_sequences, :line, regex: /^(?!#)(?<codepoints>.+?)\s*;/ do |line|
    codepoints = line["codepoints"].split
    @index[:ZWJ] << codepoints.map{|e| e.to_i(16)}
  end

  parse_file :valid_subdivisions, :xml do |xml|
    subdivisions = []
    xml.css('[idStatus="regular"], [idStatus="deprecated"]').each{ |id|
      subdivisions += id.text.split
    }
    @index[:SD] = subdivisions.uniq
  end

  parse_file :emoji_test, :line, regex: /^(?:# (?<sub>sub)?group: (?<group_name>.*)$)|(?:(?<codepoints>.+?)\s*; fully-qualified )/ do |line|
    if line["group_name"]
      if !line["sub"]
        @current_group_name = line["group_name"]
        @index[:LIST][@current_group_name] = {}
      else
        @current_subgroup_name = line["group_name"]
        @index[:LIST][@current_group_name][@current_subgroup_name] = []
      end
    else
      codepoints = line["codepoints"].split
      @index[:LIST][@current_group_name][@current_subgroup_name] << codepoints.map{|e| e.to_i(16)}.pack("U*")
    end
  end
end