Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Use your Vim colorscheme in perlmonks syntax highlighting

by pKai (Priest)
on Mar 25, 2007 at 23:10 UTC ( [id://606498]=sourcecode: print w/replies, xml ) Need Help??
Category: PerlMonks Related Scripts
Author/Contact Info user: pKai
can receive at: sp.onlinehome.de
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. :-(

#!/usr/bin/perl
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) =
        /^hi(?:ghlight)?
            (?:
                (!)?\s+link\s+(\w+)\s+(\w+)
                |
                \s+(\w+)\s+
                    (?= .*?guifg=( [a-zA-Z]+ | \#[0-9a-fA-F]{6} ) )?
                    (?= .*?gui=( \w+ ) )?
            )
        /x;
    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;
.$class
{
  color: $col;$att
}
EOS
}

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://606498]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-03-28 09:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found