Опасная зона

Опасная зона

Saturday, February 26, 2011

Syntax highlighting of code in blogger

I have not been too happy with the lack of code highlighting in blogger. For example, here is a little script I wrote "mkfile.sh" which creates a bunch of files with random contents. Switching to the HTML editor and encapsulating the code in <pre> </pre> html tags results in:

#!/bin/bash

if [ $# != 3 ]
then
  echo "Create a bunch of files with random contents."
  echo "Usage:"
  echo "$0 filename number size"
  echo "  filename: basename of the file"
  echo "  number  : number of files to be created"
  echo "  size    : file size in bytes"
  exit   
fi

for (( i = 0 ; i < $2 ; i++ ))
do
  echo "Writing $3 bytes to $1.$i"
  dd if=/dev/urandom of=$1.$i bs=$3 count=1 > /dev/null
done

which is not all that readable. There are code viewers on the web which converts the code to nicely formatted HTML, a quick googeling directs to e.g. this one:
http://codeformatter.blogspot.com/2009/06/about-code-formatter.html




 #!/bin/bash  
 if [ $# != 3 ]  
 then  
  echo "Create a bunch of files with random contents."  
  echo "Usage:"  
  echo "$0 filename number size"  
  echo " filename: basename of the file"  
  echo " number : number of files to be created"  
  echo " size  : file size in bytes"  
  exit    
 fi  
 for (( i = 0 ; i < $2 ; i++ ))  
 do  
  echo "Writing $3 bytes to $1.$i"  
  dd if=/dev/urandom of=$1.$i bs=$3 count=1   
 done  

Right, formatting has improved, but there is still no syntax highlighting. The best solution I could find is the one at http://tohtml.com

#!/bin/bash

if [ $# != 3 ]
then
  echo "Create a bunch of files with random contents."
  echo "Usage:"
  echo "$0 filename number size"
  echo "  filename: basename of the file"
  echo "  number  : number of files to be created"
  echo "  size    : file size in bytes"
  exit   
fi

for (( i = 0 ; i < $2 ; i++ ))
do
  echo "Writing $3 bytes to $1.$i"
  dd if=/dev/urandom of=$1.$i bs=$3 count=1 
done

which looks the way I wanted it!

Now I just have to figure out what to do with thousands of 1k garbage files flocking around in my directory.

2 comments:

  1. It would be nice to be able to combine the striped background with the syntax highlighting.

    ReplyDelete
  2. You can surely write a python script which fuses the two html codes. :D

    ReplyDelete