From 660d96038ade10e00fa9a95f92e2adc7e2ff6314 Mon Sep 17 00:00:00 2001 From: Harshit Bansal Date: Sun, 19 Mar 2017 08:41:24 +0000 Subject: [PATCH] build_emoji: Add `emoji_catalog` to emoji_code.js Use `emoji.json` to create a emoji catalog and add it to `emoji_code.js` file. This catalog contains the unicode codepoints of all the emojis grouped according to their category. Emojis are sorted according to the `sort_order` defined in the iamcal's dataset. --- tools/setup/emoji/build_emoji | 10 ++++++++-- tools/setup/emoji/emoji_setup_utils.py | 21 ++++++++++++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/tools/setup/emoji/build_emoji b/tools/setup/emoji/build_emoji index 7289f91eae..5e0dfa1bb8 100755 --- a/tools/setup/emoji/build_emoji +++ b/tools/setup/emoji/build_emoji @@ -16,7 +16,7 @@ from typing import Dict, Text, Union from os.path import dirname from PIL import Image, ImageDraw, ImageFont -from emoji_setup_utils import emoji_names_for_picker +from emoji_setup_utils import generate_emoji_catalog, emoji_names_for_picker ZULIP_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../../') sys.path.append(ZULIP_PATH) @@ -33,6 +33,7 @@ EMOJI_DUMP_PATH = lambda p: os.path.join(EMOJI_DUMP_DIR_PATH, p) TARGET_EMOJI_DUMP = os.path.join(ZULIP_PATH, 'static', 'generated', 'emoji') EMOJI_CACHE_PATH = "/srv/zulip-emoji-cache" EMOJI_SCRIPT_DIR_PATH = os.path.join(ZULIP_PATH, 'tools', 'setup', 'emoji') +EMOJI_DATA_PATH = os.path.join(ZULIP_PATH, 'static', 'third', 'emoji-data') EMOJI_CODES_FILE_TEMPLATE = """\ var emoji_codes = (function () { @@ -44,6 +45,8 @@ exports.codepoints = %(codepoints)s; exports.name_to_codepoint = %(name_to_codepoint)s; +exports.emoji_catalog = %(emoji_catalog)s; + return exports; }()); if (typeof module !== 'undefined') { @@ -172,6 +175,8 @@ def dump_emojis(cache_path): emoji_map = json.load(open('emoji_map.json')) code_point_to_fname_map = code_point_to_file_name_map(EMOJI_DUMP_PATH("NotoColorEmoji.ttx")) + emoji_data = json.load(open(os.path.join(EMOJI_DATA_PATH, 'emoji.json'))) + emoji_catalog = generate_emoji_catalog(emoji_data) os.chdir(EMOJI_DUMP_DIR_PATH) @@ -250,7 +255,8 @@ def dump_emojis(cache_path): emoji_codes_file.write(EMOJI_CODES_FILE_TEMPLATE % { 'names': names, 'codepoints': sorted([str(code_point) for code_point in set(emoji_map.values())]), - 'name_to_codepoint': {str(key): str(emoji_map[key]) for key in emoji_map} + 'name_to_codepoint': {str(key): str(emoji_map[key]) for key in emoji_map}, + 'emoji_catalog': emoji_catalog }) emoji_codes_file.close() diff --git a/tools/setup/emoji/emoji_setup_utils.py b/tools/setup/emoji/emoji_setup_utils.py index eec76ea643..af07e7a241 100644 --- a/tools/setup/emoji/emoji_setup_utils.py +++ b/tools/setup/emoji/emoji_setup_utils.py @@ -13,7 +13,7 @@ from itertools import permutations, chain import ujson from six.moves import range, zip -from typing import Dict, List, Text +from typing import Any, Dict, List, Text # the corresponding code point will be set to exactly these names as a # final pass, overriding any other rules. This is useful for cases @@ -223,3 +223,22 @@ def emoji_names_for_picker(emoji_map): codepoint_to_names[codepoint] = names return sorted(list(chain.from_iterable(codepoint_to_names.values()))) + +# Returns a dict from categories to list of codepoints. The list of +# codepoints are sorted according to the `sort_order` as defined in +# `emoji_data`. +def generate_emoji_catalog(emoji_data): + # type: (List[Dict[Text, Any]]) -> Dict[str, List[str]] + sort_order = {} # type: Dict[str, int] + emoji_catalog = {} # type: Dict[str, List[str]] + for emoji in emoji_data: + category = str(emoji["category"]) + codepoint = str(emoji["unified"]) + sort_order[codepoint] = emoji["sort_order"] + if category in emoji_catalog: + emoji_catalog[category].append(codepoint) + else: + emoji_catalog[category] = [codepoint, ] + for category in emoji_catalog: + emoji_catalog[category].sort(key=lambda codepoint: sort_order[codepoint]) + return emoji_catalog