Skip to content

Commit 2161c48

Browse files
committed
Parse nested examples
1 parent 321e719 commit 2161c48

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

irrelevant/json_generator.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import os
2222
import pprint
2323

24-
fpath = os.path.join(os.path.dirname( __file__ ), '..', 'README.md')
24+
fpath = os.path.join(os.path.dirname( __file__ ), '..', 'test_nb.md')
2525
examples = []
2626

2727
# The globals
@@ -55,12 +55,14 @@ def generate_markdown_block(lines):
5555
sequence_num += 1
5656
return result
5757

58+
5859
def is_interactive_statement(line):
5960
for prefix in STATEMENT_PREFIXES:
60-
if line.startswith(prefix):
61+
if line.lstrip().startswith(prefix):
6162
return True
6263
return False
6364

65+
6466
def parse_example_parts(lines, example_title_line):
6567
parts = {
6668
"build_up": [],
@@ -117,20 +119,20 @@ def parse_example_parts(lines, example_title_line):
117119
# store lines again until --- or another H3 is encountered
118120
while not (next_line.startswith("---") or
119121
next_line.startswith("### ")):
120-
if next_line.startswith("```"):
122+
if next_line.lstrip().startswith("```"):
121123
# It's a snippet, whatever found until now is text
122124
is_interactive = False
123125
if content:
124-
parts["build_up"].append(generate_markdown_block(content))
126+
parts["explanation"].append(generate_markdown_block(content))
125127
content = []
126128

127129
next_line = next(lines)
128130

129-
while not next_line.startswith("```"):
131+
while not next_line.lstrip().startswith("```"):
130132
if is_interactive_statement(next_line):
131133
is_interactive = True
132134
if (output_so_far):
133-
parts["build_up"].append(generate_code_block(statements_so_far, output_so_far))
135+
parts["explanation"].append(generate_code_block(statements_so_far, output_so_far))
134136
statements_so_far, output_so_far = [], []
135137
statements_so_far.append(next_line)
136138
else:
@@ -142,7 +144,7 @@ def parse_example_parts(lines, example_title_line):
142144
next_line = next(lines)
143145

144146
# Snippet is over
145-
parts["build_up"].append(generate_code_block(statements_so_far, output_so_far))
147+
parts["explanation"].append(generate_code_block(statements_so_far, output_so_far))
146148
statements_so_far, output_so_far = [], []
147149
next_line = next(lines)
148150
else:
@@ -156,9 +158,10 @@ def parse_example_parts(lines, example_title_line):
156158

157159
return next_line, parts
158160

161+
159162
def remove_from_beginning(tokens, line):
160163
for token in tokens:
161-
if line.startswith(token):
164+
if line.lstrip().startswith(token):
162165
line = line.replace(token, "")
163166
return line
164167

@@ -174,6 +177,7 @@ def inspect_and_sanitize_code_lines(lines):
174177
result.append(line)
175178
return is_print_present, result
176179

180+
177181
def convert_to_cells(cell_contents):
178182
cells = []
179183
for stuff in cell_contents:
@@ -240,25 +244,28 @@ def convert_to_notebook(parsed_json):
240244
explanation = parts.get("explanation")
241245
notebook_path = "test.ipynb"
242246

243-
if(build_up):
247+
if build_up:
244248
result["cells"] += convert_to_cells(build_up)
245249

246-
if(explanation):
250+
if explanation:
247251
result["cells"] += convert_to_cells(explanation)
248252

249253
pprint.pprint(result, indent=2)
250254
with open(notebook_path, "w") as f:
251255
json.dump(result, f)
252256

253257

254-
255258
with open(fpath, 'r+', encoding="utf-8") as f:
256259
lines = iter(f.readlines())
257260
line = next(lines)
258261
result = []
262+
pre_examples_phase = True
263+
pre_stuff = []
264+
post_stuff = []
259265
try:
260266
while True:
261267
if line.startswith("## "):
268+
pre_examples_phase = False
262269
# A section is encountered
263270
current_section_name = line.replace("## ", "").strip()
264271
section_text = []
@@ -281,8 +288,13 @@ def convert_to_notebook(parsed_json):
281288
section_text.append(line)
282289
line = next(lines)
283290
else:
291+
if pre_examples_phase:
292+
pre_stuff.append(line)
293+
else:
294+
post_stuff.append(line)
284295
line = next(lines)
285296

286297
except StopIteration:
287298
pprint.pprint(result, indent=2)
299+
print("POST", post_stuff)
288300
convert_to_notebook(result)

0 commit comments

Comments
 (0)