The function:
tohtm(){ IFSBACK=$IFS;IFS=$(echo -en "\n\b");string2htm(){ [[ $1 ]] && echo $1|sed -e 's/\&/\&/g' -e 's/\"/\"/g' -e 's/</\</g' -e 's/>/\>/g' -e 's/>/\>/g'; };if [ ! -t 0 ];then echo -e "<html>\n<head></head>\n<body>\n<p>";for i in $(cat /dev/stdin); do string2htm $i;echo "<br/>";done;echo -e "</p>\n</body>\n</html>";fi;IFS=$IFSBACK; }
Syntax to run this function would be is similar following example commands:
ls - la |tohtm >thisfolder.html
Or
echo 'this & that is < than "something", but > than "something else"' |tohtm
Both of the above commands should work, but there may be others that don't (if the string includes single quotes for example). I still need to do some work on initially formatting such strings, but this function has been very useful for me so far.
you can also simply use cgi.escape from python. for example:
2htm(){ if ! [ -t 0 ];then myvar=$(cat /dev/stdin); else if [ "$1" ];then myvar=$@;fi;fi;pycom=$(echo -e "from cgi import escape\nprint escape(u'''$myvar''')"); python -c "$pycom"; }
That is about all I have for this post. I will post other similar functions (and perhaps this one again) in a future post. Thanks for reading.
No comments:
Post a Comment