MCPcopy Index your code
hub / github.com/microsoft/autogen / extract_python_code_blocks

Function extract_python_code_blocks

python/check_md_code_blocks.py:17–36  ·  view source on GitHub ↗

Extract Python code blocks from a Markdown file.

(markdown_file_path: str)

Source from the content-addressed store, hash-verified

15logger.setLevel(logging.INFO)
16
17def extract_python_code_blocks(markdown_file_path: str) -> List[Tuple[str, int]]:
18 """Extract Python code blocks from a Markdown file."""
19 with open(markdown_file_path, "r", encoding="utf-8") as file:
20 lines = file.readlines()
21
22 code_blocks: List[Tuple[str, int]] = []
23 in_code_block = False
24 current_block: List[str] = []
25
26 for i, line in enumerate(lines):
27 if line.strip().startswith("```python"):
28 in_code_block = True
29 current_block = []
30 elif line.strip().startswith("```"):
31 in_code_block = False
32 code_blocks.append(("\n".join(current_block), i - len(current_block) + 1))
33 elif in_code_block:
34 current_block.append(line)
35
36 return code_blocks
37
38def check_code_blocks(markdown_file_paths: List[str]) -> None:
39 """Check Python code blocks in a Markdown file for syntax errors."""

Callers 1

check_code_blocksFunction · 0.85

Calls 1

joinMethod · 0.80

Tested by

no test coverage detected