-
Notifications
You must be signed in to change notification settings - Fork 2
/
convert-to-notebooks.py
144 lines (124 loc) · 6.36 KB
/
convert-to-notebooks.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
138
139
140
141
142
143
144
from manager import load_order, write_notebook, _clone_all_github, find_files, _copy, Path, get_file
import re
Path.copy = _copy
IMG_REGEX = re.compile(r"(?:!\[(.*?)\]\((.*?)\))")
order = load_order()
_clone_all_github()
search_file = False
in_cell, in_code = False, False
for module in Path("./github-curricula").glob("*"):
try:
sections = order['order'][module.stem]
except KeyError:
continue
cells = []
for section in sections:
file_name = Path('./github-curricula') / module.stem / 'sections' / (section + ".md")
md = file_name.read_text()
lines = md.split("\n")
for i, line in enumerate(lines):
if len(line):
if "<<<" in line: continue # skipping nav lines
is_header = line.strip().startswith("#")
is_code = line.strip().startswith("```")
is_image = re.search(IMG_REGEX, line)
if is_image and in_cell:
alt_text = is_image.groups()[0].replace('"', "'") # fixing up nested quotation marks so they don't cause trouble in html
link = is_image.groups()[1]
if not link.startswith("."):
if re.search(r"(https?:\/\/)?(www.)?github.com\/", link):
# link is on github - easy!
raw_link = "https://raw.githubusercontent.com/" + re.sub(r"(https?:\/\/)?(www.)?github.com\/", "", link).replace("/blob", "")
file_name = raw_link.split("/")[-1]
local_path = Path(f"./notebooks/img/{module.stem}/{file_name}")
if not local_path.exists():
get_file(raw_link, local_path)
new_link = f"./img/{module}/{file_name}"
this_cell['source'].extend([f"![{alt_text}]({new_link})"])
continue
else:
search_file = True
else:
search_file = True
if search_file:
found = find_files(Path(link).name,"./github-curricula/")
if len(found):
print(f"Warning: found local images that matches the one found in module {module.stem} ({link}): could be wrong image but including it in the resulting notebook.")
found_file = Path(found[0])
new_file = Path(f"./notebooks/img/{module.stem}/{found_file.name}")
if not new_file.parent.exists(): new_file.parent.mkdir(parents=True)
Path(found_file).copy(new_file)
print(f"Copying {found_file} --> {new_file}")
new_link = Path(f"./img/{module.stem}/{found_file.name}")
this_cell['source'].extend([f"![{alt_text}]({new_link})"])
search_file = False
continue
else:
print("No files could be found")
search_file = False
if is_code and in_cell:
if "python" in line.lower() and not in_code:
try:
if this_cell['source'][-1] == "\n":
this_cell['source'].pop()
this_cell['source'].append(this_cell['source'].pop().replace("\n", ""))
cells.append(this_cell)
except NameError:
pass
except IndexError:
pass
this_cell = {"cell_type": "code", "metadata": {}, "outputs": [], "execution_count": None, "source": []}
in_code = True
continue
elif line.strip() == "```" and in_code:
try:
if this_cell['source'][-1] == "\n":
this_cell['source'].pop()
this_cell['source'].append(this_cell['source'].pop().replace("\n", ""))
cells.append(this_cell)
except NameError:
pass
except IndexError:
pass
this_cell = {"cell_type": "markdown", "metadata": {}, "source": []}
in_code = False
continue
if in_code:
line = line.replace("\t", " ")
this_cell['source'].extend([line + "\n"])
continue
if is_header and not in_cell:
in_cell = True
try:
if this_cell['source'][-1] == "\n":
this_cell['source'].pop()
this_cell['source'].append(this_cell['source'].pop().replace("\n", ""))
cells.append(this_cell)
except NameError:
pass
except IndexError:
pass
this_cell = {"cell_type": "markdown", "metadata": {}, "source": []}
this_cell['source'].extend([line + "\n", "\n"])
continue
elif is_header and in_cell:
# end cell, start new
in_cell = True
try:
if this_cell['source'][-1] == "\n":
this_cell['source'].pop()
this_cell['source'].append(this_cell['source'].pop().replace("\n", ""))
cells.append(this_cell)
except NameError:
pass
except IndexError:
pass
this_cell = {"cell_type": "markdown", "metadata": {}, "source": []}
this_cell['source'].extend([line + "\n", "\n"])
continue
elif not is_header and in_cell:
this_cell['source'].extend([line + "\n", "\n"])
continue
elif not is_header and not in_cell:
raise RuntimeError("Something weird happened")
write_notebook(module.stem, cells)