matrixion/scripts/emoji_codegen.py

84 lines
2.2 KiB
Python
Raw Permalink Normal View History

2017-04-23 21:31:08 +03:00
#!/usr/bin/env python3
import sys
2020-01-24 06:18:14 +03:00
import re
2017-04-23 21:31:08 +03:00
from jinja2 import Template
class Emoji(object):
2020-01-24 06:18:14 +03:00
def __init__(self, code, shortname):
self.code = repr(code.encode('utf-8'))[1:].strip("'")
2017-04-23 21:31:08 +03: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{QString::fromUtf8("{{ e.code }}"), "{{ e.shortname }}", emoji::Emoji::Category::{{ c[0].capitalize() }}},
{%- endfor %}
{%- endfor %}
};
''')
d = dict(kwargs=kwargs)
print(tmpl.render(d))
2017-04-23 21:31:08 +03:00
if __name__ == '__main__':
if len(sys.argv) < 2:
2020-01-24 06:18:14 +03:00
print('usage: emoji_codegen.py /path/to/emoji-test.txt')
2017-04-23 21:31:08 +03:00
sys.exit(1)
filename = sys.argv[1]
2020-01-24 06:18:14 +03: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
}
current_category = ''
for line in open(filename, 'r'):
if line.startswith('# group:'):
current_category = line.split(':', 1)[1].strip()
if not line or line.startswith('#'):
continue
2017-04-23 21:31:08 +03:00
2020-01-24 06:18:14 +03:00
segments = re.split(r'\s+[#;] ', line.strip())
if len(segments) != 3:
continue
2017-04-23 21:31:08 +03:00
2020-01-24 06:18:14 +03:00
code, qualification, charAndName = segments
2017-04-23 21:31:08 +03:00
2020-01-24 06:18:14 +03:00
# skip fully qualified versions of same unicode
if code.endswith('FE0F'):
continue
2017-04-23 21:31:08 +03:00
2020-01-24 06:18:14 +03:00
if qualification == 'component':
2017-04-23 21:31:08 +03:00
continue
2020-01-24 06:18:14 +03:00
char, name = re.match(r'^(\S+) E\d+\.\d+ (.*)$', charAndName).groups()
categories[current_category].append(Emoji(char, name))
2017-04-23 21:31:08 +03:00
# Use xclip to pipe the output to clipboard.
# e.g ./codegen.py emoji.json | xclip -sel clip
2021-01-24 01:25:52 +03:00
generate_qml_list(people=people, nature=nature, food=food, activity=activity, travel=travel, objects=objects, symbols=symbols, flags=flags)