【问题标题】:How to print the CSS stylesheet for PPI::HTML highlight? [closed]如何打印 PPI::HTML 突出显示的 CSS 样式表? [关闭]
【发布时间】:2015-03-19 12:25:41
【问题描述】:

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

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

【问题讨论】:

  • 我投票决定根据 OP 的更新重新提出这个问题。请考虑重新开放投票。

标签: html css perl ppi


【解决方案1】:

稀疏文档指出:

对于您不想使用外部样式表的情况,您可以提供颜色作为哈希引用其中键是 CSS 类(通常与标记名称匹配),值是颜色 .

PPI::HTML::CodeFolder 的 POD 有一个您可以使用的类名列表,并提供以下颜色作为示例:

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 =&gt; 1 选项,你只会得到一个没有CSS 的HTML 片段。在这种情况下,您需要您的网站样式表包含必要的样式。

另一方面,您可以使用HTML::TokeParser::Simple 来简单地使用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;
}

顺便说一句,这是一个“独立的示例”:无需我跳过任何箍即可运行。您的程序无法运行,因为您将其留给了试图帮助您生成 $perl_block 内容的人。

【讨论】:

  • 我根据您的建议创建了一个哈希 %colors 并将其放入 PPI::HTML 对象中,如下所示:my $highlighter=PPI::HTML-&gt;new(line_numbers =&gt; 1, colors =&gt; \%colors); 但它并没有改变任何事情......仍然看不到颜色。
  • 我重写了问题,等待一个独立的例子。
  • 我做到了。请回答我的问题。
  • 感谢您的耐心等待。这是我的第一个问题,所以我真的不明白你认为什么是“独立的例子”。
  • 感谢您接受我的回答。请参阅Short, Self Contained, Correct (Compilable), Example 以供将来参考。
猜你喜欢
  • 1970-01-01
  • 2011-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-09
  • 2015-08-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多