| Four more modules into Nim: members, glyphs, emoji, store 3975b4b nandi 11h ago | 1 | #!/usr/bin/env python3 |
| 2 | """Generate nim/src/frq/emoji.nim from common/frq/emoji.cljc. |
| 3 | |
| 4 | The catalogue is 1,885 rows of data that Unicode generated in the first |
| 5 | place — retyping it by hand into a second language is how the two copies |
| 6 | start disagreeing. This reads the Clojure and emits the Nim, so the port is |
| 7 | a transcription a machine did and can redo. |
| 8 | |
| 9 | python3 tools/emoji2nim.py |
| 10 | """ |
| 11 | import re |
| 12 | import sys |
| 13 | from pathlib import Path |
| 14 | |
| 15 | root = Path(__file__).resolve().parent.parent |
| 16 | src = (root / "common/frq/emoji.cljc").read_text() |
| 17 | out = root / "nim/src/frq/emoji.nim" |
| 18 | |
| 19 | def 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 | |
| 24 | def 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 | |
| 34 | popular = strings(section("popular")) |
| 35 | groups = strings(section("groups")) |
| 36 | rows = [strings(r) for r in re.findall(r'\["(?:[^"\\]|\\.)*"\s+"(?:[^"\\]|\\.)*"\s+"(?:[^"\\]|\\.)*"\]', |
| 37 | section("catalog"))] |
| 38 | |
| 39 | def nimstr(s: str) -> str: |
| 40 | return '"' + s.replace("\\", "\\\\").replace('"', '\\"') + '"' |
| 41 | |
| 42 | lines = [ |
| 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 | ] |
| 71 | for glyph, name, group in rows: |
| 72 | lines.append(f" Emoji(glyph: {nimstr(glyph)}, name: {nimstr(name)}, " |
| 73 | f"group: {nimstr(group)}),") |
| 74 | lines += [" ]", ""] |
| 75 | |
| 76 | out.write_text("\n".join(lines)) |
| 77 | print(f"wrote {out.relative_to(root)}: {len(rows)} emoji, " |
| 78 | f"{len(popular)} popular, {len(groups)} groups") |