chore: use markdownlint to lint the docs (#26792)
* chore: use markdownlint to lint the docs * chore: fix markdown lints Co-authored-by: Cheng Zhao <zcbenz@gmail.com>
This commit is contained in:
parent
40e80af9a9
commit
63ca878210
7 changed files with 129 additions and 80 deletions
|
@ -1,71 +0,0 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
from __future__ import print_function, unicode_literals
|
||||
import argparse
|
||||
import io
|
||||
import os
|
||||
import sys
|
||||
|
||||
SOURCE_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
|
||||
DOCS_DIR = os.path.join(SOURCE_ROOT, 'docs')
|
||||
|
||||
def main():
|
||||
os.chdir(SOURCE_ROOT)
|
||||
|
||||
args = parse_args()
|
||||
|
||||
filepaths = []
|
||||
totalDirs = 0
|
||||
try:
|
||||
for root, dirs, files in os.walk(DOCS_DIR):
|
||||
totalDirs += len(dirs)
|
||||
for f in files:
|
||||
if f.endswith('.md'):
|
||||
filepaths.append(os.path.join(root, f))
|
||||
except KeyboardInterrupt:
|
||||
print('Keyboard interruption. Please try again.')
|
||||
return
|
||||
|
||||
trailingWhiteSpaceFiles = 0
|
||||
for path in filepaths:
|
||||
trailingWhiteSpaceFiles += hasTrailingWhiteSpace(path, args.fix)
|
||||
|
||||
print('Parsed through ' + str(len(filepaths)) +
|
||||
' files within docs directory and its ' +
|
||||
str(totalDirs) + ' subdirectories.')
|
||||
print('Found ' + str(trailingWhiteSpaceFiles) +
|
||||
' files with trailing whitespace.')
|
||||
return trailingWhiteSpaceFiles
|
||||
|
||||
def hasTrailingWhiteSpace(filepath, fix):
|
||||
try:
|
||||
with io.open(filepath, 'r', encoding='utf-8') as f:
|
||||
lines = f.read().splitlines()
|
||||
except KeyboardInterrupt:
|
||||
print('Keyboard interruption while parsing. Please try again.')
|
||||
return
|
||||
|
||||
fixed_lines = []
|
||||
for num, line in enumerate(lines):
|
||||
fixed_lines.append(line.rstrip() + '\n')
|
||||
if not fix and line != line.rstrip():
|
||||
print("Trailing whitespace on line {} in file: {}".format(
|
||||
num + 1, filepath))
|
||||
return True
|
||||
if fix:
|
||||
with io.open(filepath, 'w', newline='\n', encoding='utf-8') as f:
|
||||
print(fixed_lines)
|
||||
f.writelines(fixed_lines)
|
||||
|
||||
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__':
|
||||
sys.exit(main())
|
Loading…
Add table
Add a link
Reference in a new issue