/
build.py
executable file
·122 lines (86 loc) · 3.24 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
# -*- coding: utf-8 -*-
from os import path, getcwd, makedirs, listdir, remove
from sys import argv
from yaml import load
from shutil import rmtree
from slugify import slugify
from datetime import date, datetime
from staticjinja import make_site
from unidecode import unidecode
_AUTO_RELOAD = True
_TODAY = date.today()
# Define constants for the deployment.
_SEARCHPATH = path.join(getcwd(), 'templates')
_OUTPUTPATH = path.join(getcwd(), 'site')
# Load the data we want to use in the templates.
_EVENTS = path.join(getcwd(), 'data/events.yaml')
_PROJECTS = path.join(getcwd(), 'data/projects.yaml')
_TEAM = path.join(getcwd(), 'data/team.yaml')
_FUNDERS = path.join(getcwd(), 'data/funders.yaml')
_SLUG = lambda x: slugify(unidecode(x.lower()) if x else '')
def filters():
return {'slug': _SLUG}
def context():
dic = {}
dic['events'] = load(open(_EVENTS))
dic['projects'] = load(open(_PROJECTS))
dic['team'] = load(open(_TEAM))
dic['funders'] = load(open(_FUNDERS))
dic['events_slider_counter'] = 0
dic['projects_slider_counter'] = 0
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 cleanup():
# Remove templates that are no longer needed.
for filename in listdir(_SEARCHPATH):
filepath = '%s/%s' % (_SEARCHPATH, filename)
if filename.startswith('project-') and path.isfile(filepath):
remove(filepath)
# Clean the output folder.
if path.exists(_OUTPUTPATH):
rmtree(_OUTPUTPATH)
makedirs(_OUTPUTPATH)
def create_custom_templates(projects):
template = open('%s/project.html' % _SEARCHPATH).read()
for index, project in enumerate(projects):
filename = _SLUG(project['title'])
new_file = open('%s/project-%s.html' % (_SEARCHPATH, filename), 'w+')
new_page = template.replace('projects[0]', 'projects[%d]' % index)
new_file.write(new_page)
new_file.close()
if __name__ == '__main__':
auto = _AUTO_RELOAD
ctxt = context()
site = {}
cleanup()
create_custom_templates(ctxt['projects'])
# Accept CLI parameter to turn the auto reloader on and off.
if len(argv) == 2:
arg = argv[1].lower()
if arg in ['0', 'false', 'off', 'no']:
auto = False
elif arg in ['1', 'true', 'on', 'yes']:
auto = True
site['filters'] = filters()
site['outpath'] = _OUTPUTPATH
site['contexts'] = [(r'.*.html', lambda: ctxt)]
site['searchpath'] = _SEARCHPATH
site['staticpaths'] = ['static']
make_site(**site).render(use_reloader=auto)