| 1 |
|
| 2 |
|
| 3 |
import sys |
| 4 |
import re |
| 5 |
from unidecode import unidecode |
| 6 |
from jinja2 import Template |
| 7 |
|
| 8 |
|
| 9 |
class Emoji(object): |
| 10 |
def __init__(self, code, shortname, unicodename): |
| 11 |
self.code = ''.join(['\\U'+c.rjust(8, '0') for c in code.strip().split(' ')]) |
| 12 |
self.shortname = shortname |
| 13 |
self.unicodename = unicodename |
| 14 |
|
| 15 |
def generate_provider_class(**kwargs): |
| 16 |
entrycount = sum([len(c[1]) for c in kwargs.items()]) |
| 17 |
tmpl = Template('''\ |
| 18 |
// SPDX-FileCopyrightText: Nheko Contributors |
| 19 |
// |
| 20 |
// SPDX-License-Identifier: GPL-3.0-or-later |
| 21 |
|
| 22 |
// DO NOT EDIT Provider.h DIRECTLY! EDIT IT IN scripts/emoji_codegen.py AND RUN scripts/codegen.sh! |
| 23 |
|
| 24 |
#pragma once |
| 25 |
#include <array> |
| 26 |
#include "Emoji.h" |
| 27 |
|
| 28 |
namespace emoji { |
| 29 |
class Provider |
| 30 |
{ |
| 31 |
public: |
| 32 |
// all emoji for QML purposes |
| 33 |
static const std::array<Emoji, {{ entrycount }}> emoji; |
| 34 |
}; |
| 35 |
} // namespace emoji |
| 36 |
''') |
| 37 |
d = dict(entrycount=entrycount) |
| 38 |
print(tmpl.render(d)) |
| 39 |
def generate_qml_list(**kwargs): |
| 40 |
entrycount = sum([len(c[1]) for c in kwargs.items()]) |
| 41 |
tmpl = Template(''' |
| 42 |
std::array<Emoji, {{ entrycount }} > emoji::Provider::emoji = { |
| 43 |
{%- for c in kwargs.items() %} |
| 44 |
// {{ c[0].capitalize() }} |
| 45 |
{%- for e in c[1] %} |
| 46 |
Emoji{std::u16string_view(u"{{ e.code }}"), std::u16string_view(u"{{ e.shortname }}"), std::u16string_view(u"{{ e.unicodename }}"), emoji::Emoji::Category::{{ c[0].capitalize() }}}, |
| 47 |
{%- endfor %} |
| 48 |
{%- endfor %} |
| 49 |
}; |
| 50 |
''') |
| 51 |
d = dict(kwargs=kwargs, entrycount=entrycount) |
| 52 |
print(tmpl.render(d)) |
| 53 |
def usage(): |
| 54 |
print('usage: emoji_codegen.py {impl|header} /path/to/emoji-test /path/to/shortcodes.txt') |
| 55 |
if __name__ == '__main__': |
| 56 |
if len(sys.argv) < 4: |
| 57 |
usage() |
| 58 |
sys.exit(1) |
| 59 |
|
| 60 |
mode = sys.argv[1] |
| 61 |
if mode != 'impl' and mode != 'header': |
| 62 |
usage() |
| 63 |
sys.exit(1) |
| 64 |
filename = sys.argv[2] |
| 65 |
shortcodefilename = sys.argv[3] |
| 66 |
|
| 67 |
people = [] |
| 68 |
nature = [] |
| 69 |
food = [] |
| 70 |
activity = [] |
| 71 |
travel = [] |
| 72 |
objects = [] |
| 73 |
symbols = [] |
| 74 |
flags = [] |
| 75 |
|
| 76 |
categories = { |
| 77 |
'Smileys & Emotion': people, |
| 78 |
'People & Body': people, |
| 79 |
'Animals & Nature': nature, |
| 80 |
'Food & Drink': food, |
| 81 |
'Travel & Places': travel, |
| 82 |
'Activities': activity, |
| 83 |
'Objects': objects, |
| 84 |
'Symbols': symbols, |
| 85 |
'Flags': flags, |
| 86 |
'Component': symbols |
| 87 |
} |
| 88 |
shortcodeDict = {} |
| 89 |
|
| 90 |
for line in open(shortcodefilename, 'r', encoding="utf8"): |
| 91 |
longname, shortname = line.strip().split(':') |
| 92 |
shortcodeDict[longname] = shortname |
| 93 |
current_category = '' |
| 94 |
for line in open(filename, 'r', encoding="utf8"): |
| 95 |
if line.startswith('# group:'): |
| 96 |
current_category = line.split(':', 1)[1].strip() |
| 97 |
|
| 98 |
if not line or line.startswith('#'): |
| 99 |
continue |
| 100 |
|
| 101 |
segments = re.split(r'\s+[#;] ', line.strip()) |
| 102 |
if len(segments) != 3: |
| 103 |
continue |
| 104 |
|
| 105 |
code, qualification, charAndName = segments |
| 106 |
|
| 107 |
|
| 108 |
if qualification != 'fully-qualified': |
| 109 |
continue |
| 110 |
|
| 111 |
char, name = re.match(r'^(\S+) E\d+\.\d+ (.*)$', charAndName).groups() |
| 112 |
shortname = name |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
basicallyTheSame = False |
| 121 |
if code in shortcodeDict: |
| 122 |
shortname = shortcodeDict[code] |
| 123 |
else: |
| 124 |
shortname = shortname.lower() |
| 125 |
if shortname.endswith(' (blood type)'): |
| 126 |
shortname = shortname[:-13] |
| 127 |
if shortname.endswith(': red hair'): |
| 128 |
shortname = "red_haired_" + shortname[:-10] |
| 129 |
if shortname.endswith(': curly hair'): |
| 130 |
shortname = "curly_haired_" + shortname[:-12] |
| 131 |
if shortname.endswith(': white hair'): |
| 132 |
shortname = "white_haired_" + shortname[:-12] |
| 133 |
if shortname.endswith(': bald'): |
| 134 |
shortname = "bald_" + shortname[:-6] |
| 135 |
if shortname.endswith(': beard'): |
| 136 |
shortname = "bearded_" + shortname[:-7] |
| 137 |
if shortname.endswith(' face'): |
| 138 |
shortname = shortname[:-5] |
| 139 |
if shortname.endswith(' button'): |
| 140 |
shortname = shortname[:-7] |
| 141 |
if shortname.endswith(' banknote'): |
| 142 |
shortname = shortname[:-9] |
| 143 |
|
| 144 |
|
| 145 |
matchobj = re.match(r'^flag: (.*)$', shortname) |
| 146 |
if shortname.startswith("flag: "): |
| 147 |
country = shortname[5:] |
| 148 |
shortname = country + " flag" |
| 149 |
shortname = shortname.replace("u.s.", "us") |
| 150 |
shortname = shortname.replace("&", "and") |
| 151 |
|
| 152 |
if shortname == name.lower(): |
| 153 |
basicallyTheSame = True |
| 154 |
|
| 155 |
shortname = shortname.replace("-", "_") |
| 156 |
shortname = re.sub(r'\W', '_', shortname) |
| 157 |
shortname, = re.match(r'^_*(.+)_*$', shortname).groups() |
| 158 |
shortname = re.sub(r'_{2,}', '_', shortname) |
| 159 |
shortname = unidecode(shortname) |
| 160 |
|
| 161 |
|
| 162 |
categories[current_category].append(Emoji(code, shortname, name)) |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
func = None |
| 168 |
if mode == 'impl': |
| 169 |
func = generate_qml_list |
| 170 |
else: |
| 171 |
func = generate_provider_class |
| 172 |
func(people=people, nature=nature, food=food, activity=activity, travel=travel, objects=objects, symbols=symbols, flags=flags) |