nandi/frqpublic Fork 0
3975b4bc9fe9fd59fc0e81ca2bb7ffdba986934a
Commits
Clone
git clone https://git.rickub.com/nandi/frq.git
git clone ssh://git@rickub.com/nandi/frq.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

emoji2nim.py · 78 lines · 3.0 KBPython Blame HistoryRaw
Four more modules into Nim: members, glyphs, emoji, store 3975b4b nandi 11h ago1#!/usr/bin/env python3
2"""Generate nim/src/frq/emoji.nim from common/frq/emoji.cljc.
3
4The catalogue is 1,885 rows of data that Unicode generated in the first
5place — retyping it by hand into a second language is how the two copies
6start disagreeing. This reads the Clojure and emits the Nim, so the port is
7a transcription a machine did and can redo.
8
9 python3 tools/emoji2nim.py
10"""
11import re
12import sys
13from pathlib import Path
14
15root = Path(__file__).resolve().parent.parent
16src = (root / "common/frq/emoji.cljc").read_text()
17out = root / "nim/src/frq/emoji.nim"
18
19def strings(block: str):
20 """Every "..." in order, with Clojure's escapes undone."""
21 return [s.encode().decode("unicode_escape") if "\\" in s else s
22 for s in re.findall(r'"((?:[^"\\]|\\.)*)"', block)]
23
24def section(name: str) -> str:
25 # To the next top-level `(def ` or to EOF — `catalog` is last in the file
26 # and has no blank line after it.
27 m = re.search(r'\(def ' + name + r'\b(.*?)(?=\n\(def |\Z)', src, re.S)
28 if not m:
29 sys.exit(f"emoji2nim: no `(def {name} ...)` in emoji.cljc")
30 body = m.group(1)
31 # Drop the docstring, which is the first string and full of prose.
32 return body[body.index("["):] if "[" in body else body
33
34popular = strings(section("popular"))
35groups = strings(section("groups"))
36rows = [strings(r) for r in re.findall(r'\["(?:[^"\\]|\\.)*"\s+"(?:[^"\\]|\\.)*"\s+"(?:[^"\\]|\\.)*"\]',
37 section("catalog"))]
38
39def nimstr(s: str) -> str:
40 return '"' + s.replace("\\", "\\\\").replace('"', '\\"') + '"'
41
42lines = [
43 "## Every emoji this client can draw, with the name to search it by.",
44 "##",
45 "## GENERATED by tools/emoji2nim.py from common/frq/emoji.cljc. Do not edit:",
46 "## the catalogue is Unicode's own `emoji-test.txt` (15.1), filtered to what",
47 "## the Twemoji pack has a picture for, and a second hand-maintained copy is",
48 "## how the two languages start disagreeing about what can be drawn.",
49 "##",
50 "## Skin-tone variants are left out — they multiply the list by five and say",
51 "## nothing a reaction needs to say. `glyphs` strips a tone before looking a",
52 "## glyph up here, and keeps it for the drawing.",
53 "",
54 "type",
55 " Emoji* = object",
56 " glyph*, name*, group*: string",
57 "",
58 "const",
59 " popular* = [",
60 " " + ", ".join(nimstr(p) for p in popular),
61 " ]",
62 " ## What a reaction usually is. The picker opens on these, because the",
63 " ## whole point of reacting is that it costs less than typing.",
64 "",
65 " groups* = [",
66 " " + ",\n ".join(nimstr(g) for g in groups),
67 " ]",
68 "",
69 f" catalog*: array[{len(rows)}, Emoji] = [",
70]
71for glyph, name, group in rows:
72 lines.append(f" Emoji(glyph: {nimstr(glyph)}, name: {nimstr(name)}, "
73 f"group: {nimstr(group)}),")
74lines += [" ]", ""]
75
76out.write_text("\n".join(lines))
77print(f"wrote {out.relative_to(root)}: {len(rows)} emoji, "
78 f"{len(popular)} popular, {len(groups)} groups")