-
Notifications
You must be signed in to change notification settings - Fork 7
/
build.py
executable file
·167 lines (128 loc) · 4.7 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
# -*- coding: utf-8 -*-
from os import path, getcwd, listdir, remove, chdir
from sys import argv
import subprocess
from yaml import load
from shutil import rmtree
from slugify import slugify
from datetime import date, datetime
from unidecode import unidecode
import staticjinja
import argh
from watchdog.events import FileSystemEventHandler
from govlabstatic.cli import Manager
_TODAY = date.today()
# Define constants for the deployment.
_SASSPATH = path.join(getcwd(), 'sass')
_SEARCHPATH = path.join(getcwd(), 'templates')
_OUTPUTPATH = path.join(getcwd(), 'site')
_DATAPATH = path.join(getcwd(), 'data')
# Load the data we want to use in the templates.
_EVENTS = path.join(_DATAPATH, 'events.yaml')
_PROJECTS = path.join(_DATAPATH, 'projects.yaml')
_TEAM = path.join(_DATAPATH, 'team.yaml')
_GLOBAL_ADVISORY_COUNCIL = path.join(_DATAPATH, 'advisory-council.yaml')
_FUNDERS = path.join(_DATAPATH, 'funders.yaml')
_SMARTER_STATE_STUDIES = path.join(_DATAPATH, 'smarter-state-studies.yaml')
_PUBLICATIONS = path.join(_DATAPATH, 'publications.yaml')
_SLUG = lambda x: slugify(unicode(unidecode(unicode(x).lower())) if x else u'')
def filters():
return {'slug': _SLUG}
def clean():
'''
Clean the output folder.
'''
if path.exists(_OUTPUTPATH):
rmtree(_OUTPUTPATH)
def render_project_detail_pages(env, template, **kwargs):
'''
staticjinja rule for generating all individual project detail pages.
'''
template = env.get_template('_project.html')
for index, project in enumerate(kwargs['projects']):
out = 'project-%s.html' % (_SLUG(project['title']),)
template.stream(project=project, **kwargs).\
dump(path.join(env.outpath, out))
class ReloadingContext(FileSystemEventHandler):
'''
Regenerates a template context, and the static site, whenever files
in the data directory change.
'''
path = _DATAPATH
def __init__(self):
FileSystemEventHandler.__init__(self)
self.cache_context()
def get(self):
return self._cached_context
def add_to(self, manager):
self.site = manager.site
manager.watcher.observer.schedule(self, self.path)
def on_any_event(self, event):
self.cache_context()
self.site.render_templates(self.site.templates)
def cache_context(self):
self._cached_context = self.build_context()
def build_context(self):
dic = {}
dic['events'] = load(open(_EVENTS))
dic['projects'] = load(open(_PROJECTS))
dic['team'] = load(open(_TEAM))
dic['global_advisory_council'] = load(open(_GLOBAL_ADVISORY_COUNCIL))
dic['funders'] = load(open(_FUNDERS))
dic['studies'] = load(open(_SMARTER_STATE_STUDIES))
dic['publications'] = load(open(_PUBLICATIONS))
dic['events_slider_counter'] = 0
dic['projects_slider_counter'] = 0
dic['publications_slider_counter'] = 3
for x in dic['events']:
x['date'] = datetime.strptime(x['date'], '%m-%d-%Y').date()
x['has_passed'] = x['date'] < _TODAY
x['is_featured'] = str(x.get('is_featured', '')).lower()
if x['is_featured'] in ['1', 'true', 'yes', 'on']:
x['is_featured'] = True
if x['date'] >= _TODAY:
dic['events_slider_counter'] += 1
else:
x['is_featured'] = False
for x in dic['projects']:
x['is_featured'] = str(x.get('is_featured', '')).lower()
if x['is_featured'] in ['1', 'true', 'yes', 'on']:
x['is_featured'] = True
dic['projects_slider_counter'] += 1
else:
x['is_featured'] = False
dic['events'].sort(key=lambda x: x['date'])
return dic
def deploy():
'''
Deploy the site to production.
'''
subprocess.check_call(
'git subtree push --prefix site origin gh-pages',
shell=True
)
if __name__ == '__main__':
context = ReloadingContext()
site = staticjinja.make_site(
filters=filters(),
outpath=_OUTPUTPATH,
contexts=[
(r'.*.html', context.get),
(r'project-detail-pages.custom', context.get),
],
rules=[
(r'project-detail-pages.custom', render_project_detail_pages)
],
searchpath=_SEARCHPATH,
staticpaths=['static']
)
manager = Manager(
sass_src_path=path.join(_SASSPATH, 'styles.scss'),
sass_dest_path=path.join(_SEARCHPATH, 'static', 'styles',
'styles.css'),
site=site,
site_name='www.thegovlab.org',
)
context.add_to(manager)
argh.add_commands(manager.parser, [deploy, clean])
manager.run()