According to the limited documentation provided:
When you choose not to utilize an external stylesheet, you can input colors as a hash reference where the keys represent CSS classes (typically aligned with the token name) and the values signify the colors.
The POD for PPI::HTML::CodeFolder lists the available class names for use, along with example colors like the ones below:
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 code snippet below generates an independent HTML page featuring its own source code styled using the provided color schemes:
#!/usr/bin/env perl
use strict;
use warnings;
use PPI;
use PPI::HTML;
my %colors = (
# listed colors above
);
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 the page => 1
option is omitted from the constructor, only an HTML fragment will be obtained without the accompanying CSS. In such instances, your site's stylesheet must include the essential styles.
Alternatively, you could employ HTML::TokeParser::Simple to straightforwardly post-process the HTML snippet:
#!/usr/bin/env perl
use strict;
use warnings;
use PPI;
use PPI::HTML;
use HTML::TokeParser::Simple;
my %colors = (
# same colors as mentioned earlier
);
my $highlighter = PPI::HTML->new(line_numbers => 0);
my $html = $highlighter->html(\ do { local $/; open 0; <0> });
# print highlighted HTML content
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})) {
# shortening color codes 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;
}
By the way, this represents a "self-contained example": one that functions seamlessly without additional requirements on the user's end. It appears your program may not execute because the generation of contents within $perl_block
was left up to those assisting you.