-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgen.py
118 lines (110 loc) · 4.8 KB
/
gen.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
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import cgi
import os
import re
import string
import sys
import pygments
import pygments.lexers
import pygments.formatters
import test
Languages = {
"ada": {"name": "Ada", "suffix": ".a"},
"cpp": {"name": "C++", "suffix": ".cpp"},
"csharp": {"name": "C#", "suffix": ".cs"},
"d": {"name": "D", "suffix": ".d"},
"erlang": {"name": "Erlang", "suffix": ".erl"},
"go": {"name": "Go", "suffix": ".go"},
"haskell": {"name": "Haskell", "suffix": ".hs"},
"java": {"name": "Java", "suffix": ".java"},
"javascript": {"name": "Javascript", "suffix": ".js"},
"lua": {"name": "Lua", "suffix": ".lua"},
"pascal": {"name": "Pascal", "suffix": ".pas"},
"perl": {"name": "Perl", "suffix": ".pl"},
"php": {"name": "PHP", "suffix": ".php"},
"python": {"name": "Python (2.x and 3.x)", "suffix": ".py"},
"ruby": {"name": "Ruby", "suffix": ".rb"},
"scala": {"name": "Scala", "suffix": ".scala"},
"scheme": {"name": "Scheme", "suffix": ".scm"},
"tcl": {"name": "Tcl", "suffix": ".tcl"},
}
class UnescapedString:
def __init__(self, s):
self.s = s
def __str__(self):
return self.s
def template(file=None, text=None, vars=None):
"""Super quick and dirty template function"""
if file is not None:
with open(file) as f:
text = f.read()
escapedvars = {x: cgi.escape(y) if isinstance(y, str) else y for x, y in vars.items()}
r = ""
a = text.split("\n")
i = 0
while i < len(a):
s = a[i]
m = re.match(r"\s*@(\w+)\s+(.+?)@$", s)
if m is not None:
cmd = m.group(1)
args = m.group(2)
if cmd == "iter":
it = args
j = i + 1
while j < len(a) and not re.match(r"\w*@end@$", a[j]):
j += 1
for x in vars[it]:
if isinstance(x, dict):
d = {"_": x}
d.update(x)
r += template(text="\n".join(a[i+1:j]), vars=d)
else:
r += template(text="\n".join(a[i+1:j]), vars={"_": x})
i = j + 1
elif cmd == "include":
i += 1
try:
r += open(string.Template(args).substitute(escapedvars)).read()
except Exception as e:
if not isinstance(e, IOError) or e.errno != 2:
print(e)
else:
print("Unknown directive: {0}".format(cmd), file=sys.stderr)
sys.exit(1)
else:
t = string.Template(s)
r += t.substitute(escapedvars) + "\n"
i += 1
return r
def listrecdir(path):
for dirpath, dirnames, filenames in os.walk(path):
for filename in filenames:
yield os.path.join(dirpath, filename)
def main():
languages = sorted(test.Languages.keys())
with open("index.html", "w") as f:
f.write(template(file="index.template", vars={"languages": [dict(Languages[x].items() | {"dir": x}.items()) for x in languages]}))
for language in languages:
files = [x for x in listrecdir(language) if x.endswith(Languages[language]["suffix"]) and not os.path.basename(x).startswith("test")]
with open(os.path.join(language, "index.html"), "w") as f:
f.write(template(file="language.template", vars={
"lang": language,
"name": Languages[language]["name"],
"functions": [os.path.basename(x) for x in files],
}))
for fn in files:
with open(os.path.join(language, os.path.basename(fn) + ".html"), "w", encoding="utf-8") as f:
code = open(fn, encoding="utf-8").read()
highlighted = pygments.highlight(
code,
pygments.lexers.get_lexer_by_name(language),
pygments.formatters.HtmlFormatter()
)
f.write(template(file="function.template", vars={
"name": Languages[language]["name"],
"file": os.path.basename(fn),
"extension": Languages[language]["suffix"][1:],
"code": UnescapedString(highlighted),
"style": pygments.formatters.HtmlFormatter().get_style_defs(".highlight"),
}))
if __name__ == "__main__":
main()