- # HTML Entity Coding
- # Prints UTF-8 string as decimal Unicode Code Points
- # Useful for escaping user input for use in HTML text and attributes
- { [ $# -eq 0 ] && cat || printf %s "$*"; } \
- | hexdump -ve '/1 "%03o\n"' \
- | while read n; do
- case $n in
- # bitbanging octal UTF-8 chains into singular 7 digit octal numbers
- [01]??) printf '0000%s' $n;; # 7 bit ASCII character, nothing to do
- 2??) printf '%s' ${n#2};; # tail fragment, append 6 bit
- 3[0123]?) printf '000%s' ${n#3};; # 2 octet (11 bit) chain start
- 34?) printf '00%s' ${n#34};; # 3 octet (16 bit) chain start
- 35?) printf '01%s' ${n#35};; # 3 octet chain start, high
- 36?) printf '%s' ${n#36};; # 4 octet (21 bit) chain start
+ # Escape HTML cahracters
+ # Also escape [, ], and \n for use in html-sh
+ local str out
+ [ $# -eq 0 ] && str="$(cat)" || str="$*"
+ while [ "$str" ]; do
+ case $str in
+ \&*) out="${out}&";;
+ \<*) out="${out}<";;
+ \>*) out="${out}>";;
+ \"*) out="${out}"";;
+ \'*) out="${out}'";;
+ \[*) out="${out}[";;
+ \]*) out="${out}]";;
+ "${BR}"*) out="${out}
";;
+ *) out="${out}${str%"${str#?}"}";;