File: generate_constants.rb

package info (click to toggle)
ruby-unicode-emoji 4.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 948 kB
  • sloc: ruby: 1,344; makefile: 4
file content (344 lines) | stat: -rw-r--r-- 10,707 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
require_relative '../lib/unicode/emoji/constants'
require_relative '../lib/unicode/emoji/index'
require_relative '../lib/unicode/emoji/lazy_constants'

include Unicode::Emoji

def write_regexes(regexes, dirpath)
  regexes.each do |const_name, regex|
    write_regex(const_name, regex, dirpath)
  end
end

def write_regex(const_name, regex, dirpath)
  filename = const_name.to_s.downcase
  filepath = File.join(dirpath, "#{filename}.rb")

  File.write(filepath, <<~CONTENT)
    # This file was generated by a script, please do not edit it by hand.
    # See `$ rake generate_constants` and data/generate_constants.rb for more info.

    module Unicode
      module Emoji
        #{const_name} = #{regex.inspect}
      end
    end
  CONTENT
  puts "#{const_name} written to #{filepath}"
end

# Converts [1, 2, 3, 5, 6, 20, 21, 22, 23, 100] (it does not need to be sorted) to [[1, 2, 3], [5, 6], [20, 21, 22, 23], [100]]
def groupify(arr)
  arr = arr.sort
  prev = nil
  arr.slice_before do |el|
    (prev.nil? || el != prev + 1).tap { prev = el }
  end
end

# Converts [1, 2, 3, 5, 6, 20, 21, 22, 23, 100] (it does not need to be sorted) to [1..3, 5, 6, 20..23, 100]
def rangify(arr)
  groupify(arr).map do |group|
    group.size < 3 ? group : Range.new(group.first, group.last)
  end.flatten
end

def pack(ord)
  Regexp.escape(Array(ord).pack("U*"))
end

def join(*strings)
  "(?:" + strings.join("|") + ")"
end

def character_class(ords_with_ranges)
  "[" + ords_with_ranges.map{ |ord_or_range|
      ord_or_range.is_a?(Range) ?
        pack(ord_or_range.first) + "-" + pack(ord_or_range.last) :
        pack(ord_or_range)
    }.join +
  "]"
end

def pack_and_join(ords)
  if ords.any? { |e| e.is_a?(Array) }
    join(*ords.map { |ord| pack(ord) })
  else
    character_class(rangify(ords))
  end
end

