Syntax highlighting

Jun 10th, 2010No Comments

As we will (hopefully) over time be posting many code snippets, I’ve been looking at some syntax highlighting plugins to make any such code more readable. This is a brief demo of what I’ve come up with so far, first using a bash script I use to update backup routines on remote hosts and kickoff incremental backups;

for i in `cat ~/etc/host_list`
echo “Updating: $i ($interval)”
scp ~/etc/backup.excludes [email protected]$i:/etc
scp ~/bin/ [email protected]$i:/usr/local/bin
ssh [email protected]$i “echo ‘nice -n19 /usr/local/bin/’|at now + $interval minute”
interval=`expr $interval + 10`
echo “”

Next a bit of python I use to roll-out an updated / global ‘hosts’ file to a bunch of remote hosts, I guess this could be done in bash but for some reason I did it in Python ..

import sys
import subprocess as sub
configuration = “etc/host_table”

with open(configuration) as io:
for line in iter(io):
line = line.strip()
if line[0]==’#’: continue
host,ip = line.split()
cmd = “scp etc/hosts [email protected]%s:/etc/hosts” % host
print “Updating: %s (%s)” % (host,cmd)
pipe =         sub.Popen(cmd,bufsize=8192,stdout=sub.PIPE,shell=True)
result = pipe.communicate()
print “Done..”
if not result[1]: continue
if len(result[0]) or len(result[1]):
print “Result:”
print result[0]
print “Advisory:”
print result[1]

Ok, well that all looks pretty cool, so to add syntax highlighting to a post, simply enclose your code snipped in ‘[‘<language> ‘]’ tags, and inside the brackets there are a number of parameters you can add as follows;

  • font_size=”(size as %)”
  • light (to lighten the display)
  • toolbar=fase (to hide the toolbar)
  • gutter=false (to hide the line numbers)
  • highlight_lines=”<line>,<line>…” (to highlight specific lines)
  • num=”start” (line number to start numbering from)

Language support seems extensive, so just make a guess and it should work.

About author:

All entries by

Leave a Reply

You must be logged in to post a comment.