IkiWiki::Plugin::syntax::Vim - Uses Text::VimColor for syntax highlight in IkiWiki


IkiWiki-Plugin-syntax documentation Contained in the IkiWiki-Plugin-syntax distribution.

Index


Code Index:

NAME

Top

IkiWiki::Plugin::syntax::Vim - Uses Text::VimColor for syntax highlight in IkiWiki

VERSION

Top

This documentation refers to IkiWiki::Plugin::syntax::Vim version 0.1

SYNOPSIS

Top

This module is used internally for IkiWiki::Plugin::syntax.

DESCRIPTION

Top

See the IkiWiki::Plugin::syntax for examples of use.

SUBROUTINES/METHODS

Top

can_syntax_from( language|filename|content => value )

Return a true value if the module can work with the parameters passed to.

proposed

Check if Vim has a syntax file for the proposed language.

filename

Check if Vim has a syntax file for the filename extension.

contents

Check if Vim can determine the source language.

parse_and_html( )

Do the real work of parse the source and htmlized using the Vim editor and the Text::VimColor module.

build_plugin_info( )

Returns a hash with information about the plugin capabilities.

DIAGNOSTICS

Top

A list of every error and warning message that the module can generate.

CONFIGURATION AND ENVIRONMENT

Top

A full explanation of any configuration system(s) used by the module, including the names and locations of any configuration files, and the meaning of any environment variables or properties that can be set. These descriptions must also include details of any configuration language used.

DEPENDENCIES

Top

A list of all the other modules that this module relies upon, including any restrictions on versions, and an indication of whether these required modules are part of the standard Perl distribution, part of the module's distribution, or must be installed separately.

INCOMPATIBILITIES

Top

A list of any modules that this module cannot be used in conjunction with. This may be due to name conflicts in the interface, or competition for system or program resources, or due to internal limitations of Perl (for example, many modules that use source code filters are mutually incompatible).

BUGS AND LIMITATIONS

Top

There are no known bugs in this module. Please report problems to <Maintainer name(s)> (<contact address>). Patches are welcome.

AUTHOR

Top

Víctor Moral <victor@taquiones.net>

LICENSE AND COPYRIGHT

Top


IkiWiki-Plugin-syntax documentation Contained in the IkiWiki-Plugin-syntax distribution.

package IkiWiki::Plugin::syntax::Vim;
use base qw(IkiWiki::Plugin::syntax::base);
use strict;
use warnings;
use Carp;
use utf8;
use English qw(-no_match_vars);

use IO::File;
use IO::Dir;

use constant VIM_SYNTAX  =>   q(/usr/share/vim/vimcurrent/syntax);

# package variables
our $VERSION = '0.2';

# private variables
my %syntaxes = ();

# public methods
sub can_syntax_from {
    my  $self       =   shift;
    my  $origin     =   shift;
    my  $value      =   shift;

    if ($origin eq 'proposed') {
        # save on temporary variable because the function can modify the
        # proposed value
        if (my $lang = $self->_syntax_proposed( $value )) {
            $self->language($lang);
            return 1;
        }
    }
    elsif ($origin eq 'filename') {
        1;
    }
    elsif ($origin eq 'content') {
        # vim has autodetection capabilities
        return 1;
    }

    return 0;
}

sub _syntax_proposed {
    my  $self       =   shift;
    my  $proposed   =   shift;
    
    if (not %syntaxes) {
        $self->_build_syntaxes();
    }

    # if we have a direct relation 
    if (exists $syntaxes{lc $proposed}) {
        return $proposed;
    }
    else {
        # search for a special form
        foreach my $lang (keys %syntaxes) {
            if ($proposed =~ qr{$lang} ) {
                return $lang;
            }
        }

        return;
    }
}

sub _build_syntaxes {
    my  $self   =   shift;

    $self->logging("scanning files in " . VIM_SYNTAX);
    
    %syntaxes = ( _build_syntax_hash() );
}

sub parse_and_html {
    my  $self   =   shift;
    my  $vim    =   $self->_engine();

    my $result = $vim->syntax_mark_string( $self->source(), 
                    filetype => $self->language() )->html();

    return $self->htmlized( $result );
}

#
#   Build a hash scanning all files under the directory 
#   /usr/share/vim/vimcurrent/syntax and with extension .vim
#   The hash is keyed using the syntax language keyname.
#

sub _build_syntax_hash {
    my  $dir = IO::Dir->new( VIM_SYNTAX );
    my  %hash   =   ();

    if ($dir) {
        while (defined( my $entry = $dir->read())) {
            my $syntax_file = VIM_SYNTAX . "/${entry}";

            if (-f $syntax_file and $syntax_file =~ m{\.vim$}xms) {
                my ($key,$desc) = _scan_syntax_file( $syntax_file );

                if ($key) {
                    $hash{lc $key} = $desc;
                }
            }
        }
        $dir->close();
    }

    return %hash;
}

#
#   Scan a syntax vim file and extract two values from it: 
#   - Keyname (perl, php, pascal, html, ...)
#   - Description ( Pascal, php PHP 3/4/5, Mutt setup files, ...)
#
sub _scan_syntax_file {
    my  $file   =   shift;
    my  ($description, $keyname);

    if (my $fh = IO::File->new( $file )) {
        while (<$fh>) {
            chomp;

            if (m{^"\s+Language:\s+(.+)$}xms) {
                $description = $1;
            }
            elsif (m{^let\s+b:current_syntax\s+=\s+"(.+)"}xms) {
                $keyname = $1;
            }

            if ($description and $keyname) {
                return ($keyname => $description);
            }
        }
        $fh->close();
    }

    return ();
}

sub build_plugin_info {
    my  $self   =   shift;

    return ( 
        name        =>  'Vim',
        version     =>  $VERSION,
        external    =>  q(Text::VimColor),
        description =>  <<'EOF',
This plugin uses the Text::VimColor module and the vim editor.

Copyright 2002-2006, Geoff Richards.
EOF
        special     =>  <<EOF,
- Available the autodetection capability through vim program.
EOF
        bugs        =>  <<EOF,
- Could not show the file name extensions in the information page.
EOF
        supported   =>  
                [
                $self->_list_of_supported_syntaxes(),
                ]
        );
}    

sub _list_of_supported_syntaxes {
    my  $self       =   shift;
    my  @rlist      =   ();

    if (not %syntaxes) {
        $self->_build_syntaxes();
    }

    foreach my $syntax_name (keys %syntaxes) {
        push @rlist, { language => $syntax_name, fileext => '', 
                        description => $syntaxes{$syntax_name} };
    }                    

    return (sort { $a->{language} cmp $b->{language} } @rlist);
}

1;
__END__