forked from sdiehl/gevent-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build
executable file
·47 lines (32 loc) · 1007 Bytes
/
build
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
#!/usr/bin/env python
import re
import os
import sys
import codecs
import markdown
from cStringIO import StringIO
from cogapp import Cog
from jinja2 import Template
from jinja2 import Environment, FileSystemLoader
OUTPUT = 'index.html'
loader = FileSystemLoader(os.getcwd())
env = Environment(loader=loader)
template = env.get_template('body.tmpl')
input_file = codecs.open("tutorial.md", mode="r", encoding="utf-8")
text = input_file.read()
old_stdout = sys.stdout
sys.stdout = rstdout = StringIO()
cog = Cog()
cog.processOneFile('tutorial.md')
sys.stdout = old_stdout
cogged = rstdout.getvalue()
start_code = """<pre><code class="python">"""
end_code = """</pre></code>"""
r1 = re.sub('\[\[\[end\]\]\]', end_code, cogged)
r2 = re.sub(r'\[\[\[cog',start_code, r1)
r3 = re.sub(r'\]\]\]',end_code + '\n' + start_code, r2)
rendered_code = r3
body = markdown.markdown(rendered_code, extensions=['toc'])
with open(OUTPUT, 'w+') as f:
f.write(template.render(body=body))
print 'Wrote', OUTPUT