diff options
author | EuAndreh <eu@euandre.org> | 2022-09-01 16:33:55 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2022-09-01 16:33:55 -0300 |
commit | e65b4793a7ffb689fe201b23ee7025472f4ec448 (patch) | |
tree | eaaccc9b318f9c168a36e44ae0831f55dc92b9e0 /bin | |
parent | bin/slugify: Add new (working) utility (diff) | |
download | dotfiles-e65b4793a7ffb689fe201b23ee7025472f4ec448.tar.gz dotfiles-e65b4793a7ffb689fe201b23ee7025472f4ec448.tar.xz |
bin/html: Add new working utility
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/html | 96 |
1 files changed, 96 insertions, 0 deletions
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 |