21
21
import os
22
22
import pprint
23
23
24
- fpath = os .path .join (os .path .dirname ( __file__ ), '..' , 'README .md' )
24
+ fpath = os .path .join (os .path .dirname ( __file__ ), '..' , 'test_nb .md' )
25
25
examples = []
26
26
27
27
# The globals
@@ -55,12 +55,14 @@ def generate_markdown_block(lines):
55
55
sequence_num += 1
56
56
return result
57
57
58
+
58
59
def is_interactive_statement (line ):
59
60
for prefix in STATEMENT_PREFIXES :
60
- if line .startswith (prefix ):
61
+ if line .lstrip (). startswith (prefix ):
61
62
return True
62
63
return False
63
64
65
+
64
66
def parse_example_parts (lines , example_title_line ):
65
67
parts = {
66
68
"build_up" : [],
@@ -117,20 +119,20 @@ def parse_example_parts(lines, example_title_line):
117
119
# store lines again until --- or another H3 is encountered
118
120
while not (next_line .startswith ("---" ) or
119
121
next_line .startswith ("### " )):
120
- if next_line .startswith ("```" ):
122
+ if next_line .lstrip (). startswith ("```" ):
121
123
# It's a snippet, whatever found until now is text
122
124
is_interactive = False
123
125
if content :
124
- parts ["build_up " ].append (generate_markdown_block (content ))
126
+ parts ["explanation " ].append (generate_markdown_block (content ))
125
127
content = []
126
128
127
129
next_line = next (lines )
128
130
129
- while not next_line .startswith ("```" ):
131
+ while not next_line .lstrip (). startswith ("```" ):
130
132
if is_interactive_statement (next_line ):
131
133
is_interactive = True
132
134
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 ))
134
136
statements_so_far , output_so_far = [], []
135
137
statements_so_far .append (next_line )
136
138
else :
@@ -142,7 +144,7 @@ def parse_example_parts(lines, example_title_line):
142
144
next_line = next (lines )
143
145
144
146
# 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 ))
146
148
statements_so_far , output_so_far = [], []
147
149
next_line = next (lines )
148
150
else :
@@ -156,9 +158,10 @@ def parse_example_parts(lines, example_title_line):
156
158
157
159
return next_line , parts
158
160
161
+
159
162
def remove_from_beginning (tokens , line ):
160
163
for token in tokens :
161
- if line .startswith (token ):
164
+ if line .lstrip (). startswith (token ):
162
165
line = line .replace (token , "" )
163
166
return line
164
167
@@ -174,6 +177,7 @@ def inspect_and_sanitize_code_lines(lines):
174
177
result .append (line )
175
178
return is_print_present , result
176
179
180
+
177
181
def convert_to_cells (cell_contents ):
178
182
cells = []
179
183
for stuff in cell_contents :
@@ -240,25 +244,28 @@ def convert_to_notebook(parsed_json):
240
244
explanation = parts .get ("explanation" )
241
245
notebook_path = "test.ipynb"
242
246
243
- if ( build_up ) :
247
+ if build_up :
244
248
result ["cells" ] += convert_to_cells (build_up )
245
249
246
- if ( explanation ) :
250
+ if explanation :
247
251
result ["cells" ] += convert_to_cells (explanation )
248
252
249
253
pprint .pprint (result , indent = 2 )
250
254
with open (notebook_path , "w" ) as f :
251
255
json .dump (result , f )
252
256
253
257
254
-
255
258
with open (fpath , 'r+' , encoding = "utf-8" ) as f :
256
259
lines = iter (f .readlines ())
257
260
line = next (lines )
258
261
result = []
262
+ pre_examples_phase = True
263
+ pre_stuff = []
264
+ post_stuff = []
259
265
try :
260
266
while True :
261
267
if line .startswith ("## " ):
268
+ pre_examples_phase = False
262
269
# A section is encountered
263
270
current_section_name = line .replace ("## " , "" ).strip ()
264
271
section_text = []
@@ -281,8 +288,13 @@ def convert_to_notebook(parsed_json):
281
288
section_text .append (line )
282
289
line = next (lines )
283
290
else :
291
+ if pre_examples_phase :
292
+ pre_stuff .append (line )
293
+ else :
294
+ post_stuff .append (line )
284
295
line = next (lines )
285
296
286
297
except StopIteration :
287
298
pprint .pprint (result , indent = 2 )
299
+ print ("POST" , post_stuff )
288
300
convert_to_notebook (result )
0 commit comments