-
Notifications
You must be signed in to change notification settings - Fork 0
/
md2ipynb.py
executable file
·117 lines (97 loc) · 3.64 KB
/
md2ipynb.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
#!/usr/bin/env python3
import json
import argparse
def md_parser(path):
"""
This function reads an MD file and returns its lines in a list.
"""
with open(path, "r") as reader:
lines = reader.readlines()
return lines
def find_blocks(lines, title_depth=5):
"""
This function finds all lines that start with a `#` and are not inside a
code block, then returns a list of lists where each inner list is a block
that starts with a `#`.
"""
block_starts_idxs = []
code_blocks_idxs = []
code_block_opened = False
for i, line in enumerate(lines):
# Check if a line starts with a backtick or quotation marks then creates a
# list of lists where each inner list is a pair of indexes that mark the
# start and end of each code block
if line.startswith("`") or line.startswith("'") or line.startswith('"'):
if code_block_opened == False:
code_blocks_idxs.append([i])
code_block_opened = True
else:
code_blocks_idxs[-1].append(i)
code_block_opened = False
# Append the indexes of titles that also match the title_depth criterion
if line.startswith("#") and line.split(" ")[0] in "#"*title_depth:
block_starts_idxs.append(i)
# Remove the indexes of `#` that are inside code blocks
block_starts_idxs_remove = []
for begin, end in code_blocks_idxs:
for i in block_starts_idxs:
if begin < i < end:
block_starts_idxs_remove.append(i)
block_starts_idxs = [start for start in block_starts_idxs if start not in block_starts_idxs_remove]
# Place all the text before the first `#` in a block, if the MD file does not start
# with a `#`
if block_starts_idxs[0] == 0:
blocks = []
else:
blocks = [[lines[i] for i in range(0, block_starts_idxs[0])]]
# Iterate over the block_starts indexes
for i in range(0, len(block_starts_idxs) - 1):
# Place the lines between the two block_starts indexes in the temporary list
# and then in the final list with the rest of the blocks
block = []
for j in range(block_starts_idxs[i], block_starts_idxs[i + 1]):
block.append(lines[j])
blocks.append(block)
# Place the final block in the list
blocks.append([lines[i] for i in range(block_starts_idxs[-1], len(lines))])
return blocks
def create_nb_json(blocks):
"""
This function takes all the blocks and creates the json structure of the notebook.
"""
md_cells = [
{
"cell_type": "markdown",
"metadata": {},
"source": block
}
for block in blocks
]
metadata_cell = {}
nbformat = 4
nbformat_minor = 2
return {
"cells": md_cells,
"metadata_cell": metadata_cell,
"nbormat": nbformat,
"nbformat_minor": nbformat_minor
}
def save_ipynb(path, json_dict):
"""
This function takes the json dictionary and saves it as an ipynb.
"""
json_str = json.dumps(json_dict, indent=4)
with open(path, "w") as writer:
writer.write(json_str)
def main(i, o, d):
raw_md = md_parser(i)
blocks = find_blocks(raw_md, d)
json_dict = create_nb_json(blocks)
save_ipynb(o, json_dict)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="desc")
parser.add_argument("i", type=str, help="input, MD file path")
parser.add_argument("o", type=str, help="output, ipynb file path")
parser.add_argument("-d", type=int, default=5, help="title depth")
args = parser.parse_args()
main(args.i, args.o, args.d)