-
Notifications
You must be signed in to change notification settings - Fork 1
/
btidy.sh
executable file
·90 lines (81 loc) · 2.09 KB
/
btidy.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
#!/usr/bin/env bash
#
# btidy.sh
#
# Use HTML tidy on just the body of an HTML file.
# See https://www.html-tidy.org/ for documentation on tidy.
#
# When used as a filter, this will enclose each line of tidy output in
# an HTML comment.
#
# Usage:
# ./btidy.sh <filename> [options] ...
#
# # From vi family editors, assuming btidy.sh is in your path:
# :% ! btidy.sh [options]
# # vi example in a marked section:
# :'a,'b ! btidy.sh -i
#
# Copyright (C) 2024 by The Obscure Organization
#
# MIT licensed. See the LICENSE file for details.
#
# Release History:
#
# 1.0 (Sat Dec 14, 2024)
# First public release
# Use bash unofficial strict mode
set -euo pipefail
IFS=$'\n\t'
DEBUG=${DEBUG:-false}
# Thanks https://stackoverflow.com/a/17805088
$DEBUG && export PS4='${LINENO}: ' && set -x
TMPFILE=$(mktemp /tmp/btidy.XXXXXX)
TMPERR=$(mktemp /tmp/btidy-err.XXXXXX)
TMPOUT=$(mktemp /tmp/btidy-out.XXXXXX)
# remove tempfile on exit
function finish {
rm -f "$TMPFILE" "$TMPERR"
}
trap finish EXIT
# Nice, thanks https://www.cyberciti.biz/faq/linux-unix-bsd-apple-osx-bash-get-last-argument/
FILE="${BASH_ARGV[0]:-}"
if [ -z "$FILE" ]; then
ARGS="$*"
FILE=/dev/stdin
elif [ -f "$FILE" ]; then
# If a file is specified as the last CLI option, consume the last parameter
# tricksy - thanks https://unix.stackexchange.com/a/273531
# shellcheck disable=2124
ARGS="${@:1:$#-1}"
else
ARGS="$*"
FILE=/dev/stdin
fi
cat <<EOF > "$TMPFILE"
<!doctype html>
<html><head><title>invisible</title></head>
<body>
EOF
cat "$FILE" >> "$TMPFILE"
set +e
# shellcheck disable=SC2048,SC2086
tidy -q --show-body-only y $ARGS "$TMPFILE" > "$TMPOUT" 2>"$TMPERR"
EXITCODE=$?
set -e
if [[ "$EXITCODE" -gt 0 ]]; then
echo "WARNING: tidy had non-zero exit $EXITCODE" >> "$TMPERR"
fi
if [[ "$FILE" != "/dev/stdin" ]] \
&& echo "$ARGS" | grep --quiet -E -- '-m|--modify'
then
cat "$TMPFILE" > "$FILE"
else
if [[ -s "$TMPERR" ]]; then
TV="$(tidy --version)"
printf '<!-- %s -->\n' "$TV"
sed 's/^\(.*\)$/<!-- \1 -->/' "$TMPERR"
fi
cat "$TMPOUT"
fi
exit $EXITCODE