Syntax coloring your code samples in the blog entries

Posted : May 13, 2004 at 12:58 pm [America/Los_Angeles]

Here’s the style that I apply to my <pre> tags to display code samples:

pre
{
font: normal 9pt courier, sans-serif;
margin-left: 50px;
margin-right: 50px;
border: 1px solid #e7651a;
border-left: 1px solid #e7651a;
padding: 5px 5px 5px 10px;
background-color: #FFFFCC;
}

However, putting code samples within <pre> tags was not helping too much from a readability standpoint as the sample below shows:

/*
* $RCSfile: Main.java,v $
*/
package com.cisco.helloworld;

/**
 * Class description goes here.
 *
 * @author $Author: anasharm $
 * @version $Revision: 1.1 $ $Date: 2004/04/08 01:35:35 $
 */
public class Main {
    public static void main(String[] args) {
        HelloProvider hp = new HelloProvider();
        WorldProvider wp = new WorldProvider();
        String hello = hp.getHello();
        String world = wp.getWorld();
        System.out.println(hello + ", " + world);
    }
}

Thanks to this site, the output is much prettier now:

/*
* $RCSfile: Main.java,v $
*/
package com.cisco.helloworld;

/**
 * Class description goes here.
 *
 * @author $Author: anasharm $
 * @version $Revision: 1.1 $ $Date: 2004/04/08 01:35:35 $
 */
public class Main {

    public static void main(String[] args) {

        HelloProvider hp = new HelloProvider();
        WorldProvider wp = new WorldProvider();

        String hello = hp.getHello();
        String world = wp.getWorld();

        System.out.println(hello + ", " + world);

    }
}

I know of another open-source software called GNU source highlight which does the same, although they do not have an online tool that one could use to do an on-the-fly conversion. However, it’s output is much better and it even has support for Python! Here’s the same code converted into HTML using source-highlight:


/*
* $RCSfile: Main.java,v $
*/
package com.cisco.helloworld;

/**
 * Class description goes here.
 *
 * @author $Author: anasharm $
 * @version $Revision: 1.1 $ $Date: 2004/04/08 01:35:35 $
 */

public class Main {
    public static void main(String[] args) {

        HelloProvider hp = new HelloProvider();
        WorldProvider wp = new WorldProvider();

        String hello = hp.getHello();
        String world = wp.getWorld();

        System.out.println(hello + “, “ + world);

    }
}

Do you know a better way of doing this? How about support for PLSQL?

- Anand

Viewed: 947 times

6 Comments

I don’t know how you feel about Emacs, or if it’s a part of your blogging setup, but htmlize.el (http://fly.srk.fer.hr/~hniksic/emacs/htmlize.el) will take a font-lock coloured buffer in Emacs and generate HTML from it.

However, I have no idea how Emacs handles PL/SQL. SQL mode is kind of bad.

Posted by: Gustaf Erikson at May 13, 2004 @ 4:20 pm

Well, the only thing that I know about Emacs is that it’s an editor and quite a few friends of mine swear by it..:-)

So I am sure I can work with someone here (who is an emacs person) and check this out. Thx for the tip Gustaf.

Posted by: Anand Sharma at May 13, 2004 @ 4:57 pm

Source highlight claims to do this already, look for source-highlight-cgi here: http://www.gnu.org/software/src-highlite/source-highlight.html

Posted by: puter at May 13, 2004 @ 6:40 pm

Yup. Came across this little fact after I had published the post. However, since I do most of my blogging on WinXP, I was pretty excited to see that they also have a Windows port as well. Downloaded it, tried it and it sure worked like a charm. Another little utility for my toolbox..;-)

Anyway, thanks for pointing that out puter. I am not sure setting up a CGI for this is a better idea (it sure could be of service to others) or just using the Windows port. We’ll see..:-)

Unfortunately, source-highlite does not seem to support SQL or PSLQL. Any pointers for that?

Posted by: Anand Sharma at May 13, 2004 @ 7:04 pm

how about this?
http://www.java2html.de/

Posted by: aaa at May 16, 2004 @ 1:55 pm

Thanks ‘aaa’..;-) I actually did know about it, but this tool only supports Java and I tend to post code samples which not necessarily always Java. Anyway, thanks for the pointer.

Posted by: Anand Sharma at May 16, 2004 @ 9:23 pm