Salient features
- Supports colorization of C#, C++, Java, JavaScript, VBScript
- Can be extended easily to provide colorization for other languages
- The colorization/parsing happens on the client side. You can post plain code not encumbered by embedded HTML tags in any way.
How to use
1. Paste the following code in the head section of your HTML markup. For blogspot you need to edit the XML file representing the page layout. Goto Blogger->Layout->Edit HTML. Download the XML file. Update the head section and upload your changes.
<link href='http://tinyurl.com/yd6kzwq' rel='stylesheet' type='text/css'/>
<script src='http://tinyurl.com/y87zt6f' type='text/javascript'/>
<script type='text/javascript'>
function run_colorizer()
{
var preTags = document.getElementsByTagName("pre");
var colorizer = new Colorizer(preTags);
colorizer.Run();
}
</script>
2. Add an onload event handler to your body tag...
<body onload='run_colorizer()'>
NOTE: The above two steps need to be done only once.
3. Surround your code with the pre tag; also indicate the language as follows
<pre class="cppcode">
// WSHTest.cpp : Defines the entry point for the console application.
//
#import "progid:Wscript.Shell" no_namespace \
rename("FreeSpace", "WshFreeSpace") \
rename("ExpandEnvironmentStrings", "WshExpandEnvironmentStrings") \
rename("AddPrinterConnection", "WshAddPrinterConnection") \
rename("SetDefaultPrinter", "WshSetDefaultPrinter") \
rename("DeleteFile", "WshDeleteFile") \
rename("MoveFile", "WshMoveFile") \
rename("CopyFile", "WshCopyFile")
int main()
{
return 0;
}
</pre>
Here is how your readers will see it...
// WSHTest.cpp : Defines the entry point for the console application.
//
#import "progid:Wscript.Shell" no_namespace \
rename("FreeSpace", "WshFreeSpace") \
rename("ExpandEnvironmentStrings", "WshExpandEnvironmentStrings") \
rename("AddPrinterConnection", "WshAddPrinterConnection") \
rename("SetDefaultPrinter", "WshSetDefaultPrinter") \
rename("DeleteFile", "WshDeleteFile") \
rename("MoveFile", "WshMoveFile") \
rename("CopyFile", "WshCopyFile")
int main()
{
return 0;
}
NOTE: Languages available as of now are ...
1. jscode - JavaScript
2. cppcode - C++
3. csharpcode - C#
4. vbscriptcode - VBScript //Incomplete
5. javacode - Java
Checkout other entries in this blog to see some more colorful code.
That's all for now. In the next part of this entry I will show you how you can extend the colorizer for your favourite language.
UPDATE
I have updated the code to handle some Internet Explorer quirks. It should now work with
1. IE 6
2. Firefox 3.5.4
3. Chrome 3.0.195.27
I Haven't tested with other browsers or other versions of the above browsers for that matter but I think this covers a lot of ground. But still if somebody does find a fringe case where the Colorizer stops working then please do let me know.