forked from doocs/leetcode
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_format.py
121 lines (101 loc) · 3.72 KB
/
run_format.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
fromtypingimportList
importos.path
importre
importblack
suffixes= ['md', 'py', 'java', 'c', 'cpp', 'go', 'php', 'cs', 'rs', 'js', 'ts']
code_blocks= ['python', 'java', 'cpp', 'c', 'go', 'ts', 'js', 'php', 'cs', 'rs']
defadd_header(path: str):
"""Add header to php and go files"""
print(f'[add header] path: {path}')
withopen(path, 'r', encoding='utf-8') asf:
content=f.read()
ifpath.endswith('.php'):
content='<?php\n'+content
elifpath.endswith('.go') and'sorting'notinpath:
content='package main\n'+content
else:
return
withopen(path, 'w', encoding='utf-8') asf:
f.write(content)
defremove_header(path: str):
"""Remove header from php and go files"""
print(f'[remove header] path: {path}')
withopen(path, 'r', encoding='utf-8') asf:
content=f.read()
ifpath.endswith('.php'):
content=content.rstrip()
content=content.replace('<?php\n', '')
elifpath.endswith('.go'):
content=content.rstrip()
if'sorting'notinpath:
content=content.replace('package main\n\n', '').replace(
'package main\n', ''
)
else:
return
withopen(path, 'w', encoding='utf-8') asf:
f.write(content)
deffind_all_paths() ->List[str]:
"""Find all paths of files with suffixes"""
paths= []
forroot, _, filesinos.walk(os.getcwd()):
forfileinfiles:
path=root+'/'+file
if'node_modules'inpathor'__pycache__'inpathor'.git'inpath:
continue
ifany(path.endswith(f'.{suf}') forsufinsuffixes):
paths.append(path)
returnpaths
defformat_inline_code(path: str):
"""Format inline code in .md file"""
ifnotpath.endswith('.md'):
return
withopen(path, 'r', encoding='utf-8') asf:
content=f.read()
root=path[: path.rfind('/')]
print(f'[format inline code] path: {path}')
forsufincode_blocks:
res=re.findall(f'```{suf}\n(.*?)```', content, re.S)
forblockinresor []:
# skip empty code block
ifnotblockornotblock.strip():
continue
ifsufin ['c', 'cpp', 'java', 'go']:
file=f'{root}/Solution2.{suf}'
withopen(file, 'w', encoding='utf-8') asf:
f.write(block)
ifsuf=='go':
add_header(file)
os.system(f'gofmt -w "{file}"')
remove_header(file)
else:
os.system(f'npx clang-format -i --style=file "{file}"')
withopen(file, 'r', encoding='utf-8') asf:
new_block=f.read()
content=content.replace(block, new_block)
os.remove(file)
elifsuf=='python':
new_block=black.format_str(
block, mode=black.FileMode(string_normalization=False)
)
content=content.replace(block, new_block)
withopen(path, 'w', encoding='utf-8') asf:
f.write(content)
defrun():
"""Start formatting"""
paths=find_all_paths()
forpathinpaths:
add_header(path)
ifany(path.endswith(suf) forsufin ['c', 'cpp', 'java']):
# format with clang-format
os.system(f'npx clang-format -i --style=file "{path}"')
# format with prettier
os.system('npx prettier --write "**/*.{md,js,ts,php}"')
# format with gofmt
os.system('gofmt -w .')
forpathinpaths:
remove_header(path)
forpathinpaths:
format_inline_code(path)
if__name__=='__main__':
run()