diff options
author | EuAndreh <eu@euandre.org> | 2023-04-03 13:29:32 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2023-04-03 13:29:32 -0300 |
commit | 81dc688c94c710d9658861245a889c55543dffcd (patch) | |
tree | a2eb88223b03458eb301d48e231ce1cdb5274fb1 | |
parent | bin/uri: Allow string argument to be given instead of STDIN (diff) | |
download | dotfiles-81dc688c94c710d9658861245a889c55543dffcd.tar.gz dotfiles-81dc688c94c710d9658861245a889c55543dffcd.tar.xz |
bin/htmlesc: Allow string argument to be given instead of STDIN
-rwxr-xr-x | bin/htmlesc | 33 |
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 > 5 && !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|&|\&|g' \ -e 's|<|<|g' \ -e 's|>|>|g' \ -e 's|"|"|g' \ -e "s|'|'|g" -else +} + +encode() { sed \ -e 's|&|\&|g' \ -e 's|<|\<|g' \ -e 's|>|\>|g' \ -e 's|"|\"|g' \ -e "s|'|\'|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 |