-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.py
232 lines (195 loc) · 8.86 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import json, os, shutil, urllib.parse, hashlib
from datetime import datetime
def sort_posts():
print("Standardizing dates and sorting posts.json by date...")
with open('posts.json', 'r+') as json_file:
data = json.load(json_file)
# Standardize dates in the posts
for post in data['posts']:
if 'date' in post:
# Split the date into components
year, month, day = post['date'].split('-')
# Zero-pad month and day if needed
month = month.zfill(2)
day = day.zfill(2)
# Reconstruct the standardized date
post['date'] = f"{year}-{month}-{day}"
# Sort posts in descending order by date
data['posts'].sort(key=lambda post: post['date'], reverse=True)
# Write the sorted and standardized data back into the JSON file (with tab indentation)
json_file.seek(0)
json.dump(data, json_file, indent='\t', separators=(',', ': '))
json_file.truncate()
def build_index():
sort_posts()
print("Using posts.json to build index.html...")
with open('index.html', 'r') as html_file:
lines = html_file.readlines()
state = "scanning" # Set initial state
with open('temp.html', 'w', encoding='utf-8') as file:
for line in lines:
# Get state based on file flags
if line.strip().startswith('<!-- ANNOUNCEMENTS START !-->'):
file.write(line)
state = "announcements"
elif line.strip().startswith('<!-- ANNOUNCEMENTS END !-->'):
state = "scanning"
elif line.strip().startswith('<!-- FEED START !-->'):
file.write(line)
state = "posts"
elif line.strip().startswith('<!-- FEED END !-->'):
state = "scanning"
# Execute based on state (replace content between start and end tags)
if state == "scanning":
# Copy exiting file
file.write(line)
elif state == "announcements":
# Write announcement to file
file.write(announcements_to_html())
state = "waiting" # do nothing
elif state == "posts":
# Write posts to file
file.write(json_to_html())
state = "waiting" # do nothing
# file.write("\n\t\t\t\t</div>\n\t\t\t</div>\n\t\t</div>\n\t</body>\n</html>")
shutil.move('temp.html', 'index.html')
def json_to_html():
with open('posts.json') as json_file:
data = json.load(json_file)
posts = data['posts']
html = ""
for post in posts:
date = datetime.strptime(f"{post['date']} {datetime.strptime(post['end_time'], '%I:%M%p').strftime('%H:%M')}", '%Y-%m-%d %H:%M')
start_time = post["start_time"]
end_time = post["end_time"]
cancelled = post["cancelled"]
# Header
html += f'\n\t\t\t\t\t<div class="post" data-isodate="{date}">\n'
html += '\t\t\t\t\t\t<div class="post-header">\n'
if date and start_time and end_time:
html += f'\t\t\t\t\t\t\t<div class="calendar-link noselect">\n'
html += f'\t\t\t\t\t\t\t\t<div class="circled-date">{date.day}</div>\n'
month = date.strftime('%B')
html += f'\t\t\t\t\t\t\t\t<div class="month-and-time">{month}<br>\n'
start_time = datetime.strptime(start_time, "%I:%M%p").strftime("%-I:%M%p").lower()
end_time = datetime.strptime(end_time, "%I:%M%p").strftime("%-I:%M%p").lower()
if start_time[-2:] != end_time[-2:]:
time_string = f"{start_time} - {end_time}"
else:
time_string = f"{start_time[:-2]} - {end_time}"
html += f'\t\t\t\t\t\t\t\t\t<div class="time">{time_string}</div>\n'
html += '\t\t\t\t\t\t\t\t</div>\n'
html += '\t\t\t\t\t\t\t</div>\n'
# Title
html += '\t\t\t\t\t\t\t<div class="title-and-subtitle-wrapper">\n'
html += '\t\t\t\t\t\t\t\t<div class="title-and-subtitle">\n'
html += f'\t\t\t\t\t\t\t\t\t<div class="title">'
if cancelled:
html += '(Cancelled) '
html += f'{post["title"]}</div>\n'
html += f'\t\t\t\t\t\t\t\t\t<div class="subtitle">{post["subtitle"]}</div>\n'
html += '\t\t\t\t\t\t\t\t</div>\n'
html += '\t\t\t\t\t\t\t</div>\n'
html += '\t\t\t\t\t\t\t<div class="x-box" onClick="togglePostOpened(this)">\n'
html += '\t\t\t\t\t\t\t\t<div class="x"></div>\n'
html += '\t\t\t\t\t\t\t</div>\n'
html += '\t\t\t\t\t\t</div>\n'
# Image
html += '\t\t\t\t\t\t<div class="closeable">\n'
html += '\t\t\t\t\t\t\t<div class="post-body">\n'
if post["image"]:
html += f'\t\t\t\t\t\t\t\t<img class="post-image" src="/resources/images/{post["image"]}" loading="lazy">\n'
html += f'\t\t\t\t\t\t\t\t<div class="post-text"'
if post["image"]:
html += '>'
else:
html += ' style="width: 100%">'
# Description
if cancelled:
html += f'<s>{post["description"]}</s>\n'
html += '\t\t\t\t\t\t\t\t\t<br><p><b>This event has been cancelled.</b></p>\n'
else:
html += f'{post["description"]}\n'
# Event details (time, location, cost)
if not cancelled:
html += '\t\t\t\t\t\t\t\t\t<p>\n'
if date and start_time and end_time:
formatted_date_str = f"{date.strftime('%A')}, {date.strftime('%B')} {date.day}{date_suffix(date.day)}"
html += f'\t\t\t\t\t\t\t\t\t<b>Time</b>: {formatted_date_str} from {time_string}<br>\n'
location_str = ""
if post["offsite_location"]:
location_str = post["offsite_location"]
elif post["offered_in_person"] and post["offered_online"]:
location_str = 'Online and in person at Hacksburg; <a href="https://www.google.com/maps/dir/?api=1&destination=Hacksburg+Blacksburg,+VA" target="_blank">1872 Pratt Drive Suite 1620</a>.<br>\n\t\t\t\t\t\t\t\t\tEnter through the covered double doors at the back of the building.'
elif post["offered_in_person"]:
location_str = 'In person at Hacksburg; <a href="https://www.google.com/maps/dir/?api=1&destination=Hacksburg+Blacksburg,+VA" target="_blank">1872 Pratt Drive Suite 1620</a>. Enter through the covered double doors at the back of the building.'
elif post["offered_online"]:
location_str = 'Online only.'
if location_str:
html += f'\t\t\t\t\t\t\t\t\t<b>Place</b>: {location_str}<br>\n'
if post["offered_online"]:
html += '\t\t\t\t\t\t\t\t\t<b>URL</b>: '
html += '<a href="https://meet.hacksburg.org/class" target="_blank">meet.hacksburg.org/class</a><br>\n'
if post["member_price"] == 0 and post["non_member_price"] == 0:
html += '\t\t\t\t\t\t\t\t\t<b>Cost</b>: Free!\n'
elif post["member_price"] == 0:
html += f'\t\t\t\t\t\t\t\t\t<b>Cost</b>: Free for Hacksburg members; ${post["non_member_price"]} for non-members. Pay in person or online at <a href="https://paypal.me/hacksburg" target="_blank">paypal.me/hacksburg</a>.\n'
elif post["member_price"] == post["non_member_price"]:
html += f'\t\t\t\t\t\t\t\t\t<b>Cost</b>: ${post["non_member_price"]}. Pay in person or online at <a href="https://paypal.me/hacksburg" target="_blank">paypal.me/hacksburg</a>.\n'
else:
html += f'\t\t\t\t\t\t\t\t\t<b>Cost</b>: ${post["member_price"]} for Hacksburg members; ${post["non_member_price"]} for non-members. Pay in person or online at <a href="https://paypal.me/hacksburg" target="_blank">paypal.me/hacksburg</a>.\n'
html += '\t\t\t\t\t\t\t\t\t</p>\n'
# Meetup/RSVP link
if post["meetup_link"]:
if not cancelled:
html += f'\t\t\t\t\t\t\t\t\t<a class="button rsvp-button" href="{post["meetup_link"]}" target="_blank">RSVP on Meetup</a>\n'
html += '\t\t\t\t\t\t\t\t\t<div class="below-button-text">\n'
subject = f'RSVP for {post["title"]}'
body = f'I am confirming my RSVP for \"{post["title"]}\" on {formatted_date_str} from {time_string}.'
html += f'\t\t\t\t\t\t\t\t\t\tor <a href="{build_mailto(subject, body)}" target="_blank">RSVP by Email</a>\n'
html += '\t\t\t\t\t\t\t\t\t</div>\n'
else:
html += f'\t\t\t\t\t\t\t\t\t<a class="button rsvp-button" href="{post["meetup_link"]}" target="_blank">View on Meetup</a>\n'
html += '\t\t\t\t\t\t\t\t</div>\n'
html += '\t\t\t\t\t\t\t</div>\n'
html += '\t\t\t\t\t\t</div>\n'
html += '\t\t\t\t\t</div>\n'
return html
def announcements_to_html():
with open('posts.json') as json_file:
data = json.load(json_file)
posts = data['announcements']
html = ""
for post in posts:
html += f'\n\t\t\t\t\t<div class="post">\n'
html += '\t\t\t\t\t\t<div class="post-header">\n'
html += '\t\t\t\t\t\t\t<div class="title-and-subtitle">\n'
html += f'\t\t\t\t\t\t\t\t<div class="title">{post["title"]}</div>\n'
html += f'\t\t\t\t\t\t\t\t<div class="subtitle">{post["subtitle"]}</div>\n'
html += '\t\t\t\t\t\t\t</div>\n'
html += '\t\t\t\t\t\t\t<div class="info">ⓘ</div>\n'
html += '\t\t\t\t\t\t</div>\n'
html += '\t\t\t\t\t\t<div class="post-body">\n'
html += f'\t\t\t\t\t\t\t<div class="post-text" style="width: 100%">{post["description"]}</div>\n'
html += '\t\t\t\t\t\t</div>\n'
html += '\t\t\t\t\t</div>\n'
return html
def date_suffix(day):
if 11 <= day <= 13:
return "th"
last_digit = day % 10
if last_digit == 1:
return "st"
elif last_digit == 2:
return "nd"
elif last_digit == 3:
return "rd"
else:
return "th"
def build_mailto(subject, body):
subject = urllib.parse.quote(subject)
body = urllib.parse.quote(body)
return f'mailto:rsvp@hacksburg.org?subject={subject}&body={body}'
if __name__ == "__main__":
build_index()
print("Build complete!")