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
#!/usr/bin/env python3
import os
import sys
import json
import urllib.request
url_str = "https://html.spec.whatwg.org/entities.json"
with urllib.request.urlopen(url_str) as url:
entities = json.load(url)
records = []
for name in entities:
if name[-1] != ';':
continue
codepoints = entities[name]["codepoints"]
if len(codepoints) > 2:
print('Entity {} needs {} codepints; may need to update the .c code '
' accordingly.'.format(name, len(codepoints)), file=sys.stderr)
sys.exit(1)
while len(codepoints) < 2:
codepoints.append(0)
codepoints_str = map(str, codepoints)
records.append(" { \"" + name + "\", { " + ", ".join(codepoints_str) + " } }")
records.sort()
sys.stdout.write("static const ENTITY ENTITY_MAP[] = {\n")
sys.stdout.write(",\n".join(records))
sys.stdout.write("\n};\n\n")