aboutsummaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2022-09-01 16:33:55 -0300
committerEuAndreh <eu@euandre.org>2022-09-01 16:33:55 -0300
commite65b4793a7ffb689fe201b23ee7025472f4ec448 (patch)
treeeaaccc9b318f9c168a36e44ae0831f55dc92b9e0 /bin
parentbin/slugify: Add new (working) utility (diff)
downloaddotfiles-e65b4793a7ffb689fe201b23ee7025472f4ec448.tar.gz
dotfiles-e65b4793a7ffb689fe201b23ee7025472f4ec448.tar.xz
bin/html: Add new working utility
Diffstat (limited to 'bin')
-rwxr-xr-xbin/html96
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 &gt; 5 &amp;&amp; !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|&amp;|\&|g' \
+ -e 's|&lt;|<|g' \
+ -e 's|&gt;|>|g' \
+ -e 's|&quot;|"|g' \
+ -e "s|&#39;|'|g"
+else
+ sed \
+ -e 's|&|\&amp;|g' \
+ -e 's|<|\&lt;|g' \
+ -e 's|>|\&gt;|g' \
+ -e 's|"|\&quot;|g' \
+ -e "s|'|\&#39;|g"
+fi