如何为 PPI::HTML 突出显示打印 CSS 样式表?

How to print the CSS stylesheet for PPI::HTML highlight?

PPI::HTML 完美地为我的 Perl 代码格式化 HTML 突出显示,就像 CPAN 上的示例一样。但是如果没有 CSS 样式,输出就不太可用,我不知道如何合并。

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>";

能否请您举一个打印彩色代码的简单例子?目前一切都默认显示颜色。

稀疏文档指出:

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.

PPI::HTML::CodeFolder 的 POD 有一个您可以使用的 class 个名称的列表,并给出了以下颜色的示例:

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',

以下代码生成一个自包含的 HTML 页面,其中 its own source code 使用给定样式设置样式:

#!/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);

如果在构造函数中不使用page => 1选项,您将只会得到一个没有CSS的HTML片段。在这种情况下,您的站点样式表需要包含必要的样式。

另一方面,您可以使用 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:]])
                    ([[:xdigit:]])
                    ([[:xdigit:]])
                    \z
                }{#}x;
                $token->set_attr(style => "color:$color");
            }
        }
        $out .= $token->as_is;
    }
    $out;
}

顺便说一下,这是一个 "self-contained example": 运行 不需要我跳过任何障碍的一个。你的程序不能 运行 因为你把它留给了试图帮助你生成 $perl_block.

内容的人