From 996c9149129bff8d4dd20c64b5ad1d41fb1681f5 Mon Sep 17 00:00:00 2001 From: joeydlee95 Date: Tue, 12 Dec 2017 18:32:47 -0800 Subject: [PATCH 1/3] :white_check_mark: Add test to check for relative links in docs directory --- script/check-relative-doc-links.py | 111 +++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 script/check-relative-doc-links.py diff --git a/script/check-relative-doc-links.py b/script/check-relative-doc-links.py new file mode 100644 index 000000000000..601056962613 --- /dev/null +++ b/script/check-relative-doc-links.py @@ -0,0 +1,111 @@ +#!/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 = [] + try: + for root, dirs, files in os.walk(DOCS_DIR): + for file in files: + if file.endswith('.md'): + filepaths.append(os.path.join(root, file)) + except KeyboardInterrupt: + print('Keyboard interruption. Please try again.') + return + except: + print('Error: Could not read files in directories.') + return + + totalBrokenLinks = 0 + for path in filepaths: + totalBrokenLinks += getBrokenLinks(path) + + print('Parsed through ' + str(len(filepaths)) + ' files.') + print('Found ' + str(totalBrokenLinks) + ' broken relative links.') + + +def getBrokenLinks(filepath): + currentDir = os.path.dirname(filepath) + brokenLinks = [] + + try: + file = open(filepath, 'r') + lines = file.readlines() + except KeyboardInterrupt: + print('Keyboard interruption whle parsing. Please try again.') + except: + print('Error: Could not open file ', filepath) + finally: + file.close() + + regexLink = re.compile('\[(.*?)\]\((?P(.*?))\)') + 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, link, lines, filepath): + 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.') + except: + print('Error: Could not open file ', filepath) + finally: + newFile.close() + + if not checkSections(sections, link, newLines, tempFile): + 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, link, lines, path): + sectionHeader = sections[1].replace('-', '') + regexSectionTitle = re.compile('# (?P
.*)') + 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()) From a4db8e1c551388c78457bfb2179d23abea579a66 Mon Sep 17 00:00:00 2001 From: joeydlee95 Date: Wed, 13 Dec 2017 14:39:13 -0800 Subject: [PATCH 2/3] Add executable test to package.json --- package.json | 8 ++++++-- script/check-relative-doc-links.py | 0 2 files changed, 6 insertions(+), 2 deletions(-) mode change 100644 => 100755 script/check-relative-doc-links.py diff --git a/package.json b/package.json index acf290904f64..3b1d4f5ebcbc 100644 --- a/package.json +++ b/package.json @@ -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" + } } diff --git a/script/check-relative-doc-links.py b/script/check-relative-doc-links.py old mode 100644 new mode 100755 From ac2caef37fc731786022b3367cfb2f49c614f2aa Mon Sep 17 00:00:00 2001 From: joeydlee95 Date: Wed, 13 Dec 2017 19:06:16 -0800 Subject: [PATCH 3/3] test for relative links adheres to npm run lint --- script/check-relative-doc-links.py | 31 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/script/check-relative-doc-links.py b/script/check-relative-doc-links.py index 601056962613..9df185471c37 100755 --- a/script/check-relative-doc-links.py +++ b/script/check-relative-doc-links.py @@ -13,23 +13,24 @@ def main(): os.chdir(SOURCE_ROOT) filepaths = [] + totalDirs = 0 try: for root, dirs, files in os.walk(DOCS_DIR): - for file in files: - if file.endswith('.md'): - filepaths.append(os.path.join(root, file)) + 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 - except: - print('Error: Could not read files in directories.') - return totalBrokenLinks = 0 for path in filepaths: totalBrokenLinks += getBrokenLinks(path) - print('Parsed through ' + str(len(filepaths)) + ' files.') + print('Parsed through ' + str(len(filepaths)) + + ' files within docs directory and its ' + + str(totalDirs) + ' subdirectories.') print('Found ' + str(totalBrokenLinks) + ' broken relative links.') @@ -38,14 +39,12 @@ def getBrokenLinks(filepath): brokenLinks = [] try: - file = open(filepath, 'r') - lines = file.readlines() + f = open(filepath, 'r') + lines = f.readlines() except KeyboardInterrupt: print('Keyboard interruption whle parsing. Please try again.') - except: - print('Error: Could not open file ', filepath) finally: - file.close() + f.close() regexLink = re.compile('\[(.*?)\]\((?P(.*?))\)') links = [] @@ -60,7 +59,7 @@ def getBrokenLinks(filepath): sections = link.split('#') if len(sections) > 1: if str(link).startswith('#'): - if not checkSections(sections, link, lines, filepath): + if not checkSections(sections, lines): brokenLinks.append(link) else: tempFile = os.path.join(currentDir, sections[0]) @@ -70,12 +69,10 @@ def getBrokenLinks(filepath): newLines = newFile.readlines() except KeyboardInterrupt: print('Keyboard interruption whle parsing. Please try again.') - except: - print('Error: Could not open file ', filepath) finally: newFile.close() - if not checkSections(sections, link, newLines, tempFile): + if not checkSections(sections, newLines): brokenLinks.append(link) else: brokenLinks.append(link) @@ -88,7 +85,7 @@ def getBrokenLinks(filepath): return len(brokenLinks) -def checkSections(sections, link, lines, path): +def checkSections(sections, lines): sectionHeader = sections[1].replace('-', '') regexSectionTitle = re.compile('# (?P
.*)') for line in lines: