-
Notifications
You must be signed in to change notification settings - Fork 47
/
publish.py
executable file
·379 lines (303 loc) · 11.3 KB
/
publish.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/python3
import os, sys, datetime
PRE_HEADER = """
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<style>
@media (prefers-color-scheme: dark) {
body {
background-color: #1c1c1c;
color: white;
}
.markdown-body table tr {
background-color: #1c1c1c;
}
.markdown-body table tr:nth-child(2n) {
background-color: black;
}
}
</style>
"""
HEADER_TEMPLATE = """
<link rel="stylesheet" type="text/css" href="$root/css/common-vendor.b8ecfc406ac0b5f77a26.css">
<link rel="stylesheet" type="text/css" href="$root/css/fretboard.f32f2a8d5293869f0195.css">
<link rel="stylesheet" type="text/css" href="$root/css/pretty.0ae3265014f89d9850bf.css">
<link rel="stylesheet" type="text/css" href="$root/css/pretty-vendor.83ac49e057c3eac4fce3.css">
<link rel="stylesheet" type="text/css" href="$root/css/global.css">
<link rel="stylesheet" type="text/css" href="$root/css/misc.css">
<script type="text/x-mathjax-config">
<script>
MathJax = {
tex: {
inlineMath: [['$', '$'], ['\\(', '\\)']]
},
svg: {
fontCache: 'global',
}
};
</script>
<script type="text/javascript" id="MathJax-script" async
src="$root/scripts/tex-svg.js">
</script>
<style>
</style>
<div id="doc" class="container-fluid markdown-body comment-enabled" data-hard-breaks="true">
<div id="color-mode-switch">
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M12 3v1m0 16v1m9-9h-1M4 12H3m15.364 6.364l-.707-.707M6.343 6.343l-.707-.707m12.728 0l-.707.707M6.343 17.657l-.707.707M16 12a4 4 0 11-8 0 4 4 0 018 0z" />
</svg>
<input type="checkbox" id="switch" />
<label for="switch">Dark Mode Toggle</label>
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M20.354 15.354A9 9 0 018.646 3.646 9.003 9.003 0 0012 21a9.003 9.003 0 008.354-5.646z" />
</svg>
</div>
"""
TOGGLE_COLOR_SCHEME_JS = """
<script type="text/javascript">
// Update root html class to set CSS colors
const toggleDarkMode = () => {
const root = document.querySelector('html');
root.classList.toggle('dark');
}
// Update local storage value for colorScheme
const toggleColorScheme = () => {
const colorScheme = localStorage.getItem('colorScheme');
if (colorScheme === 'light') localStorage.setItem('colorScheme', 'dark');
else localStorage.setItem('colorScheme', 'light');
}
// Set toggle input handler
const toggle = document.querySelector('#color-mode-switch input[type="checkbox"]');
if (toggle) toggle.onclick = () => {
toggleDarkMode();
toggleColorScheme();
}
// Check for color scheme on init
const checkColorScheme = () => {
const colorScheme = localStorage.getItem('colorScheme');
// Default to light for first view
if (colorScheme === null || colorScheme === undefined) localStorage.setItem('colorScheme', 'light');
// If previously saved to dark, toggle switch and update colors
if (colorScheme === 'dark') {
toggle.checked = true;
toggleDarkMode();
}
}
checkColorScheme();
</script>
"""
RSS_LINK = """
<link rel="alternate" type="application/rss+xml" href="{}/feed.xml" title="{}">
"""
TITLE_TEMPLATE = """
<br>
<h1 style="margin-bottom:7px"> {0} </h1>
<small style="float:left; color: #888"> {1} </small>
<small style="float:right; color: #888"><a href="{2}/index.html">See all posts</a></small>
<br> <br> <br>
<title> {0} </title>
"""
TOC_TITLE_TEMPLATE = """
<title> {0} </title>
<br>
<center><h1 style="border-bottom:0px"> {0} </h1></center>
"""
FOOTER = """ </div> """
TOC_START = """
<br>
<ul class="post-list" style="padding-left:0">
"""
TOC_END = """ </ul> """
TOC_ITEM_TEMPLATE = """
<li>
<span class="post-meta">{}</span>
<h3 style="margin-top:12px">
<a class="post-link" href="{}">{}</a>
</h3>
</li>
"""
TWITTER_CARD_TEMPLATE = """
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="{}" />
<meta name="twitter:image" content="{}" />
"""
RSS_ITEM_TEMPLATE = """
<item>
<title>{title}</title>
<link>{link}</link>
<guid>{link}</guid>
<pubDate>{pub_date}</pubDate>
<description>{description}</description>
</item>
"""
RSS_MAIN_TEMPLATE = """
<?xml version="1.0" ?>
<rss version="2.0">
<channel>
<title>{title}</title>
<link>{link}</link>
<description>{title}</description>
<image>
<url>{icon}</url>
<title>{title}</title>
<link>{link}</link>
</image>
{items}
</channel>
</rss>
"""
def extract_metadata(fil, filename=None):
metadata = {}
if filename:
assert filename[-3:] == '.md'
metadata["filename"] = filename[:-3]+'.html'
while 1:
line = fil.readline()
if line and line[0] == '[' and ']' in line:
key = line[1:line.find(']')]
value_start = line.find('(')+1
value_end = line.rfind(')')
if key in ('category', 'categories'):
metadata['categories'] = set([
x.strip().lower() for x in line[value_start:value_end].split(',')
])
assert '' not in metadata['categories']
else:
metadata[key] = line[value_start:value_end]
else:
break
return metadata
def metadata_to_path(global_config, metadata):
return os.path.join(
global_config.get('posts_directory', 'posts'),
metadata['date'],
metadata['filename']
)
def generate_feed(global_config, metadatas):
def get_link(route):
return global_config['domain'] + "/" + route
def get_date(date_text):
year, month, day = (int(x) for x in date_text.split('/'))
date = datetime.date(year, month, day)
return date.strftime('%a, %d %b %Y 00:00:00 +0000')
def get_item(metadata):
return RSS_ITEM_TEMPLATE.format(
title=metadata['title'],
link=get_link('/'.join([global_config['posts_directory'], metadata['date'], metadata['filename']])),
pub_date=get_date(metadata['date']), description=''
)
return RSS_MAIN_TEMPLATE.strip().format(
title=global_config['title'],
link=get_link(''),
icon=global_config['icon'],
items="\n".join(map(get_item, metadatas))
)
def make_twitter_card(title, global_config):
return TWITTER_CARD_TEMPLATE.format(title, global_config['icon'])
def defancify(text):
return text \
.replace("’", "'") \
.replace('“', '"') \
.replace('”', '"') \
.replace('…', '...') \
def make_categories_header(categories, root_path):
o = ['<center><hr>']
for category in categories:
template = '<span class="toc-category" style="font-size:{}%"><a href="{}/categories/{}.html">{}</a></span>'
o.append(template.format(min(100, 900 // len(category)), root_path, category, category.capitalize()))
o.append('<hr></center>')
return '\n'.join(o)
def get_printed_date(metadata):
year, month, day = metadata['date'].split('/')
month = 'JanFebMarAprMayJunJulAugSepOctNovDec'[int(month)*3-3:][:3]
return year + ' ' + month + ' ' + day
def make_toc_item(global_config, metadata, root_path):
link = metadata_to_path(global_config, metadata)
return TOC_ITEM_TEMPLATE.format(get_printed_date(metadata), root_path + '/' + link, metadata['title'])
def make_toc(toc_items, global_config, all_categories, category=None):
if category:
title = category.capitalize()
root_path = '..'
else:
title = global_config['title']
root_path = '.'
return (
PRE_HEADER +
RSS_LINK.format(root_path, title) +
HEADER_TEMPLATE.replace('$root', root_path) +
TOGGLE_COLOR_SCHEME_JS +
make_twitter_card(title, global_config) +
TOC_TITLE_TEMPLATE.format(title) +
make_categories_header(all_categories, root_path) +
TOC_START +
''.join(toc_items) +
TOC_END
)
if __name__ == '__main__':
# Get blog config
global_config = extract_metadata(open('config.md'))
# Special case: '--sync' option
if '--sync' in sys.argv:
os.system('rsync -av site/. {}:{}'.format(global_config['server'], global_config['website_root']))
sys.exit()
# Normal case: process each provided file
for file_location in sys.argv[1:]:
filename = os.path.split(file_location)[1]
print("Processing file: {}".format(filename))
# Extract path
file_data = open(file_location).read()
metadata = extract_metadata(open(file_location), filename)
path = metadata_to_path(global_config, metadata)
# Generate the html file
options = metadata.get('pandoc', '')
os.system('pandoc -o /tmp/temp_output.html {} {}'.format(file_location, options))
root_path = '../../../..'
total_file_contents = (
PRE_HEADER +
RSS_LINK.format(root_path, metadata['title']) +
HEADER_TEMPLATE.replace('$root', root_path) +
TOGGLE_COLOR_SCHEME_JS +
make_twitter_card(metadata['title'], global_config) +
TITLE_TEMPLATE.format(metadata['title'], get_printed_date(metadata), root_path) +
defancify(open('/tmp/temp_output.html').read()) +
FOOTER
)
print("Path selected: {}".format(path))
# Make sure target directory exists
truncated_path = os.path.split(path)[0]
os.system('mkdir -p {}'.format(os.path.join('site', truncated_path)))
# Put it in the desired location
out_location = os.path.join('site', path)
open(out_location, 'w').write(total_file_contents)
# Reset ToC
metadatas = []
categories = set()
for filename in os.listdir('posts'):
if filename[-4:-1] != '.sw':
metadatas.append(extract_metadata(open(os.path.join('posts', filename)), filename))
categories = categories.union(metadatas[-1]['categories'])
categories = sorted(categories)
print("Detected categories: {}".format(' '.join(categories)))
sorted_metadatas = sorted(metadatas, key=lambda x: x['date'], reverse=True)
feed = generate_feed(global_config, sorted_metadatas)
os.system('mkdir -p {}'.format(os.path.join('site', 'categories')))
print("Building tables of contents...")
homepage_toc_items = [
make_toc_item(global_config, metadata, '.') for metadata in sorted_metadatas if
global_config.get('homepage_category', '') in metadata['categories'].union({''})
]
for category in categories:
category_toc_items = [
make_toc_item(global_config, metadata, '..') for metadata in sorted_metadatas if
category in metadata['categories']
]
toc = make_toc(category_toc_items, global_config, categories, category)
open(os.path.join('site', 'categories', category+'.html'), 'w').write(toc)
open('site/feed.xml', 'w').write(feed)
open('site/index.html', 'w').write(make_toc(homepage_toc_items, global_config, categories))
# Copy CSS and scripts files
this_file_directory = os.path.dirname(__file__)
os.system('cp -r {} site/'.format(os.path.join(this_file_directory, 'css')))
os.system('cp -r {} site/'.format(os.path.join(this_file_directory, 'scripts')))
os.system('rsync -av images site/')