def compile(emoji_character:, emoji_modifier:, emoji_modifier_base:, emoji_component:, emoji_presentation:, text_presentation:, picto:, picto_no_emoji:)
  visual_component = pack_and_join(VISUAL_COMPONENT)

  emoji_presentation_sequence = \
    join(
      text_presentation + pack(EMOJI_VARIATION_SELECTOR),
      emoji_presentation + "(?!" + pack(TEXT_VARIATION_SELECTOR) + ")" + pack(EMOJI_VARIATION_SELECTOR) + "?",
    )

  non_component_emoji_presentation_sequence = \
    "(?!" + emoji_component + ")" + emoji_presentation_sequence

  basic_emoji = \
    join(
      non_component_emoji_presentation_sequence,
      visual_component,
    )

  text_keycap_sequence = \
    pack_and_join(EMOJI_KEYCAPS) + pack(EMOJI_KEYCAP_SUFFIX)

  text_presentation_sequence = \
    join(
      text_presentation + "(?!" + join(emoji_modifier, pack(EMOJI_VARIATION_SELECTOR)) + ")" + pack(TEXT_VARIATION_SELECTOR) + "?",
      emoji_presentation + pack(TEXT_VARIATION_SELECTOR),
    )

  text_emoji = \
    join(
      "(?!" + emoji_component + ")" + text_presentation_sequence,
      text_keycap_sequence,
    )

  emoji_modifier_sequence = \
    emoji_modifier_base + emoji_modifier

  emoji_keycap_sequence = \
    pack_and_join(EMOJI_KEYCAPS) + pack([EMOJI_VARIATION_SELECTOR, EMOJI_KEYCAP_SUFFIX])

  emoji_valid_flag_sequence = \
    pack_and_join(VALID_REGION_FLAGS)

  emoji_well_formed_flag_sequence = \
    '\p{RI}{2}'

  emoji_core_sequence = \
    join(
      emoji_keycap_sequence,
      emoji_modifier_sequence,
      non_component_emoji_presentation_sequence,
    )

  # Sort to make sure complex sequences match first
  emoji_rgi_tag_sequence = \
    pack_and_join(RECOMMENDED_SUBDIVISION_FLAGS.sort_by(&:length).reverse)

  emoji_valid_tag_sequence = \
    "(?:" +
      pack(EMOJI_TAG_BASE_FLAG) +
      "(?:" + VALID_SUBDIVISIONS.sort_by(&:length).reverse.map{ |sd|
        sd.tr("\u{30}-\u{39}\u{61}-\u{7A}", "\u{E0030}-\u{E0039}\u{E0061}-\u{E007A}")
      }.join("|") + ")" +
      pack(CANCEL_TAG) +
    ")"

  emoji_well_formed_tag_sequence = \
    "(?:" +
      join(
        non_component_emoji_presentation_sequence,
        emoji_modifier_sequence,
      ) +
      pack_and_join(SPEC_TAGS) + "{1,30}" +
      pack(CANCEL_TAG) +
    ")"

  # Sort to make sure complex sequences match first
  emoji_rgi_zwj_sequence = \
    pack_and_join(RECOMMENDED_ZWJ_SEQUENCES.sort_by(&:length).reverse)

  # FQE+MQE: Make VS16 optional after ZWJ has appeared
  emoji_rgi_include_mqe_zwj_sequence = emoji_rgi_zwj_sequence.gsub(
      /#{ pack(ZWJ) }[^|]+?\K#{ pack(EMOJI_VARIATION_SELECTOR) }/,
      pack(EMOJI_VARIATION_SELECTOR) + "?"
    )

  # FQE+MQE+UQE: Make all VS16 optional
  emoji_rgi_include_mqe_uqe_zwj_sequence = emoji_rgi_zwj_sequence.gsub(
      pack(EMOJI_VARIATION_SELECTOR),
      pack(EMOJI_VARIATION_SELECTOR) + "?",
    )

  emoji_valid_zwj_element = \
    join(
      emoji_modifier_sequence,
      emoji_presentation_sequence,
      emoji_character,
    )

  emoji_valid_zwj_sequence = \
    "(?:" +
      "(?:" + emoji_valid_zwj_element + pack(ZWJ) + ")+" + emoji_valid_zwj_element +
    ")"

  emoji_rgi_sequence = \
    join(
      emoji_rgi_zwj_sequence,
      emoji_rgi_tag_sequence,
      emoji_valid_flag_sequence,
      emoji_core_sequence,
      visual_component,
    )

  emoji_rgi_sequence_include_text = \
    join(
      emoji_rgi_zwj_sequence,
      emoji_rgi_tag_sequence,
      emoji_valid_flag_sequence,
      emoji_core_sequence,
      visual_component,
      text_emoji,
    )

  emoji_rgi_include_mqe_sequence = \
    join(
      emoji_rgi_include_mqe_zwj_sequence,
      emoji_rgi_tag_sequence,
      emoji_valid_flag_sequence,
      emoji_core_sequence,
      visual_component,
    )

  emoji_rgi_include_mqe_uqe_sequence = \
    join(
      emoji_rgi_include_mqe_uqe_zwj_sequence,
      text_emoji, # also uqe
      emoji_rgi_tag_sequence,
      emoji_valid_flag_sequence,
      emoji_core_sequence,
      visual_component,
    )

  emoji_valid_sequence = \
    join(
      emoji_valid_zwj_sequence,
      emoji_valid_tag_sequence,
      emoji_valid_flag_sequence,
      emoji_core_sequence,
      visual_component,
    )

  emoji_valid_sequence_include_text = \
    join(
      emoji_valid_zwj_sequence,
      emoji_valid_tag_sequence,
      emoji_valid_flag_sequence,
      emoji_core_sequence,
      visual_component,
      text_emoji,
    )

  emoji_well_formed_sequence = \
    join(
      emoji_valid_zwj_sequence,
      emoji_well_formed_tag_sequence,
      emoji_well_formed_flag_sequence,
      emoji_core_sequence,
      visual_component,
    )

  emoji_well_formed_sequence_include_text = \
    join(
      emoji_valid_zwj_sequence,
      emoji_well_formed_tag_sequence,
      emoji_well_formed_flag_sequence,
      emoji_core_sequence,
      visual_component,
      text_emoji,
    )

  emoji_possible_modification = \
    join(
      emoji_modifier,
      pack([EMOJI_VARIATION_SELECTOR, EMOJI_KEYCAP_SUFFIX]) + "?",
      "[󠀠-󠁾]+󠁿" # raw tags
    )

  emoji_possible_zwj_element = \
    join(
      emoji_well_formed_flag_sequence,
      emoji_character + emoji_possible_modification + "?"
    )

  emoji_possible = \
    emoji_possible_zwj_element + "(?:" + pack(ZWJ) + emoji_possible_zwj_element + ")*"

  regexes = {}

  # Matches basic singleton emoji and all kind of sequences, but restrict zwj and tag sequences to known sequences (rgi)
  regexes[:REGEX] = Regexp.compile(emoji_rgi_sequence)

  # rgi + singleton text
  regexes[:REGEX_INCLUDE_TEXT] = Regexp.compile(emoji_rgi_sequence_include_text)

  # Matches basic singleton emoji and all kind of sequences, but restrict zwj and tag sequences to known sequences (rgi)
  # Also make VS16 optional if not at first emoji character
  regexes[:REGEX_INCLUDE_MQE] = Regexp.compile(emoji_rgi_include_mqe_sequence)

  # Matches basic singleton emoji and all kind of sequences, but restrict zwj and tag sequences to known sequences (rgi)
  # Also make VS16 optional even at first emoji character
  regexes[:REGEX_INCLUDE_MQE_UQE] = Regexp.compile(emoji_rgi_include_mqe_uqe_sequence)

  # Matches basic singleton emoji and all kind of valid sequences
  regexes[:REGEX_VALID] = Regexp.compile(emoji_valid_sequence)

  # valid + singleton text
  regexes[:REGEX_VALID_INCLUDE_TEXT] = Regexp.compile(emoji_valid_sequence_include_text)
  
  # Matches basic singleton emoji and all kind of sequences
  regexes[:REGEX_WELL_FORMED] = Regexp.compile(emoji_well_formed_sequence)
  
  # well-formed + singleton text
  regexes[:REGEX_WELL_FORMED_INCLUDE_TEXT] = Regexp.compile(emoji_well_formed_sequence_include_text)

  # Quick test which might lead to false positves
  # See https://www.unicode.org/reports/tr51/#EBNF_and_Regex
  regexes[:REGEX_POSSIBLE] = Regexp.compile(emoji_possible)

  # Matches only basic single, non-textual emoji, ignores some components like simple digits
  regexes[:REGEX_BASIC] = Regexp.compile(basic_emoji)

  # Matches only basic single, textual emoji, ignores components like modifiers or simple digits
  regexes[:REGEX_TEXT] = Regexp.compile(text_emoji)
  regexes[:REGEX_TEXT_PRESENTATION] = Regexp.compile(text_presentation)

  # Export regexes for Emoji properties so they can be used with newer Unicode than Ruby's
  regexes[:REGEX_PROP_EMOJI] = Regexp.compile(emoji_character)
  regexes[:REGEX_PROP_MODIFIER] = Regexp.compile(emoji_modifier)
  regexes[:REGEX_PROP_MODIFIER_BASE] = Regexp.compile(emoji_modifier_base)
  regexes[:REGEX_PROP_COMPONENT] = Regexp.compile(emoji_component)
  regexes[:REGEX_PROP_PRESENTATION] = Regexp.compile(emoji_presentation)

  # Same goes for ExtendedPictographic
  regexes[:REGEX_PICTO] = Regexp.compile(picto)
  regexes[:REGEX_PICTO_NO_EMOJI] = Regexp.compile(picto_no_emoji)

  # Emoji keycaps
  regexes[:REGEX_EMOJI_KEYCAP] = Regexp.compile(emoji_keycap_sequence)

  regexes
