Skip to content

Commit 7df33cb

Browse files
committed
Bump for 1970907
1 parent 91cc245 commit 7df33cb

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

bin/check-docs.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from __future__ import annotations
2+
3+
import re
4+
import ast
5+
from pathlib import Path
6+
7+
# Taken from https://github.com/asottile/blacken-docs/blob/main/blacken_docs.py
8+
MARKDOWN_CODE_BLOCK_RE = re.compile(
9+
r"(?P<before>^(?P<indent> *)```\s*python\n)" r"(?P<code>.*?)" r"(?P<after>^(?P=indent)```\s*$)",
10+
re.DOTALL | re.MULTILINE,
11+
)
12+
13+
14+
def check_file(path: Path) -> None:
15+
contents = path.read_text()
16+
for match in MARKDOWN_CODE_BLOCK_RE.finditer(contents):
17+
code = match.group("code")
18+
try:
19+
ast.parse(code)
20+
except Exception:
21+
print(f"---- The {path} file contains invalid Python code ---")
22+
print(code)
23+
print("---- See above output ----")
24+
raise
25+
26+
27+
def main() -> int:
28+
# NOTE: this may catch files that we don't want to check but it's fine for now
29+
for file in Path.cwd().glob("**/*.md"):
30+
check_file(file)
31+
32+
return 0
33+
34+
35+
if __name__ == "__main__":
36+
raise SystemExit(main())

0 commit comments

Comments
 (0)