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.

Four more modules into Nim: members, glyphs, emoji, store 3975b4b · on 3975b4bc9fe9fd59fc0e81ca2bb7ffdba986934a · nandi · 9h ago
emoji2nim.py · 78 lines · 3.0 KBPython Blame HistoryRaw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python3
"""Generate nim/src/frq/emoji.nim from common/frq/emoji.cljc.

The catalogue is 1,885 rows of data that Unicode generated in the first
place — retyping it by hand into a second language is how the two copies
start disagreeing. This reads the Clojure and emits the Nim, so the port is
a transcription a machine did and can redo.

    python3 tools/emoji2nim.py
"""
import re
import sys
from pathlib import Path

root = Path(__file__).resolve().parent.parent
src = (root / "common/frq/emoji.cljc").read_text()
out = root / "nim/src/frq/emoji.nim"

def strings(block: str):
    """Every "..." in order, with Clojure's escapes undone."""
    return [s.encode().decode("unicode_escape") if "\\" in s else s
            for s in re.findall(r'"((?:[^"\\]|\\.)*)"', block)]

def section(name: str) -> str:
    # To the next top-level `(def ` or to EOF — `catalog` is last in the file
    # and has no blank line after it.
    m = re.search(r'\(def ' + name + r'\b(.*?)(?=\n\(def |\Z)', src, re.S)
    if not m:
        sys.exit(f"emoji2nim: no `(def {name} ...)` in emoji.cljc")
    body = m.group(1)
    # Drop the docstring, which is the first string and full of prose.
    return body[body.index("["):] if "[" in body else body

popular = strings(section("popular"))
groups = strings(section("groups"))
rows = [strings(r) for r in re.findall(r'\["(?:[^"\\]|\\.)*"\s+"(?:[^"\\]|\\.)*"\s+"(?:[^"\\]|\\.)*"\]',
                                       section("catalog"))]

def nimstr(s: str) -> str:
    return '"' + s.replace("\\", "\\\\").replace('"', '\\"') + '"'

lines = [
    "## Every emoji this client can draw, with the name to search it by.",
    "##",
    "## GENERATED by tools/emoji2nim.py from common/frq/emoji.cljc. Do not edit:",
    "## the catalogue is Unicode's own `emoji-test.txt` (15.1), filtered to what",
    "## the Twemoji pack has a picture for, and a second hand-maintained copy is",
    "## how the two languages start disagreeing about what can be drawn.",
    "##",
    "## Skin-tone variants are left out — they multiply the list by five and say",
    "## nothing a reaction needs to say. `glyphs` strips a tone before looking a",
    "## glyph up here, and keeps it for the drawing.",
    "",
    "type",
    "  Emoji* = object",
    "    glyph*, name*, group*: string",
    "",
    "const",
    "  popular* = [",
    "    " + ", ".join(nimstr(p) for p in popular),
    "  ]",
    "    ## What a reaction usually is. The picker opens on these, because the",
    "    ## whole point of reacting is that it costs less than typing.",
    "",
    "  groups* = [",
    "    " + ",\n    ".join(nimstr(g) for g in groups),
    "  ]",
    "",
    f"  catalog*: array[{len(rows)}, Emoji] = [",
]
for glyph, name, group in rows:
    lines.append(f"    Emoji(glyph: {nimstr(glyph)}, name: {nimstr(name)}, "
                 f"group: {nimstr(group)}),")
lines += ["  ]", ""]

out.write_text("\n".join(lines))
print(f"wrote {out.relative_to(root)}: {len(rows)} emoji, "
      f"{len(popular)} popular, {len(groups)} groups")