Merge pull request #11420 from joeydlee95/master

 Add test to check for relative links in docs
This commit is contained in:
Zeke Sikelianos 2018-01-05 08:39:34 -08:00 committed by GitHub
commit aeffef766a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 114 additions and 2 deletions

View file

@ -50,7 +50,8 @@
"lint-js": "standard && cd spec && standard",
"lint-cpp": "python ./script/cpplint.py",
"lint-py": "python ./script/pylint.py",
"lint-docs": "remark docs -qf && npm run lint-js-in-markdown && npm run create-typescript-definitions",
"lint-docs": "remark docs -qf && npm run lint-js-in-markdown && npm run create-typescript-definitions && npm run lint-docs-relative-links",
"lint-docs-relative-links": "python ./script/check-relative-doc-links.py",
"lint-js-in-markdown": "standard-markdown docs",
"create-api-json": "electron-docs-linter docs --outfile=out/electron-api.json --version=$npm_package_version",
"create-typescript-definitions": "npm run create-api-json && electron-typescript-definitions --in=out/electron-api.json --out=out/electron.d.ts",
@ -71,5 +72,8 @@
"author": "Electron Community",
"keywords": [
"electron"
]
],
"dependencies": {
"lint": "^1.1.2"
}
}

View file

@ -0,0 +1,108 @@
#!/usr/bin/env python
import os
import sys
import re
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)
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
totalBrokenLinks = 0
for path in filepaths:
totalBrokenLinks += getBrokenLinks(path)
print('Parsed through ' + str(len(filepaths)) +
' files within docs directory and its ' +
str(totalDirs) + ' subdirectories.')
print('Found ' + str(totalBrokenLinks) + ' broken relative links.')
def getBrokenLinks(filepath):
currentDir = os.path.dirname(filepath)
brokenLinks = []
try:
f = open(filepath, 'r')
lines = f.readlines()
except KeyboardInterrupt:
print('Keyboard interruption whle parsing. Please try again.')
finally:
f.close()
regexLink = re.compile('\[(.*?)\]\((?P<links>(.*?))\)')
links = []
for line in lines:
matchLinks = regexLink.search(line)
if matchLinks:
relativeLink = matchLinks.group('links')
if not str(relativeLink).startswith('http'):
links.append(relativeLink)
for link in links:
sections = link.split('#')
if len(sections) > 1:
if str(link).startswith('#'):
if not checkSections(sections, lines):
brokenLinks.append(link)
else:
tempFile = os.path.join(currentDir, sections[0])
if os.path.isfile(tempFile):
try:
newFile = open(tempFile, 'r')
newLines = newFile.readlines()
except KeyboardInterrupt:
print('Keyboard interruption whle parsing. Please try again.')
finally:
newFile.close()
if not checkSections(sections, newLines):
brokenLinks.append(link)
else:
brokenLinks.append(link)
else:
if not os.path.isfile(os.path.join(currentDir, link)):
brokenLinks.append(link)
print_errors(filepath, brokenLinks)
return len(brokenLinks)
def checkSections(sections, lines):
sectionHeader = sections[1].replace('-', '')
regexSectionTitle = re.compile('# (?P<header>.*)')
for line in lines:
matchHeader = regexSectionTitle.search(line)
if matchHeader:
matchHeader = filter(str.isalnum, str(matchHeader.group('header')))
if matchHeader.lower() == sectionHeader:
return True
return False
def print_errors(filepath, brokenLink):
if brokenLink:
print "File Location: " + filepath
for link in brokenLink:
print "\tBroken links: " + link
if __name__ == '__main__':
sys.exit(main())