-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar-check.sh
executable file
·139 lines (130 loc) · 4.1 KB
/
grammar-check.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env bash
# Copyright 2024 Zaphiro Technologies
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
error=0
errorMessages=""
declare -a ignorefiles
parameters=""
clean_brackets() {
if [[ $(uname) == "Darwin" ]]; then
# macOS requires an argument for -i to specify the backup file extension
sed -E -i '' '/```/,/```/! s/`//g' "$1"
else
# Linux systems do not require an argument for -i
sed -E -i '/```/,/```/! s/`//g' "$1"
fi
}
while getopts m:f:mode:file: flag
do
case "${flag}" in
m|mode) mode=${OPTARG};;
f|file) file=${OPTARG};;
esac
done
if [ -z ${mode+x} ]; then
echo "Mode not set using default";
mode="github-action"
parameters=""
else
case $mode in
github-action)
parameters=""
;;
local)
parameters="-e casing -e colloquialisms -e compounding -e confused_words -e false_friends -e gender_neutrality -e grammar -e misc -e punctuation -e redundancy -e repetitions -e regionalisms -e semantics -e style -e typos"
;;
local-non-interactive)
parameters="-e casing -e colloquialisms -e compounding -e confused_words -e false_friends -e gender_neutrality -e grammar -e misc -e punctuation -e redundancy -e repetitions -e regionalisms -e semantics -e style -e typos -p"
;;
*)
echo "Invalid Mode"
exit 1
;;
esac
fi
echo "Mode: $mode";
echo "Paramaters: $parameters"
if [ -z ${file+x} ]; then
if [ -f .grammarignore ]; then
echo 'Loading ignore list...'
ignorefiles=(`cat ".grammarignore"`)
fi
while read line; do
file=${line:2}
if [[ ! ${ignorefiles[*]} =~ "$file" ]]
then
echo ""
echo "...processing $file"
echo ""
filesize=$(wc -c $line | awk '{print $1}')
if [[ $filesize -gt 20000 && $file ]]
then
echo "The file $file is too big, it will be split in sub files and checked:"
csplit -k -n 4 -s -f 'tmp' $line '/##/' '{100}'
while read tmp; do
clean_brackets $tmp
npx gramma check -m -p $parameters "$tmp"
if [ $? -ne 0 ]; then
error=1
if [[ $errorMessages != *"$file"* ]]; then
errorMessages=$errorMessages"\n - ${file}"
fi
fi
done <<<$(find . -name "tmp*" -type f)
rm -rf tmp*
else
cp $line tmp
clean_brackets tmp
npx gramma check -m -p $parameters tmp
if [ $? -ne 0 ]; then
error=1
errorMessages=$errorMessages"\n - ${file}"
fi
rm tmp
fi
fi
done <<<$(find . -iname "*.md" -type f)
if [ $error -ne 0 ]; then
echo "The following files contain errors:"
echo -e $errorMessages
echo ""
exit 1
fi
else
echo "Checking list of files: $file"
eval "arr=($file)"
for f in "${arr[@]}"; do
echo "processing file $f"
filesize=$(wc -c $f | awk '{print $1}')
if [[ $filesize -gt 20000 && $f ]]
then
echo "The file $f is too big, it will be split in sub files and checked:"
csplit -k -n 4 -s -f 'tmp' $f '/##/' '{100}'
while read tmp; do
clean_brackets $tmp
npx gramma check -m -p $parameters "$tmp"
done <<<$(find . -name "tmp*" -type f)
rm -rf tmp*
else
if [ "$mode" != "local" ]; then
cp $f tmp
clean_brackets tmp
npx gramma check -m $parameters tmp
rm tmp
else
npx gramma check -m $parameters $f
fi
fi
done
fi