Debian Codegraph @

lib/common/entities.py

raw
1 #!/usr/bin/env python3
2
3 """entities.h generator"""
4
5 import re
6 import sys
7 import textwrap
8 from pathlib import Path
9
10 # get names for html-4.0 characters from:
11 # http://www.w3.org/TR/REC-html40/sgml/entities.html
12 entity_name_length_max: int = 0
13 entity: dict[str, str] = {}
14 with open(Path(__file__).parent / "entities.html", "rb") as f:
15 for rec in f:
16 if m := re.match(
17 rb'&lt;!ENTITY\s+(?P<name>[^\s]*)\s+CDATA\s+"&amp;#(?P<val>\d+);"\s+--', rec
18 ):
19 name = m.group("name").decode("utf-8")
20 val = m.group("val").decode("utf-8")
21 entity[name] = val
22 entity_name_length_max = max(entity_name_length_max, len(name))
23
24 with open(sys.argv[1], "wt", encoding="utf-8") as f:
25 f.write(
26 textwrap.dedent(
27 f"""\
28 /// @file
29 /// @ingroup common_utils
30 /*
31 * Generated file - do not edit directly.
32 *
33 * This file was generated from:
34 * http://www.w3.org/TR/REC-html40/sgml/entities.html
35 * by means of the script:
36 * {Path(__file__).name}
37 */
38
39 #ifdef __cplusplus
40 extern "C" {{
41 #endif
42
43 static const struct entities_s {{
44 char *name;
45 int value;
46 }} entities[] = {{
47 """
48 )
49 )
50 for name, val in sorted(list(entity.items())):
51 f.write(f' {{"{name}", {val}}},\n')
52 f.write(
53 textwrap.dedent(
54 f"""\
55 }};
56
57 #define ENTITY_NAME_LENGTH_MAX {entity_name_length_max}
58 #define NR_OF_ENTITIES (sizeof(entities) / sizeof(entities[0]))
59
60 #ifdef __cplusplus
61 }}
62 #endif
63 """
64 )
65 )