Monday, March 8, 2010

Code Syntax Highlighting - The Sequel

Time for an update. I've followed the advice of some fellow bloggers and configured the Syntax Highlighter JavaScript library.

Here's the world famous highlighter at work:


// Hello.java
import javax.swing.JApplet;
import java.awt.Graphics;

public class Hello extends JApplet
{
public void paintComponent(Graphics g)
{
g.drawString("Hello, world!", 65, 95);
}
}

So, was that worth the wait?

For the record, it wasn't really that hard, but I still think its something that should be built into the blog editor.

Thanks to Alex Gorbatchev, Carter Cole, Hendy Irawan, and all the other posters that helped get me here...

Thursday, March 4, 2010

Technical Blogs (Code Syntax Highlighting)

I realize that I am relatively new to blogging, but I have to say that I am a bit surprised at the immaturity of some of the editing capabilities built into most blogging platforms (mainly in the formatting options that are available).

I've used plenty of wiki platforms that have very nice code formatting tags (along with many, many others) built right in, but apparently the easiest way to show some Java code examples on blogger is to use the SyntaxHighlighter JavaScript libraries from Alex Gorbatchev.

I'm not looking for anything fancy, just something simple, like this:

// Hello.java 
import javax.swing.JApplet;
import java.awt.Graphics;   

public class Hello extends JApplet 
{     
   public void paintComponent(Graphics g) 
   {         
      g.drawString("Hello, world!", 65, 95);     
   }     
}

One answer is to just copy and paste from another HTML/wiki editor (like I did above), but that seems like too much of a kludge to the developer in me. The blogger editor didn't really like it, it wasn't very clean, and I lost some of the formatting (courier font, etc.). There's got to be a better way.

After searching longer than I should have, for something that I figured would be a very easy thing (display code in a blog), I've found a couple posts that lay it pretty clearly. And honestly, I'm sure its not that hard, but to me this seems like a pretty big oversight (aren't blogs typically targeting a relatively technical audience, and I think code would be a pretty frequent topic for display in a blog).

I apologize if this comes across as being harsh, but I still remember the first time that I used an editor that had color syntax highlighting (yes, I know that I am dating myself, and yes, I said editor and not IDE, because thats all it was - TextPad, for those of you that are familiar). After my first experience with syntax highlighting, I was hooked - coolness factor aside, I think its a huge productivity boost and development aid. It was probably one of the first steps towards many of the productivity gains offered by today's IDEs (maybe that's a topic for another day?)

Maybe I'm wrong about blogger's capabilities, so please tell me if there is an easier way to do this. Otherwise, I guess that I'll get to work on pulling those JavaScript libraries into my blog template, so that I can actually start on the next post that I was planning...