Sunday Jun 01, 2008
Today's Page Hits: 379
Sunday Jun 01, 2008
After writing the previous post, an idea provoked me to look for a solution to post code into my blog. Actually, I wanted to give an in-code line by line explanation, instead of just an overview, and that obviously needed pasting the code in the blog post, not just attaching or linking to it as an uploaded file. Most people just use the simple approach of putting code in <pre> </pre> tags, or maybe using div's with stylized classes in the blog's CSS. I wanted more than that. I've seen a lot of blogs post beautiful syntax colored, indented, stylized code into their blogs using stylish blocks implemented using CSS. There is also, ofcourse, a Drupal module which automatically does formatting of any code posting in <code> </code> tags. That's neat. Now I wanted a solution to be able to easily do just that, without the hassles of doing it using an external tool, or doing the conversion/formatting manually each time. I thought to myself, now, there must be some web 2.0 site which allows you to signup, maintain a collection of code snippets, choose source language (C, C++, Java, XML, etc.) and provide widgets, clippets, RSS feeds of the code or the like, which you can easily paste in your blog using <script> </script> tags. But there obviously wasn't something this exotic.. I was dreaming. Good project idea though, if you're crazy enough :) Since I was primarily targeting to do this on my Sun blog, I thought there must be some roller plugin to do automatic syntax highlighting on the server side identified by code tags in the blog post, but yet again, there wasn't any such innovation available.
Thereafter, I sought the help of the Sun Blogging Community alias, which has always come of help whenever there is any blogs.sun.com related trouble. Here are the many different solutions they gave:
SyntaxHighlighter is an amazing project. It gives you compressed javacsript code, so you can use it invariable of which server your blog is running on and requires no scripting support at all, everything neatly done on the client-side, with no compromise in speed, and very little overhead on page download size.
For those of you wanting to install it to your blogs.sun.com blog:
<!-- Syntax Highlighter Plugin -->
<script language="javascript" src="/angad/resource/shCore.js"></script>
<script language="javascript" src="/angad/resource/shBrushCpp.js"></script>
<script language="javascript" src="/angad/resource/shBrushCss.js"></script>
<script language="javascript" src="/angad/resource/shBrushJScript.js"></script>
<script language="javascript" src="/angad/resource/shBrushJava.js"></script>
<script language="javascript" src="/angad/resource/shBrushPhp.js"></script>
<script language="javascript" src="/angad/resource/shBrushXml.js"></script>
<script language="javascript">
function startHighlighter()
{
dp.sh.ClipboardSwf = 'http://blogs.sun.com/angad/resource/clipboard.swf';
dp.sh.HighlightAll('code');
}
</script>
<link rel="stylesheet" type="text/css" href="/angad/resource/SyntaxHighlighter.css"></link>
<!-- End Syntax Highlighter Plugin -->
Be sure to change the links from blogs.sun.com/angad to your own blogs' links ;)
<script language="javascript"> startHighlighter() </script>
In reply to the "Exporting source files from Netbeans as HTML", just refer the following links and you're done.
[1] http://ui.netbeans.org/docs/hi/promoB/print2html.html
[2] http://wiki.netbeans.org/MainMenu
[3] http://wiki.netbeans.org/TS_60_EditorUtilities
Happy NetBeaning!
Posted by Varun Nischal on June 01, 2008 at 10:20 AM IST #
Hey thanks man.
Posted by Angad Singh on June 01, 2008 at 03:43 PM IST #
The problem with SyntaxHighlighter is that it is eminently uncustomizable. If the only languages you need are those it supports, then it is a fine, drop-in solution. However, there's no way to add support for a language, nor is it possible to easily re-style the output. (a lot of bloggers are having this problem trying to embed Scala snippets)
Personally, I'm quite partial to GeSHi, which I've customized a fair bit to improve the highlighting and ensure the result is consistent across all languages. It's more work, but definitely worth it.
Posted by Daniel Spiewak on June 02, 2008 at 04:47 AM IST #
Try this site.
http://www.slists.com/code2html/
Posted by akhel on June 02, 2008 at 05:27 AM IST #
I created Special Copy/Paste because I did not like the colors of SyntaxHighlighter and also it was a simple exercise of the Netbeans API. It should actually work with any language NetBeans support since it uses the installed lexers but I'm still not 100% sure of how to inform NetBeans of this through the layer file.
Posted by Casper Bang on June 02, 2008 at 06:35 AM IST #
I use this: http://code.google.com/p/google-code-prettify/
A few kinks here and there, but easy to use and very unobtrusive. You don't need to call any Javascript methods, just include the file and assign the class "prettyprint" to your code block.
Posted by matthew on June 02, 2008 at 07:14 AM IST #
@Daniel
You can add language support to SyntaxHighlighter too by creating new brush files. It uses regular expressions to define languages. See the code for existing languages in the shBrush*.sh. But yes, GeShi does look more promising on the adding language support front. Will try it out.
@akhel
Java/javascript only..
@Casper
True, It would have been the best solution had it supported more languages since Netbeans is my primary IDE.
@matthew
Code prettifier is neat, no need to even specify the language, though the same problem, unability to add support for a new language.
Posted by Angad Singh on June 02, 2008 at 08:30 AM IST #
Great, now my blog will also look little colorful !
Posted by Vaibhav Choudhary on June 02, 2008 at 08:45 AM IST #
Humm, although I'm an emacs guy I think I'm going to try your recipe for SyntaxHighlighter - and see what it does!
Thanks for posting that!
-- daniel
Posted by daniel on June 02, 2008 at 06:11 PM IST #
Note that there is a nice WordPress plugin for SyntaxHighlighter, too!
Posted by Eric Wendelin on June 02, 2008 at 10:34 PM IST #