-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
website.py
276 lines (228 loc) · 6.35 KB
/
website.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
"""
This module emits the content for your archive.
It emits HTML, and YAML, mostly by calling
into other modules.
As of April 2021, the generated html pages can be hosted simply with `python -m
http.server`.
This module is probably the most likely module to be forked if
you have unique requirements for how your archive should look.
If you are interested in porting this system away from Python to your
language of choice, this is probably the best place to start.
"""
from pathlib import Path
from distutils.dir_util import copy_tree
import html
from shutil import copyfile
from .url import (
sanitize_stream,
sanitize_topic,
)
from .files import (
open_main_page,
open_stream_topics_page,
open_topic_messages_page,
read_zulip_messages_for_topic,
read_zulip_stream_info,
)
from .html import (
format_message_html,
last_updated_footer_html,
topic_page_links_html,
stream_list_page_html,
topic_list_page_html,
)
from .url import (
archive_stream_url,
)
def to_topic_page_head_html(title):
return f'<html>\n<head><meta charset="utf-8"><title>{title}</title></head>\n'
def build_website(
json_root,
md_root,
site_url,
html_root,
title,
zulip_url,
zulip_icon_url,
repo_root,
page_head_html,
page_footer_html,
):
stream_info = read_zulip_stream_info(json_root)
streams = stream_info["streams"]
date_footer_html = last_updated_footer_html(stream_info)
write_main_page(
md_root,
site_url,
html_root,
title,
streams,
date_footer_html,
page_head_html,
page_footer_html,
)
write_css(md_root)
for stream_name in streams:
print("building: ", stream_name)
stream_data = streams[stream_name]
topic_data = stream_data["topic_data"]
write_stream_topics(
md_root,
site_url,
html_root,
title,
stream_name,
stream_data,
date_footer_html,
page_head_html,
page_footer_html,
)
for topic_name in topic_data:
write_topic_messages(
json_root,
md_root,
site_url,
html_root,
title,
zulip_url,
zulip_icon_url,
stream_name,
streams[stream_name],
topic_name,
date_footer_html,
page_head_html,
page_footer_html,
)
# Copy the entire content of <repo_root>/assets into md_root.
# We use copy_tree from distutils instead of shutil.copytree so that it
# doesn't raise an error when assets/ already exists inside the md_root.
copy_tree(str(Path(repo_root) / "assets"), str(Path(md_root) / "assets"))
# Copy .nojekyll into md_root as well.
copyfile(str(Path(repo_root) / ".nojekyll"), str(Path(md_root) / ".nojekyll"))
# writes the index page listing all streams.
# `streams`: a dict mapping stream names to stream json objects as described in the header.
def write_main_page(
md_root,
site_url,
html_root,
title,
streams,
date_footer_html,
page_head_html,
page_footer_html,
):
"""
The main page in our website lists streams:
Streams:
general (70 topics)
announce (42 topics)
"""
outfile = open_main_page(md_root)
content_html = stream_list_page_html(streams)
outfile.write(page_head_html)
outfile.write(content_html)
outfile.write(date_footer_html)
outfile.write(page_footer_html)
outfile.close()
def write_stream_topics(
md_root,
site_url,
html_root,
title,
stream_name,
stream,
date_footer_html,
page_head_html,
page_footer_html,
):
"""
A stream page lists all topics for the stream:
Stream: social
Topics:
lunch (4 messages)
happy hour (1 message)
"""
sanitized_stream_name = sanitize_stream(stream_name, stream["id"])
outfile = open_stream_topics_page(md_root, sanitized_stream_name)
stream_url = archive_stream_url(site_url, html_root, sanitized_stream_name)
topic_data = stream["topic_data"]
content_html = topic_list_page_html(stream_name, stream_url, topic_data)
outfile.write(page_head_html)
outfile.write(content_html)
outfile.write(date_footer_html)
outfile.write(page_footer_html)
outfile.close()
def write_topic_messages(
json_root,
md_root,
site_url,
html_root,
title,
zulip_url,
zulip_icon_url,
stream_name,
stream,
topic_name,
date_footer_html,
page_head_html,
page_footer_html,
):
"""
Writes the topics page, which lists all messages
for one particular topic within a stream:
Stream: social
Topic: lunch
Alice:
I want pizza!
Bob:
No, let's get tacos!
"""
stream_id = stream["id"]
sanitized_stream_name = sanitize_stream(stream_name, stream_id)
sanitized_topic_name = sanitize_topic(topic_name)
messages = read_zulip_messages_for_topic(
json_root, sanitized_stream_name, sanitized_topic_name
)
outfile = open_topic_messages_page(
md_root,
sanitized_stream_name,
sanitized_topic_name,
)
topic_links = topic_page_links_html(
site_url,
html_root,
zulip_url,
sanitized_stream_name,
sanitized_topic_name,
stream_name,
topic_name,
)
# We use a topic-specific title instead of `page_head_html` to improve
# search engine indexing.
outfile.write(
to_topic_page_head_html(
html.escape(topic_name) + " · " + html.escape(stream_name) + " · " + title
)
)
outfile.write(topic_links)
outfile.write(
f'\n<head><link href="{html.escape(site_url)}/style.css" rel="stylesheet"></head>\n'
)
for msg in messages:
msg_html = format_message_html(
site_url,
html_root,
zulip_url,
zulip_icon_url,
stream_name,
stream_id,
topic_name,
msg,
)
outfile.write(msg_html)
outfile.write("\n\n")
outfile.write(date_footer_html)
outfile.write(page_footer_html)
outfile.close()
def write_css(md_root):
copyfile("style.css", md_root / "style.css")