-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic-purity.py
More file actions
491 lines (406 loc) · 14.4 KB
/
static-purity.py
File metadata and controls
491 lines (406 loc) · 14.4 KB
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#!/usr/bin/env python3
######################################################################
# Copyright 2020 James Evans <jwhevans@hotmail.com>
#
# This file constitutes the program static-purity and is the static
# website generator for mnml.blog
#
# This file is free software and may be modified and/or redistribuited
# under the terms of the GNU General Public License by the Free
# Software Foundation.
#
# I built the software for my own learning and use. I hope you find it
# useful. This software comes with NO WARRANTY; without even an
# implied warranty of merchantability or fitness for a particular
# purpose. See the GNU General Public License for more details.
#
# There should be a copy of the GNU General Public License stored in
# any repository hosting this software. If you have not received one,
# please see <http:www.gnu.org/licenses/>
######################################################################
import os, sys, markdown as md, time
from datetime import date
VERSION = "2020-02-20"
PROJECT_NAME = "static-purity"
AUTHOR = "James Evans"
COPYRIGHT = ""
TODAY = date.today().strftime('%Y-%m-%d')
all_md_sorted_rev = []
default_dirs = ['css', 'drafts', 'html', 'img', 'md']
help_msg = """
COMMAND
static-purity.py
DESCRIPTION
static-purity scans a directory for markdown files that are new
or have been modified since the last time the program ran. It
then generates html files from those markdown files and creates
or modifies a directory structure to host a small blog.
Available options:
--build Build entire site. All files and
link structure are regenerated
--update Update only files that have been
added modified since last program run
(This option not available currently)
-h, --help View this page and exit
"""
available_options = ["-h", "--help", "--build", "--update"]
######################################################################
def main():
t0 = time.time()
print(f"Running {PROJECT_NAME} - v{VERSION}...")
if len(sys.argv) > 1 and sys.argv[1] == '--build':
# TODO: rebuild the site
print(f" Rebuilding {PROJECT_NAME}")
elif len(sys.argv) > 1 and sys.argv[1] == '--update':
# TODO: update the site
print(f" Updating {PROJECT_NAME}")
sys.exit(0)
else:
print(help_msg)
sys.exit(0)
# Verifying directory structure and setting up if needed
print(" Verifying directory structure...")
for dir in default_dirs:
if not os.path.exists(dir):
print(f""" Directory {dir} is missing. Creating...""")
os.makedirs(dir)
# Check if at least one markdown file and ./css/styles.css exist
# If not give the user an option to insert a template
# or to exit and create their own before running again
if not any(fnm.endswith('.md') for fnm in os.listdir('./md/')):
file_is_missing("Markdown")
if not os.path.exists('./css/styles.css'):
file_is_missing("CSS")
print(" Sanitizing file names...")
sanitize_filenames('./md/')
print(" Sorting md files...")
global all_md_sorted_rev
all_md_sorted_rev = sorted(os.listdir('./md/'), reverse=True)
print(" Converting markdown to html...")
md_to_html('./md/')
print(" Creating archive...")
create_archive()
t1 = time.time()
print(f"Execution complete. Time: {t1-t0:.4f} s")
######################################################################
def file_is_missing(fnm):
res = input(f"""
WARNING:
{fnm} files were not found. These files are necessary for {PROJECT_NAME}
to build your site. You may continue and a template file will be
created or you may quit and create {fnm} files first.
Continue? y/n: """)
if res == 'y' or res == 'Y':
print(f"Creating template {fnm}...")
create_missing_file(fnm)
pass
elif res == 'n' or res == 'N':
print("Exiting...")
sys.exit(0)
else:
print("Improper input. Exiting...")
sys.exit(0)
def sanitize_filenames(path):
fnms = os.listdir(path)
for fnm in fnms:
os.rename(os.path.join(path, fnm),
os.path.join(path, fnm.replace(' ', '-')))
def create_missing_file(fnm):
if fnm == "Markdown":
f = open('./md/' + TODAY + '-Template.md', 'w')
f.write(default_index_md)
f.close()
elif fnm == "CSS":
f = open('./css/styles.css', 'w')
f.write(default_styles_css)
f.close()
else:
print(f"WARNING: Could not create necessary file")
def md_to_html(path):
for md_file in all_md_sorted_rev:
if md_file[-2:] == 'md':
f = open(path + md_file, 'r')
html = md.markdown(f.read())
f.close()
title = str(md_file[9:-3]).title().replace('-', ' ')
create_html(html, md_file, title)
def create_html(content, name, title):
dir_depth = 1 # deepest directory level - all execept index
html_bef = html_before
html_aft = html_after
idx = all_md_sorted_rev.index(name)
if idx == 0:
# first record - this is the index
dir_depth = 0
html_bef = html_bef.replace('{{PREV_LINK}}', '←prev')
html_aft = html_aft.replace('{{PREV_LINK}}', '←prev')
if len(all_md_sorted_rev) > 1:
next_fnm = './html/' + all_md_sorted_rev[idx + 1][:-2] + 'html'
next_link = f"""<a href="{next_fnm}">next→</a>"""
html_bef = html_bef.replace('{{NEXT_LINK}}', next_link)
html_aft = html_aft.replace('{{NEXT_LINK}}', next_link)
else:
next_fnm = '../archive.html'
next_link = f"""<a href="{next_fnm}">next→</a>"""
html_bef = html_bef.replace('{{NEXT_LINK}}', next_link)
html_aft = html_aft.replace('{{NEXT_LINK}}', next_link)
if idx == 1:
# second record - prev must point to index
prev_fnm = '../index.html'
prev_link = f"""<a href="{prev_fnm}">←prev</a>"""
html_bef = html_bef.replace('{{PREV_LINK}}', prev_link)
html_aft = html_aft.replace('{{PREV_LINK}}', prev_link)
next_fnm = all_md_sorted_rev[idx + 1][:-2] + 'html'
next_link = f"""<a href="{next_fnm}">next→</a>"""
html_bef = html_bef.replace('{{NEXT_LINK}}', next_link)
html_aft = html_aft.replace('{{NEXT_LINK}}', next_link)
if idx > 1 and idx < len(all_md_sorted_rev) - 2:
# middle records - process normally
prev_fnm = all_md_sorted_rev[idx - 1][:-2] + 'html'
prev_link = f"""<a href="{prev_fnm}">←prev</a>"""
html_bef = html_bef.replace('{{PREV_LINK}}', prev_link)
html_aft = html_aft.replace('{{PREV_LINK}}', prev_link)
next_fnm = all_md_sorted_rev[idx + 1][:-2] + 'html'
next_link = f"""<a href="{next_fnm}">next→</a>"""
html_bef = html_bef.replace('{{NEXT_LINK}}', next_link)
html_aft = html_aft.replace('{{NEXT_LINK}}', next_link)
if idx == len(all_md_sorted_rev) - 2:
# last record - next must point to archive
prev_fnm = all_md_sorted_rev[idx - 1][:-2] + 'html'
prev_link = f"""<a href="{prev_fnm}">←prev</a>"""
html_bef = html_bef.replace('{{PREV_LINK}}', prev_link)
html_aft = html_aft.replace('{{PREV_LINK}}', prev_link)
next_fnm = '../archive.html'
next_link = f"""<a href="{next_fnm}">next→</a>"""
html_bef = html_bef.replace('{{NEXT_LINK}}', next_link)
html_aft = html_aft.replace('{{NEXT_LINK}}', next_link)
html_bef = html_bef.replace('{{PATH_TO_CSS}}', '../' * dir_depth + 'css/styles.css')
html_bef = html_bef.replace('{{PATH_TO_INDEX}}', '../' * dir_depth + 'index.html')
html_bef = html_bef.replace('{{PATH_TO_ARCHIVE}}', '../' * dir_depth + 'archive.html')
html_bef = html_bef.replace('{{PATH_TO_ABOUT}}', '../' * dir_depth + 'about.html')
html_bef = html_bef.replace('{{PROJECT_NAME}}', PROJECT_NAME)
html_bef = html_bef.replace('{{TITLE}}', title)
html_bef = html_bef.replace('{{AUTHOR}}', AUTHOR)
html_aft = html_aft.replace('{{COPYRIGHT}}', COPYRIGHT)
html_aft = html_aft.replace('{{MD_SOURCE}}', '../' * dir_depth + 'md/' + all_md_sorted_rev[idx])
if idx == 0:
f = open('./index.html', 'w')
else:
f = open('./html/' + name[:-2] + 'html', 'w')
f.write(html_bef)
f.write(content)
f.write(html_aft)
f.close()
def create_archive():
group = all_md_sorted_rev[0][:4]
with open('./archive.md', 'w') as f:
f.write(' * ' + group + '\n')
for md_file in all_md_sorted_rev:
idx = all_md_sorted_rev.index(md_file)
if not str(md_file).startswith('.'):
if idx == 0:
f.write(' * [' + md_file[9:-3].title().replace('-',' ')\
+ '](index.html)\n')
elif group == md_file[:4]:
f.write(' * [' + md_file[9:-3].title().replace('-',' ')\
+ '](./html/' + md_file[0:-2] + 'html)\n')
else:
idx = all_md_sorted_rev.index(md_file)
group = all_md_sorted_rev[idx][:4]
f.write(' * ' + group + '\n')
f.write(' * [' + md_file[9:-3].title().replace('-',' ')\
+ '](./html/' + md_file[0:-2] + 'html)\n')
with open('./archive.md', 'r') as f:
content = md.markdown(f.read())
f.close()
prev_fnm = './html/' + all_md_sorted_rev[len(all_md_sorted_rev) - 2][:-2] + 'html'
prev_link = f"""<a href="{prev_fnm}">←prev</a>"""
html_bef = html_before.replace('{{PREV_LINK}}', prev_link)
html_aft = html_after.replace('{{PREV_LINK}}', prev_link)
html_bef = html_bef.replace('{{NEXT_LINK}}', 'next→')
html_aft = html_aft.replace('{{NEXT_LINK}}', 'next→')
html_bef = html_bef.replace('{{PATH_TO_CSS}}', './css/styles.css')
html_bef = html_bef.replace('{{PATH_TO_INDEX}}', './index.html')
html_bef = html_bef.replace('{{PATH_TO_ARCHIVE}}', './archive.html')
html_bef = html_bef.replace('{{PATH_TO_ABOUT}}', './about.html')
html_bef = html_bef.replace('{{PROJECT_NAME}}', PROJECT_NAME)
html_bef = html_bef.replace('{{TITLE}}', 'Archive')
html_bef = html_bef.replace('{{AUTHOR}}', AUTHOR)
html_aft = html_aft.replace('{{COPYRIGHT}}', COPYRIGHT)
html_aft = html_aft.replace('{{MD_SOURCE}}', './archive.md')
with open('./archive.html', 'w') as f:
f.write(html_bef)
f.write(content)
f.write(html_aft)
f.close()
######################################################################
default_index_md = f"""<p class = "dateline">{TODAY}</p>
#{PROJECT_NAME}
This is a template file generated by {PROJECT_NAME}[1]
* add your own markdown files to the `./md/` directory of your site
to begin creating your blog.
this file will be overwritten automatically as you add your own posts
* an archive file will be generated each time you build the site
* an about page is not generated automatically in this release.
[1]: https://github.com/jwhevans/static-purity
"""
######################################################################
default_styles_css = """
* {
box-sizing: border-box;
}
html {
font-family: "Helvetica", "Arial", sans-serif;
}
body {
color: #222;
line-height: 1.25;
background-color: #fff;
}
#content {
max-width: 600px;
margin: auto;
}
#header {
font-weight: bold;
font-size: x-large;
text-align: center;
padding: 5px 10px 5px 10px;
}
#header a {
text-decoration: none;
}
#top-nav, #bottom-nav {
font-size: small;
display: flex;
padding: 5px 10px 5px 10px;
border-bottom: 1px solid #ddd;
}
.nav-right {
margin-left: auto;
}
.nav-middle {
margin-left: auto;
text-align: center;
}
.dateline {
font-size: small;
line-height: 0.25em;
}
#article-content {
padding: 5px 10px 5px 10px;
}
#footer {
padding: 10px;
font-size: small;
}
caption {
font-style: italic;
font-size: small;
color: #555;
}
a:link {
color: #3A4089;
}
table {
background-color: #eee;
padding-left: 2px;
border: 1px solid #d4d4d4;
border-collapse: collapse;
}
th {
background-color: #d4d4d4;
padding-right: 4px;
}
tr, td, th {
border: 2px solid #d4d4d4;
padding-left: 4px;
padding-right: 4px;
}
dt {
font-weight: bold;
}
code {
background-color: #eee;
}
pre {
background-color: #eee;
border: 1px solid #ddd;
padding-left: 6px;
padding-right: 2px;
padding-bottom: 5px;
padding-top: 5px;
}
blockquote {
padding-top: 2px;
padding-bottom: 2px;
padding-left: 16px;
padding-right: 16px;
}
blockquote code, blockquote pre {
background-color: #cad2e4;
border-style: none;
}
h1 {
font-size: x-large;
font-style: bold;
}
h2 {
font-size: large;
font-style: bold;
}
h1, h2, h3, h4, h5, h6 {
color: #3A4089;
}
h3, h4, h5, h6 {
font-style: italic;
}
"""
# end of defadefault_styles_css
######################################################################
html_before = """\
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta name="generator" content="{{PROJECT_NAME}}" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<meta name="author" content="{{AUTHOR}}" />
<title>{{PROJECT_NAME}} - {{TITLE}}</title>
<link rel="stylesheet" href="{{PATH_TO_CSS}}" />
</head>
<body>
<div id="content">
<div id="header"><a href="{{PATH_TO_INDEX}}">mnml.blog</a></div>
<div id="article-box">
<div id="top-nav">
<div>{{PREV_LINK}}</div>
<div class="nav-middle">
<a href="{{PATH_TO_ABOUT}}">about</a> | <a href="{{PATH_TO_ARCHIVE}}">archive</a>
</div> <!-- nav-middle -->
<div class="nav-right">{{NEXT_LINK}}</div>
</div> <!-- top-nav -->
<div id=article-content">
"""
# end of html_before
######################################################################
html_after = """
</div> <!-- article-content -->
<div id="bottom-nav">
<div>{{PREV_LINK}}</div>
<div class="nav-right">{{NEXT_LINK}}</a></div>
</div> <!-- bottom-nav -->
</div> <!-- article-box -->
<div id="footer">
{{COPYRIGHT}}
<a href="{{MD_SOURCE}}">Markdown source for this page</a><br/>
(Site generated by
<a href="https://github.com/jwhevans/static-purity">Static-Purity</a>)
</div> <!-- footer -->
</div> <!-- content -->
</body>
</html>
"""
# end of html_after
######################################################################
main()