| 1 |
"""Generate gemoji data.""" |
| 2 |
import sys |
| 3 |
import os |
| 4 |
import json |
| 5 |
current_dir = os.path.dirname(os.path.abspath(__file__)) |
| 6 |
|
| 7 |
U_JOIN = 0x200d |
| 8 |
U_VARIATION_SELECTOR_16 = 0xfe0f |
| 9 |
U_EXTRA = (U_JOIN, U_VARIATION_SELECTOR_16) |
| 10 |
|
| 11 |
if sys.maxunicode == 0xFFFF: |
| 12 |
|
| 13 |
def get_code_points(s): |
| 14 |
"""Get the Unicode code points.""" |
| 15 |
|
| 16 |
pt = [] |
| 17 |
|
| 18 |
def is_full_point(p, point): |
| 19 |
""" |
| 20 |
Check if we have a full code point. |
| 21 |
|
| 22 |
Surrogates are stored in point. |
| 23 |
""" |
| 24 |
v = ord(p) |
| 25 |
if 0xD800 <= v <= 0xDBFF: |
| 26 |
del point[:] |
| 27 |
point.append(p) |
| 28 |
return False |
| 29 |
if point and 0xDC00 <= v <= 0xDFFF: |
| 30 |
point.append(p) |
| 31 |
return True |
| 32 |
del point[:] |
| 33 |
return True |
| 34 |
|
| 35 |
return [(''.join(pt) if pt else c) for c in s if is_full_point(c, pt)] |
| 36 |
|
| 37 |
def get_ord(c): |
| 38 |
"""Get Unicode ordinal number.""" |
| 39 |
|
| 40 |
if len(c) == 2: |
| 41 |
high, low = (ord(p) for p in c) |
| 42 |
ordinal = (high - 0xD800) * 0x400 + low - 0xDC00 + 0x10000 |
| 43 |
else: |
| 44 |
ordinal = ord(c) |
| 45 |
|
| 46 |
return ordinal |
| 47 |
|
| 48 |
else: |
| 49 |
def get_code_points(s): |
| 50 |
"""Get the Unicode code points.""" |
| 51 |
|
| 52 |
return list(s) |
| 53 |
|
| 54 |
def get_ord(c): |
| 55 |
"""Get Unicode ordinal number.""" |
| 56 |
|
| 57 |
return ord(c) |
| 58 |
|
| 59 |
|
| 60 |
def get_unicode(value): |
| 61 |
"""Get Unicode.""" |
| 62 |
|
| 63 |
uc = '-'.join( |
| 64 |
['%04x' % get_ord(point) for point in get_code_points(value['emoji']) if get_ord(point) not in U_EXTRA] |
| 65 |
) |
| 66 |
|
| 67 |
uc_alt = '-'.join( |
| 68 |
['%04x' % get_ord(point) for point in get_code_points(value['emoji'])] |
| 69 |
) |
| 70 |
|
| 71 |
if uc == uc_alt: |
| 72 |
uc_alt = None |
| 73 |
|
| 74 |
return uc, uc_alt |
| 75 |
|
| 76 |
|
| 77 |
def get_gemoji_specific(value): |
| 78 |
"""Get alternate Unicode form or return the original.""" |
| 79 |
|
| 80 |
return value['aliases'][0] |
| 81 |
|
| 82 |
|
| 83 |
def parse(repo, tag): |
| 84 |
"""Save test files.""" |
| 85 |
# Load emoji database |
| 86 |
with open(os.path.join(current_dir, 'tags', repo, repo, 'db', 'emoji.json'), 'r', encoding='utf-8') as f: |
| 87 |
emojis = json.loads(f.read()) |
| 88 |
|
| 89 |
emoji_db = {} |
| 90 |
shortnames = set() |
| 91 |
aliases = {} |
| 92 |
for v in emojis: |
| 93 |
short = v['aliases'][0] |
| 94 |
shortnames.add(':%s:' % short) |
| 95 |
if 'emoji' in v: |
| 96 |
uc, uc_alt = get_unicode(v) |
| 97 |
emoji_db[':%s:' % short] = { |
| 98 |
'name': v.get('description', short), |
| 99 |
'unicode': uc, |
| 100 |
'category': v['category'] |
| 101 |
} |
| 102 |
if uc_alt: |
| 103 |
emoji_db[':%s:' % short]['unicode_alt'] = uc_alt |
| 104 |
else: |
| 105 |
emoji_db[':%s:' % short] = { |
| 106 |
'name': v.get('description', short) |
| 107 |
} |
| 108 |
|
| 109 |
for alias in v['aliases'][1:]: |
| 110 |
aliases[':%s:' % alias] = ':%s:' % short |
| 111 |
|
| 112 |
# Save test files |
| 113 |
for test in ('png', 'entities'): |
| 114 |
with open('../tests/extensions/emoji/gemoji (%s).txt' % test, 'w') as f: |
| 115 |
f.write('# Emojis\n') |
| 116 |
count = 0 |
| 117 |
for emoji in sorted(shortnames): |
| 118 |
f.write(''.join('{} {}<br>\n'.format(emoji[1:-1], emoji))) |
| 119 |
count += 1 |
| 120 |
if test != 'png' and count == 10: |
| 121 |
break |
| 122 |
|
| 123 |
with open(os.path.join(current_dir, 'tags', repo, repo, 'LICENSE')) as f: |
| 124 |
license_content = f.read() |
| 125 |
|
| 126 |
# Write out essential info |
| 127 |
with open('../pymdownx/gemoji_db.py', 'w') as f: |
| 128 |
# Dump emoji db to file and strip out PY2 unicode specifiers |
| 129 |
f.write('"""Gemoji autogen.\n\nGenerated from gemoji source. Do not edit by hand.\n\n%s"""\n' % license_content) |
| 130 |
f.write('version = "%s"\n' % tag) |
| 131 |
f.write('name = "gemoji"\n') |
| 132 |
f.write('emoji = %s\n' % json.dumps(emoji_db, sort_keys=True, indent=4, separators=(',', ': '))) |
| 133 |
f.write('aliases = %s\n' % json.dumps(aliases, sort_keys=True, indent=4, separators=(',', ': '))) |