end

regexes = compile(
  emoji_character:      pack_and_join(EMOJI_CHAR),
  emoji_modifier:       pack_and_join(EMOJI_MODIFIERS),
  emoji_modifier_base:  pack_and_join(EMOJI_MODIFIER_BASES),
  emoji_component:      pack_and_join(EMOJI_COMPONENT),
  emoji_presentation:   pack_and_join(EMOJI_PRESENTATION),
  text_presentation:    pack_and_join(TEXT_PRESENTATION),
  picto:                pack_and_join(EXTENDED_PICTOGRAPHIC),
  picto_no_emoji:       pack_and_join(EXTENDED_PICTOGRAPHIC_NO_EMOJI)
)
write_regexes(regexes, File.expand_path("../lib/unicode/emoji/generated", __dir__))

native_regexes = compile(
  emoji_character:      "\\p{Emoji}",
  emoji_modifier:       "\\p{EMod}",
  emoji_modifier_base:  "\\p{EBase}",
  emoji_component:      "\\p{EComp}",
  emoji_presentation:   "\\p{EPres}",
  text_presentation:    "[\\p{Emoji}&&\\P{EPres}]",
  picto:                "\\p{ExtPict}",
  picto_no_emoji:       "[\\p{ExtPict}&&\\P{Emoji}]"
)
write_regexes(native_regexes, File.expand_path("../lib/unicode/emoji/generated_native", __dir__))