Skip to content

Commit e18e56e

Browse files
committed
Working experimental notebook generator
1 parent 2cd7e3f commit e18e56e

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

irrelevant/json_generator.py

+18-10
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__ ), '..', 'test_nb.md')
24+
fpath = os.path.join(os.path.dirname( __file__ ), '..', 'README.md')
2525
examples = []
2626

2727
# The globals
@@ -73,11 +73,12 @@ def parse_example_parts(lines, example_title_line):
7373
output_so_far = []
7474
next_line = example_title_line
7575
# store build_up till an H4 (explanation) is encountered
76-
while not next_line.startswith("#### "):
76+
while not (next_line.startswith("#### ")or next_line.startswith('---')):
7777
# Watching out for the snippets
78-
if next_line.startswith("```"):
78+
if next_line.startswith("```py"):
7979
# It's a snippet, whatever found until now is text
8080
is_interactive = False
81+
output_encountered = False
8182
if content:
8283
parts["build_up"].append(generate_markdown_block(content))
8384
content = []
@@ -95,6 +96,8 @@ def parse_example_parts(lines, example_title_line):
9596
# can be either output or normal code
9697
if is_interactive:
9798
output_so_far.append(next_line)
99+
elif output_encountered:
100+
output_so_far.append(next_line)
98101
else:
99102
statements_so_far.append(next_line)
100103
next_line = next(lines)
@@ -119,7 +122,7 @@ def parse_example_parts(lines, example_title_line):
119122
# store lines again until --- or another H3 is encountered
120123
while not (next_line.startswith("---") or
121124
next_line.startswith("### ")):
122-
if next_line.lstrip().startswith("```"):
125+
if next_line.lstrip().startswith("```py"):
123126
# It's a snippet, whatever found until now is text
124127
is_interactive = False
125128
if content:
@@ -231,25 +234,31 @@ def convert_to_cells(cell_contents):
231234
return cells
232235

233236

234-
def convert_to_notebook(parsed_json):
237+
def convert_to_notebook(pre_examples_content, parsed_json, post_examples_content):
235238
result = {
236239
"cells": [],
237240
"metadata": {},
238241
"nbformat": 4,
239242
"nbformat_minor": 2
240243
}
244+
245+
notebook_path = "test.ipynb"
246+
247+
result["cells"] += convert_to_cells([generate_markdown_block(pre_examples_content)])
248+
241249
for example in parsed_json:
242250
parts = example["parts"]
243251
build_up = parts.get("build_up")
244252
explanation = parts.get("explanation")
245-
notebook_path = "test.ipynb"
246253

247254
if build_up:
248255
result["cells"] += convert_to_cells(build_up)
249256

250257
if explanation:
251258
result["cells"] += convert_to_cells(explanation)
252259

260+
result["cells"] += convert_to_cells([generate_markdown_block(post_examples_content)])
261+
253262
pprint.pprint(result, indent=2)
254263
with open(notebook_path, "w") as f:
255264
json.dump(result, f)
@@ -271,7 +280,7 @@ def convert_to_notebook(parsed_json):
271280
section_text = []
272281
line = next(lines)
273282
# Until a new section is encountered
274-
while not (line.startswith("## " )):
283+
while not (line.startswith("## ") or line.startswith("# ")):
275284
# check if it's a H3
276285
if line.startswith("### "):
277286
# An example is encountered
@@ -294,7 +303,6 @@ def convert_to_notebook(parsed_json):
294303
post_stuff.append(line)
295304
line = next(lines)
296305

297-
except StopIteration:
306+
except StopIteration as e:
298307
pprint.pprint(result, indent=2)
299-
print("POST", post_stuff)
300-
convert_to_notebook(result)
308+
convert_to_notebook(pre_stuff, result, post_stuff)

0 commit comments

Comments
 (0)