-
Notifications
You must be signed in to change notification settings - Fork 3
/
update_docs.py
138 lines (109 loc) · 4.29 KB
/
update_docs.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
import os
import json
import copy
# cutting notebooks
def split(name, destination):
with open("tutorials/{}".format(name), "r") as f:
notebook = json.load(f)
before_collapsible = copy.deepcopy(notebook)
before_collapsible['cells'] = before_collapsible['cells'][0:2]
collapsible = copy.deepcopy(notebook)
collapsible['cells'] = [collapsible['cells'][2]]
after_collapsible = copy.deepcopy(notebook)
after_collapsible['cells'] = after_collapsible['cells'][3:]
clean_name = name.strip().split('.')[0]
with open("{}/{}_before_collapsible.ipynb".format(destination,
clean_name), "w") as f:
json.dump(before_collapsible, f)
with open("{}/{}_collapsible.ipynb".format(destination,
clean_name), "w") as f:
json.dump(collapsible, f)
with open("{}/{}_after_collapsible.ipynb".format(destination,
clean_name), "w") as f:
json.dump(after_collapsible, f)
os.system("mkdir docs/cutted")
split('calculating_covariants.ipynb', 'docs/cutted/')
split('getting_insights_about_the_model.ipynb', 'docs/cutted/')
split('constructor_or_non_standard_sequence.ipynb', 'docs/cutted/')
split('sequential_fitting.ipynb', 'docs/cutted/')
split('custom_regressors_into_purifiers.ipynb', 'docs/cutted/')
# converting notebooks to rst
def make_substitution(lines, index):
lines_before = lines[0:index]
end = len(lines)
for j in range(index + 1, len(lines)):
if not(lines[j].strip() == "" or lines[j].startswith(' ')):
end = j
break
lines_raw = lines[index + 1 : end]
raw_from = 0
for i in range(len(lines_raw)):
if (lines_raw[i].strip() != ''):
raw_from = i
break
lines_raw = lines_raw[raw_from:]
raw_to = 0
for i in range(len(lines_raw)):
if (lines_raw[i].strip() != ''):
raw_to = i + 1
lines_raw = lines_raw[:raw_to]
lines_for_insertion = [".. raw:: html\n",
"\n",
"<embed>\n",
"<pre>\n",
'<p style="margin-left: 5%;font-size:12px;line-height: 1.2; overflow:auto" >\n']
lines_for_insertion = lines_for_insertion + lines_raw
lines_for_insertion = lines_for_insertion + ["</p>\n", "</pre>\n", "</embed>\n", '\n']
for i in range(1, len(lines_for_insertion)):
lines_for_insertion[i] = ' ' + lines_for_insertion[i]
return lines_before + lines_for_insertion + lines[end:]
return lines[index : end]
def get_bad_block(lines):
for i in range(len(lines)):
if (lines[i].strip() == ".. parsed-literal::"):
return i
return None
def iterate(lines):
while True:
index = get_bad_block(lines)
if index is None:
return lines
lines = make_substitution(lines, index)
def fix_awful_nvconvert_format(file):
lines = []
with open(file, "r") as f:
lines = list(f)
lines = iterate(lines)
with open(file, "w") as f:
for line in lines:
f.write(line)
os.chdir('docs/cutted/')
names = [name for name in os.listdir('.') if name.endswith('.ipynb')]
for name in names:
dir_name = name.split('.')[0]
os.system("mkdir {}".format(dir_name))
os.system("cp {} {}/".format(name, dir_name))
os.chdir(dir_name)
os.system('jupyter nbconvert --to rst {}'.format(name))
fix_awful_nvconvert_format(name.split('.')[0] + '.rst')
names_inner = os.listdir('.')
for name_inner in names_inner:
if (name_inner.endswith('_files')):
os.system('cp -r {} ../../'.format(name_inner))
os.chdir('../')
os.chdir('../..')
os.system("rm -r ../build/*")
os.chdir("./docs")
os.system("sphinx-apidoc -f -o . ../nice")
os.system("make html")
os.chdir("../")
os.system("git checkout -f gh-pages")
os.system("git rm -r *")
os.system("cp -r ../build/html/* .")
with open(".nojekyll", "w") as f:
pass
os.system("git add *")
os.system("git add .nojekyll")
os.system("git commit -m 'automatic docs build'")
os.system("git push")
os.system("git checkout master")