1
0
Fork 0
mirror of https://git.sr.ht/~seirdy/seirdy.one synced 2024-11-27 22:12:10 +00:00
seirdy.one/scripts/bin/validate-feed

80 lines
1.7 KiB
Bash
Executable file

#!/usr/bin/env dash
set -u
# the name of this program
progname="$(basename "${0}")"
help_text="Usage: ${progname} [options...] URL
Validate the given feed URL, excluding false-positives
Options:
-h Print this help and exit
"
usage() {
printf '%s' "${help_text}"
}
# when the user passess bad args, send a msg to stderr and exit
# usage: bad_option <option> <reason>
bad_option() {
echo "${progname}: option ${1}: ${2}" >&2
usage >&2
exit 1
}
while getopts "ho" flags; do
case ${flags} in
h)
usage
exit 0
;;
*)
bad_option "${flags}" 'invalid option'
exit 1
;;
esac
done
url="$1"
find_rel_mention_count() {
curl -s "$url" | grep -cF 'rel="mentioned"'
}
rel_mention_count="$(find_rel_mention_count)"
run_validator() {
"$GHQ_ROOT/github.com/w3c/feedvalidator/src/demo.py" "$url"
}
validate_feed() {
rel_mention_string="Unregistered link relationship \($rel_mention_count occurrence"
if [ "$rel_mention_count" = '1' ]; then
rel_mention_string="Unregistered link relationship"
fi
# silence "self reference doesn't match" because i'm testing a localhost copy.
# 'should not contian" has a false positive triggered by ARIA
# entries with the same timestamp isn't a big deal
# unregistered link relationship is a false positive caused by an unknown namespace.
full_regex="Use of unknown namespace|Self reference doesn't match|should not contain (role|aria-labelledby|aria-label|aria-describedby|aria-hidden|controls) attribute|$rel_mention_string|entries with the same value|Validating $url"
run_validator \
| grep -Ev "$full_regex"
}
validator_output="$(validate_feed)"
if [ "$validator_output" = '' ]; then
echo "Feed is valid"
exit 0
else
echo "Feed errors found!" 1>&2
echo "$validator_output"
exit 1
fi
# vi:ft=sh