Arch
view release on metacpan or search on metacpan
perllib/Arch/FileHighlighter.pm view on Meta::CPAN
$html =~ s!\b(m4_\w+)\b!<span class="syntax_builtin">$1<\/span>!g;
$html =~ s!\b(if|then|else|fi)\b!<span class="syntax_keyword">$1<\/span>!g;
}
if ($file_name =~ /\.($xml_extension_regexp)$/) {
$html =~ s!(<\!--.*?-->)!<span class="syntax_comment">$1<\/span>!sg;
$html =~ s!(</?\w+.*?>)!<span class="syntax_keyword">$1<\/span>!sg;
while ($html =~ s!(>(?:<[\w-]+)?\s+)([\w-]+)(=)("[^"]*"|'[^']'|[^\s]*)!$1<span class="syntax_special">$2<\/span>$3<span class="syntax_string">$4<\/span>!sg) {}
}
return \$html;
}
sub _highlight_none ($$$%) {
my $self = shift;
my $file_name = shift;
my $content_ref = shift;
my %args = @_;
if (%args) {
return undef unless _match_file_extension($file_name, \%args);
}
my $html = htmlize($$content_ref);
return \$html;
}
1;
__END__
=head1 NAME
Arch::FileHighlighter - syntax-highlight file's content using markup
=head1 SYNOPSIS
use Arch::FileHighlighter;
my $fh = Arch::FileHighlighter->new(
[ 'internal(pm+c)', 'none(txt), 'enscript', 'internal', ]
);
my $html_ref = $fh->highlight($0);
print $$html_ref;
print ${$fh->highlight('file.c', '/* some code */')};
=head1 DESCRIPTION
This class processes file contents and produces syntax highlighting markup.
This may be used together with css that defines exact text colors and faces.
The default is to use the builtin "internal" processing, that is pretty
poor; only very basic file types and syntax constructions are supported.
It is suggested to configure and use the external "enscript" utility.
GNU enscript understands quite a rich number of file types and produces
a useful syntax highlighting. "enscript" filter is used by default if
/usr/bin/enscript is found.
It is possible to configure different filters ("none", "internal",
"enscript") depending on file name extension. In any case the resulting
markup is always unified, i.e. all special characters are HTML-encoded
using SGML entities, and the markup that looks like
E<lt>spanclass="syntax_foo"E<gt>barE<lt>/spanE<gt> is used.
=head1 METHODS
The following methods are available:
B<new>,
B<instance>,
B<highlight>.
=over 4
=item B<new> [I<filters>]
Create a new instance of L<Arch::FileHighlighter>.
I<filters> is arrayref of strings of the form I<filter>(ext1+ext2+...)",
where I<filter> is one of "enscript", "internal" or "none". Special
extension ":xml" is a shortcut for "html+htm+sgml+xml+wml+rss+glade".
The filters optionally constrained by file extensions are probed
sequentially and the first passed one is used.
Note that if enscript is configured in the sequence, but is not installed,
then its probing may print a warning to stderr. The "enscript" filter is
handled a bit specially, it may take parameters "mono" (less colors) and
"asis" instead of the file extensions. If enscript returns html without
any tags, then the filter is handled as failed, unless "asis" is given.
By default, I<filters> is [ 'internal' ], or [ 'enscript', 'internal' ]
depending on presense of '/usr/bin/enscript'.
=item B<instance> [I<filters>]
Alternative constructor. Return the last created instance of
L<Arch::FileHighlighter> or create a new one.
The purpose of this alternative constructor is to allow the singleton
behaviour as well as certain Aspect Oriented Programming practices.
=item B<highlight> I<filename> [I<content>]
Process I<filename> using configured filters (as described in the
constructor) and produce the file content with embeded E<lt>span
class="I<class>"E<gt>...E<lt>/spanE<gt> markup. I<class> is one of:
syntax_keyword
syntax_builtin
syntax_comment
syntax_special
syntax_funcdef
syntax_vartype
syntax_string
syntax_constant
If I<content> is provided (either string or reference to string), it is
used, otherwise the content of I<filename> is loaded.
=back
=head1 BUGS
( run in 0.729 second using v1.01-cache-2.11-cpan-ceb78f64989 )