summary refs log tree commit diff
path: root/scripts/emoji_codegen.py
blob: 5378b32d98241bef15bb0932611d30498b485647 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python3

import sys
import re
from unidecode import unidecode
from jinja2 import Template


class Emoji(object):
    def __init__(self, code, shortname, unicodename):
        self.code = ''.join(['\\U'+c.rjust(8, '0') for c in code.strip().split(' ')])
        self.shortname = shortname
        self.unicodename = unicodename

def generate_provider_class(**kwargs):
    entrycount = sum([len(c[1]) for c in kwargs.items()])
    tmpl = Template('''\
    // SPDX-FileCopyrightText: Nheko Contributors
    //
    // SPDX-License-Identifier: GPL-3.0-or-later

    // DO NOT EDIT Provider.h DIRECTLY! EDIT IT IN scripts/emoji_codegen.py AND RUN scripts/codegen.sh!

    #pragma once
    #include <array>
    #include "Emoji.h"

    namespace emoji {
    class Provider
    {
    public:
        // all emoji for QML purposes
        static const std::array<Emoji, {{ entrycount }}> emoji;
    };
    } // namespace emoji
    ''')
    d = dict(entrycount=entrycount)
    print(tmpl.render(d))
def generate_qml_list(**kwargs):
    entrycount = sum([len(c[1]) for c in kwargs.items()])
    tmpl = Template('''
    std::array<Emoji, {{ entrycount }} > emoji::Provider::emoji = {
    {%- for c in kwargs.items() %}
    // {{ c[0].capitalize() }}
    {%- for e in c[1] %}
    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() }}},
    {%- endfor %}
    {%- endfor %}
};
    ''')
    d = dict(kwargs=kwargs, entrycount=entrycount)
    print(tmpl.render(d))
def usage():
    print('usage: emoji_codegen.py {impl|header} /path/to/emoji-test /path/to/shortcodes.txt')
if __name__ == '__main__':
    if len(sys.argv) < 4:
        usage()
        sys.exit(1)

    mode = sys.argv[1]
    if mode != 'impl' and mode != 'header':
        usage()
        sys.exit(1)
    filename = sys.argv[2]
    shortcodefilename = sys.argv[3]

    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,
        'Component': symbols
    }
    shortcodeDict = {}
    # for my sanity - this strips newlines
    for line in open(shortcodefilename, 'r', encoding="utf8"):
        longname, shortname = line.strip().split(':')
        shortcodeDict[longname] = shortname
    current_category = ''
    for line in open(filename, 'r', encoding="utf8"):
        if line.startswith('# group:'):
            current_category = line.split(':', 1)[1].strip()

        if not line or line.startswith('#'):
            continue

        segments = re.split(r'\s+[#;] ', line.strip())
        if len(segments) != 3:
            continue

        code, qualification, charAndName = segments

        # skip unqualified versions of same unicode
        if qualification != 'fully-qualified':
            continue

        char, name = re.match(r'^(\S+) E\d+\.\d+ (.*)$', charAndName).groups()
        shortname = name
        # until skin tone is handled, keep them around
        ## discard skin tone variants for sanity
        # if "skin tone" in name and qualification != 'component': 
        #    continue
        # if qualification == 'component' and not "skin tone" in name:
        #    continue
        #TODO: Handle skintone modifiers in a sane way
        basicallyTheSame = False
        if code in shortcodeDict:
            shortname = shortcodeDict[code]
        else:
            shortname = shortname.lower()
            if shortname.endswith(' (blood type)'):
                shortname = shortname[:-13]
            if shortname.endswith(': red hair'):
                shortname = "red_haired_" + shortname[:-10]
            if shortname.endswith(': curly hair'):
                shortname = "curly_haired_" + shortname[:-12]
            if shortname.endswith(': white hair'):
                shortname = "white_haired_" + shortname[:-12]
            if shortname.endswith(': bald'):
                shortname = "bald_" + shortname[:-6]
            if shortname.endswith(': beard'):
                shortname = "bearded_" + shortname[:-7]
            if shortname.endswith(' face'):
                shortname = shortname[:-5]
            if shortname.endswith(' button'):
                shortname = shortname[:-7]
            if shortname.endswith(' banknote'):
                shortname = shortname[:-9]

            # FIXME: Is there a better way to do this?
            matchobj = re.match(r'^flag: (.*)$', shortname)
            if shortname.startswith("flag: "):
                country = shortname[5:]
                shortname = country + " flag"
            shortname = shortname.replace("u.s.", "us")
            shortname = shortname.replace("&", "and")

            if shortname == name.lower():
                basicallyTheSame = True

            shortname = shortname.replace("-", "_")
            shortname = re.sub(r'\W', '_', shortname)
            shortname, = re.match(r'^_*(.+)_*$', shortname).groups()
            shortname = re.sub(r'_{2,}', '_', shortname)
            shortname = unidecode(shortname)
        # if basicallyTheSame: 
        #    shortname = ""
        categories[current_category].append(Emoji(code, shortname, name))

    # Use xclip to pipe the output to clipboard.
    # e.g ./emoji_codegen.py emoji.json | xclip -sel clip
    # alternatively - delete the var from src/emoji/Provider.cpp, and do ./codegen.sh emojis shortcodes >> ../src/emoji/Provider.cpp
    func = None
    if mode == 'impl':
        func = generate_qml_list
    else:
        func = generate_provider_class
    func(people=people, nature=nature, food=food, activity=activity, travel=travel, objects=objects, symbols=symbols, flags=flags)