Friday 17 February 2012

Preparing for syntax highlighting to include code snippets onto my blog

As I intend to use Python extensively, it's about the time to think how to enable code snippets to be included into a blog. I mean with syntax highlighting and all that.

After a bit of research, I think I may go for http://alexgorbatchev.com/syntaxhighlighter/

Here is the recipe to get things running
1. Go to your blogger.com, log on, go to you "Dashboard" -> "Design" -> "Edit HTML"
2. Within "Edit template" search for </head>
3. Copy-and-paste the following code, right before the </head> tag

<!-- Adding CSS styles -->
<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/shCoreDefault.css' rel='stylesheet' type='text/css'/> 
 
<!-- Adding JS scripts -->
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'/> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shAutoloader.js' type='text/javascript'/> 
 
<!-- Adding Brushes -->
<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/shBrushCSharp.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/shBrushSql.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/shBrushCpp.js' type='text/javascript'/> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPython.js' type='text/javascript'/> 
<!-- Of course, you can remove those unused -->

 
<!-- Calling SyntaxHighlighter -->
<script language='javascript' type='text/javascript'> 
    SyntaxHighlighter.config.bloggerMode = true;
    SyntaxHighlighter.all();
</script>


As stated, you can remove lines with unwanted brushes.

And here JS example
function foo()
{
}

and some python example
class MyClass():
   def __init__(self):
      print 'It works!'

Naturally, all the credits go to the author of this lovely tool, so check out http://alexgorbatchev.com/

To correctly post e.g. HTML code, use any of the encoders, like this one: http://www.helperblogger.com/p/html-code-encoder.html