-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
generate_charmap_table.py
executable file
·84 lines (68 loc) · 2.3 KB
/
generate_charmap_table.py
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
79
80
81
82
83
84
#!/usr/bin/env python
"""This script is used to generate the tables for `charmap-reference.rst`.
Uses the tabulate module from PyPI.
"""
from __future__ import annotations
import argparse
import unicodedata
from typing import Iterable, Iterator
from tabulate import tabulate
import tcod.tileset
def get_character_maps() -> Iterator[str]:
"""Return an iterator of the current character maps from tcod.tileset."""
for name in dir(tcod.tileset):
if name.startswith("CHARMAP_"):
yield name[len("CHARMAP_") :].lower()
def escape_rst(string: str) -> str:
"""Escape RST symbols and disable Sphinx smart quotes."""
return (
string.replace("\\", "\\\\")
.replace("*", "\\*")
.replace("|", "\\|")
.replace("`", "\\`")
.replace("'", "\\'")
.replace('"', '\\"')
)
def generate_table(charmap: Iterable[int]) -> str:
"""Generate and RST table for `charmap`."""
headers = ("Tile Index", "Unicode", "String", "Name")
table = []
for i, ch in enumerate(charmap):
hex_len = len(f"{ch:x}")
if hex_len % 2: # Prevent an odd number of hex digits.
hex_len += 1
try:
name = unicodedata.name(chr(ch))
except ValueError:
# Skip names rather than guessing, the official names are enough.
name = ""
string = escape_rst(f"{chr(ch)!r}")
table.append((i, f"0x{ch:0{hex_len}X}", string, name))
return tabulate(table, headers, tablefmt="rst")
def main() -> None:
"""Main entry point."""
parser = argparse.ArgumentParser(
description="Generate an RST table for a tcod character map.",
)
parser.add_argument(
"charmap",
action="store",
choices=list(get_character_maps()),
type=str,
help="which character map to generate a table from",
)
parser.add_argument(
"-o",
"--out-file",
action="store",
type=argparse.FileType("w", encoding="utf-8"),
default="-",
help="where to write the table to (stdout by default)",
)
args = parser.parse_args()
charmap = getattr(tcod.tileset, f"CHARMAP_{args.charmap.upper()}")
output = generate_table(charmap)
with args.out_file as f:
f.write(output)
if __name__ == "__main__":
main()