Skip to content

Commit 6ccf11c

Browse files
committed
Fix edge case in literal block processing
Case where the next line after a literal block opens another literal block.
1 parent f5f9eef commit 6ccf11c

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

jupyterlite_sphinx/_try_examples.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,18 +226,35 @@ def _process_literal_blocks(md_text):
226226
indent_level = len(line) - len(line.lstrip())
227227

228228
if in_literal_block and (indent_level > 0 or line.strip() == ""):
229-
literal_block_accumulator.append(line)
229+
literal_block_accumulator.append(line.lstrip())
230230
elif in_literal_block:
231-
new_lines.extend(["```"] + literal_block_accumulator + ["```", line])
231+
new_lines.extend(["```"] + literal_block_accumulator + ["```"])
232232
literal_block_accumulator = []
233-
in_literal_block = False
234-
elif line.endswith("::"):
235-
in_literal_block = True
236-
new_lines.append(line[:-2])
233+
if line.endswith("::"):
234+
# If the line endswith ::, a new literal block is starting.
235+
line = line[:-2] # Strip off the :: from the end
236+
if not line:
237+
# If the line contains only ::, we ignore it.
238+
continue
239+
else:
240+
# Only set in_literal_block to False if not starting new
241+
# literal block.
242+
in_literal_block = False
243+
# We've appended the entire literal block which just ended, but
244+
# still need to append the current line.
245+
new_lines.append(line)
237246
else:
247+
if line.endswith("::"):
248+
# A literal block is starting.
249+
in_literal_block = True
250+
line = line[:-2]
251+
if not line:
252+
# As above, if the line contains only ::, ignore it.
253+
continue
238254
new_lines.append(line)
239255

240256
if literal_block_accumulator:
257+
# Handle case where a literal block ends the markdown cell.
241258
new_lines.extend(["```"] + literal_block_accumulator + ["```"])
242259

243260
return "\n".join(new_lines)

0 commit comments

Comments
 (0)