Thursday, August 9, 2012

How to Escape Code in Blogger Blogs

Trying to escape code in blogging software is always an adventure. My experience with Wordpress blogs is that some characters will sneak through the code tags you use, so precautions still need to be taken. This is also the case with escaping code in Blogger, but there's a fairly robust library available to use for the purpose.

Using Syntax Highlighter to Escape Code

The Syntax Highlighter library can either be downloaded from http://alexgorbatchev.com/SyntaxHighlighter/ or you can call it directly from their server. Either way, you'll want to edit your theme and include the following above the closing </head> tag:
<!--SYNTAX HIGHLIGHTER BEGINS-->
<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/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCpp.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.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/shBrushJava.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/shBrushPython.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushRuby.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushSql.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPerl.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>
<!--SYNTAX HIGHLIGHTER ENDS-->

Syntax Highlighter Usage and Precautions

  • To escape html code, use the following tag: <pre class="brush:html">code goes here</pre>.
  • To escape php code, use the following tag: <pre class="brush:php">code goes here</pre>.
Note that if you use the brush:html option, you'll still need to use something to escape your html to escape the html characters (the less than sign, greater than sign and the ampersand need to be escaped so that the html won't get interpreted).

No comments:

Post a Comment