21
21
import os
22
22
import pprint
23
23
24
- fpath = os .path .join (os .path .dirname ( __file__ ), '..' , 'test_nb .md' )
24
+ fpath = os .path .join (os .path .dirname ( __file__ ), '..' , 'README .md' )
25
25
examples = []
26
26
27
27
# The globals
@@ -73,11 +73,12 @@ def parse_example_parts(lines, example_title_line):
73
73
output_so_far = []
74
74
next_line = example_title_line
75
75
# 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 ( '---' ) ):
77
77
# Watching out for the snippets
78
- if next_line .startswith ("```" ):
78
+ if next_line .startswith ("```py " ):
79
79
# It's a snippet, whatever found until now is text
80
80
is_interactive = False
81
+ output_encountered = False
81
82
if content :
82
83
parts ["build_up" ].append (generate_markdown_block (content ))
83
84
content = []
@@ -95,6 +96,8 @@ def parse_example_parts(lines, example_title_line):
95
96
# can be either output or normal code
96
97
if is_interactive :
97
98
output_so_far .append (next_line )
99
+ elif output_encountered :
100
+ output_so_far .append (next_line )
98
101
else :
99
102
statements_so_far .append (next_line )
100
103
next_line = next (lines )
@@ -119,7 +122,7 @@ def parse_example_parts(lines, example_title_line):
119
122
# store lines again until --- or another H3 is encountered
120
123
while not (next_line .startswith ("---" ) or
121
124
next_line .startswith ("### " )):
122
- if next_line .lstrip ().startswith ("```" ):
125
+ if next_line .lstrip ().startswith ("```py " ):
123
126
# It's a snippet, whatever found until now is text
124
127
is_interactive = False
125
128
if content :
@@ -231,25 +234,31 @@ def convert_to_cells(cell_contents):
231
234
return cells
232
235
233
236
234
- def convert_to_notebook (parsed_json ):
237
+ def convert_to_notebook (pre_examples_content , parsed_json , post_examples_content ):
235
238
result = {
236
239
"cells" : [],
237
240
"metadata" : {},
238
241
"nbformat" : 4 ,
239
242
"nbformat_minor" : 2
240
243
}
244
+
245
+ notebook_path = "test.ipynb"
246
+
247
+ result ["cells" ] += convert_to_cells ([generate_markdown_block (pre_examples_content )])
248
+
241
249
for example in parsed_json :
242
250
parts = example ["parts" ]
243
251
build_up = parts .get ("build_up" )
244
252
explanation = parts .get ("explanation" )
245
- notebook_path = "test.ipynb"
246
253
247
254
if build_up :
248
255
result ["cells" ] += convert_to_cells (build_up )
249
256
250
257
if explanation :
251
258
result ["cells" ] += convert_to_cells (explanation )
252
259
260
+ result ["cells" ] += convert_to_cells ([generate_markdown_block (post_examples_content )])
261
+
253
262
pprint .pprint (result , indent = 2 )
254
263
with open (notebook_path , "w" ) as f :
255
264
json .dump (result , f )
@@ -271,7 +280,7 @@ def convert_to_notebook(parsed_json):
271
280
section_text = []
272
281
line = next (lines )
273
282
# Until a new section is encountered
274
- while not (line .startswith ("## " )):
283
+ while not (line .startswith ("## " ) or line . startswith ( "# " )):
275
284
# check if it's a H3
276
285
if line .startswith ("### " ):
277
286
# An example is encountered
@@ -294,7 +303,6 @@ def convert_to_notebook(parsed_json):
294
303
post_stuff .append (line )
295
304
line = next (lines )
296
305
297
- except StopIteration :
306
+ except StopIteration as e :
298
307
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