From 6c56a6b33463add8006cdc37141e90474d7afa27 Mon Sep 17 00:00:00 2001 From: Ben Edgington Date: Fri, 10 Mar 2023 09:14:40 +0000 Subject: [PATCH] Add trailing white space check --- bin/build/prebuild.js | 19 +++++++++++++++++++ bin/build/whitespacecheck.sh | 10 ++++++++++ 2 files changed, 29 insertions(+) create mode 100755 bin/build/whitespacecheck.sh diff --git a/bin/build/prebuild.js b/bin/build/prebuild.js index 47cc1cc..c79f9aa 100644 --- a/bin/build/prebuild.js +++ b/bin/build/prebuild.js @@ -7,6 +7,7 @@ const glob = require('glob') // - Checks that HTML tags are properly balanced // - Spellcheck // - Repeated words check +// - Trailing whitespace check // - Lints the source markdown (see lintSourceMarkdown in this file) // - Splits the source markdown into individual pages // - Lints the split markdown (see lintSplitMarkdown in this file) @@ -15,6 +16,7 @@ const doInternalLinks = true const doHtmlCheck = true const doSpellCheck = true const doRepeatCheck = true +const doWhitespaceCheck = true const doSourceLint = true const doSplitLint = true @@ -22,6 +24,7 @@ const linkChecker = 'bin/build/links.pl' const htmlChecker = 'bin/build/html.pl' const spellChecker = 'bin/build/spellcheck.sh' const repeatChecker = 'bin/build/repeatcheck.sh' +const whitespaceChecker = 'bin/build/whitespacecheck.sh' const mdSplitter = 'bin/build/update.sh' const sourceMarkdown = 'src/book.md' @@ -105,6 +108,22 @@ module.exports.runChecks = (reporter) => { reporter.warn('Skipping repeat check') } + if (doWhitespaceCheck) { + reporter.info('Performing trailing whitespace check...') + try { + const out = execSync(`${whitespaceChecker} ${sourceMarkdown}`, {encoding: 'utf8', stdio: 'pipe'}) + if (out !== '') { + reporter.warn('Found trailing whitespace:') + printLines(out, reporter.warn) + } + } catch (err) { + reporter.warn('Unable to perform whitespace check:') + printLines(err.toString(), reporter.warn) + } + } else { + reporter.warn('Skipping whitespace check') + } + if (doSourceLint) { reporter.info('Linting source markdown...') try { diff --git a/bin/build/whitespacecheck.sh b/bin/build/whitespacecheck.sh new file mode 100755 index 0000000..a34a22b --- /dev/null +++ b/bin/build/whitespacecheck.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +# Check the book source, supplied as $1, for trailing whitespace + +grep -n '\s$' $1 | awk 'BEGIN {FS=":"} {print "Line " $1}' + +# The following allows us to handle the return from grep ("1" is not an error!) +r=("${PIPESTATUS[@]}") +(( ${r[0]} )) && (( ${r[0]} != 1 )) && exit ${r[0]} +exit ${r[1]}