Debian Codegraph @

tools/gen_twemoji.py

raw
1 """Generate emojione data."""
2 import os
3 import json
4 import sys
5 import copy
6 sys.path.append('../pymdownx/')
7 current_dir = os.path.dirname(os.path.abspath(__file__))
8
9 ADDITIONAL_ALIASES = {}
10
11 # Special emoji
12 SPECIAL_EMOJI = {
13 # # An unofficial emoji supported by very few
14 ":shibuya:": {
15 "category": "travel",
16 "name": "Shibuya 109",
17 "unicode": "e50a"
18 },
19
20 # Skier skin tones (not supported by Unicode spec)
21 ":skier_tone1:": {
22 "category": "activity",
23 "name": "skier: light skin tone",
24 "unicode": "26f7-1f3fb"
25 },
26 ":skier_tone2:": {
27 "category": "activity",
28 "name": "skier: medium-light skin tone",
29 "unicode": "26f7-1f3fc"
30 },
31 ":skier_tone3:": {
32 "category": "activity",
33 "name": "skier: medium skin tone",
34 "unicode": "26f7-1f3fd"
35 },
36 ":skier_tone4:": {
37 "category": "activity",
38 "name": "skier: medium-dark skin tone",
39 "unicode": "26f7-1f3fe"
40 },
41 ":skier_tone5:": {
42 "category": "activity",
43 "name": "skier: dark skin tone",
44 "unicode": "26f7-1f3ff"
45 },
46
47 # Woman levitate (not supported in Unicode spec)
48 ":woman_levitate:": {
49 "category": "people",
50 "name": "woman in business suit levitating",
51 "unicode": "1f574-fe0f-200d-2640-fe0f",
52 "aliases": ["woman_in_business_suit_levitating"]
53 },
54 ":woman_levitate_tone1:": {
55 "category": "people",
56 "name": "woman in business suit levitating: light skin tone",
57 "unicode": "1f574-1f3fb-200d-2640-fe0f",
58 "aliases": [
59 "woman_in_business_suit_levitating_tone1",
60 "woman_in_business_suit_levitating_light_skin_tone"
61 ]
62 },
63 ":woman_leviate_tone2:": {
64 "category": "people",
65 "name": "woman in business suit levitating: medium-light skin tone",
66 "unicode": "1f574-1f3fc-200d-2640-fe0f",
67 "aliases": [
68 "woman_in_business_suit_levitating_tone2",
69 "woman_in_business_suit_levitating_medium_light_skin_tone"
70 ]
71 },
72 ":woman_leviate_tone3:": {
73 "category": "people",
74 "name": "woman in business suit levitating: medium skin tone",
75 "unicode": "1f574-1f3fd-200d-2640-fe0f",
76 "aliases": [
77 "woman_in_business_suit_levitating_tone3",
78 "woman_in_business_suit_levitating_medium_skin_tone"
79 ]
80 },
81 ":woman_leviate_tone4:": {
82 "category": "people",
83 "name": "woman in business suit levitating: medium-dark skin tone",
84 "unicode": "1f574-1f3fe-200d-2640-fe0f",
85 "aliases": [
86 "woman_in_business_suit_levitating_tone4",
87 "woman_in_business_suit_levitating_medium_dark_skin_tone"
88 ]
89 },
90 ":woman_leviate_tone5:": {
91 "category": "people",
92 "name": "woman in business suit levitating: dark skin tone",
93 "unicode": "1f574-1f3ff-200d-2640-fe0f",
94 "aliases": [
95 "woman_in_business_suit_levitating_tone5",
96 "woman_in_business_suit_levitating_dark_skin_tone"
97 ]
98 },
99
100 # Man in Santa hat and variants
101 # Twitter has Santa Claus and man in Santa hat backwards
102 ":man_in_santa_hat:": {
103 "category": "people",
104 "name": "man in santa hat",
105 "unicode": "1f468-200d-1f384"
106 },
107
108 ":man_in_santa_hat_tone1:": {
109 "category": "people",
110 "name": "man in santa hat: light skin tone",
111 "unicode": "1f468-1f3fb-200d-1f384"
112 },
113
114 ":man_in_santa_hat_tone2:": {
115 "category": "people",
116 "name": "man in santa hat: medium-light skin tone",
117 "unicode": "1f468-1f3fc-200d-1f384"
118 },
119
120 ":man_in_santa_hat_tone3:": {
121 "category": "people",
122 "name": "man in santa hat: medium skin tone",
123 "unicode": "1f468-1f3fd-200d-1f384"
124 },
125
126 ":man_in_santa_hat_tone4:": {
127 "category": "people",
128 "name": "man in santa hat: medium-dark skin tone",
129 "unicode": "1f468-1f3fe-200d-1f384"
130 },
131
132 ":man_in_santa_hat_tone5:": {
133 "category": "people",
134 "name": "man in santa hat: dark skin tone",
135 "unicode": "1f469-1f3ff-200d-1f384"
136 },
137
138 # Woman in Santa hat and variants
139 # Twitter has Mrs. Claus and woman in Santa hat backwards
140 ":woman_in_santa_hat:": {
141 "category": "people",
142 "name": "woman in santa hat",
143 "unicode": "1f469-200d-1f384"
144 },
145
146 ":woman_in_santa_hat_tone1:": {
147 "category": "people",
148 "name": "woman in santa hat: light skin tone",
149 "unicode": "1f469-1f3fb-200d-1f384"
150 },
151
152 ":woman_in_santa_hat_tone2:": {
153 "category": "people",
154 "name": "woman in santa hat: medium-light skin tone",
155 "unicode": "1f468-1f3ff-200d-1f384"
156 },
157
158 ":woman_in_santa_hat_tone3:": {
159 "category": "people",
160 "name": "woman in santa hat: medium skin tone",
161 "unicode": "1f469-1f3fe-200d-1f384"
162 },
163
164 ":woman_in_santa_hat_tone4:": {
165 "category": "people",
166 "name": "woman in santa hat: medium-dark skin tone",
167 "unicode": "1f469-1f3fd-200d-1f384"
168 },
169
170 ":woman_in_santa_hat_tone5:": {
171 "category": "people",
172 "name": "woman in santa hat: dark skin tone",
173 "unicode": "1f469-1f3fc-200d-1f384"
174 }
175 }
176
177 IGNORE_EMOJI = [
178 # Alternative man levitating
179 # Per spec, "levitate" is already a man,
180 # no need to explicitly have a man variant.
181 "1f574-fe0f-200d-2642-fe0f",
182 "1f574-1f3fb-200d-2642-fe0f",
183 "1f574-1f3fc-200d-2642-fe0f",
184 "1f574-1f3fd-200d-2642-fe0f",
185 "1f574-1f3fe-200d-2642-fe0f",
186 "1f574-1f3ff-200d-2642-fe0f",
187 ]
188
189
190 def parse(repo, tag, jtag, emojis, emoji_aliases):
191 """Save test files."""
192
193 asset_path = os.path.join(current_dir, 'tags', repo, repo, 'assets', 'svg')
194
195 emoji_db = {}
196 shortnames = set()
197 aliases = {}
198 unsupported = []
199
200 for asset in os.listdir(asset_path):
201 if os.path.isfile(os.path.join(asset_path, asset)) and asset.endswith('.svg'):
202 unicode_value = asset[:-4]
203 unicode_alt = unicode_value
204 codes = unicode_alt.split('-')
205 diff = 4 - len(codes[0])
206 if diff > 0:
207 codes[0] = ('0' * diff) + codes[0]
208 unicode_alt = '-'.join(codes)
209 found = False
210 for k, v in emojis.items():
211 db_unicode = v.get('unicode_alt', v.get('unicode'))
212 if (
213 db_unicode and
214 (unicode_alt == db_unicode or unicode_alt.replace('-fe0f', '') == db_unicode.replace('-fe0f', ''))
215 ):
216 shortnames.add(k)
217 emoji_db[k] = copy.copy(v)
218 emoji_db[k]['unicode'] = unicode_value
219 if 'unicode_alt' in emoji_db[k] and unicode_alt == unicode_value:
220 del emoji_db[k]['unicode_alt']
221 elif unicode_alt != unicode_value:
222 emoji_db[k]['unicode_alt'] = unicode_alt
223 found = True
224 break
225 if not found:
226 unsupported.append(unicode_value)
227
228 for k, v in emoji_aliases.items():
229 if v in emoji_db:
230 aliases[k] = v # noqa: PERF403
231
232 # Copy emoji only found in EmojiOne
233 for k, v in SPECIAL_EMOJI.items():
234 if k not in emoji_db and k not in aliases:
235 shortnames.add(k)
236 copy_emoji = copy.copy(v)
237 if 'aliases' in copy_emoji:
238 del copy_emoji['aliases']
239 emoji_db[k] = copy_emoji
240 code_point = v.get('unicode_alt', v['unicode'])
241 if code_point in unsupported:
242 index = unsupported.index(code_point)
243 del unsupported[index]
244 special_aliases = v.get('aliases', [])
245 for spec_alias in special_aliases:
246 if spec_alias not in aliases:
247 aliases[spec_alias] = k
248
249 # Additional aliases
250 for k, v in ADDITIONAL_ALIASES.items():
251 if k in emoji_db:
252 for a in v:
253 if a not in aliases:
254 aliases[a] = k
255
256 # Remove ignored emoji from unsupported list
257 for ignore in IGNORE_EMOJI:
258 if ignore in unsupported:
259 index = unsupported.index(ignore)
260 del unsupported[index]
261
262 # Save test files
263 for test in ('png', 'svg', 'entities'):
264 with open('../tests/extensions/emoji/twemoji (%s).txt' % test, 'w') as f:
265 f.write('# Emojis\n')
266 count = 0
267 for emoji in sorted(shortnames):
268 f.write(''.join('{} {}<br>\n'.format(emoji[1:-1], emoji)))
269 count += 1
270 if test != 'png' and count == 10:
271 break
272
273 # Write out essential info
274 with open('../pymdownx/twemoji_db.py', 'w') as f:
275 # Dump emoji db to file and strip out PY2 unicode specifiers
276 f.write('"""Twemoji autogen.\n\nNames from emojione database. Do not edit by hand.\n"""\n')
277 f.write('version = "%s"\n' % tag)
278 f.write('index_version = "%s"\n' % jtag)
279 f.write('name = "twemoji"\n')
280 f.write('emoji = %s\n' % json.dumps(emoji_db, sort_keys=True, indent=4, separators=(',', ': ')))
281 f.write('aliases = %s\n' % json.dumps(aliases, sort_keys=True, indent=4, separators=(',', ': ')))
282
283 # Print unsupported emoji
284 if unsupported:
285 print('Unsupported:')
286 for code in unsupported:
287 print(code)