aboutsummaryrefslogtreecommitdiff
path: root/bin/htmlesc
diff options
context:
space:
mode:
Diffstat (limited to 'bin/htmlesc')
-rwxr-xr-xbin/htmlesc33
1 files changed, 27 insertions, 6 deletions
diff --git a/bin/htmlesc b/bin/htmlesc
index d9c59bd..2655b74 100755
--- a/bin/htmlesc
+++ b/bin/htmlesc
@@ -5,7 +5,7 @@ set -eu
usage() {
cat <<-'EOF'
Usage:
- htmlesc [-e|d]
+ htmlesc [-e|d] [CONTENT...]
htmlesc -h
EOF
}
@@ -18,15 +18,18 @@ help() {
-d unescape (de-escape?) the string
-h, --help show this message
+ CONTENT a literal string to be escaped
- Get a string from STDIN and convert it to/from HTML escaping.
+
+ Convert data to/from HTML escaping. In CONTENT is not given,
+ get data from STDIN.
Examples:
- oij
+ Escape HTML control characters in a string:
- $ printf 'a > 5 && !b' | htmlesc
+ $ htmlesc 'a > 5 && !b'
a &gt; 5 &amp;&amp; !b
@@ -79,18 +82,36 @@ if [ "$ENCODE" = true ] && [ "$DECODE" = true ]; then
printf 'Both -e and -d given. Pick one.\n' >&2
usage >&2
exit 2
-elif [ "$DECODE" = true ]; then
+fi
+
+decode() {
sed \
-e 's|&amp;|\&|g' \
-e 's|&lt;|<|g' \
-e 's|&gt;|>|g' \
-e 's|&quot;|"|g' \
-e "s|&#39;|'|g"
-else
+}
+
+encode() {
sed \
-e 's|&|\&amp;|g' \
-e 's|<|\&lt;|g' \
-e 's|>|\&gt;|g' \
-e 's|"|\&quot;|g' \
-e "s|'|\&#39;|g"
+}
+
+if [ "$DECODE" = true ]; then
+ FN=decode
+else
+ FN=encode
+fi
+
+if [ $# = 0 ]; then
+ "$FN"
+else
+ for s in "$@"; do
+ printf '%s\n' "$s" | "$FN"
+ done
fi