Skip to content

Exclude code blocks that aren't run #5307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions bin/check-all-md.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
from pathlib import Path
import os
import re
import sys
from run_markdown import _parse_md

PAT = re.compile(r"^```python\n(.+?)\n```", re.MULTILINE | re.DOTALL)
TMP_FILE = "tmp.py"

for filename in sys.argv[1:]:
content = Path(filename).read_text()
blocks = PAT.findall(content)
for i, b in enumerate(blocks):
Path(TMP_FILE).write_text(b.strip())
blocks = _parse_md(content)
for i, block in enumerate(blocks):
Path(TMP_FILE).write_text(block["code"].strip())
sys.stdout.write(f"\n{'=' * 40}\n{filename}: {i}\n")
sys.stdout.flush()
sys.stdout.write(f"{'-' * 40}\n")
Expand Down
25 changes: 17 additions & 8 deletions bin/run_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,17 +162,26 @@ def _parse_md(content):
blocks = []
current_block = None
in_code_block = False
in_region_block = False

for i, line in enumerate(lines):
# Check for region start/end markers
if "<!-- #region" in line:
in_region_block = True
elif "<!-- #endregion" in line:
in_region_block = False

# Start of Python code block
if line.strip().startswith("```python"):
in_code_block = True
current_block = {
"start_line": i,
"end_line": None,
"code": [],
"type": "python",
}
elif line.strip().startswith("```python"):
# Only process code blocks that are NOT inside region blocks
if not in_region_block:
in_code_block = True
current_block = {
"start_line": i,
"end_line": None,
"code": [],
"type": "python",
}

# End of code block
elif line.strip() == "```" and in_code_block:
Expand Down