Skip to content

Commit b96e2fb

Browse files
committed
Respect the read_only rule
1 parent ef68d1b commit b96e2fb

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

irrelevant/json_generator.py

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ def is_interactive_statement(line):
6363
return False
6464

6565

66-
def parse_example_parts(lines, example_title_line):
66+
def parse_example_parts(lines, title, current_line):
6767
parts = {
6868
"build_up": [],
6969
"explanation": []
7070
}
71-
content = []
71+
content = [title]
7272
statements_so_far = []
7373
output_so_far = []
74-
next_line = example_title_line
74+
next_line = current_line
7575
# store build_up till an H4 (explanation) is encountered
7676
while not (next_line.startswith("#### ")or next_line.startswith('---')):
7777
# Watching out for the snippets
@@ -181,7 +181,7 @@ def inspect_and_sanitize_code_lines(lines):
181181
return is_print_present, result
182182

183183

184-
def convert_to_cells(cell_contents):
184+
def convert_to_cells(cell_contents, read_only):
185185
cells = []
186186
for stuff in cell_contents:
187187
if stuff["type"] == "markdown":
@@ -194,13 +194,26 @@ def convert_to_cells(cell_contents):
194194
}
195195
)
196196
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+
197209
is_print_present, sanitized_code = inspect_and_sanitize_code_lines(stuff["statements"])
198210
if is_print_present:
199211
cells.append(
200212
{
201213
"cell_type": "code",
202214
"metadata": {
203-
"collapsed": True
215+
"collapsed": True,
216+
204217
},
205218
"execution_count": None,
206219
"outputs": [{
@@ -244,22 +257,23 @@ def convert_to_notebook(pre_examples_content, parsed_json, post_examples_content
244257

245258
notebook_path = "test.ipynb"
246259

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)
248261

249262
for example in parsed_json:
250263
parts = example["parts"]
251264
build_up = parts.get("build_up")
252265
explanation = parts.get("explanation")
266+
read_only = example.get("read_only")
253267

254268
if build_up:
255-
result["cells"] += convert_to_cells(build_up)
269+
result["cells"] += convert_to_cells(build_up, read_only)
256270

257271
if explanation:
258-
result["cells"] += convert_to_cells(explanation)
272+
result["cells"] += convert_to_cells(explanation, read_only)
259273

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)
261275

262-
pprint.pprint(result, indent=2)
276+
#pprint.pprint(result, indent=2)
263277
with open(notebook_path, "w") as f:
264278
json.dump(result, f)
265279

@@ -284,13 +298,22 @@ def convert_to_notebook(pre_examples_content, parsed_json, post_examples_content
284298
# check if it's a H3
285299
if line.startswith("### "):
286300
# 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+
288310
example_details = {
289311
"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
292315
}
293-
line, example_details["parts"] = parse_example_parts(lines, line)
316+
line, example_details["parts"] = parse_example_parts(lines, title_line, line)
294317
result.append(example_details)
295318
current_example += 1
296319
else:
@@ -304,5 +327,5 @@ def convert_to_notebook(pre_examples_content, parsed_json, post_examples_content
304327
line = next(lines)
305328

306329
except StopIteration as e:
307-
pprint.pprint(result, indent=2)
330+
#pprint.pprint(result, indent=2)
308331
convert_to_notebook(pre_stuff, result, post_stuff)

0 commit comments

Comments
 (0)