Code syntax highlighting on your pages

Since I am a web developer I knew I'll need to public some code samples in my articles. But there's of course a difference between code and code with syntax highlighting. Highlighted syntax is just fancy and I definately wanted that on my pages. So I started to search for a solution and here it is. The answer was using Ge(neric)S(yntax)Hi(ghlighter). It can be found here, with demos, examples and documentation.

Using GeSHi


GeSHi is free software and it's really very simple to use it. If you are a user of for example WordPress you can find a plugin with syntax highlighting which uses GeSHi. I however don't belong to this group, so I had to make a workaround. I am using my own CMS and I type these lines in TinyMCE WYSIWYG editor. I call the following php method to highlight my code:

private function HighLightSyntax($code,$lang)
{
    $geshi = new GeSHi($code,$lang);
 
    return $geshi->parse_code();
}//HighLightSyntax


This is of course from the context of one of my classes. The important part is inside this private method.

Before using this code I parse code contents from article content using special symbols. I use these symbols to enclose code. The final touch was a simple CSS formatting of code content, here it is:

div.code {
    width: 100%;
    overflow: auto;
    font-size: 1.1em;
    background: #373737 url(../img/code-bg.gif) left top repeat;
}


And that's pretty much all there is :)