File: gen_twemoji.py

package info (click to toggle)
pymdown-extensions 10.13-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,104 kB
  • sloc: python: 60,117; javascript: 846; sh: 8; makefile: 5
file content (287 lines) | stat: -rw-r--r-- 9,570 bytes parent folder | download | duplicates (2)
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
"""Generate emojione data."""
import os
import json
import sys
import copy
sys.path.append('../pymdownx/')
current_dir = os.path.dirname(os.path.abspath(__file__))

ADDITIONAL_ALIASES = {}

# Special emoji
SPECIAL_EMOJI = {
    # # An unofficial emoji supported by very few
    ":shibuya:": {
        "category": "travel",
        "name": "Shibuya 109",
        "unicode": "e50a"
    },

    # Skier skin tones (not supported by Unicode spec)
    ":skier_tone1:": {
        "category": "activity",
        "name": "skier: light skin tone",
        "unicode": "26f7-1f3fb"
    },
    ":skier_tone2:": {
        "category": "activity",
        "name": "skier: medium-light skin tone",
        "unicode": "26f7-1f3fc"
    },
    ":skier_tone3:": {
        "category": "activity",
        "name": "skier: medium skin tone",
        "unicode": "26f7-1f3fd"
    },
    ":skier_tone4:": {
        "category": "activity",
        "name": "skier: medium-dark skin tone",
        "unicode": "26f7-1f3fe"
    },
    ":skier_tone5:": {
        "category": "activity",
        "name": "skier: dark skin tone",
        "unicode": "26f7-1f3ff"
    },

    # Woman levitate (not supported in Unicode spec)
    ":woman_levitate:": {
        "category": "people",
        "name": "woman in business suit levitating",
        "unicode": "1f574-fe0f-200d-2640-fe0f",
        "aliases": ["woman_in_business_suit_levitating"]
    },
    ":woman_levitate_tone1:": {
        "category": "people",
        "name": "woman in business suit levitating: light skin tone",
        "unicode": "1f574-1f3fb-200d-2640-fe0f",
        "aliases": [
            "woman_in_business_suit_levitating_tone1",
            "woman_in_business_suit_levitating_light_skin_tone"
        ]
    },
    ":woman_leviate_tone2:": {
        "category": "people",
        "name": "woman in business suit levitating: medium-light skin tone",
        "unicode": "1f574-1f3fc-200d-2640-fe0f",
        "aliases": [
            "woman_in_business_suit_levitating_tone2",
            "woman_in_business_suit_levitating_medium_light_skin_tone"
        ]
    },
    ":woman_leviate_tone3:": {
        "category": "people",
        "name": "woman in business suit levitating: medium skin tone",
        "unicode": "1f574-1f3fd-200d-2640-fe0f",
        "aliases": [
            "woman_in_business_suit_levitating_tone3",
            "woman_in_business_suit_levitating_medium_skin_tone"
        ]
    },
    ":woman_leviate_tone4:": {
        "category": "people",
        "name": "woman in business suit levitating: medium-dark skin tone",
        "unicode": "1f574-1f3fe-200d-2640-fe0f",
        "aliases": [
            "woman_in_business_suit_levitating_tone4",
            "woman_in_business_suit_levitating_medium_dark_skin_tone"
        ]
    },
    ":woman_leviate_tone5:": {
        "category": "people",
        "name": "woman in business suit levitating: dark skin tone",
        "unicode": "1f574-1f3ff-200d-2640-fe0f",
        "aliases": [
            "woman_in_business_suit_levitating_tone5",
            "woman_in_business_suit_levitating_dark_skin_tone"
        ]
    },

    # Man in Santa hat and variants
    # Twitter has Santa Claus and man in Santa hat backwards
    ":man_in_santa_hat:": {
        "category": "people",
        "name": "man in santa hat",
        "unicode": "1f468-200d-1f384"
    },

    ":man_in_santa_hat_tone1:": {
        "category": "people",
        "name": "man in santa hat: light skin tone",
        "unicode": "1f468-1f3fb-200d-1f384"
    },

    ":man_in_santa_hat_tone2:": {
        "category": "people",
        "name": "man in santa hat: medium-light skin tone",
        "unicode": "1f468-1f3fc-200d-1f384"
    },

    ":man_in_santa_hat_tone3:": {
        "category": "people",
        "name": "man in santa hat: medium skin tone",
        "unicode": "1f468-1f3fd-200d-1f384"
    },

    ":man_in_santa_hat_tone4:": {
        "category": "people",
        "name": "man in santa hat: medium-dark skin tone",
        "unicode": "1f468-1f3fe-200d-1f384"
    },

    ":man_in_santa_hat_tone5:": {
        "category": "people",
        "name": "man in santa hat: dark skin tone",
        "unicode": "1f469-1f3ff-200d-1f384"
    },

    # Woman in Santa hat and variants
    # Twitter has Mrs. Claus and woman in Santa hat backwards
    ":woman_in_santa_hat:": {
        "category": "people",
        "name": "woman in santa hat",
        "unicode": "1f469-200d-1f384"
    },

    ":woman_in_santa_hat_tone1:": {
        "category": "people",
        "name": "woman in santa hat: light skin tone",
        "unicode": "1f469-1f3fb-200d-1f384"
    },

    ":woman_in_santa_hat_tone2:": {
        "category": "people",
        "name": "woman in santa hat: medium-light skin tone",
        "unicode": "1f468-1f3ff-200d-1f384"
    },

    ":woman_in_santa_hat_tone3:": {
        "category": "people",
        "name": "woman in santa hat: medium skin tone",
        "unicode": "1f469-1f3fe-200d-1f384"
    },

    ":woman_in_santa_hat_tone4:": {
        "category": "people",
        "name": "woman in santa hat: medium-dark skin tone",
        "unicode": "1f469-1f3fd-200d-1f384"
    },

    ":woman_in_santa_hat_tone5:": {
        "category": "people",
        "name": "woman in santa hat: dark skin tone",
        "unicode": "1f469-1f3fc-200d-1f384"
    }
}

