scorpiox-bot-plugin-notes
←gen_ui.pypython
105 lines · 3 KBHEADBlameDownload
1#!/usr/bin/env python3
2"""tools/gen_ui.py - compile src/ui.html into src/ui_html.h.
3
4The page lives in one readable file (src/ui.html) so it can be diffed and
5reviewed like any other source. This script turns it into a single C string
6constant with two control-character placeholders:
7
8 0x01 -> the bot theme prepaint script + <style> + theme tokens
9 0x02 -> the bot theme handoff script (just before </body>)
10
11main() splits the constant on those bytes and emits the macros from
12src/bot_theme_plugin.h in their place, so the theme can never drift out of
13sync with the rest of the bot.
14
15 python3 tools/gen_ui.py # write src/ui_html.h
16 python3 tools/gen_ui.py --check # exit 1 when the header is stale
17"""
18
19import os
20import sys
21
22ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
23SRC = os.path.join(ROOT, "src", "ui.html")
24OUT = os.path.join(ROOT, "src", "ui_html.h")
25
26TOKENS = "<!--TOKENS-->"
27MARK_TOKENS = "\u0001"
28MARK_HANDOFF = "\u0002"
29
30
31def c_literal(text):
32 """Escape text for a C string literal, one line per source line."""
33 out = []
34 for line in text.splitlines(True):
35 piece = []
36 for ch in line:
37 if ch == "\\":
38 piece.append("\\\\")
39 elif ch == '"':
40 piece.append('\\"')
41 elif ch == "\n":
42 piece.append("\\n")
43 elif ch == "\r":
44 piece.append("\\r")
45 elif ch == "\t":
46 piece.append("\\t")
47 elif ch == MARK_TOKENS:
48 piece.append("\\001")
49 elif ch == MARK_HANDOFF:
50 piece.append("\\002")
51 elif ord(ch) < 0x20:
52 piece.append("\\%03o" % ord(ch))
53 else:
54 piece.append(ch)
55 out.append(' "%s"\n' % "".join(piece))
56 return "".join(out)
57
58
59def render():
60 with open(SRC, "r", encoding="utf-8") as f:
61 html = f.read()
62
63 if TOKENS not in html:
64 raise SystemExit("src/ui.html has no %s placeholder" % TOKENS)
65 html = html.replace(TOKENS, MARK_TOKENS)
66
67 # Handoff script goes right before the closing body tag.
68 if "</body>" not in html:
69 raise SystemExit("src/ui.html has no </body>")
70 html = html.replace("</body>", MARK_HANDOFF + "</body>", 1)
71
72 header = (
73 "/* Generated by tools/gen_ui.py from src/ui.html. Do not edit by hand. */\n"
74 "#ifndef SX_NOTES_UI_HTML_H\n"
75 "#define SX_NOTES_UI_HTML_H\n"
76 "\n"
77 "static const char *UI_HTML =\n" + c_literal(html) + " ;\n"
78 "\n"
79 "#endif /* SX_NOTES_UI_HTML_H */\n"
80 )
81 return header
82
83
84def main():
85 header = render()
86 check = "--check" in sys.argv[1:]
87 if check:
88 try:
89 with open(OUT, "r", encoding="utf-8") as f:
90 cur = f.read()
91 except OSError:
92 cur = ""
93 if cur != header:
94 sys.stderr.write("src/ui_html.h is stale - run tools/gen_ui.py\n")
95 return 1
96 print("src/ui_html.h is up to date")
97 return 0
98 with open(OUT, "w", encoding="utf-8") as f:
99 f.write(header)
100 print("wrote %s (%d bytes)" % (os.path.relpath(OUT, ROOT), len(header)))
101 return 0
102
103
104if __name__ == "__main__":
105 sys.exit(main())