From e7279a0383a2ac32522a602255657bec33714723 Mon Sep 17 00:00:00 2001 From: Ben Edgington Date: Sat, 14 Jun 2025 15:14:47 +0100 Subject: [PATCH] Make some of the checks read from stdin as well as file --- bin/build/checks/html.pl | 7 +------ bin/build/checks/latex.pl | 7 +------ bin/build/checks/repeats.pl | 7 +------ bin/build/checks/spellcheck.sh | 17 ++++++++++++++--- bin/build/checks/whitespace.pl | 7 +------ bin/build/prebuild.js | 11 ++++++++--- 6 files changed, 26 insertions(+), 30 deletions(-) diff --git a/bin/build/checks/html.pl b/bin/build/checks/html.pl index fe2c738..7651afd 100755 --- a/bin/build/checks/html.pl +++ b/bin/build/checks/html.pl @@ -10,14 +10,9 @@ $\ = "\n"; # set output record separator my @html_entities = ('ndash', 'nbsp', 'trade', 'ldquo', 'rdquo'); my %entities = map { $_ => 1 } @html_entities; -my $fh = *STDIN; -if (my $file = shift) { - open $fh, '<', $file or die "Can't open $file: $!"; -} - my @tags = (); -while(<$fh>) { +while(<>) { while (/<(\/{0,1})([a-z]+).*?(\/{0,1})>/g) { my $thisTag = $2; diff --git a/bin/build/checks/latex.pl b/bin/build/checks/latex.pl index 28df185..e826393 100755 --- a/bin/build/checks/latex.pl +++ b/bin/build/checks/latex.pl @@ -10,11 +10,6 @@ use IPC::Run3; $\ = "\n"; # set output record separator -my $fh = *STDIN; -if (my $file = shift) { - open $fh, '<', $file or die "Can't open $file: $!"; -} - # Add any exclusions here by adding "-n#" where # is the warning number my @command = ["chktex", "-q"]; @@ -23,7 +18,7 @@ my $ignore = qr/\$(\[1,r\))\$/; my $latex = ''; my $inMath = 0; -while(<$fh>) { +while(<>) { chomp; diff --git a/bin/build/checks/repeats.pl b/bin/build/checks/repeats.pl index f032e44..b26f594 100755 --- a/bin/build/checks/repeats.pl +++ b/bin/build/checks/repeats.pl @@ -7,12 +7,7 @@ use warnings; $\ = "\n"; # set output record separator -my $fh = *STDIN; -if (my $file = shift) { - open $fh, '<', $file or die "Can't open $file: $!"; -} - -while(<$fh>) { +while(<>) { while (/\b([_[:alpha:]]+)\s+(\1)\b/g) { print "Line $.: $1 $2"; } diff --git a/bin/build/checks/spellcheck.sh b/bin/build/checks/spellcheck.sh index 4ecbad6..71fa9e6 100755 --- a/bin/build/checks/spellcheck.sh +++ b/bin/build/checks/spellcheck.sh @@ -1,14 +1,25 @@ #!/bin/bash -# Spell check the book source, supplied as $1, exceptions are in the file $2. +# Spell check the book source, supplied as $1 (or stdin), exceptions are in the file $2. # # Aspell has input filters for markdown etc, but it's honestly easier just to preprocess stuff # with the spellcheck_prep.pl script. +if [ "$#" -eq 1 ]; then + input=/dev/stdin + exceptions=$1 +elif [ "$#" -eq 2 ]; then + input=$1 + exceptions=$2 +else + echo "Usage: spellcheck.sh [markdown_file] exceptions_file" + exit 1 +fi + export LANG=en_GB.UTF-8 here=$(cd $(dirname "$0") && pwd) -$here/spellcheck_prep.pl $1 \ - | aspell --home-dir . -p $2 --dont-suggest pipe --dict-dir=$here/../dicts -d en_GB-ise-w_accents \ +$here/spellcheck_prep.pl $input \ + | aspell --home-dir . -p $exceptions --dont-suggest pipe --dict-dir=$here/../dicts -d en_GB-ise-w_accents \ | tail -n +2 \ | awk 'BEGIN {n=1} /^$/{n++} /^..+$/{print "Line " n ": "$2}' diff --git a/bin/build/checks/whitespace.pl b/bin/build/checks/whitespace.pl index 85fc488..4d600ee 100755 --- a/bin/build/checks/whitespace.pl +++ b/bin/build/checks/whitespace.pl @@ -5,11 +5,6 @@ use strict; use warnings; -my $fh = *STDIN; -if (my $file = shift) { - open $fh, '<', $file or die "Can't open $file: $!"; -} - -while(<$fh>) { +while(<>) { print "Line $." if /\h$/; } diff --git a/bin/build/prebuild.js b/bin/build/prebuild.js index 3d5f94c..9678532 100644 --- a/bin/build/prebuild.js +++ b/bin/build/prebuild.js @@ -79,6 +79,11 @@ const colour = { green: (s) => '\x1b[38;5;34m' + s + '\x1b[0m', }; +const chars = { + tick: colour.green('\u2713'), + cross: colour.orange('\u2717'), +}; + const myLogger = { info: (m) => { console.log(colour.blue('info ') + m); @@ -98,14 +103,14 @@ async function runCheck({ name, enabled, checker }, logger) { try { const out = await checker(); if (out === '' || out === null) { - logger.info(colour.green('\u2713') + ` Passed ${name} check`); + logger.info(chars.tick + ` Passed ${name} check`); } else { - logger.warn(`Issues were found by ${name} check:`); + logger.warn(chars.cross + ` Issues were found by ${name} check:`); printLines(out, logger); success = false; } } catch (err) { - logger.warn(`An error occurred during ${name} check:`); + logger.warn(chars.cross + ` An error occurred during ${name} check:`); printLines(err.toString(), logger); success = false; }