-
Notifications
You must be signed in to change notification settings - Fork 3
/
templ.py
executable file
·34 lines (26 loc) · 1.4 KB
/
templ.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os, re
# Produce a usable header
assetsh = b"#include <string>\n#include <unordered_map>\n"
assetsh += b"typedef std::string(*t_templatefn)(std::string, std::string, bool);\n"
assetsh += b"extern const std::unordered_map<std::string, t_templatefn> templates;\n"
# Read template HTML files and generate templates.cc asset
assets = b"#include \"templates.h\"\n#include <string>\n#include <unordered_map>\n\n"
fnentries = []
for i,f in enumerate(sorted(os.listdir("templates/"))):
if f.endswith(".html"):
cont = open(os.path.join("templates", f), "rb").read()
cont = cont.replace(b"\\", b"\\\\").replace(b'"', b'\\"').replace(b"\n", b"\\n")
cont = cont.replace(b"{{hostname}}", b'" + hostname + "')
cont = cont.replace(b"{{follow_page}}", b'" + follow_page + "')
cont = re.sub(b"{{loginfailed}}(.*){{/loginfailed}}", b'" + (err ? "\\1" : "") + "', cont)
assets += b"std::string login_%d(std::string hostname, std::string follow_page, bool err) {\n" % i
assets += b"return \"" + cont + b"\";\n}\n"
fnentries.append(b" {\"%s\", %s},\n" % (f.split(".")[0].encode("utf-8"), b"login_%d" % i))
# Generate a map of functions indexed on template name
assets += b"const std::unordered_map<std::string, t_templatefn> templates = {\n"
assets += b"".join(fnentries)
assets += b"};"
open("templates.cc", "wb").write(assets)
open("templates.h", "wb").write(assetsh)