build: auto-fix whitespace in docs in pre-commit hook (#17490)
This commit is contained in:
parent
6555be2636
commit
784f9742bc
9 changed files with 29 additions and 11 deletions
|
@ -116,8 +116,9 @@
|
||||||
"node script/lint.js --py --fix --only --",
|
"node script/lint.js --py --fix --only --",
|
||||||
"git add"
|
"git add"
|
||||||
],
|
],
|
||||||
"docs/api/*.md": [
|
"docs/api/**/*.md": [
|
||||||
"node script/gen-filenames.js",
|
"node script/gen-filenames.js",
|
||||||
|
"python script/check-trailing-whitespace.py --fix",
|
||||||
"git add filenames.auto.gni"
|
"git add filenames.auto.gni"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import argparse
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
@ -9,6 +10,8 @@ DOCS_DIR = os.path.join(SOURCE_ROOT, 'docs')
|
||||||
def main():
|
def main():
|
||||||
os.chdir(SOURCE_ROOT)
|
os.chdir(SOURCE_ROOT)
|
||||||
|
|
||||||
|
args = parse_args()
|
||||||
|
|
||||||
filepaths = []
|
filepaths = []
|
||||||
totalDirs = 0
|
totalDirs = 0
|
||||||
try:
|
try:
|
||||||
|
@ -23,7 +26,7 @@ def main():
|
||||||
|
|
||||||
trailingWhiteSpaceFiles = 0
|
trailingWhiteSpaceFiles = 0
|
||||||
for path in filepaths:
|
for path in filepaths:
|
||||||
trailingWhiteSpaceFiles += hasTrailingWhiteSpace(path)
|
trailingWhiteSpaceFiles += hasTrailingWhiteSpace(path, args.fix)
|
||||||
|
|
||||||
print('Parsed through ' + str(len(filepaths)) +
|
print('Parsed through ' + str(len(filepaths)) +
|
||||||
' files within docs directory and its ' +
|
' files within docs directory and its ' +
|
||||||
|
@ -32,7 +35,7 @@ def main():
|
||||||
' files with trailing whitespace.')
|
' files with trailing whitespace.')
|
||||||
return trailingWhiteSpaceFiles
|
return trailingWhiteSpaceFiles
|
||||||
|
|
||||||
def hasTrailingWhiteSpace(filepath):
|
def hasTrailingWhiteSpace(filepath, fix):
|
||||||
try:
|
try:
|
||||||
f = open(filepath, 'r')
|
f = open(filepath, 'r')
|
||||||
lines = f.read().splitlines()
|
lines = f.read().splitlines()
|
||||||
|
@ -41,12 +44,26 @@ def hasTrailingWhiteSpace(filepath):
|
||||||
finally:
|
finally:
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
|
fixed_lines = []
|
||||||
for line in lines:
|
for line in lines:
|
||||||
if line != line.rstrip():
|
fixed_lines.append(line.rstrip() + '\n')
|
||||||
|
if not fix and line != line.rstrip():
|
||||||
print "Trailing whitespace in: " + filepath
|
print "Trailing whitespace in: " + filepath
|
||||||
return True
|
return True
|
||||||
|
if fix:
|
||||||
|
with open(filepath, 'w') as f:
|
||||||
|
print(fixed_lines)
|
||||||
|
f.writelines(fixed_lines)
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def parse_args():
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description='Check for trailing whitespace in md files')
|
||||||
|
parser.add_argument('-f', '--fix',
|
||||||
|
help='Automatically fix trailing whitespace issues',
|
||||||
|
action='store_true')
|
||||||
|
return parser.parse_known_args()[0]
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
sys.exit(main())
|
sys.exit(main())
|
||||||
|
|
Loading…
Reference in a new issue