Javascript Code Highlighter: Chili vs SyntaxHighlighter

Posted by Matt on Sat, Jun 23 2007

One of my goals with this blog is to post more code snippets. It is something I did occasionally on my old blog. It's one of those things that should be simple, but ends up being a bit of a pain. I've used a Wordpress plugin in the past, but I was never fully satisfied with it. Lately I've seen some other programming blogs I read using a client side Javascript solution. After some digging the two "ready for prime time" packages I found were Chili and SyntaxHighlighter.

This is a comparison of those two. If you feel I missed another package let me know in the comments.

Chili

License: MIT
Includes: jQuery(21KB), core (7KB), PHP CSS(1KB), PHP JS (37KB)

One of the things I initially liked about this package was that it used jQuery, which is my new favorite Javascript library, although then I realized, as much as I use jQuery on my web apps, I don't really have a need for it on this blog. So having to include it was just additional baggage.

I'm not crazy about the overall size. The PHP "recipe" is 37KB alone. Much bigger than any of the others. Another drawback is the weak documentation. The Chili page is long, dominated by the change log - which is good information, but placed poorly. A simple "how to use Chili" section with code samples - USING THE FREAKING PACKAGE would be nice [Update July 24, 2007 - Chili has a new quickstart page. I'll leave me rant here for entertainment purposes].

You know, something like:

1) Include jQuery and the Chili Javascript files. Your paths may be different.

<script type="text/javascript" src="/js/jquery.js"></script>
<script type="text/JavaScript" src="/js/chili-1.8b.pack.js"></script>

2) Chili can automatically find the recipes it needs - simply tell it where to look.

<script id="setup" type="text/javascript">
    ChiliBook.automatic = true;
    ChiliBook.recipeFolder     = "chili/js/";
    ChiliBook.stylesheetFolder = "chili/css/";
</script>

3) Wrap your code and tell Chili which recipe to use. Remember to replace & with &amp; < with &lt; and > with &gt;

<pre><code class="php">
<?php
    class family_friendly() {
        function censor($word) {
            return '*' . gettext($word) . '*';
        }
    }

    function get_license() {
        return 'Creative Commons';
    }

    printf('This guide is released under the %s license.<br />', get_license());
    printf('Seriously, Chili author, if you are reading this, move 95% of the %s onto secondary pages and include this simple tutorial.<br />', family_friendly::censor("crap"));
?>
</code></pre>

That's all you need. Thats all the information 99% of your users need. I shouldn't have to figure out which of the five example pages I need to view source on to get this working. Especially when they have such confusing names as: examples-all-combinations.html (which appears to be a joke, that I don't get), examples-dynamic-automatic-adhoc.html, examples-dynamic-automatic-adhoc-metaobjects.html, examples-static-automatic-adhoc.html. Huh?

I'm done with my rant. I'll say some nice things about Chili now. One of the features that is really cool is that it's completely automated. You simply wrap your code with the correct tag and Chili does the rest. Also since it is a plugin for jQuery it degrades nicely, leaving the code less pretty, but still completely readable.

Now that I said something nice I can go back to ranting. The samples provided with the download have Google Ads in them. I can understand having them on the actual site, but in the downloaded samples it seems a bit tacky to me. Even worse the ads are all food related, that is when they're actually in English. I assume the author simply took the pages directly from the site and rolled them into the download, but I still find it annoying.

Also the author makes the claim "You just put the code you want highlighted in a code tag AS IS". Then not even 300px later he states "Always escape the content of the code element for HTML: at least substitute any & by &amp; and any < by &lt; in this order. If you want to highlight HTML you also need to escape any > with &gt;." So it isn't exactly "AS IS".

SyntaxHighlighter

License: GNU
Includes:CSS(4KB), core(11KB), PHP (5KB)

SyntaxHighlighter is obviously a much lighter solution. It too has separate includes for each language, called brushes here. The instruction page is also well done. In addition to highlighting and formatting the code SyntaxHighlighter adds buttons that allow the user to view plain, copy to clipboard and print. On the downside it isn't automated like Chili, although you could certainly use jQuery, or some custom javascript to execute HighlightAll on a page load. Like Chile you need to encode the < character (but not > or &).

1) Include the core files and the brushes for any language you want. Your paths may be different.

    <script type="text/javascript" src="/syntaxhighlighter/js/shCore.js"></script>
    <script type="text/javascript" src="/syntaxhighlighter/js/shBrushPhp.js"></script>
    <script type="text/javascript" src="/syntaxhighlighter/js/shBrushJScript.js"></script>
    <script type="text/javascript" src="/syntaxhighlighter/js/shBrushXml.js"></script>

2) Put this at the end of your file. If you put it before the code you want highlighted it won't work.

<script class="javascript">
dp.SyntaxHighlighter.ClipboardSwf = "syntaxhighlighter/js/clipboard.swf";
dp.SyntaxHighlighter.HighlightAll("code");
</script>

3) Wrap your code and tell SyntaxHighlighter which brush to use. Remember to replace < with &lt;

<pre name="code"  class="php">
<?php
    class Highlighters() {
        function result($highlighter) {
            switch ($highlighter) {
               case "chili":
               default:
                  return "FTL";
               case "syntaxhighlighter":
                  return "FTW";
            }
        }
    }

    $highlighters = new Highlighters();
    printf("Chili is a nice effort, but just not for me.  Chili %s", $highlighters->result("chili"));
    printf("I am going to give SyntaxHighlighter a shot.  SyntaxHighlighter %s", $highlighters->result("syntaxhighlighter"));
