#!/usr/bin/env python3
import re
import sys
from pathlib import Path
def validate_markdown(file_path):
print(f"Validating {file_path}...")
content = Path(file_path).read_text()
# Extract python code blocks
code_blocks = re.findall(r"```python\n(.*?)\n```", content, re.DOTALL)
if not code_blocks:
print(f"No python code blocks found in {file_path}")
return True
success = True
for i, block in enumerate(code_blocks):
print(f" Testing block {i+1}...")
try:
# We use a shared globals dict to simulate a continuous session if desired,
# but for docs verification, each block should usually be self-contained
# or we should execute them in sequence.
exec(block, {"__name__": "__main__"})
print(f" ✓ Block {i+1} passed")
except Exception as e:
print(f" ✗ Block {i+1} failed: {e}")
# Print the block content for debugging
print("---" + "CODE" + "---")
print(block)
print("------------")
success = False
return success
if __name__ == "__main__":
files_to_validate = ["README.md"]
# Add other docs if they have python examples
docs_dir = Path("docs")
if docs_dir.exists():
files_to_validate.extend([str(f) for f in docs_dir.glob("*.md")])
all_success = True
for file in files_to_validate:
if Path(file).exists():
if not validate_markdown(file):
all_success = False
else:
print(f"Skipping {file} (not found)")
if not all_success:
sys.exit(1)
print("All documentation examples validated successfully!")