| 1 |
# -*-Shell-script-*- |
| 2 |
# |
| 3 |
# Copyright (C) 2015-2020 SUSE Software Solutions Germany GmbH |
| 4 |
# |
| 5 |
# Author: |
| 6 |
# Frank Sundermeyer <fsundermeyer at opensuse dot org> |
| 7 |
# |
| 8 |
########################################################################### |
| 9 |
# |
| 10 |
# SPELLCHECK |
| 11 |
# |
| 12 |
# Spellchecks a file specified with -f or --file or |
| 13 |
# the document specified by rootid. |
| 14 |
# |
| 15 |
########################################################################### |
| 16 |
|
| 17 |
function spellcheck { |
| 18 |
local SHORT_OPTS LONG_OPTS SUB_CMD |
| 19 |
local COUNT FILE LISTMODE LISTOUTPUT LIST_PARAM OPEN_PARAM SC |
| 20 |
local SPELLCMD SPELL_CHECKER_CMD |
| 21 |
local -a FILELIST SKIPLIST |
| 22 |
|
| 23 |
SUB_CMD=$1 |
| 24 |
shift |
| 25 |
|
| 26 |
SHORT_OPTS="h" |
| 27 |
LONG_OPTS="extra-dict:,file:,help,lang:,list,rootid:,spellchecker:" |
| 28 |
|
| 29 |
parse_args "$SHORT_OPTS" "$LONG_OPTS" "$SUB_CMD" "$@" |
| 30 |
eval set -- "$P_REMAIN_ARGS" |
| 31 |
|
| 32 |
# checker was provided on the command line |
| 33 |
if [[ -n "$SPELL_CHECKER" ]]; then |
| 34 |
# check whether the given spellchecker is valid |
| 35 |
[[ "aspell" = $SPELL_CHECKER || "hunspell" = $SPELL_CHECKER ]] || \ |
| 36 |
exit_on_error "Wrong value for --spellchecker. Needs to be \"aspell\" or \"hunspell\"" |
| 37 |
# check whether given spellchecker is installed: |
| 38 |
[[ $(which --skip-alias --skip-functions "$SPELL_CHECKER" 2>/dev/null) ]] || ccecho "error" "Error: '$SPELL_CHECKER' is not installed" |
| 39 |
else |
| 40 |
if [[ -z "$SPELL_CHECKER" ]]; then |
| 41 |
# no spell checker defined in the configs or on the command line |
| 42 |
# searching for aspell or hunspell |
| 43 |
# If both are installed, favor aspell |
| 44 |
for SC in aspell hunspell; do |
| 45 |
SPELL_CHECKER="$(which --skip-alias --skip-functions "$SC" 2>/dev/null)" |
| 46 |
[[ -n "$SPELL_CHECKER" ]] && break; |
| 47 |
done |
| 48 |
[[ -z "$SPELL_CHECKER" ]] && exit_on_error "Cannot perform a spell check; neither aspell nor hunspell is installed" |
| 49 |
fi |
| 50 |
fi |
| 51 |
|
| 52 |
#------ Computing the values returned from the parser ----- |
| 53 |
if [[ 1 -eq $P_HELP ]]; then |
| 54 |
help_scmd_head "$SUB_CMD" "${HELP_SUBCOMMAND[$SUB_CMD]}" |
| 55 |
help_extra-dict |
| 56 |
help_file |
| 57 |
help_help |
| 58 |
help_lang |
| 59 |
help_list |
| 60 |
help_rootid |
| 61 |
help_spellchecker |
| 62 |
echo -e " NOTES: * Options --file (-f) and --rootid exclude one another.\n * If neither file nor rootid is specified, the rootid\n from the DC-file is used\n * $SUB_CMD follows xi:includes\n" |
| 63 |
exit 0 |
| 64 |
fi |
| 65 |
|
| 66 |
[[ -n "$P_FILE" ]] && FILELIST[0]="$P_FILE" |
| 67 |
|
| 68 |
# set FILELIST if rootid was specified |
| 69 |
if [[ -z "$P_ROOTID" && -z "$P_FILE" ]]; then |
| 70 |
if [[ 0 -ne $VERBOSITY ]]; then |
| 71 |
ccecho "info" "Neither file nor rootid specified, using rootid from DC-file" |
| 72 |
fi |
| 73 |
elif [[ -n "$P_ROOTID" && -n "$P_FILE" ]]; then |
| 74 |
exit_on_error "Options --file and --rootid exclude one another.\nPlease specify only one of these options" |
| 75 |
fi |
| 76 |
|
| 77 |
if [[ -z "${FILELIST[0]}" ]]; then ## --file was not specified |
| 78 |
FILELIST=( $(call_make list-srcfiles silent LIST_NODC=1 LIST_NOENT=1 LIST_NOIMG=1 ) ) |
| 79 |
if [[ 0 -ne $? ]]; then |
| 80 |
exit_on_error "Failed to get filelist for ROOTID \"$ROOTID\"" |
| 81 |
fi |
| 82 |
fi |
| 83 |
|
| 84 |
# Set language - if not specified anywhere try to get from $MAIN |
| 85 |
# |
| 86 |
if [[ -n "$P_LANG" ]]; then |
| 87 |
SPELL_LANG="$P_LANG" |
| 88 |
else |
| 89 |
if [[ -z "$SPELL_LANG" ]]; then |
| 90 |
SPELL_LANG=$($XSLTPROC --stylesheet "${DAPSROOT}/daps-xslt/common/get-language.xsl" --file "$MAIN" "$XSLTPROCESSOR" 2>/dev/null) |
| 91 |
if [[ -n "$SPELL_LANG" ]]; then |
| 92 |
ccecho "info" "Using language $SPELL_LANG." |
| 93 |
else |
| 94 |
exit_on_error "Failed to get language from $MAIN. Please specify it with --lang." |
| 95 |
fi |
| 96 |
fi |
| 97 |
fi |
| 98 |
|
| 99 |
#------------------ |
| 100 |
# create spellchecking command for both aspell and hunspell |
| 101 |
# |
| 102 |
|
| 103 |
# first, create skiplist for aspell (not supported by hunspell) |
| 104 |
if [[ "aspell" = ${SPELL_CHECKER##*/} ]]; then |
| 105 |
COUNT=0 |
| 106 |
for SKIP in $SPELL_SKIP_TAGS; do |
| 107 |
SKIPLIST[$COUNT]="--add-sgml-skip=$SKIP" |
| 108 |
let COUNT++ |
| 109 |
done |
| 110 |
fi |
| 111 |
# second, create the command |
| 112 |
case "${SPELL_CHECKER##*/}" in |
| 113 |
aspell ) |
| 114 |
# check and sanitize path to extra-dict |
| 115 |
sanitize_path "$2" "SPELL_EXTRA_DICT" |
| 116 |
# create skiplist (not supported by hunspell) |
| 117 |
COUNT=0 |
| 118 |
for SKIP in $SPELL_SKIP_TAGS; do |
| 119 |
SKIPLIST[$COUNT]="--add-sgml-skip=$SKIP" |
| 120 |
let COUNT++ |
| 121 |
done |
| 122 |
SPELLCMD="$SPELL_CHECKER --mode=sgml ${SKIPLIST[*]} --encoding=utf-8 --lang=$SPELL_LANG" |
| 123 |
if [[ -n "$SPELL_EXTRA_DICT" ]]; then |
| 124 |
SPELLCMD+=" --extra-dicts=$SPELL_EXTRA_DICT" |
| 125 |
fi |
| 126 |
OPEN_PARAM=" -c" |
| 127 |
LIST_PARAM="list" |
| 128 |
;; |
| 129 |
hunspell ) |
| 130 |
# check language |
| 131 |
if [[ ! "$SPELL_LANG" =~ ^[a-z][a-z]_[A-Z][A-Z]$ ]]; then |
| 132 |
if [[ -n "$P_LANG" ]]; then |
| 133 |
exit_on_error "Wrong data format for --lang; must be xx_XX, e.g. en_US" |
| 134 |
else |
| 135 |
exit_on_error "Could not automatically determine correct language parameter\n(Format must be xx_XX, e.g. en_US), please specify it with --lang" |
| 136 |
fi |
| 137 |
fi |
| 138 |
SPELLCMD="$SPELL_CHECKER -H -i utf-8 -d $SPELL_LANG" |
| 139 |
if [[ -n "$SPELL_EXTRA_DICT" ]]; then |
| 140 |
SPELLCMD+=",$SPELL_EXTRA_DICT" |
| 141 |
fi |
| 142 |
LIST_PARAM="-l" |
| 143 |
;; |
| 144 |
esac |
| 145 |
|
| 146 |
# show spellchecker command with --debug |
| 147 |
[[ 1 -eq $DEBUG ]] && echo "DEBUG: $SPELLCMD" |
| 148 |
|
| 149 |
# do spellchecking on the list of files |
| 150 |
for FILE in "${FILELIST[@]}"; do |
| 151 |
test -f "$FILE" || ccecho "warn" "File $FILE does not exist" |
| 152 |
if [[ 1 -eq $LISTMODE ]]; then |
| 153 |
LISTOUTPUT=$($SPELLCMD $LIST_PARAM < "$FILE" | sort -u 2>/dev/null) |
| 154 |
[[ -n "$LISTOUTPUT" ]] && echo -e "$FILE\n------------------------------\n$LISTOUTPUT\n" |
| 155 |
LISTOUTPUT="" |
| 156 |
else |
| 157 |
echo "Checking $FILE..." |
| 158 |
$SPELLCMD $OPEN_PARAM $FILE |
| 159 |
fi |
| 160 |
done |
| 161 |
exit |
| 162 |
} |