Edit

kc3-lang/md4c/scripts/build_entity_map.py

Branch :

  • Show log

    Commit

  • Author : Martin Mitas
    Date : 2024-01-11 03:34:24
    Hash : e25ea3d1
    Message : Update list of named entities.

  • scripts/build_entity_map.py
  • #!/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")