How to highlight source code in HTML?

Niyaz picture Niyaz · Sep 16, 2008 · Viewed 37.7k times · Source

I want to highlight C/C++/Java/C# etc source codes in my website.

How can I do this?

Is it a CPU intensive job to highlight the source code?

Answer

Daniel Spiewak picture Daniel Spiewak · Sep 16, 2008

You can either do this server-side or client-side. It's not very processor intensive, but if you do it client side (using Javascript) there will be a noticeable lag. Most client side solutions revolve around Google Code's syntax highlighting engine. This seems to be the most popular one: SyntaxHighlighter

Server-side solutions tend to be more flexible, especially in the way of defining new languages and configuring how they are highlighted (e.g. colors used). I use GeSHi, which is a PHP solution with a moderately nice plugin for Wordpress. There are also a few libraries built for Java, and even some that are based upon VIM (usually requiring a Perl module to be installed from CPAN).

In short: you have quite a few options, what are your criteria? It's hard to make a solid recommendation without knowing your requirements.