1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
package Tk::CodeText::Pod;
use vars qw($VERSION);
$VERSION = '0.2';
use strict;
use base 'Tk::CodeText::Template';
sub new {
my ($proto, $rules) = @_;
my $class = ref($proto) || $proto;
if (not defined($rules)) {
$rules = [
['Text'],
['Bold', -foreground => 'purple'],
['Italic', -foreground => 'purple'],
['Exact', -foreground => 'brown'],
['Command', -foreground => 'orange'],
['Space', -background => 'beige'],
['Tab', -background => 'pale green'],
];
};
my $self = $class->SUPER::new($rules);
bless ($self, $class);
$self->listAdd('specchars', 'B', 'I');
$self->listAdd('specmodes', 'Bold', 'Italic');
$self->stackPush('Text');
return $self;
}
sub highlight {
my ($hlt, $in) = @_;
$hlt->snippetParse;
my $out = $hlt->out;
@$out = ();
my $first = substr($in, 0, 1);
if (substr($in, 0, 5) eq '=head') {
#head mode
$hlt->snippet($in);
$hlt->tokenParse('Command');
} elsif ($first eq '=') {
#command mode
$in =~ /(=[^\s]+)/g;
$hlt->snippet($1);
$hlt->tokenParse('Command');
$hlt->parseText(substr($in, length($1), length($in) - length($1)));
} elsif (($first eq "\t") or ($first eq ' ')) {
#exact mode
$in =~ /(^[^\S]+)/g;
my @sp = split //, $1;
while (@sp) {
my $k = shift @sp;
if ($k eq " ") {
$hlt->snippet($k);
$hlt->tokenParse('Space');
} elsif ($k eq "\t") {
$hlt->snippet($k);
$hlt->tokenParse('Tab');
}
}
$hlt->tokenParse('Command');
$hlt->snippet(substr($in, length($1), length($in) - length($1)));
$hlt->tokenParse('Exact');
} else {
#text mode
$hlt->parseText($in);
}
return @$out;
}
sub parseText {
my $hlt = shift;
my @c = split //, shift;
while (@c) {
my $t = shift @c;
if ($hlt->tokenTest($t, 'specchars')) {
if ((@c) and ($c[0] eq '<')) {
if ($t eq 'B') {
$hlt->snippetParse;
$hlt->snippetAppend($t);
$hlt->stackPush('Bold');
} elsif ($t eq 'I') {
$hlt->snippetParse;
$hlt->snippetAppend($t);
$hlt->stackPush('Italic');
} else {
$hlt->snippetAppend($t);
}
} else {
$hlt->snippetAppend($t);
}
} elsif ($t eq '>') {
if ($hlt->tokenTest($hlt->stackTop, 'specmodes')) {
$hlt->snippetAppend($t);
$hlt->snippetParse;
$hlt->stackPull;
}
} else {
$hlt->snippetAppend($t);
}
};
$hlt->snippetParse;
}
1;
__END__
=head1 NAME
Tk::CodeText::Pod - a Plugin for syntax highlighting of pod files.
=head1 SYNOPSIS
require Tk::CodeText::Pod;
my $sh = new Tk::CodeText::Pod([
['Text'],
['Bold', -font => [-weight => 'bold']],
['Italic', -font => [-slant => 'italic']],
['Exact', -foreground => 'brown'],
['Command', -foreground => 'orange'],
['Space', -background => 'beige'],
['Tab', -background => 'pale green'],
]);
=head1 DESCRIPTION
Tk::CodeText::Pod is a plugin module that provides syntax highlighting
for pod files to a Tk::CodeText text widget.
It inherits Tk::CodeText::Template. See also there.
=head1 METHODS
=over 4
=item B<highlight>(I<$string>);
returns a list of string snippets and tags that can be inserted
in a Tk::Text like widget instantly.
=item B<syntax>
returns 'Pod'.
=back
=cut
=head1 AUTHOR
Hans Jeuken (haje@toneel.demon.nl)
=cut
=head1 BUGS
Unknown
=cut
|