PPI::HTML formats the HTML highlighting for my Perl code perfectly, just as in the example on CPAN. But the output is not quite usable without the CSS styles, which I don't know how to incorporate.
use PPI;
use PPI::HTML;
my %colors=(
cast => '#339999',
comment => '#008080',
core => '#FF0000',
double => '#999999',
heredoc_content => '#FF0000',
interpolate => '#999999',
keyword => '#0000FF',
line_number => '#666666',
literal => '#999999',
magic => '#0099FF',
match => '#9900FF',
number => '#990000',
operator => '#DD7700',
pod => '#008080',
pragma => '#990000',
regex => '#9900FF',
single => '#999999',
substitute => '#9900FF',
transliterate => '#9900FF',
word => '#999999'
);
my $highlighter=PPI::HTML->new(line_numbers => 1, colors => \%colors);
my $perl_doc=PPI::Document->new(\$perl_block); # read from a file
my $perl_block_highlighted=$highlighter->html($perl_doc);
print "<p>$perl_block_highlighted</p>";
Could you please give a simple example which prints colored code? Currently everything is showed by default color.
The sparse documentation states:
For cases where you don't want to use an external stylesheet, you can provide colors as a hash reference where the keys are CSS classes (generally matching the token name) and the values are colours.
The POD for PPI::HTML::CodeFolder has a list of class names you can use, and gives as an example the following colors:
cast => '#339999',
comment => '#008080',
core => '#FF0000',
double => '#999999',
heredoc_content => '#FF0000',
interpolate => '#999999',
keyword => '#0000FF',
line_number => '#666666',
literal => '#999999',
magic => '#0099FF',
match => '#9900FF',
number => '#990000',
operator => '#DD7700',
pod => '#008080',
pragma => '#990000',
regex => '#9900FF',
single => '#999999',
substitute => '#9900FF',
transliterate => '#9900FF',
word => '#999999',
The following code produces a self-contained HTML page with its own source code styled using the given styles:
#!/usr/bin/env perl
use strict;
use warnings;
use PPI;
use PPI::HTML;
my %colors = (
cast => '#339999',
comment => '#008080',
core => '#FF0000',
double => '#999999',
heredoc_content => '#FF0000',
interpolate => '#999999',
keyword => '#0000FF',
line_number => '#666666',
literal => '#999999',
magic => '#0099FF',
match => '#9900FF',
number => '#990000',
operator => '#DD7700',
pod => '#008080',
pragma => '#990000',
regex => '#9900FF',
single => '#999999',
substitute => '#9900FF',
transliterate => '#9900FF',
word => '#999999'
);
my $highlighter = PPI::HTML->new(page => 1, line_numbers => 1, colors => \%colors);
my $perl_doc = PPI::Document->new(
do {
local $/;
open 0;
\ <0>;
}
);
print $highlighter->html($perl_doc);
If you do not use the page => 1
option in the constructor, you will only get an HTML fragment without the CSS. In that case, you need your site stylesheet to include the requisite styles.
On the other hand, you could use HTML::TokeParser::Simple to simply post-process the HTML fragment:
#!/usr/bin/env perl
use strict;
use warnings;
use PPI;
use PPI::HTML;
use HTML::TokeParser::Simple;
my %colors = (
# as above
);
my $highlighter = PPI::HTML->new(line_numbers => 0);
my $html = $highlighter->html(\ do { local $/; open 0; <0> });
print qq{<pre style="background-color:#fff;color:#000">},
map_class_to_style($html, \%colors),
qq{</pre>\n}
;
sub map_class_to_style {
my $html = shift;
my $colors = shift;
my $parser = HTML::TokeParser::Simple->new(string => $html);
my $out;
while (my $token = $parser->get_token) {
next if $token->is_tag('br');
my $class = $token->get_attr('class');
if ($class) {
$token->delete_attr('class');
if (defined(my $color = $colors->{$class})) {
# shave off some characters if possible
$color =~ s{
\A \#
([[:xdigit:]])\1
([[:xdigit:]])\2
([[:xdigit:]])\3
\z
}{#$1$2$3}x;
$token->set_attr(style => "color:$color");
}
}
$out .= $token->as_is;
}
$out;
}
Incidentally, this is a "self-contained example": One that runs without me having to jump through any hoops. Your program could not run because you left it to the people who are trying to help you to generate the contents of $perl_block
.