-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
100 lines (76 loc) · 2.16 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
import os.path as op
from mako.template import Template
from mako.lookup import TemplateLookup
import bottle
from invoke import run, task
from pathlib2 import Path
SITE = '/javascript-examples/'
IMPORTS = [
'from filters import markdown, rst'
]
lookup = TemplateLookup(directories=['templates'])
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 not file_.endswith('.html'):
return bottle.static_file(path, root='site')
return generate(file_)
@task
def serve():
from livereload import Server
from livereload.watcher import Watcher
watcher = Watcher()
watcher.watch('site', ignore=lambda p: p.endswith('.babel'))
watcher.watch('templates')
server = Server(app, watcher)
server.serve(port=8000)
@task
def build():
clean()
buildjs()
for src in Path('site').rglob('*?.*'):
dest = Path('build') / src.relative_to('site')
# The sourcemap files are useless in production.
if dest.suffix == '.map':
continue
print dest
copy_or_generate(src, dest)
@task
def clean():
if op.isdir('build'):
run('rm -rf build/*')
@task
def buildjs():
run('webpack --progress --colors --optimize-minimize')
@task
def watchjs():
run('webpack --progress --colors -d --watch')
@task
def publish():
build()
run('ghp-import -n -p build')
def get_file(path):
result = op.join('site', path)
if op.isfile(result):
return result
if op.isdir(result) and op.isfile(op.join(result, 'index.html')):
return op.join(result, 'index.html')
return 'site/404.html'
def generate(path):
template = Template(open(path).read(), lookup=lookup, imports=IMPORTS)
return template.render(site=SITE)
def copy_or_generate(src, dest):
import shutil
if not dest.exists():
dest.parent.mkdir(parents=True, exist_ok=True)
if src.suffix == '.html':
with dest.open('w') as fp:
fp.write(generate(str(src)))
else:
shutil.copy(str(src), str(dest))