| 1 |
"""Generate emojione data.""" |
| 2 |
import os |
| 3 |
import json |
| 4 |
current_dir = os.path.dirname(os.path.abspath(__file__)) |
| 5 |
LICENSE = """ |
| 6 |
MIT license. |
| 7 |
|
| 8 |
Copyright (c) http://www.emojione.com |
| 9 |
""" |
| 10 |
|
| 11 |
|
| 12 |
def get_unicode_alt(value): |
| 13 |
"""Get alternate Unicode form or return the original.""" |
| 14 |
|
| 15 |
return value['unicode_alt'] |
| 16 |
|
| 17 |
|
| 18 |
def parse(repo, tag): |
| 19 |
"""Save test files.""" |
| 20 |
# Load emoji database |
| 21 |
with open(os.path.join(current_dir, 'tags', repo, repo, 'emoji.json')) as f: |
| 22 |
emojis = json.loads(f.read()) |
| 23 |
|
| 24 |
emoji_db = {} |
| 25 |
shortnames = set() |
| 26 |
aliases = {} |
| 27 |
for v in emojis.values(): |
| 28 |
shortnames.add(v['shortname']) |
| 29 |
emoji_db[v['shortname']] = { |
| 30 |
'name': v['name'], |
| 31 |
'unicode': v['unicode'], |
| 32 |
'category': v['category'] |
| 33 |
} |
| 34 |
alt = get_unicode_alt(v) |
| 35 |
if alt: |
| 36 |
emoji_db[v['shortname']]['unicode_alt'] = alt |
| 37 |
|
| 38 |
for alias in v['aliases']: |
| 39 |
aliases[alias] = v['shortname'] |
| 40 |
|
| 41 |
# Save test files |
| 42 |
for test in ('png', 'png sprite', 'svg', 'svg sprite', 'entities', 'long title', 'no title'): |
| 43 |
with open('../tests/extensions/emoji/emoji1 (%s).txt' % test, 'w') as f: |
| 44 |
f.write('# Emojis\n') |
| 45 |
count = 0 |
| 46 |
for emoji in sorted(shortnames): |
| 47 |
f.write(''.join('{} {}<br>\n'.format(emoji[1:-1], emoji))) |
| 48 |
count += 1 |
| 49 |
if test != 'png' and count == 10: |
| 50 |
break |
| 51 |
|
| 52 |
# Write out essential info |
| 53 |
with open('../pymdownx/emoji1_db.py', 'w') as f: |
| 54 |
# Dump emoji db to file and strip out PY2 unicode specifiers |
| 55 |
f.write('"""Emojione autogen.\n\nGenerated from emojione source. Do not edit by hand.\n%s"""\n' % LICENSE) |
| 56 |
f.write('version = "%s"\n' % tag) |
| 57 |
f.write('name = "emojione"\n') |
| 58 |
f.write('emoji = %s\n' % json.dumps(emoji_db, sort_keys=True, indent=4, separators=(',', ': '))) |
| 59 |
f.write('aliases = %s\n' % json.dumps(aliases, sort_keys=True, indent=4, separators=(',', ': '))) |