Thursday, July 22, 2010

Color My World – Syntax highlighter

crayon

Finally found a syntax highlighter for my blogger site. It's called Syntax Highlighter [Official Site]. Duh!

Steps to enable syntax highlighter into your blog site:

1.  Go to Design > Edit Html  image

2. Find the syntax </head>.

3. Copy and Paste below statements BEFORE syntax </head> 

<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/> 
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeRDark.css' rel='stylesheet' type='text/css'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCpp.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCss.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPhp.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPython.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushRuby.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushSql.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushVb.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPerl.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPowerShell.js' type='text/javascript'/>
<script language='javascript'>
SyntaxHighlighter.config.bloggerMode=true;
SyntaxHighlighter.config.clipboardSwf=&#39;http://alexgorbatchev.com/pub/sh/current/scripts/clipboard.swf&#39;;
SyntaxHighlighter.all();
</script>
 
&nsbp;

4. If you’re using windows live writer, you can integrate syntax highlighter using Precode Code Snippet Manager plug-in [Download here].


image


5. Here is example of Syntax Highlighter in action:

         public void ConvertStringDecimal(string stringVal) {
decimal decimalVal = 0;

try {
decimalVal = System.Convert.ToDecimal(stringVal);
System.Console.WriteLine(
"The string as a decimal is {0}.", decimalVal);
}
catch (System.OverflowException){
System.Console.WriteLine(
"The conversion from string to decimal overflowed.");
}
catch (System.FormatException) {
System.Console.WriteLine(
"The string is not formatted as a decimal.");
}
catch (System.ArgumentNullException) {
System.Console.WriteLine(
"The string is null.");
}

// Decimal to string conversion will not overflow.
stringVal = System.Convert.ToString(decimalVal);
System.Console.WriteLine(
"The decimal as a string is {0}.", stringVal);
}


If you don’t like to use the plug-in, you can directly surrounding your code snippet with <pre> </pre>, example:

<pre class="brush: js">     
/** * SyntaxHighlighter */
function foo()
{
if (counter <= 10)
return; // it works!
}
</pre>

Last, Welcome to my colorful world!

5 comments:

benjamin said...

Hey there,
Thanks for your code. I ran into trouble though:
adding the statements between 'head' and '/head' (full tag not allowed in comment), right before the end-tag, Blogger returns:

Your template could not be parsed as it is not well-formed. Please make sure all XML elements are closed properly.
XML error message: The entity "nsbp" was referenced, but not declared.


Do you have any suggestion? Thanks.

benjamin said...

Back again, removing the "nsbp" element and indenting the tags did prevent the error message.
But the code doesn't work for me though :(

Abhilash Kuntar said...
This comment has been removed by the author.
Abhilash Kuntar said...

It works fine. Thanks. Was looking for using syntax highlighter in my blog.
All, please remember one thing after doing the above steps mentioned, don't forget to read this:
http://alexgorbatchev.com/SyntaxHighlighter/manual/installation.html

Unknown said...
This comment has been removed by the author.