Howto Install SyntaxHighligher In Blogger

SyntaxHighlighter helps a developer/coder to post code snippets online with ease and have it look pretty. SyntaxHighlighter is developed in Javascript by Alex Gorbatchev. SyntaxHighlighter is the most popular script for syntax highlighting and widely used by many blogs, websites. It has many wonderful features like: view source code in plain text, copy to clipboard, valid XHTML, … SyntaxHighlighter supports many languages and has bunch of themes.

Install SyntaxHighlighter In Blogger

Installing SyntaxHighlighter In Blogger is easy, just open your template and paste the following code right before the close tag </head>:

<link href="http://alexgorbatchev.com/pub/sh/current/styles/shCore.css" rel="stylesheet" type="text/css"></link>
<link href="http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css" rel="stylesheet" type="text/css"></link>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js" type="text/javascript"></script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCss.js" type="text/javascript"></script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js" type="text/javascript"></script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPhp.js" type="text/javascript"></script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js" type="text/javascript"></script>
<script language='javascript'>
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = 'http://alexgorbatchev.com/pub/sh/current/scripts/clipboard.swf';
SyntaxHighlighter.all();
</script>

SyntaxHighlighter supports many languages, each language has its own javascript file. To install syntax highlighting for more languages, you can view list of supported languages, get the javascript’s file name and include it with the following syntax:

<script src="http://alexgorbatchev.com/pub/sh/current/scripts/LANGUAGE_FILE_NAME.js" type="text/javascript"></script>

There’re many themes for SyntaxHighlighter, too. To change theme, go to the homepage and get the file name for theme and change this line:

<link href="http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css" rel="stylesheet" type="text/css"></link>

into:

<link href="http://alexgorbatchev.com/pub/sh/current/styles/THEME_FILE_NAME" rel="stylesheet" type="text/css"></link>

Howto Use SyntaxHighlighter

To highlight code, just wrap it into a pre tag with the following syntax:

<pre class="brush: js">
// Your code here
</pre>

Notice the class="brush: js". It defines the language for syntax highlighting, in this situation: JS – Javascript. To know more about brushes, you can see a list of them on the homepage.

The second important note is the code inside pre tag must be escaped (< and > must be changed into &lt; and &gt;). There’re many online tools for blogger to do that, one of them is Code Converter.

Using pre tag is the traditional way. Another way to highlight code is using script tag:

<script type="syntaxhighlighter" class="brush: html"><![CDATA[
// Your code here
]]></script>

The benefit of this method is ability to place virtually anything inside the CDATA without having to escape anything, so this allows for a straight ‘cut and paste’ experience from your favorite text editor.

If you need more information about SyntaxHighlighter or more configuration, please go to the homepage. You can see SyntaxHighlighter in action here, at Deluxe Blog Tips. It’s really pretty and I’m feel good with it.

1 Comment

  1. Hi. Rilwis.. i really want to know how to create a blogger template from scratch… maybe you can write a tutorial about this. 🙂

Leave a Reply