Code Formatting in Blogger Using SyntaxHighlighter



>> Friday, August 20, 2010

I wanted to use SyntaxHighlighter, but it turns out it takes a little tweaking on Blogger. I found a number of blogs that had instructions, but none of them seemed to work. Here is what I did to get the code formatting.

1. Upload files to Google Sites (Optional)
You need to link to a few javascript and css files. It looks like SyntaxHighlighter has a hosted version, but I decided to host the specific files I wanted. Here are the files you need:

  1. shCore.js
  2. shCore.css
  3. shCoreDefault.css (or your theme, I used shCoreEclipse.css)
  4. Brushes (for type of code). I used shBrushPlain.css, shBrushJava.css, and shBrushGroovy.css
2. Edit your Blogger template
Go to Design > Edit HTML. Add this code towards the bottom:





The "trick" for blogger is to set the "bloggerMode" to true.

3. Wrap your code in "pre"
For example, if I wanted to use blog about this code in Java, I would use class="brush: java" like this:


public static void main(String[] args){
System.out.println("Hello World!");
}


And it would look like this:

public static void main(String[] args){
System.out.println("Hello World!");
}

0 comments:

Post a Comment

  © Blogger template Webnolia by Ourblogtemplates.com 2009

Back to TOP