@@ -63,15 +63,15 @@ def is_interactive_statement(line):
63
63
return False
64
64
65
65
66
- def parse_example_parts (lines , example_title_line ):
66
+ def parse_example_parts (lines , title , current_line ):
67
67
parts = {
68
68
"build_up" : [],
69
69
"explanation" : []
70
70
}
71
- content = []
71
+ content = [title ]
72
72
statements_so_far = []
73
73
output_so_far = []
74
- next_line = example_title_line
74
+ next_line = current_line
75
75
# store build_up till an H4 (explanation) is encountered
76
76
while not (next_line .startswith ("#### " )or next_line .startswith ('---' )):
77
77
# Watching out for the snippets
@@ -181,7 +181,7 @@ def inspect_and_sanitize_code_lines(lines):
181
181
return is_print_present , result
182
182
183
183
184
- def convert_to_cells (cell_contents ):
184
+ def convert_to_cells (cell_contents , read_only ):
185
185
cells = []
186
186
for stuff in cell_contents :
187
187
if stuff ["type" ] == "markdown" :
@@ -194,13 +194,26 @@ def convert_to_cells(cell_contents):
194
194
}
195
195
)
196
196
elif stuff ["type" ] == "code" :
197
+ if read_only :
198
+ # Skip read only
199
+ # TODO: Fix
200
+ cells .append (
201
+ {
202
+ "cell_type" : "markdown" ,
203
+ "metadata" : {},
204
+ "source" : ["```py\n " ] + stuff ["statements" ] + ["```\n " ] + ["```py\n " ] + stuff ['output' ] + ["```\n " ]
205
+ }
206
+ )
207
+ continue
208
+
197
209
is_print_present , sanitized_code = inspect_and_sanitize_code_lines (stuff ["statements" ])
198
210
if is_print_present :
199
211
cells .append (
200
212
{
201
213
"cell_type" : "code" ,
202
214
"metadata" : {
203
- "collapsed" : True
215
+ "collapsed" : True ,
216
+
204
217
},
205
218
"execution_count" : None ,
206
219
"outputs" : [{
@@ -244,22 +257,23 @@ def convert_to_notebook(pre_examples_content, parsed_json, post_examples_content
244
257
245
258
notebook_path = "test.ipynb"
246
259
247
- result ["cells" ] += convert_to_cells ([generate_markdown_block (pre_examples_content )])
260
+ result ["cells" ] += convert_to_cells ([generate_markdown_block (pre_examples_content )], False )
248
261
249
262
for example in parsed_json :
250
263
parts = example ["parts" ]
251
264
build_up = parts .get ("build_up" )
252
265
explanation = parts .get ("explanation" )
266
+ read_only = example .get ("read_only" )
253
267
254
268
if build_up :
255
- result ["cells" ] += convert_to_cells (build_up )
269
+ result ["cells" ] += convert_to_cells (build_up , read_only )
256
270
257
271
if explanation :
258
- result ["cells" ] += convert_to_cells (explanation )
272
+ result ["cells" ] += convert_to_cells (explanation , read_only )
259
273
260
- result ["cells" ] += convert_to_cells ([generate_markdown_block (post_examples_content )])
274
+ result ["cells" ] += convert_to_cells ([generate_markdown_block (post_examples_content )], False )
261
275
262
- pprint .pprint (result , indent = 2 )
276
+ # pprint.pprint(result, indent=2)
263
277
with open (notebook_path , "w" ) as f :
264
278
json .dump (result , f )
265
279
@@ -284,13 +298,22 @@ def convert_to_notebook(pre_examples_content, parsed_json, post_examples_content
284
298
# check if it's a H3
285
299
if line .startswith ("### " ):
286
300
# An example is encountered
287
- title = line .replace ("### " , "" )
301
+ title_line = line
302
+ line = next (lines )
303
+ read_only = False
304
+ while line .strip () == "" or line .startswith ('<!--' ):
305
+ #TODO: Capture example ID here using regex.
306
+ if '<!-- read-only -->' in line :
307
+ read_only = True
308
+ line = next (lines )
309
+
288
310
example_details = {
289
311
"id" : current_example ,
290
- "title" : line .replace ("### " , "" ),
291
- "section" : current_section_name
312
+ "title" : title_line .replace ("### " , "" ),
313
+ "section" : current_section_name ,
314
+ "read_only" : read_only
292
315
}
293
- line , example_details ["parts" ] = parse_example_parts (lines , line )
316
+ line , example_details ["parts" ] = parse_example_parts (lines , title_line , line )
294
317
result .append (example_details )
295
318
current_example += 1
296
319
else :
@@ -304,5 +327,5 @@ def convert_to_notebook(pre_examples_content, parsed_json, post_examples_content
304
327
line = next (lines )
305
328
306
329
except StopIteration as e :
307
- pprint .pprint (result , indent = 2 )
330
+ # pprint.pprint(result, indent=2)
308
331
convert_to_notebook (pre_stuff , result , post_stuff )
0 commit comments