« Hello World! | Main | Switch between different JDK versions in Windows »

Blog your code using SyntaxHighlighter

SyntaxHighlighter is an excellent tool for displaying code snippets with colored syntax highlighting, line highlighting and line numbers. Below is an example showing a Java code rendered using SyntaxHighlighter.

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

It's a purely client-side tool, as all the processing is done strictly within the browser through JavaScript. There is no server side processing. To include code in your blog post, just copy the code from your favorite IDE/Editor and paste it in your post. As of v1.5.1, SyntaxHighlighter supports C, C++, C#, CSS, Delphi, HTML, Java, JavaScript, PHP, Pascal, Python, Ruby, SQL, VB, VB.NET, XML, XSLT.

Setting Up SyntaxHighlighter with Movable Type

Download latest version of SyntaxHighlighter and extract the archive.You can see that there is a JavaScript file for each language e.g. shBrushJava.js for Java. These scripts use regular expressions to parse the text and classify them as keywords, comments etc.

Now upload the Scripts and Styles folders to your blog. I have uploaded them to Site Root/SyntaxHighlighter/ folder as shown below.

Snap1.jpg

Now add script and style references to your blog. For this, insert following code just before </head> tag.















and following code just before </body> tag in Main Index template and all Archive templates.


Using SyntaxHighlighter

You can highlight the code by surrounding it with <pre> tag as shown below.

public void printHello(){
    System.out.println("Hello World!");
}

Set name attribute to code and class attribute to one of the language aliases you wish to use, such as java, xml, sql, ruby etc. (Full list of supported languages is here). You can also specify options with class attribute as shown here.

SyntaxHighlighter is a very handy tool for posting your codes in formatted manner to your blog. Also you can add support for other languages easily. A quick start guide is located here.

Note: During posting a blog post, choose Text Formatting as "None" instead of "Convert Line Breaks" in Movable Type Blog Post Editor otherwise extra <br> tags would be inserted in the code.

TrackBack

TrackBack URL for this entry:
http://blogs.oracle.com/mt/mt-tb.cgi/8839

Listed below are links to weblogs that reference Blog your code using SyntaxHighlighter:

» Testing Syntax Highlighter from blogs.oracle.com
This is to test my configuration of SyntaxHighlighter from this post . public void printHello(){ System [Read More]

Comments (2)

I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.

Miriam

http://www.craigslistguide.info

Good state! Add to favorite

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About This Entry

This page contains a single entry from the blog posted on December 6, 2008 7:18 PM.

The previous post in this blog was Hello World!.

The next post in this blog is Switch between different JDK versions in Windows.

Many more can be found on the main index page or by looking through the archives.

Creative Commons License
This weblog is licensed under a Creative Commons License.
Powered by
Movable Type and Oracle