2016-12-12 11:18:36 +01:00
#!/bin/bash
###########################################################################
# chkspelling.sh
# ---------------------
# Date : December 2016
# Copyright : (C) 2016 by Denis Rouzaud
# Email : denis.rouzaud@gmail.com
###########################################################################
# #
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License, or #
# (at your option) any later version. #
# #
###########################################################################
# optional arguments: files to be checked
2016-12-15 15:36:52 +01:00
DIR = " $( cd " $( dirname " ${ BASH_SOURCE [0] } " ) " && pwd ) "
2016-12-12 11:18:36 +01:00
2016-12-15 15:36:52 +01:00
AGIGNORE = ${ DIR } /.agignore
2016-12-12 11:18:36 +01:00
RE = $( cut -d: -f1 scripts/spelling.dat | tr '\n' '\|' | sed -e 's/|$//' )
if [ ! $# -eq 0 ] ; then
2016-12-15 15:36:52 +01:00
EXCLUDE = $( cat $AGIGNORE | sed -e 's/\s*#.*$//' -e '/^\s*$/d' | tr '\n' '|' | sed -e 's/|$//' )
2016-12-12 11:18:36 +01:00
FILES = $( echo $@ | tr -s '[[:blank:]]' '\n' | egrep -iv " $EXCLUDE " | tr '\n' ' ' )
echo " Running spell check on files: $FILES "
else
FILES = "."
fi
exec 5>& 1
2016-12-19 07:50:40 +01:00
# "path-to-ignore" option differs on ag version: --path-to-ignore on fedora, --path-to-agignore on ubuntu 16.04: using short option
OUTPUT = $( unbuffer ag --smart-case --all-text --nopager --numbers --word-regexp -p $AGIGNORE " $RE " $FILES | tee /dev/fd/5)
2016-12-12 11:18:36 +01:00
if [ [ ! -z $OUTPUT ] ] ; then
echo "Spelling errors have been found"
2016-12-19 10:42:56 +01:00
echo "****"
# < ---------- get files + error ---------------------------------------------------------------------->
ag --smart-case --only-matching --nogroup --nonumbers --all-text --word-regexp -p $AGIGNORE " $RE " $FILES | \
2016-12-19 11:45:47 +01:00
# <-- generate sed command .... <------------------------------ get correction word -----------------------------> <------------------------------- match case -------------------------------------------> <-----replace : by / and add word boundary------> ...finalize sed command>
sed -e 's/\(\S*\):\([[:alnum:]]*\)$/ echo "sed -i \x27s\/"$( echo "\2:$(ag --nonumbers --ignore-case --word-regexp \2 scripts\/spelling.dat | cut -d: -f2)" | sed -r \x27s\/([A-Z]+):(.*)\/\\1:\\U\\2\/; s\/([A-Z][a-z]+):([a-z])\/\\1:\\U\\2\\L\/\x27 | sed -r \x27s\/(\\S\*):\/\\\\b\\1\\\\b\\\/\/\x27)"\/g\x27 \1" /e' | \
2016-12-19 10:42:56 +01:00
# remove duplicate line
sort -u
echo "****"
echo "Run above commands to fix spelling errors."
2016-12-12 11:18:36 +01:00
exit 1
else
exit 0
fi