File: entities.py

package info (click to toggle)
graphviz 14.0.5-2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 139,388 kB
  • sloc: ansic: 141,938; cpp: 11,957; python: 7,766; makefile: 4,043; yacc: 3,030; xml: 2,972; tcl: 2,495; sh: 1,388; objc: 1,159; java: 560; lex: 423; perl: 243; awk: 156; pascal: 139; php: 58; ruby: 49; cs: 31; sed: 1
file content (65 lines) | stat: -rw-r--r-- 1,608 bytes parent folder | download
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
#!/usr/bin/env python3

"""entities.h generator"""

import re
import sys
import textwrap
from pathlib import Path

# get names for html-4.0 characters from:
#          http://www.w3.org/TR/REC-html40/sgml/entities.html
entity_name_length_max: int = 0
entity: dict[str, str] = {}
with open(Path(__file__).parent / "entities.html", "rb") as f:
    for rec in f:
        if m := re.match(
            rb'&lt;!ENTITY\s+(?P<name>[^\s]*)\s+CDATA\s+"&amp;#(?P<val>\d+);"\s+--', rec
        ):
            name = m.group("name").decode("utf-8")
            val = m.group("val").decode("utf-8")
            entity[name] = val
            entity_name_length_max = max(entity_name_length_max, len(name))

with open(sys.argv[1], "wt", encoding="utf-8") as f:
    f.write(
        textwrap.dedent(
            f"""\
    /// @file
    /// @ingroup common_utils
    /*
     * Generated file - do not edit directly.
     *
     * This file was generated from:
     *       http://www.w3.org/TR/REC-html40/sgml/entities.html
     * by means of the script:
     *       {Path(__file__).name}
     */

    #ifdef __cplusplus
    extern "C" {{
    #endif

    static const struct entities_s {{
    	char	*name;
    	int	value;
    }} entities[] = {{
    """
        )
    )
    for name, val in sorted(list(entity.items())):
        f.write(f'	{{"{name}", {val}}},\n')
    f.write(
        textwrap.dedent(
            f"""\
    }};

    #define ENTITY_NAME_LENGTH_MAX {entity_name_length_max}
    #define NR_OF_ENTITIES (sizeof(entities) / sizeof(entities[0]))

    #ifdef __cplusplus
    }}
    #endif
    """
        )
    )