Syntax coloring / highlighting

Everyone knows that “higher level management” like to look at colorful things, especially on PowerPoint presentations, and source codes are most boring things you can include in documentation. How to help it? You can colorize your codes.

There are many software packages that can “beautify” sources. Most of them have one limitation, which can ruin whole experience: small amount of supported programming languages.

One of the most known syntax highlighting libraries is GeSHi written in PHP. Its name stands for Generic Syntax Highlighter, what is quite accurate, because it colors over 70 programming languages and variants. But it’s only library, so you’d have to look for some its application, or write few (probably below 10) lines of code that would best suit your needs (stdin highlighting, single file highlighting, batch highlighting etc.)

Trac, very versatile project management system (soon to be described here), uses pygments for syntax highlighting. Pygments, providing support for not too many file formats (not too many, but coloring those most important), has one key feature: exporting output to RTF format.

If you are *NIX user, you can use GNU Source-highlight – stand-alone application that “knows” about 80 languages. It can produce output in many different formats, most notably (X)HTML, DocBook and LaTeX.

But 70-80 programming languages is still not enough with ever growing number of dialects and file formats. Here comes solution for all problems of humanity: ViM. ViM has incredible (and still growing) amount of syntax specification files. Now it’s over 400 (!) supported file types. It is very helpful, especially if you’re working with some exotic template systems or you’d like to colorize configuration files for Apache.

But it’s ViM – how to make some file output? There is a library written at first in Perl, but later translated to other programming languages. This library, Text::VimColor, can be found in CPAN archives. In package there’s also command-line tool, so you don’t need to write anything to start using this library. There are many applications of this library, for example, syntax highlighting plugin for WordPress.

I you would like to code something own but don’t like Perl, there is also VimColor library for Python. Probably if you look around, there will be translation of this library for most mainstream programming language.


4 thoughts on “Syntax coloring / highlighting”

  1. You’re right, I forgot about pygments (engine behind Trac’s highlighting). Pygments, not having too many supported formats (not too many, but providing support for those most important), has one key feature: export to RTF format.

    Thanks, sotn3m, I’ll update this entry.

  2. @Piotr: I didn’t know about that option :-) But for my purpose, it’d be ineffective, because I’ve had few hundreds of files to highlight, so I’ve used command-line tool from Text::VimColor package with single-line shell script :)

  3. Yes. We can colorize our codes. What line you want to highlight from your code that line you will colorize using different software’s. Suppose, I want to colorize meta tags from my site code just i will apply software what I want. Give information about more software’s.

    Thanks for sharing.


Leave a Reply

Your email address will not be published. Required fields are marked *