From e65b4793a7ffb689fe201b23ee7025472f4ec448 Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Thu, 1 Sep 2022 16:33:55 -0300 Subject: bin/html: Add new working utility --- bin/html | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100755 bin/html (limited to 'bin') diff --git a/bin/html b/bin/html new file mode 100755 index 0000000..f3f23de --- /dev/null +++ b/bin/html @@ -0,0 +1,96 @@ +#!/bin/sh +set -eu + + +usage() { + cat <<-'EOF' + Usage: + html [-e|d] + html -h + EOF +} + +help() { + cat <<-'EOF' + + Options: + -e escape the string (the default action) + -d unescape (de-escape?) the string + -h, --help show this message + + + Get a string from STDIN and convert it to/from HTML escaping. + + + Examples: + + oij + + $ printf 'a > 5 && !b' | html + a > 5 && !b + + + Unescape the content from a file: + + $ html -d < file.html + EOF +} + + +for flag in "$@"; do + case "$flag" in + --) + break + ;; + --help) + usage + help + exit + ;; + *) + ;; + esac +done + +ENCODE=false +DECODE=false +while getopts 'edh' flag; do + case "$flag" in + e) + ENCODE=true + ;; + d) + DECODE=true + ;; + h) + usage + help + exit + ;; + *) + usage >&2 + exit 2 + ;; + esac +done +shift $((OPTIND - 1)) + +if [ "$ENCODE" = true ] && [ "$DECODE" = true ]; then + printf 'Both -e and -d given. Pick one.\n' >&2 + usage >&2 + exit 2 +elif [ "$DECODE" = true ]; then + sed \ + -e 's|&|\&|g' \ + -e 's|<|<|g' \ + -e 's|>|>|g' \ + -e 's|"|"|g' \ + -e "s|'|'|g" +else + sed \ + -e 's|&|\&|g' \ + -e 's|<|\<|g' \ + -e 's|>|\>|g' \ + -e 's|"|\"|g' \ + -e "s|'|\'|g" +fi -- cgit v1.2.3