-
Notifications
You must be signed in to change notification settings - Fork 5
/
fix.py
71 lines (58 loc) · 2.6 KB
/
fix.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
import re
def find_files(root_dir):
for dir_path, dir_names, file_names in os.walk(root_dir):
for filename in file_names:
name_without_ext, ext = os.path.splitext(filename)
if ext == '.md' and name_without_ext.isdigit():
new_filename = name_without_ext.zfill(4) + ext
os.rename(os.path.join(dir_path, filename), os.path.join(dir_path, new_filename))
def find_incomplete_files(root_dir):
for dir_path, dir_names, file_names in os.walk(root_dir):
for filename in file_names:
name_without_ext, ext = os.path.splitext(filename)
if ext == '.md' and name_without_ext.isdigit():
file_path = os.path.join(dir_path, filename)
with open(file_path, 'r') as f:
content = f.read()
if '```' not in content or '算法复杂度' not in content or '$' in content:
print(os.path.join(dir_path, filename))
def find_code_block(root_dir):
for file_name in os.listdir(root_dir):
if os.path.isdir(os.path.join(root_dir, file_name)):
lang = file_name
dir_path = os.path.join(root_dir, file_name)
for filename in os.listdir(dir_path):
fix_code_block(lang, os.path.join(dir_path, filename))
else:
lang = os.path.basename(root_dir)
fix_code_block(lang, os.path.join(root_dir, file_name))
def fix_code_block(lang, file_path):
name_without_ext, ext = os.path.splitext(os.path.basename(file_path))
if ext != '.md' or not name_without_ext.isdigit():
return
with open(file_path, 'r') as fr:
content = fr.read()
pattern = re.compile(r'```(.*?)```', re.DOTALL)
matches = pattern.findall(content)
need_fix = False
for match in matches:
if lang not in match and \
not match.startswith('sql') \
and not match.startswith('bash') \
and not match.startswith('mysql'):
replacement = f'```{lang}{match}```'
content = content.replace(f'```{match}```', replacement)
need_fix = True
if lang == 'solidity':
old_content = content
content = re.sub(r'```\np', '```solidity\np', content)
if old_content != content:
need_fix = True
if need_fix:
with open(file_path, 'w') as fw:
fw.write(content)
print(f'Fix {file_path}')
if __name__ == '__main__':
find_code_block('.')
find_incomplete_files('.')