-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathcode_qualify_check.py
More file actions
48 lines (41 loc) · 1.31 KB
/
Copy pathcode_qualify_check.py
File metadata and controls
48 lines (41 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import glob
import re
import os
import sys
def check_exist_qualify_target(text: str, filename: str) -> bool:
code = ""
in_code_block = False
found_error = False
for line in text.split("\n"):
is_code_block = line.strip().startswith("```")
if is_code_block:
in_code_block = not in_code_block
continue
if in_code_block:
code += line + "\n"
continue
m = re.fullmatch(r'[\*-] (.*?)\[(link |color |italic)(.*?)\]', line)
if m:
if code and len(code) > 0:
target = m.group(1)
if not target in code: # FIXME: 単語検索にしたい
print("{}: `{}` not found in code block.\n{}".format(filename, target, code))
found_error = True
else:
code = ""
return not found_error
def check() -> bool:
found_error = False
current_dir = os.getcwd()
outer_link_dict = dict()
found_error = False
for p in sorted(list(glob.glob("**/*.md", recursive=True))):
dirname = os.path.dirname(p)
with open(p) as f:
text = f.read()
if not check_exist_qualify_target(text, p):
found_error = True
return not found_error
if __name__ == '__main__':
if not check():
sys.exit(1)