IGNORE_EMOJI = [
    # Alternative man levitating
    # Per spec, "levitate" is already a man,
    # no need to explicitly have a man variant.
    "1f574-fe0f-200d-2642-fe0f",
    "1f574-1f3fb-200d-2642-fe0f",
    "1f574-1f3fc-200d-2642-fe0f",
    "1f574-1f3fd-200d-2642-fe0f",
    "1f574-1f3fe-200d-2642-fe0f",
    "1f574-1f3ff-200d-2642-fe0f",
]


def parse(repo, tag, jtag, emojis, emoji_aliases):
    """Save test files."""

    asset_path = os.path.join(current_dir, 'tags', repo, repo, 'assets', 'svg')

    emoji_db = {}
    shortnames = set()
    aliases = {}
    unsupported = []

    for asset in os.listdir(asset_path):
        if os.path.isfile(os.path.join(asset_path, asset)) and asset.endswith('.svg'):
            unicode_value = asset[:-4]
            unicode_alt = unicode_value
            codes = unicode_alt.split('-')
            diff = 4 - len(codes[0])
            if diff > 0:
                codes[0] = ('0' * diff) + codes[0]
                unicode_alt = '-'.join(codes)
            found = False
            for k, v in emojis.items():
                db_unicode = v.get('unicode_alt', v.get('unicode'))
                if (
                    db_unicode and
                    (unicode_alt == db_unicode or unicode_alt.replace('-fe0f', '') == db_unicode.replace('-fe0f', ''))
                ):
                    shortnames.add(k)
                    emoji_db[k] = copy.copy(v)
                    emoji_db[k]['unicode'] = unicode_value
                    if 'unicode_alt' in emoji_db[k] and unicode_alt == unicode_value:
                        del emoji_db[k]['unicode_alt']
                    elif unicode_alt != unicode_value:
                        emoji_db[k]['unicode_alt'] = unicode_alt
                    found = True
                    break
            if not found:
                unsupported.append(unicode_value)

    for k, v in emoji_aliases.items():
        if v in emoji_db:
            aliases[k] = v  # noqa: PERF403

    # Copy emoji only found in EmojiOne
    for k, v in SPECIAL_EMOJI.items():
        if k not in emoji_db and k not in aliases:
            shortnames.add(k)
            copy_emoji = copy.copy(v)
            if 'aliases' in copy_emoji:
                del copy_emoji['aliases']
            emoji_db[k] = copy_emoji
            code_point = v.get('unicode_alt', v['unicode'])
            if code_point in unsupported:
                index = unsupported.index(code_point)
                del unsupported[index]
            special_aliases = v.get('aliases', [])
            for spec_alias in special_aliases:
                if spec_alias not in aliases:
                    aliases[spec_alias] = k

    # Additional aliases
    for k, v in ADDITIONAL_ALIASES.items():
        if k in emoji_db:
            for a in v:
                if a not in aliases:
                    aliases[a] = k

    # Remove ignored emoji from unsupported list
    for ignore in IGNORE_EMOJI:
        if ignore in unsupported:
            index = unsupported.index(ignore)
            del unsupported[index]

    # Save test files
    for test in ('png', 'svg', 'entities'):
        with open('../tests/extensions/emoji/twemoji (%s).txt' % test, 'w') as f:
            f.write('# Emojis\n')
            count = 0
            for emoji in sorted(shortnames):
                f.write(''.join('{} {}<br>\n'.format(emoji[1:-1], emoji)))
                count += 1
                if test != 'png' and count == 10:
                    break

    # Write out essential info
    with open('../pymdownx/twemoji_db.py', 'w') as f:
        # Dump emoji db to file and strip out PY2 unicode specifiers
        f.write('"""Twemoji autogen.\n\nNames from emojione database. Do not edit by hand.\n"""\n')
        f.write('version = "%s"\n' % tag)
        f.write('index_version = "%s"\n' % jtag)
        f.write('name = "twemoji"\n')
        f.write('emoji = %s\n' % json.dumps(emoji_db, sort_keys=True, indent=4, separators=(',', ': ')))
        f.write('aliases = %s\n' % json.dumps(aliases, sort_keys=True, indent=4, separators=(',', ': ')))

    # Print unsupported emoji
    if unsupported:
        print('Unsupported:')
        for code in unsupported:
            print(code)