-
Notifications
You must be signed in to change notification settings - Fork 57
/
to_html.py
79 lines (67 loc) · 2.17 KB
/
to_html.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
from __future__ import print_function
import os
import sys
import errno
from glob import glob
import shutil
import time
import io
OUTPUT_DIR = 'html'
if len(sys.argv) > 1:
OUTPUT_DIR = sys.argv[1]
print("OUTPUTTING TO",OUTPUT_DIR)
time.sleep(1)
program = "jupyter nbconvert --CodeFoldingPreprocessor.remove_folded_code=True --TagRemovePreprocessor.remove_cell_tags=\"{'remove_cell_html'}\""
target_default = 'html_toc'
targets = {'Book.ipynb':'html'}
def mkdir_p(path):
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise
def replace_in_file(fn,src,dest):
f = open(fn,'r')
#f = io.open(fn, mode="r", encoding="utf-8")
lines = ''.join(f.readlines())
f.close()
f = None
f = open(fn+'.tmp','w')
f.write(lines.replace(src,dest))
f.close()
os.replace(fn+'.tmp',fn)
for fn in glob("*.ipynb"):
print("Running on ",fn)
os.system('%s --to %s --output-dir=%s "%s"'%(program,targets.get(fn,target_default),OUTPUT_DIR,fn))
for fn in glob(OUTPUT_DIR+"/*.html"):
try:
replace_in_file(fn,".ipynb\"",".html\"")
replace_in_file(fn,".ipynb#",".html#")
except Exception as e:
print()
print("Encountered exception replacing .ipynb to .html in",fn)
print(e)
print("Skipping...")
eqn_numbering_location = """<!-- End of mathjax configuration --></head>"""
inject_eqn_numbering = """
<script type="text/x-mathjax-config">
// make sure that equations numbers are enabled
MathJax.Hub.Config({ TeX: { equationNumbers: {
autoNumber: "all", // All AMS equations are numbered
useLabelIds: true, // labels as ids
// format the equation number - uses an offset eqNumInitial (default 0)
formatNumber: function (n) {return String(Number(n)+Number(1)-1)}
} }
});
</script>
"""
for fn in glob(OUTPUT_DIR+"/*.html"):
try:
replace_in_file(fn,eqn_numbering_location,inject_eqn_numbering+eqn_numbering_location)
except Exception as e:
print()
print("Encountered exception injecting mathjax header in",fn)
print(e)
print("Skipping...")