/
tasks.py
202 lines (149 loc) · 4.73 KB
/
tasks.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
import os
import re
import shutil
import subprocess
from pathlib2 import Path
from mako.template import Template
from mako.lookup import TemplateLookup
import bottle
from invoke import run, task
import stylus
from filters import markdown, rst
SITE = '/leaflet-examples/'
IMPORTS = [
'from filters import markdown, rst'
]
PAGE_FORMATS = ('.html', '.rst', '.md', '.jade')
NODE_PATH = '/usr/local/lib/node_modules'
# Set up lookup directories for Mako.
lookup = TemplateLookup(directories=['templates'])
# Set up Stylus compiler.
os.environ['NODE_PATH'] = NODE_PATH
stylus_compiler = stylus.Stylus()
bottle.debug(True)
app = bottle.Bottle()
if SITE != '/':
@app.route('/')
def index():
return '<a href="%s">Go to site</a>' % SITE
@app.route(SITE)
@app.route(SITE + '<path:path>')
def page(path=''):
file_ = get_file(path)
if file_ is None:
with open('site/404.html') as fp:
return fp.read()
if file_.suffix in PAGE_FORMATS:
return render_page(file_)
if file_.suffix == '.styl':
bottle.response.content_type = 'text/css'
return render_stylesheet(file_)
if file_.suffix == '.coffee':
bottle.response.content_type = 'text/javascript'
return compile_coffeescript(file_)
return bottle.static_file(path, root='site')
@task
def serve():
from livereload import Server
from livereload.watcher import Watcher
watcher = Watcher()
watcher.watch('site')
watcher.watch('templates')
server = Server(app, watcher)
server.serve(port=8000)
@task
def build():
clean()
for src in Path('site').rglob('*?.*'):
dest_dir = Path('build') / src.relative_to('site').parent
dest_file = copy_or_generate(src, dest_dir)
print dest_file
@task
def clean():
if Path('build').is_dir():
run('rm -rf build/*')
@task
def publish():
build()
run('ghp-import -n -p build')
def get_file(path):
filepath = Path('site') / path
if filepath.is_file():
return filepath
for format in PAGE_FORMATS:
index_file = filepath / ('index' + format)
if filepath.is_dir() and index_file.is_file():
return index_file
if filepath.suffix == '.css':
style_file = filepath.parent / (filepath.stem + '.styl')
if style_file.exists():
return style_file
if filepath.suffix == '.js':
coffee_file = filepath.parent / (filepath.stem + '.coffee')
if coffee_file.exists():
return coffee_file
return None
def get_slug(path):
if str(path) == 'site/index.html':
return '/'
elif path.stem == 'index':
return str(path.parent.name)
else:
return str(path.stem)
def split_markup(markup):
"""
Given some markup, return a tuple containing the decoded data and the
template code.
"""
import yaml
match = re.search(r'\n={3,}\n', markup)
if match:
start, end = match.span()
data = yaml.load(markup[0:start])
template_code = markup[end:]
else:
data = {}
template_code = markup
return data, template_code
def render_page(path):
data, template_code = split_markup(path.read_text())
data.update(site=SITE, slug=get_slug(path))
if path.suffix == '.jade':
return render_jade(template_code, data)
content = render(template_code, data)
if path.suffix == '.rst':
html = rst(content)
return render('<%inherit file="base.html" />\n' + html, data)
if path.suffix == '.md':
html = markdown(content)
return render('<%inherit file="base.html" />\n' + html, data)
# It's just a plan Mako template, so just return the rendered results.
return content
def render(template_code, data):
template = Template(template_code, lookup=lookup, imports=IMPORTS)
return template.render(**data)
def render_jade(template_code, data):
from pyjade.ext.mako import preprocessor
mako_code = preprocessor(template_code)
return render(mako_code, data)
def render_stylesheet(path):
source = path.read_text()
return stylus_compiler.compile(source)
def compile_coffeescript(path):
cmd = ['coffee', '-c', '-p', str(path)]
return subprocess.check_output(cmd)
def copy_or_generate(src, dest_dir):
if not dest_dir.exists():
dest_dir.mkdir(parents=True, exist_ok=True)
if src.suffix in PAGE_FORMATS:
dest = dest_dir / (src.stem + '.html')
with dest.open('w') as fp:
fp.write(render_page(src))
return dest
if src.suffix == '.styl':
dest = dest_dir / (src.stem + '.css')
with dest.open('w') as fp:
fp.write(render_stylesheet(src))
return dest
shutil.copy(str(src), str(dest_dir))
return dest_dir / src.name