-
Notifications
You must be signed in to change notification settings - Fork 4
/
_build.py
279 lines (213 loc) · 6.32 KB
/
_build.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
"""Generate markdown from template.
This module converts bespoke markdown into markdown compatible with
the bespoke mkdocs theme developed for Avalon.
"""
import sys
import json
import time
import shutil
import contextlib
import subprocess
from tempfile import mkdtemp
@contextlib.contextmanager
def tempfile(name):
try:
tempdir = mkdtemp()
fname = os.path.join(tempdir, name)
yield fname
finally:
shutil.rmtree(tempdir)
def on_template(template):
definition = template.strip("{{").rstrip().rstrip("}}")
key, value = definition.split(":")
if key == "schema":
return on_schema(value)
if key == "api" and value == "members":
return on_api_members()
return template
def on_block(language, block):
if language == "python":
if block[0].startswith("# Untested"):
return "```python\n%s```" % "".join(block)
return on_python(block)
return ""
def on_page(page):
formatted_time = time.strftime("%b %d %Y %H:%M:%S GMT+0", time.gmtime())
return """\
<p>{time}</p>
<br>
{content}\
""".format(time=formatted_time)
def on_api_members():
from avalon import api
table = """\
| Member | Description
|:-------|:--------
"""
row = "| `{name}` | {description}\n"
for name in api.__all__:
member = getattr(api, name)
doc = member.__doc__
if doc is None:
raise SyntaxError("'%s' is missing a docstring." % name)
table += row.format(
name=name,
description=doc.splitlines()[0]
)
return table
def on_schema(name):
from avalon import schema
schema = schema._cache[name]
description = """\
```json
{dump}
```
""".format(dump=json.dumps({
key: value.get("description", "")
for key, value in schema["properties"].items()
}, indent=4, sort_keys=True))
example = """\
**Example**
```json
{dump}
```
""".format(dump=json.dumps({
key: value.get("example", "")
for key, value in schema["properties"].items()
}, indent=4, sort_keys=True))
definition = """\
**Definition**
| Key | Description
|:----|:------------
"""
row = "| `{key}` | {description}\n"
for key, data in schema["properties"].items():
if "requires" in schema and key not in schema["requires"]:
continue
if "description" not in data:
raise SyntaxError("'%s' of %s must have a "
"description" % (key, name))
data["key"] = key
try:
data["type"] = {
"string": "str",
"number": "int",
"array": "list",
"object": "dict"
}[data["type"]]
except KeyError:
data["type"] = "any"
data["required"] = str(key in schema.get("required", {}))
definition += row.format(**data)
root = "https://github.com/getavalon/core/tree/master/avalon/schema"
link = """\
<a href="{root}/{name}" title="{name}" class="md-source-file">
{name}
</a>
""".format(root=root, name=name)
return os.linesep.join([link, description, example])
def on_python(block):
with tempfile("block.py") as fname:
with open(fname, "w") as f:
f.write(os.linesep.join(block))
try:
output = subprocess.check_output(
[sys.executable, fname],
stderr=subprocess.STDOUT,
universal_newlines=True
)
except subprocess.CalledProcessError as e:
output = e.output
output = "\n".join(
"<span class=\"p\">{line}</span>".format(line=line)
for line in output.splitlines()
)
source = """\
```python
{input}
```
""".format(input="".join(block))
output = """\
<table class="codehilitetable output">
<tbody>
<tr>
<td class="code">
<div class="codehilite" id="__code_1">
<pre>
{output}\
</pre>
</div>
</td>
</tr>
</tbody>
</table>
""".format(output=output) if output else ""
return "\n".join([source, output])
def parse(fname):
parsed = list()
blocks = list()
with open(fname) as f:
in_block = False
current_block = None
current_language = None
line_no = 0
for line in f:
line_no += 1
if line_no == 1 and line.startswith("build: false"):
print("Skipping '%s'.." % fname)
parsed = f.read()
break
if line.startswith("{{"):
line = on_template(line)
if in_block and line.startswith("```"):
print("Running Python..")
print("".join("\t%s" % line for line in current_block))
line = on_block(current_language, current_block)
in_block = False
current_language = None
parsed.append(line)
elif in_block:
current_block.append(line)
elif line.startswith("```python"):
in_block = True
current_language = "python"
current_block = list()
blocks.append(current_block)
else:
parsed.append(line)
return "".join(parsed)
if __name__ == '__main__':
import os
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("path", nargs='?')
args = parser.parse_args()
cd = os.path.abspath(os.path.dirname(__file__))
os.chdir(cd)
if args.path and os.path.isfile(args.path):
files = [args.path]
else:
files = list()
path = args.path
for base, dirs, fnames in os.walk("pages"):
for fname in fnames:
name, ext = os.path.splitext(fname)
if ext != ".md":
continue
src = os.path.join(base, fname)
files.append(src)
results = list()
for src in files:
print("Building '%s'.." % src)
dst = src.replace("pages", "build")
parsed = parse(src)
results.append((dst, parsed))
# Parsing can take some time, so write
# files all in one batch when done
for dst, parsed in results:
try:
os.makedirs(os.path.dirname(dst))
except OSError:
pass
with open(dst, "w") as f:
f.write(parsed)