forked from prbc/farese
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-text-directory.py
83 lines (62 loc) · 2.47 KB
/
generate-text-directory.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
# A script to generate a webpage for each state/country, based on a JSON file.
import json
from itertools import groupby
def region_data():
# Load up our mappings from ISO country codes to display strings
# Can potentially replace with something like pycountry
with open('regions.json') as region_file:
region_data = json.load(region_file)
return region_data
def region_display_name(region_code):
for region in region_data():
if region['code'] == region_code:
return region['display-name']
def region_file_name(region_code):
for region in region_data():
if region['code'] == region_code:
return region['file-name']
# Generates an html page for the given region based on the given church JSON.
# If this gets complex we should think about using something like Jinja2.
def generate_html(region_code, churches):
# Load up the template
with open('text-dir-template.htm') as template_file:
template = template_file.read()
# Insert the title of the country
template = template.replace('{% REGION_NAME %}', region_display_name(region_code))
churches_html = ""
for church in churches:
churches_html += """
<tr>
<td>
<hr WIDTH="100%%"></td>
</tr>
<tr>
<td><b><font face="Calibri">%s </font></b>
<br><font face="Calibri">%s</font>
<br><font face="Calibri">%s</font>
<br><font face="Calibri"><a href="%s">%s</a></font></td>
</tr>
""" % (
church['properties']['name'],
church['properties']['address'],
church['properties']['note'],
church['properties']['website'],
church['properties']['website']
)
template = template.replace('{% CHURCHES %}', churches_html)
return template.encode('utf-8')
def main():
# Load up the church data
with open('map/data.json') as json_string:
data = json.load(json_string)
all_churches = data['features']
# Group churches by region
for (region, region_churches) in groupby(all_churches, lambda x: x['properties']['region']):
print('Generating html for region: %s' % region_display_name(region))
# Generate a page for this region.
html = generate_html(region, region_churches)
# Write html to a file.
with open('rbcd/%s.htm' % (region_file_name(region)), 'w') as outfile:
outfile.write(html)
if __name__ == "__main__":
main()