Exotic Ideas..
Angad's Blog
About this blog
I am Angad Singh. I have served as the Sun Campus Ambassador of JIIT University, Noida (India) from August 2007 to July 2008 and as a Campus Ambassador Tech Lead from July 2008 to July 2009. This was my sun blog. Here I jotted down all my random scribblings, reports on all activities I conducted as CA at my university, my little projects, hacks, geeky stuff and new technology I came across, all the way to things I learnt in my exciting journey with Sun..
About Me
View Angad Singh's profile on LinkedIn
Technorati Authority
View blog authority
Subscribe
Search

10 Recent Entries
Archives
« July 2009
SunMonTueWedThuFriSat
   
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 
       
Today
Links
My..
Blogs that interest me
 

Today's Page Hits: 927

Best Viewed in..

Mozilla Firefox

Locations of visitors to this page
« Solaris Network... | Main | Multibooting OpenSol... »
Sunday Jun 01, 2008
Posting syntax highlighted code on a blog

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:

  1. Download the latest release of SyntaxHighlighter from its google project homepage (current at 1.5.1)
  2. Login to roller at blogs.sun.com
  3. Open Create & Edit > File Uploads and upload the following from the SyntaxHighlighter release:
    • Scripts/shCore.js
    • Scripts/clipboard.swf
    • Styles/SyntaxHighlighter.css
    • Scripts/shBrushJava.js, Scripts/shBrushXml.js and the appropriate script for any other language you'd like to see in colors.
  4. Open up Design > Templates > weblog, and add the following code at the end of the <head> </head> section:
    <!-- 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 ;)
  5. Add the following just before the end of the body tag:
    <script language="javascript">
        startHighlighter()
    </script>
  6. Repeate steps 4 and 5 for the permalink template.
  7. That's it, you're done. Now whenever you want to insert code into a blog post, do so in a <pre name="code" class="java"> </pre> tag. Replace java with whichever language code that is. You can checkout the configurable options here.
For those having custom drupal or wordpress blogs, there's even a TinyMCE plugin for SyntaxHighlighter that you may want to try out.
Posted at 01:58AM Jun 01, 2008 by Angad Singh in Personal  |  Comments[10]  |  del.icio.us digg slashdot technorati Stumble It! Share on Facebook furl reddit Share on Twitter    

Comments:

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 #

Post a Comment:
  • HTML Syntax: NOT allowed
Creative Commons License

This work by Angad Singh is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.