summaryrefslogtreecommitdiffstats
path: root/scripts/emoji_codegen.py
blob: cfa724250b1e52185cea3bf2177340be025e84cc (plain)
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
#!/usr/bin/env python3

import sys
import json

from jinja2 import Template


class Emoji(object):
    def __init__(self, code, shortname, category, order):
        self.code = ''.join(list(map(code_to_bytes, code.split('-'))))
        self.shortname = shortname
        self.category = category
        self.order = int(order)


def code_to_bytes(codepoint):
    '''
    Convert hex unicode codepoint to hex byte array.
    '''
    bytes = chr(int(codepoint, 16)).encode('utf-8')

    return str(bytes)[1:].strip("'")


def generate_code(emojis, category):
    tmpl = Template('''
const QList<Emoji> EmojiProvider::{{ category }} = {
    {%- for e in emoji %}
        Emoji{QString::fromUtf8("{{ e.code }}"), "{{ e.shortname }}"},
    {%- endfor %}
};
    ''')

    d = dict(category=category, emoji=emojis)
    print(tmpl.render(d))


if __name__ == '__main__':
    if len(sys.argv) < 2:
        print('usage: emoji_codegen.py /path/to/emoji.json')
        sys.exit(1)

    filename = sys.argv[1]
    data = {}

    with open(filename, 'r') as filename:
        data = json.loads(filename.read())

    emojis = []

    for emoji_name in data:
        tmp = data[emoji_name]

        l = len(tmp['unicode'].split('-'))

        if l > 1 and tmp['category'] == 'people':
            continue

        emojis.append(
            Emoji(
                tmp['unicode'],
                tmp['shortname'],
                tmp['category'],
                tmp['emoji_order']
            )
        )

    emojis.sort(key=lambda x: x.order)

    people = list(filter(lambda x: x.category == "people", emojis))
    nature = list(filter(lambda x: x.category == "nature", emojis))
    food = list(filter(lambda x: x.category == "food", emojis))
    activity = list(filter(lambda x: x.category == "activity", emojis))
    travel = list(filter(lambda x: x.category == "travel", emojis))
    objects = list(filter(lambda x: x.category == "objects", emojis))
    symbols = list(filter(lambda x: x.category == "symbols", emojis))
    flags = list(filter(lambda x: x.category == "flags", emojis))

    # Use xclip to pipe the output to clipboard.
    # e.g ./codegen.py emoji.json | xclip -sel clip
    generate_code(people, 'people')
    generate_code(nature, 'nature')
    generate_code(food, 'food')
    generate_code(activity, 'activity')
    generate_code(travel, 'travel')
    generate_code(objects, 'objects')
    generate_code(symbols, 'symbols')
    generate_code(flags, 'flags')