Tuesday, May 18, 2010

Display code blocks on web page

In my blog, almost all my posts have some code blocks. Whenever I tried to post some code, I had greate difficulties to show the code blocks on the web page as how it should be on the normal editor. Later I realized that we can do it easily with the help of <pre> tag and some more changes and here is the script to do that some more.
usage ()
    cat <<EOF
Usage: $(basename $0) <code file>
exit 1;

# Check for the input argument
if [ $# -ne 1 ]; then

if [ ! -e "$1" ]; then
    echo "ERROR: File ($1) does not exist"
    exit 1

cat <<EOF
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<meta content="MSHTML 6.00.6000.17023" name="GENERATOR">
<meta http-equiv="Content-Type" content="text/html; charset=unicode">

sed -e 's/\&/\&amp;/g' -e 's/\"/\&quot;/g' -e 's/˜/\&tilde;/g' -e 's/>/\&gt;/g' -e 's/</\&lt;/g' $1

cat <<EOF