File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
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 ())
You can’t perform that action at this time.
0 commit comments