如何打印PPI :: HTML突出显示的CSS样式表?
PPI::HTML完美地为我的Perl代码设置了HTML高亮显示格式,就像在CPAN上的例子一样。但是如果没有CSS样式,我不知道如何合并输出。如何打印PPI :: HTML突出显示的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>";
请给出一个打印彩色代码的简单例子吗?目前一切都以默认颜色显示。
稀疏文档状态:
对于您不想使用外部的样式表,你可以提供的颜色作为哈希参考案例,其中键是CSS类(一般匹配标记名称)和值是颜色。
的POD为PPI::HTML::CodeFolder有可以使用的类名的列表,并给出了作为一个例子以下颜色:
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',
下面的代码生成使用与its own source code一个自包含HTML页面风格给定的样式:
#!/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
选项,你只会得到一个HTML片段没有CSS。在这种情况下,你需要你的网站样式表包含必要的样式。
在另一方面,你可以使用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对象是这样的: '我的$荧光笔= PPI :: HTML的“新的(line_numbers => 1,颜色=> \%颜色);' 但它没有改变一件事...仍然看不到颜色。 – adamvagyok 2015-03-25 13:23:52
我重写了这个问题,并等待一个独立的例子。 – adamvagyok 2015-03-26 13:09:51
我做到了。请回答我的问题。 – adamvagyok 2015-03-27 14:26:52
我投票根据OP的更新重新打开这个问题。请考虑投票重新开放。 – 2015-03-27 15:10:45