How to colorize your code using Perl and Syntax::Highlight::Engine::Kate

Yesterday I was scratching my head on how to send an email to myself when an application failed or a certain error occurred. You see, I wanted to be able to read an email on my crackberry (Blackberry) that showed that a problem occurred and more importantly, I wanted the error messages and the snippet of code where the problem occurred.

The first part of sending the email was very simple by using Mail::Sendmail, but I wanted to extend it a bit by sending highlighted code. The problem is that the documentation of Syntax::Highlight::Engine::Kate isn’t too clear on how to use it.

The Syntax::Highlight::Engine::Kate is surprisingly easy to use once you understand that Kate just parses whatever string you give it for tokens and lets you handle how you want each type of token highlighted. In truth, you could easily forsake HTML and have it spit out XML or something else if you so desired.

package dbS::misc;
use strict;
use warnings;

our $PROC = basename($0);

    use Exporter ();

    $VERSION = 1.0.0;

    @ISA = qw(Exporter);
    @EXPORT = qw(&any_to_html);

sub any_to_html {
    my ($type, $content) = @_;

    require Syntax::Highlight::Engine::Kate;

    my $hl = new Syntax::Highlight::Engine::Kate(
        language => $type,
        substitutions => {
            "< " => "< ",
            ">" => ">",
            "&" => "&",
            " " => " ",
            "\\t" => "   ",
            "\\n" => "\\n",
        format_table => {
            Alert => ["<span style="color: #0000ff;">", "</span>"],
            BaseN => ["<span style="color: #000000;">", "</span>"],
            BString => ["<span style="color: #00c9ff;">", "</span>"],
            Char => ["<span style="color: #00ffff;">", "</span>"],
            Comment => ["<span style="color: #007f7f;"><em>", "</em></span>"],
            DataType => ["<span style="color: #0000ff;">", "</span>"],
            DecVal => ["<span style="color: #00007f;">", "</span>"],
            Error => ["<span style="color: #00ff00;"><strong><em>", "</em></strong></span>"],
            Float => ["<span style="color: #00007f;">", "</span>"],
            Function => ["<span style="color: #000000;">", "</span>"],
            IString => ["<span style="color: #00ff00;">", ""],
            Keyword => ["</span><span style="color: #000e00;"><strong>", "</strong></span>"],
            Normal => ["", ""],
            Operator => ["<span style="color: #00ff00;">", "</span>"],
            Others => ["<span style="color: #00b060;">", "</span>"],
            RegionMarker => ["<span style="color: #0096ff;"><em>", "</em></span>"],
            Reserved => ["<span style="color: #009bff;"><strong>", "</strong></span>"],
            String => ["<span style="color: #00ff00;">", "</span>"],
            Variable => ["<span style="color: #0000ff;"><strong>", "</strong></span>"],
            Warning => ["<span style="color: #0000ff;"><strong><em>", "</em></strong></span>"],

    return $hl->highlightText($content);


I wrote a wrapper subroutine for Syntax::Highlight::Engine::Kate so I can just call any_to_html(‘parsing template’, $string) and it will return HTML code.


use strict;
use warnings;

use File::Basename;
use Syntax::Highlight::Engine::Kate;

use dbS::misc;
use dbS::Sybase::Parse::SQL_File;

our $PROC = basename($0);


my $message = "";

if ( my $sql_batch = dbS::Sybase::Parse::SQL_File::get_batch("/dbms/sybase/ASE-15_0/scripts/installmontables", undef, 1) ) {
    while (my $query = $sql_batch->next ) {
        $message .= $query;
        $message .= "go\\n\\n";
} else {
    warn("unable to open the SQL file\\n");

print $message

The language option (a parsing template) and format_table option (token into html code) are required but the substitutions option is optional but I strongly recommend using it else the output will look odd.

The following is the output of $SYBASE/$SYBASE_ASE/scripts/installmontables using our little application. Note that we are reading the batches one by one (just because we can) even though it isn’t necessary for this example.

Continue reading “How to colorize your code using Perl and Syntax::Highlight::Engine::Kate”

Share Button