Wednesday, 11 June 2008

Highlight.js vs. dp.SyntaxHilighter

I need to include source code snippets in my posts from time to time. As a new user of BlogSpot, I want to find a good code formatter/beautifier which can work in BlogSpot for free.

I came across Highlight and SyntaxHiligher.

I first tried Highlight.js. It tries to guess what language the code is in and then applies the highlighting accordingly. Sometimes, the guessing is wrong and you are left with some wierd highlights. It sort of works but the result is not so beautiful, considering it is a code beautifier. A Highlight.js output screenshot is shown below.
I then tried Google's SyntaxHilighter. It does not try to guess the code language. Instead the user specifies the language using the class attribute of the <pre> tag. It produces far more superior results with line numbers and alternate line background colors. A sample output is shown below.
public int CountAddressReferences(Address.Domain.AddressField af)  {
    int id=(int) af.GetAddressFieldId();
    AddressModel.AddressField entity = addressContext.AddressField
        .Where(t => t.addressFieldId == id).First();
    Console.WriteLine("before count=" + entity.Address.Count);
    addressContext.Detach(entity); // extra line
    addressContext.Attach(entity); // extra line
    entity.Address.Load(); // extra line
    Console.WriteLine("after count=" + entity.Address.Count);
    return entity.Address.Count
The choice is clear, I am sticking with Google


Dmytrii Nagirniak said...

You can set the language using class the same way. Automatic language guessing is for the lazy ones :)

Romen said...

Thanks Dmytrii. I have not researched them for a while now because I have grown comfortable with SyntaxHilighter. Maybe I should keep looking for the best.