-
Notifications
You must be signed in to change notification settings - Fork 9
/
generate_new_toc.py
115 lines (95 loc) · 3.88 KB
/
generate_new_toc.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
#! /usr/bin/env python3
import sys
import os
from string import Template
import shutil
from natsort import natsorted
# remove any previous attempts
if os.path.isdir('_tmp'):
shutil.rmtree('_tmp')
os.mkdir('_tmp')
shutil.copytree(os.sep.join(('notebooks','images')),
os.sep.join(('_tmp','images')))
shutil.copytree('_static', os.sep.join(('_tmp', '_static')))
skiplist = [os.sep.join(('notebooks', 'images'))]
def use_nested():
return sys.platform != 'win32'
def fix(path, copy=True):
if use_nested():
newpath = path.replace('notebooks'+os.sep, '', 1)
else:
newpath = '__'.join(path.split(os.sep)[1:])
if copy:
os.makedirs(os.sep.join(('_tmp', os.path.dirname(newpath))),
exist_ok=True)
shutil.copy(path, os.sep.join(('_tmp', newpath)))
return newpath
def change_path(root, base):
if use_nested():
print(root.replace('notebooks', base, 1))
print(root.replace(root.rsplit(os.sep, 1)[0],
base, 1))
return root.replace('notebooks', base, 1)
else:
return root.replace(root.rsplit(os.sep, 1)[0],
base, 1)
### quick and dirty argument parsing
def argv_or_default(key, default):
try:
return sys.argv[key]
except IndexError:
return default
baseurl = argv_or_default(1, '')
url = argv_or_default(2, '')
### Set up _config.yml
with open('_newconfig.yml.in', 'r') as template_file:
template = Template(template_file.read())
with open('_tmp'+os.sep+'_config.yml', 'w') as outfile:
outfile.write(template.substitute(logopath=fix(os.sep.join(("notebooks",
"images",
"logo",
"logo.png")))))
### Set up table of contents
chapter = Template("- file: ${path}\n")
section = Template(" - file: ${path}\n")
subsection = Template(" - file: ${path}\n")
leveltext = [None, chapter, section, subsection]
with open('_tmp'+os.sep+'_toc.yml', 'w') as outfile:
outfile.write("- file: " + fix(os.sep.join(('notebooks', 'intro.md'))) + "\n")
for root, dirs, files in os.walk('notebooks'):
dirs.sort()
if root == 'notebooks':
# Skip the base level
continue
for _dir in dirs:
if _dir.startswith('.'):
skiplist.append(os.sep.join((root, _dir)))
if root.startswith(tuple(skiplist)):
print('Not copying ', root)
continue
exts = set(os.path.splitext(file)[1] for file in files)
if ('.ipynb' and '.md') not in exts:
try:
shutil.copytree(root, change_path(root, '_tmp'))
shutil.copytree(root, change_path(root, os.sep.join(('_tmp',
'_build',
'html'))))
except FileExistsError:
pass
for intro in ['intro.md', 'intro.ipynb']:
if intro in files:
filepath = os.sep.join((root, intro))
level = filepath.count(os.sep)
outfile.write(leveltext[level - 1].substitute(path=fix(filepath)))
if 'a_module' not in root:
outfile.write(" " * (level - 1) + 'sections:\n')
files.remove(intro)
for file in natsorted(files):
_, ext = os.path.splitext(file)
filepath = os.sep.join((root, file))
level = filepath.count(os.sep)
if ext in ('.ipynb',):
outfile.write(leveltext[level].substitute(path=fix(filepath)))
break
fix('genindex.rst')
outfile.write("- title: \"Index\"\n file: genindex.rst\n")