?>
</pre>

Wrap Up

If you're already using jQuery and want to highlight non-PHP then Chili is worth checking out. For this blog I'm going to give SyntaxHighlighter a shot. I think end result of SyntaxHighlighter is nicer looking and the additional buttons are useful features.

If you have any experience with either feel free to write about it in the comments.

Posted in Javascript

14 Comments

Bennett said on Jun 23, 2007
That's a useful comparison -- I will have to check these packages out if I ever get any spare time. As for the encoding issues, you can use Code Markup or some similar plugin in conjunction with Chile or SyntaxHighlighter -- then you won't need to manually encode your HTML symbols.
Matt said on Jun 23, 2007
Bennett,
Thats a good point. Save the step of doing a find and replace before pasting the code.
Thanks.
Andrea said on Jul 07, 2007
I almost love rants as much as nice words.
And I loughed a bit too. (no sarcasm here)
So thanks for your review :-)

AS IS. Newer browsers are better than older ones at correctly interpreting poorly escaped programs, but it's not safe to let them do it.

PHP size. There is a big difference between SyntaxHighlighter's 5K and Chili's 37K: the former highlights 273 PHP functions, and the latter 3309. If you want to save 33K, you can safely delete the "func" step of the PHP recipe.

Buttons. 'print' and '?' are superfluous, while 'view plain' and 'copy to clipboard' are more or less the same, with a glitch. The latter preserves escaped chars and the former does not. In IE the latter preserves white space, and in FF does not. Chili does not need either: you just do it the usual way.

Manual. Chili's manual is certainly not a quick start guide, but it's complete, I hope. On the other side, all the examples begin with a little section showing the exact setup for that same page. I cannot imagine a single programmer having any trouble at spotting what she needs to copy and paste.

Ads. They inevitably slip in the zip... Moreover, diet ads on a site about coding make me sick: 7 months to get to 40 dollars! :'(

And now for the real differences, in my opinion.

* SyntaxHighlighter supports line numbers, but Chili does not.
* Chili supports a plethora of options (which leads to the "all combinations" joke ;-), but SyntaxHighlighter does not. For example, inline highlighting: a code-tagged snippet inside a p-tagged explanation
Matt said on Jul 08, 2007
Andrea,
Thanks for the response. You're clearly right about the size issue. SyntaxHighlighter only covers a limited subset of PHP functions. Although it seems to hit the most common ones, there are some glaring exceptions.

I disagree about the buttons. If I could click in a Chili code box and hit ctrl-A (rather than using the mouse to select the text), ctrl-C the I may agree, but unfortunately the select all gives me the whole page, not just the code section. Obviously this is very dependent on the browser, but I think having the buttons helps negate the issue.

Thanks again for pointing out some of the advantages of Chili that I missed.
Andrea said on Jul 22, 2007
Matt,
You'll be glad to know that I've just published a new release of Chili, with a new Quick Start Guide, which is located at http://noteslog.com/personal/projects/chili/
Thanks
for your suggestions.
Matt said on Jul 24, 2007
Andrea,
Nice update. I'll make a note in the post.
Olivier Mengué said on Nov 29, 2007
I choose an other solution for syntax highlighting on my blog: I'm using the highlight features of my text editor and its HTML output. I've documented everything (including the pluggable stylesheet) in this post: Syntax highlighting on this blog using semantic tags and Vim.
mike said on Dec 07, 2007
Good article. It helped me make my mind up.

Personally I prefer the look of SyntaxHighlighter but prefer the easy functionality of Chili.

It's a shame Chili doesn't have switchable options for line striping and numbering.

There again it is a jQuery extension and therefore modable, I may have a go myself.
amanda said on Jan 14, 2008
I'm looking for a tool that will let me colorcode HTML *inside* of a text area. Have you seen such a thing?
retool2 said on Jun 09, 2008
vev said on Feb 24, 2010
jQuery has a handful of wrappers: http://plugins.jquery.com/taxonomy/term/102
And
there is one for syntaxhighlighter (v2.0) here: http://plugins.jquery.com/project/beautyOfCode
I
think i am going to fall for this. Cheers
Samuel said on Mar 18, 2010
jQuery.Syntax is an extremely fast and lightweight syntax highlighter. It has dynamic loading of syntax source files and integrates cleanly using CSS or modelines.

It was developed specifically to fill a gap – that is: a fast, clean, client-side syntax parser.

Please see the project homepage for more information, documentation, download, etc.

http://www.oriontransfer.co.nz/software/jquery-syntax
jQuery Syntax Highlighter is a new one based on version 3 of Alex Gorbatchev's Syntax Highlighter - a really really really popular plain javascript syntax highlighter.

It supports such things as `code` and `pre` blocks, able to use classnames like `language-javascript` to indicate we want it to highlight, as well as wordwrap. You can copy and paste code by selecting it normally instead of having to open a raw view like many others. It can be further customised by using the HTML5 data attribute `data-sh` or via specifying options at initialisation. A great stable choice which is updated regularly.

Here's the link :-)
http://www.balupton.com/projects/jquery-syntaxhighlighter/
Artur Ejsmont said on Jul 23, 2010
Very helpful, thanks.

Art

Add new comment