/
build.py
149 lines (110 loc) · 3.61 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
from os import path, getcwd, mkdir
from sys import stdout
import subprocess
from yaml import load
from slugify import Slugify
from datetime import date
from staticjinja import make_site
from dateutil.parser import parse
import argh
from govlabstatic.cli import Manager
slugify = Slugify(to_lower=True)
ROOT_DIR = path.abspath(path.dirname(__file__))
# We define constants for the deployment.
searchpath = path.join(ROOT_DIR, 'templates')
outputpath = path.join(ROOT_DIR, 'site')
# We load the data we want to use in the templates.
PEOPLE = load(open('data/people.yaml'))
CLINICS = load(open('data/clinics.yaml'))
LIBRARY = load(open('data/library.yaml'))
COACHING = load(open('data/coaching.yaml'))
PROJECTS = load(open('data/project-gallery.yaml'))
WORKSHOPS = load(open('data/workshops.yaml'))
for p in PEOPLE:
p['fullName'] = '%s %s' % (p['name']['first'], p['name']['last'])
PEOPLE = sorted(PEOPLE, key=lambda x: x['name']['last'])
for item in COACHING:
item['start_date'] = parse(item['date']['start']).date()
item['outdated'] = item['start_date'] < date.today()
COACHING = sorted(COACHING, key=lambda x: x['start_date'])
def loadAcademyData():
return {
'people': PEOPLE,
'clinics': CLINICS,
'projects': PROJECTS,
'coaching': COACHING,
'workshops': WORKSHOPS
}
def debug(text):
print('text')
stdout.flush()
return ''
def isEmpty(seq):
return len([k for k in seq]) == 0
def nameTest(name, value):
return '%s %s' % (name['first'], name['last']) == value
def maxLen(string, n):
if len(string) > n:
return string[:n] + '...'
else:
return string
filters = {
'slug': slugify,
'debug': debug,
'byName': lambda x: [p for p in PEOPLE if p.name == x],
'isEmpty': isEmpty,
'max': maxLen
}
def render_project_detail_pages(env, template, **kwargs):
'''
staticjinja rule for generating all individual project detail pages.
'''
template = env.get_template('_project-detail-page.html')
outdir = path.join(env.outpath, 'project')
if not path.exists(outdir):
mkdir(outdir)
for index, project in enumerate(PROJECTS):
filename = '%s.html' % slugify(project['title'])
template.stream(project=project, **kwargs).\
dump(path.join(outdir, filename))
def render_coaching_detail_pages(env, template, **kwargs):
'''
staticjinja rule for generating all individual coaching detail pages.
'''
template = env.get_template('_coaching-detail-page.html')
for index, coaching_class in enumerate(COACHING):
filename = '%s-detail.html' % slugify(coaching_class['name'])
template.stream(coaching_class=coaching_class, **kwargs).\
dump(path.join(env.outpath, filename))
def deploy():
'''
Deploy the site.
'''
subprocess.check_call(
'git subtree push --prefix site origin gh-pages',
shell=True
)
site = make_site(
filters=filters,
outpath=outputpath,
contexts=[
(r'.*.html', loadAcademyData),
(r'.*.custom', loadAcademyData),
],
rules=[
(r'coaching-detail-pages.custom', render_coaching_detail_pages),
(r'project-detail-pages.custom', render_project_detail_pages),
],
searchpath=searchpath,
staticpaths=['static', '../data'],
)
manager = Manager(
site_name='govlabacademy.org',
site=site,
sass_src_path=path.join(ROOT_DIR, 'sass', 'styles.scss'),
sass_dest_path=path.join(searchpath, 'static', 'styles',
'styles.css')
)
argh.add_commands(manager.parser, [deploy])
if __name__ == '__main__':
manager.run()