nheko/scripts/emoji_codegen.py

109 lines
3.4 KiB
Python
Raw Normal View History

2017-04-23 20:31:08 +02:00
#!/usr/bin/env python3
import sys
2020-01-24 04:18:14 +01:00
import re
from unidecode import unidecode
2017-04-23 20:31:08 +02:00
from jinja2 import Template
class Emoji(object):
2020-01-24 04:18:14 +01:00
def __init__(self, code, shortname):
self.code = ''.join(['\\U'+c.rjust(8, '0') for c in code.strip().split(' ')])
2017-04-23 20:31:08 +02:00
self.shortname = shortname
def generate_qml_list(**kwargs):
tmpl = Template('''
const QVector<Emoji> emoji::Provider::emoji = {
{%- for c in kwargs.items() %}
// {{ c[0].capitalize() }}
{%- for e in c[1] %}
Emoji{QStringLiteral(u"{{ e.code }}"), QStringLiteral(u"{{ e.shortname }}"), emoji::Emoji::Category::{{ c[0].capitalize() }}},
{%- endfor %}
{%- endfor %}
};
''')
d = dict(kwargs=kwargs)
print(tmpl.render(d))
2017-04-23 20:31:08 +02:00
if __name__ == '__main__':
if len(sys.argv) < 3:
print('usage: emoji_codegen.py /path/to/emoji-test.txt /path/to/shortcodes.txt')
2017-04-23 20:31:08 +02:00
sys.exit(1)
filename = sys.argv[1]
shortcodefilename = sys.argv[2]
2017-04-23 20:31:08 +02:00
2020-01-24 04:18:14 +01:00
people = []
nature = []
food = []
activity = []
travel = []
objects = []
symbols = []
flags = []
categories = {
'Smileys & Emotion': people,
'People & Body': people,
'Animals & Nature': nature,
'Food & Drink': food,
'Travel & Places': travel,
'Activities': activity,
'Objects': objects,
'Symbols': symbols,
'Flags': flags
}
shortcodeDict = {}
# for my sanity - this strips newlines
for line in open(shortcodefilename, 'r', encoding="utf8"):
longname, shortname = line.strip().split(':')
shortcodeDict[longname] = shortname
2020-01-24 04:18:14 +01:00
current_category = ''
2021-09-25 08:19:44 +02:00
for line in open(filename, 'r', encoding="utf8"):
2020-01-24 04:18:14 +01:00
if line.startswith('# group:'):
current_category = line.split(':', 1)[1].strip()
if not line or line.startswith('#'):
continue
2017-04-23 20:31:08 +02:00
2020-01-24 04:18:14 +01:00
segments = re.split(r'\s+[#;] ', line.strip())
if len(segments) != 3:
continue
2017-04-23 20:31:08 +02:00
2020-01-24 04:18:14 +01:00
code, qualification, charAndName = segments
2017-04-23 20:31:08 +02:00
# skip unqualified versions of same unicode
if qualification != 'fully-qualified':
2020-01-24 04:18:14 +01:00
continue
2017-04-23 20:31:08 +02:00
2020-01-24 04:18:14 +01:00
char, name = re.match(r'^(\S+) E\d+\.\d+ (.*)$', charAndName).groups()
#TODO: Handle skintone modifiers in a sane way
if name in shortcodeDict:
# TODO: this duplicates emoji
categories[current_category].append(Emoji(code, shortcodeDict[name]))
if name.endswith(' face'):
name = name[:-5]
elif name.endswith(' button'):
2022-04-22 17:19:30 +02:00
name = name[:-7]
else:
matchobj = re.match(r'^flag: (.*)$', name)
if matchobj:
country, = matchobj.groups()
name = country + " flag"
name = name.replace(" ", "_")
name = name.replace("", "")
name = name.replace("", "")
name = name.replace(":", "")
2022-04-22 17:19:30 +02:00
name = name.replace("-", "_")
name = re.sub(r'_{2,}', '_', name)
name = name.lower()
name = unidecode(name)
categories[current_category].append(Emoji(code, name))
2017-04-23 20:31:08 +02:00
# Use xclip to pipe the output to clipboard.
# e.g ./codegen.py emoji.json | xclip -sel clip
# alternatively - delete the var from src/emoji/Provider.cpp, and do ./codegen.py emojis shortcodes >> src/emoji/Provider.cpp
2021-01-23 23:25:52 +01:00
generate_qml_list(people=people, nature=nature, food=food, activity=activity, travel=travel, objects=objects, symbols=symbols, flags=flags)