Category: PerlMonks Related Scripts
Author/Contact Info user: pKai
can receive at:
Description: When I read about Free Nodelet hack: Syntax colouring I was (and still am) somewhat sceptical about the usefulness.

For the very least that highlighting has to resemble my Vim colorscheme. ;-)

So I wrote the following to read my (custom) color scheme file and create a corresponding CSS to be used with the "jaap" variation Corion supplied.

Disclaimer: This is far from a full-fledged vimscript parser/interpreter. It just reads a color scheme (try "zellner.vim" it should at least work with that) with the diamond operator and only "knows" about the "highlight" command from Vim. I hope it interprets at least this reasonably close to Vim's implementation.

If you actually produce something useful with if you have to host that CSS file somewhere and refer to it instead of the file Corion provided on his own site.

Update: Added some minimal comments.

As you might deduce from the questions in there, I'm not really a vimscript programmer. And I also didn't bother to mock with my vim files to test those. :-(

use strict;
use warnings;

my %css;

my $fg = 'black';    # default foreground colour.
    # You might want to change this at least for a dark background.

# part 1: Read the file (files even, in case you provide)
while (<>) {
    my  ($force, $from, $to, $name, $col, $att) =
                    (?= .*?guifg=( [a-zA-Z]+ | \#[0-9a-fA-F]{6} ) )?
                    (?= .*?gui=( \w+ ) )?
    next unless $from or $name and ($col or $att);
    #    no warnings 'uninitialized';
    #    print "$force, $from, $to, $name, $col, $att\n";

    if ($name) {
        $name = lc $name;
        $css{$name} = {};
        $css{$name}->{col} = $col if $col;
        $css{$name}->{col} ||= $fg;
        $css{$name}->{att} = $att if $att and uc $att ne 'NONE';
    else {    # link
        next if $css{$from} and not $force;
        $from = lc $from;
        $css{$from}->{lnk} = lc $to;

#part 2: Resolve "links".
# Does Vim handle forward declared links?
# Anyway, the code does.
my $more = keys %css;
while (--$more) {
    my $found = 0;
    for my $group (keys %css) {
        next unless $css{$group}->{lnk};
        $found = 1;
        my $to = $css{$group}->{lnk};
        if (exists $css{$to}) {
            $css{$group} = $css{$to};
        else {
            $css{$group} = {col => $fg};
    next unless $found;

#part 3: Create the CSS

# Use this Vim "group" => JS className translation.
# Is Vim case sensitive on the names?
my %map = (
    quotedString => 'string',
    comment => 'comment',
    operator => 'normal',
    builtinVariable => 'identifier',
    variableSpecifier => 'identifier',
    keyword => 'keyword',
    builtinFunction => 'keyword',
    identifier => 'identifier',
    number => 'number',

for my $class (keys %map) {
    my $id = $map{$class};
    my $cid = $css{$id} || $css{Normal};
    my $col = lc $cid->{col} || $fg;
    my $att = $cid->{att}||q() eq 'bold' ? "\n  font-weight:bold;" : q
    print <<EOS;
  color: $col;$att