Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

I use GeSHi ("Generic Syntax Highlighter") on pastebin.com

pastebin has high traffic, so I do cache the results of the transformation, which certainly reduces the load.

Personally, I prefer offline tools: I don't see the point of parsing the code (particularly large ones) over and over, for each served page, or even worse, on each browser (for JS libraries), because as pointed above, these libraries often lag (you often see raw source before it is formatted).

There are a number of tools to do this job, some pointed above. I just use the export feature of my favorite editor (SciTE) because it just respects the choices of color I carefully set up... :-) And it can output XML, PDF, RTF and LaTeX too.

Pygment is a good Python library to generate HTML, RTF, ANSI (terminal-style) or LaTeX code. It supports a large range of languages (C, C++, Lua, Erlang, ...) and you can even write your own output formatter.

I use google-code-prettify. It is the simplest to set up and works great with all C-style languages.

If you use jEdit, you might want to use the Code2HTML plugin.

Just run it through a tool like: http://www.gnu.org/software/src-highlite/

If you are using PHP, you can use GeSHi to highlight many different languages. I've used it before and it works quite well. A quick googling will also uncover GeSHi plugins for wordpress and drupal.

I wouldn't consider highlighting to be CPU intensive unless you are intending to display megabytes of it all at once. And even then, the CPU load would be minimal and your main problem would be transfer speed for it all.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top