forked from pelson/pelson.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make.py
executable file
·103 lines (87 loc) · 3.22 KB
/
make.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
#!/usr/bin/env conda-execute
# conda execute
# env:
# - python>3.5
# - pelican
# - markdown
# - ipython<6
# - notebook
# - tidy-html5
# - pygments
# channels:
# - defaults
# - conda-forge
# - pelson
# run_with: python
import os
import glob
import shutil
import subprocess
def html():
cmd = ['pelican', 'content', '--output', 'output', '--settings', 'pelicanconf.py']
subprocess.check_call(cmd)
def reload():
cmd = ['pelican', 'content', '--autoreload', '--output', 'output',
'--settings', 'pelicanconf.py']
subprocess.check_call(cmd)
def publish():
cmd = ['pelican', 'content', '--output', 'output',
'--settings', 'publishconf.py']
subprocess.check_call(cmd)
# Remove all ordinary files (not .git/.nojekyl though)
for fname in glob.glob('output_branch/*'):
if os.path.isdir(fname):
shutil.rmtree(fname)
else:
os.unlink(fname)
if not os.path.exists('output_branch'):
os.mkdir('output_branch')
# Copy all files
for root, dirs, files in os.walk('output'):
new_root = os.path.join('output_branch',
os.path.normpath(os.path.relpath(root, 'output')))
for dir in dirs:
if dir.startswith('.'):
dirs.remove(dir)
continue
new = os.path.join(new_root, dir)
os.mkdir(new)
for fname in files:
if fname.startswith('.'):
continue
old = os.path.join(root, fname)
new = os.path.join(new_root, fname)
if fname.endswith('.html'):
print('\nConverting {}:'.format(old))
cmd = ['tidy5', '-config', 'tidy_config.txt', old]
with open(new, 'w') as fh:
try:
code = subprocess.check_call(cmd, stdout=fh)
except subprocess.CalledProcessError as err:
if err.returncode != 1:
raise
else:
shutil.copy(old, new)
for fname in sorted(glob.glob('output/*')):
continue
if os.path.isdir(fname):
shutil.copytree(fname, os.path.join('output_branch', os.path.basename(fname)))
elif fname.endswith('.html'):
cmd = ['tidy5', '-config', 'tidy_config.txt', fname]
with open(os.path.join('output_branch', os.path.basename(fname)), 'w') as fh:
subprocess.check_call(cmd, stdout=fh)
else:
shutil.copy(fname, os.path.join('output_branch', os.path.basename(fname)))
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser('Help')
subparsers = parser.add_subparsers(dest='subcommand')
subparsers.required = True
parser_html = subparsers.add_parser('html', help="Make the html")
parser_html.set_defaults(func=html)
parser_publish = subparsers.add_parser('publish', help="Make publishable html, and put it in the output_branch.")
parser_publish.set_defaults(func=publish)
parser_reload = subparsers.add_parser('reload', help="Make the html, and watch the folder for any changes.")
parser_reload.set_defaults(func=reload)
args = parser.parse_args()
args.func()