-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathjekyll.py
239 lines (192 loc) · 5.93 KB
/
jekyll.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
'''
This module emits the content for your archive.
It emits markdown, HTML, and YAML, mostly by calling
into other modules.
As I write this today (December 2019), we are mostly testing with
Jekyll, so the output is geared toward a Jekyll system, which
is what GitHub Pages uses, too. We will try to make this more
flexible as we go.
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 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 .front_matter import (
write_main_page_header,
write_stream_topics_header,
write_topic_messages_header,
)
from .html import (
format_message,
homepage_link,
last_updated_footer,
topic_page_links,
)
from .markdown import (
stream_list_page,
topic_list_page,
)
from .url import (
archive_stream_url,
)
def build_website(json_root, md_root, site_url, html_root, title, zulip_url, zulip_icon_url):
stream_info = read_zulip_stream_info(json_root)
streams = stream_info['streams']
date_footer = last_updated_footer(stream_info)
write_main_page(md_root, site_url, html_root, title, streams, date_footer, zulip_icon_url)
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,
zulip_icon_url
)
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,
)
# 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, zulip_icon_url):
'''
The main page in our website lists streams:
Streams:
general (70 topics)
announce (42 topics)
'''
outfile = open_main_page(md_root)
write_main_page_header(outfile, html_root, title)
content = stream_list_page(streams)
outfile.write(f'\n{homepage_link(site_url, zulip_icon_url)}\n')
outfile.write(content)
outfile.write(date_footer)
outfile.close()
def write_stream_topics(md_root, site_url, html_root, title, stream_name, stream, date_footer, zulip_icon_url):
'''
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)
write_stream_topics_header(outfile, site_url, html_root, title, stream_name, stream)
stream_url = archive_stream_url(site_url, html_root, sanitized_stream_name)
topic_data = stream['topic_data']
content = topic_list_page(stream_name, stream_url, topic_data)
outfile.write(f'\n{homepage_link(site_url, zulip_icon_url)}\n')
outfile.write(content)
outfile.write(date_footer)
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,
):
'''
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,
)
write_topic_messages_header(
outfile,
site_url,
html_root,
zulip_url,
title,
stream_name,
stream_id,
topic_name,
)
topic_links = topic_page_links(
site_url,
html_root,
zulip_url,
sanitized_stream_name,
sanitized_topic_name,
stream_name,
topic_name,
)
outfile.write(f'\n{homepage_link(site_url, zulip_icon_url)}\n')
outfile.write(topic_links)
outfile.write(f'\n<head><link href="{site_url}/style.css" rel="stylesheet"></head>\n')
outfile.write('\n{% raw %}\n')
for msg in messages:
msg_html = format_message(
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('\n{% endraw %}\n')
outfile.write(date_footer)
outfile.close()
def write_css(md_root):
copyfile('style.css', md_root / 'style.css')
# escape | character with \|
def escape_pipes(s):
return s.replace('|','\|').replace(']','\]').replace('[','\[')