46 lines
1.2 KiB
Python
46 lines
1.2 KiB
Python
# retoor <retoor@molodetz.nl>
|
|
import json
|
|
from functools import lru_cache
|
|
|
|
import emoji
|
|
|
|
from molodetz import config
|
|
|
|
CUSTOM_SHORTCODES = {
|
|
"molodetz": "\U0001F58B\uFE0F",
|
|
"af": "\u2705",
|
|
}
|
|
|
|
GENERATED_JS = config.STATIC_DIR / "js" / "generated" / "EmojiMap.js"
|
|
|
|
|
|
@lru_cache(maxsize=1)
|
|
def shortcode_map():
|
|
mapping = {}
|
|
for char, data in emoji.EMOJI_DATA.items():
|
|
if data.get("status") != emoji.STATUS["fully_qualified"]:
|
|
continue
|
|
names = [data.get("en", "")] + list(data.get("alias", []))
|
|
for name in names:
|
|
key = name.strip(":")
|
|
if key and key not in mapping:
|
|
mapping[key] = char
|
|
mapping.update(CUSTOM_SHORTCODES)
|
|
return mapping
|
|
|
|
|
|
def write_js_module(path=GENERATED_JS):
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
payload = json.dumps(dict(sorted(shortcode_map().items())), ensure_ascii=False, separators=(",", ":"))
|
|
path.write_text(
|
|
"// retoor <retoor@molodetz.nl>\n"
|
|
f"export const EMOJI_MAP = Object.freeze({payload});\n",
|
|
encoding="utf-8",
|
|
)
|
|
return path
|
|
|
|
|
|
def is_single_emoji(value):
|
|
value = (value or "").strip()
|
|
return emoji.emoji_count(value) == 1 and emoji.purely_emoji(value)
|