Debian Codegraph @

scripts/build_entity_map.py

raw
1 #!/usr/bin/env python3
2
3 import os
4 import sys
5 import json
6 import urllib.request
7
8
9 url_str = "https://html.spec.whatwg.org/entities.json"
10
11 with urllib.request.urlopen(url_str) as url:
12 entities = json.load(url)
13
14 records = []
15
16 for name in entities:
17 if name[-1] != ';':
18 continue
19
20 codepoints = entities[name]["codepoints"]
21
22 if len(codepoints) > 2:
23 print('Entity {} needs {} codepints; may need to update the .c code '
24 ' accordingly.'.format(name, len(codepoints)), file=sys.stderr)
25 sys.exit(1)
26
27 while len(codepoints) < 2:
28 codepoints.append(0)
29
30 codepoints_str = map(str, codepoints)
31 records.append(" { \"" + name + "\", { " + ", ".join(codepoints_str) + " } }")
32
33 records.sort()
34
35 sys.stdout.write("static const ENTITY ENTITY_MAP[] = {\n")
36 sys.stdout.write(",\n".join(records))
37 sys.stdout.write("\n};\n\n")