| 1 | #!/usr/bin/env python3 |
| 2 | """Build crates/app/assets/emoji/emoji.tsv from emojibase-data (MIT). |
| 3 | |
| 4 | Usage: scripts/emoji-index.py [emojibase-data version, default 17.0.0] |
| 5 | Line: emoji <TAB> English gemoji codes (space) <TAB> German aliases (|), sorted by emojibase order. |
| 6 | """ |
| 7 | import json |
| 8 | import pathlib |
| 9 | import subprocess |
| 10 | import sys |
| 11 | import tarfile |
| 12 | import tempfile |
| 13 | |
| 14 | version = sys.argv[1] if len(sys.argv) > 1 else "17.0.0" |
| 15 | repo_root = pathlib.Path(__file__).resolve().parent.parent |
| 16 | output_path = repo_root / "crates/app/assets/emoji/emoji.tsv" |
| 17 | |
| 18 | with tempfile.TemporaryDirectory() as work_directory: |
| 19 | subprocess.run( |
| 20 | ["npm", "pack", f"emojibase-data@{version}", "--silent"], |
| 21 | cwd=work_directory, check=True, stdout=subprocess.DEVNULL, |
| 22 | ) |
| 23 | archive = next(pathlib.Path(work_directory).glob("emojibase-data-*.tgz")) |
| 24 | with tarfile.open(archive) as tar: |
| 25 | tar.extractall(work_directory, filter="data") |
| 26 | package = pathlib.Path(work_directory) / "package" |
| 27 | load = lambda relative: json.loads((package / relative).read_text(encoding="utf-8")) |
| 28 | english = load("en/compact.json") |
| 29 | german = {entry["hexcode"]: entry for entry in load("de/compact.json")} |
| 30 | english_codes = load("en/shortcodes/github.json") |
| 31 | german_codes = load("de/shortcodes/cldr.json") |
| 32 | |
| 33 | as_list = lambda value: value if isinstance(value, list) else [value] |
| 34 | lines = [] |
| 35 | for entry in sorted(english, key=lambda entry: entry.get("order", 1 << 30)): |
| 36 | hexcode = entry["hexcode"] |
| 37 | codes = as_list(english_codes.get(hexcode, [])) |
| 38 | if not codes: |
| 39 | continue |
| 40 | german_entry = german.get(hexcode, {}) |
| 41 | aliases = [] |
| 42 | candidates = [german_entry.get("label", "")] |
| 43 | candidates += [code.replace("_", " ") for code in as_list(german_codes.get(hexcode, []))] |
| 44 | candidates += german_entry.get("tags", []) |
| 45 | for alias in candidates: |
| 46 | alias = alias.lower().strip() |
| 47 | if alias and alias not in aliases and "|" not in alias and "\t" not in alias: |
| 48 | aliases.append(alias) |
| 49 | emoji = entry["unicode"] |
| 50 | if emoji.endswith("\ufe0f") and len(emoji) == 2 and ord(emoji[0]) >= 0x1F300: |
| 51 | emoji = emoji[0] |
| 52 | lines.append(f"{emoji}\t{' '.join(codes)}\t{'|'.join(aliases)}") |
| 53 | |
| 54 | output_path.parent.mkdir(parents=True, exist_ok=True) |
| 55 | output_path.write_text("\n".join(lines) + "\n", encoding="utf-8") |
| 56 | print(f"{len(lines)} emoji -> {output_path.relative_to(repo_root)}") |