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.
This commit is contained in:
Harshit Bansal
2017-03-19 08:41:24 +00:00
committed by Tim Abbott
parent 4470a3947b
commit 660d96038a
2 changed files with 28 additions and 3 deletions

View File

@@ -16,7 +16,7 @@ from typing import Dict, Text, Union
from os.path import dirname from os.path import dirname
from PIL import Image, ImageDraw, ImageFont 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__)), '../../../') ZULIP_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../../')
sys.path.append(ZULIP_PATH) 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') TARGET_EMOJI_DUMP = os.path.join(ZULIP_PATH, 'static', 'generated', 'emoji')
EMOJI_CACHE_PATH = "/srv/zulip-emoji-cache" EMOJI_CACHE_PATH = "/srv/zulip-emoji-cache"
EMOJI_SCRIPT_DIR_PATH = os.path.join(ZULIP_PATH, 'tools', 'setup', 'emoji') 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 = """\ EMOJI_CODES_FILE_TEMPLATE = """\
var emoji_codes = (function () { var emoji_codes = (function () {
@@ -44,6 +45,8 @@ exports.codepoints = %(codepoints)s;
exports.name_to_codepoint = %(name_to_codepoint)s; exports.name_to_codepoint = %(name_to_codepoint)s;
exports.emoji_catalog = %(emoji_catalog)s;
return exports; return exports;
}()); }());
if (typeof module !== 'undefined') { if (typeof module !== 'undefined') {
@@ -172,6 +175,8 @@ def dump_emojis(cache_path):
emoji_map = json.load(open('emoji_map.json')) emoji_map = json.load(open('emoji_map.json'))
code_point_to_fname_map = code_point_to_file_name_map(EMOJI_DUMP_PATH("NotoColorEmoji.ttx")) 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) os.chdir(EMOJI_DUMP_DIR_PATH)
@@ -250,7 +255,8 @@ def dump_emojis(cache_path):
emoji_codes_file.write(EMOJI_CODES_FILE_TEMPLATE % { emoji_codes_file.write(EMOJI_CODES_FILE_TEMPLATE % {
'names': names, 'names': names,
'codepoints': sorted([str(code_point) for code_point in set(emoji_map.values())]), '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() emoji_codes_file.close()

View File

@@ -13,7 +13,7 @@ from itertools import permutations, chain
import ujson import ujson
from six.moves import range, zip 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 # the corresponding code point will be set to exactly these names as a
# final pass, overriding any other rules. This is useful for cases # 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 codepoint_to_names[codepoint] = names
return sorted(list(chain.from_iterable(codepoint_to_names.values()))) 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