Friday, 18 July 2008

SyntaxHighlighter

Ok so I've given in and blogged. I needed somewhere to leave a trail of breadcrumbs so I can find my way back. So here I am. This is more of a personal blog rather than a platform - sometimes it's easier to link to a posting than post a long answer in a forum for the technical. Other times I just want to order my thoughts outside my head, and my handwritting is excruciating.

One of the requirements of the blog was to be able to post highlighted code syntax particularly in python. For this I'm using SyntaxHighlighter 1.5.1 which is excellent for this purpose but true to the spirit of the blog needs a little tweaking especially to use with blogger.
  1.  Download and unrar the code from the SyntaxHighlighter website
  2. To make the code xhtml compatible alter line 145 in shCore.js: change "if(tags[i].getAttribute('name')==name)" to "if(tags[i].getAttribute('title')==name)"
  3. The python highlighter doesn't work in safari so get my shBrushPython file and use that in place.
  4. Upload your files somewhere (I added mine to Google Pages as others have suggested - which I had to do in IE 7 as Safari doesn't show the upload function on Google Pages.
  5. Change your Blog template as described in this post.
  6. Use the newer Blogger in draft post editor to add your posts to avoid the annoying < br / > insertions by the default editor (at July 08). You should be able to add something like this to your posts!
<pre class="python" title="code">#A module
import something
</pre>

Which will give you hopefully highlighted code like this:

#A Module
import something

Some other tests with the syntax highlighter. Its pretty basic but good enough for my purposes.

a_single_quoted_string = 'single'
a_double_quoted_string = "double"
triple_quoted = """ triple
across lines"""
def somefunction(some_arg=None):
   pass
   return
somefunction()
class someclass(object):
   """Code here"""
   def __init__():
      pass

0 comments:

Hedged Down