您可以打印格式的人类可读的HTML与TreeBuilder作为方法:
$h = HTML::TreeBuilder->new_from_content($html);
print $h->as_HTML('',"\t");
,但如果你仍然喜欢这个有问题的漂亮打印机尝试删除问题标签,不知道为什么有人需要...
$h = HTML::TreeBuilder->new_from_content($html);
while(my $n = $h->look_down(_tag=>img,'src'=>undef)) { $n->delete }
UPD:
那么......那么我们可以修复PrettyPrinter。这纯粹是Perl模块,所以让我们看看... 不知道从哪里上的所有Windows的Perl模块是对我来说这/usr/local/share/perl/5.10.1/HTML/PrettyPrinter.pm
也许不是优雅解决方案,但将工作,我希望。 此子解析属性/值对,一点点修正,它会在PrettyPrinter.pm 到底
〜行756添加单个“/”我,标志着我加入### <的刺< < < < <末
#
# format the attributes
#
sub _attributes {
my ($self, $e) = @_;
my @result =(); # list of ATTR="value" strings to return
my $self_closing = 0; ###<<<<<<
my @attrs = $e->all_external_attr(); # list (name0, val0, name1, val1, ...)
while (@attrs) {
my ($a,$v) = (shift @attrs,shift @attrs); # get current name, value pair
if($a eq '/') { ###<<<<<<
$self_closing=1; ###<<<<<<
next; ###<<<<<<
} ###<<<<<<
# string for output: 1. attribute name
my $s = $self->uppercase? "\U$a" : $a;.
# value part, skip for boolean attributes if desired
unless ($a eq lc($v) &&
$self->min_bool_attr &&.
exists($HTML::Tagset::boolean_attr{$e->tag}) &&
(ref($HTML::Tagset::boolean_attr{$e->tag}).
? $HTML::Tagset::boolean_attr{$e->tag}{$a}.
: $HTML::Tagset::boolean_attr{$e->tag} eq $a)) {
my $q = '';
# quote value?
if ($self->quote_attr || $v =~ tr/a-zA-Z0-9.-//c) {
# use single quote if value contains double quotes but no single quotes
$q = ($v =~ tr/"// && $v !~ tr/'//) ? "'" : '"'; # catch emacs ");
}
# add value part
$s .= '='.$q.(encode_entities($v,$q.$self->entities)).$q;
}
# add string to resulting list
push @result, $s;
}
push @result,'/' if $self_closing; ###<<<<<<
return @result; # return list ('attr="val"','attr="val"',...);
}
感谢您的回答。但是,这些都不是解决方案。即使使用了你建议的tab缩进选项,'print $ h-> as_HTML'仍然以奇怪的方式运行,这是人类永远不会想到的(例如,所有的h2都与前面的p标记在同一行上运行) 。因此使用PrettyPrinter。我想你错误地理解了我对PrettyPrinter的模仿例子。我的img标签没有任何问题 - PrettyPrinter将所有自闭标签打印为标准标签,并将/属性设置为“/”,例如'
'变成'
' – SenatorForLife 2012-01-19 15:05:19
我更新了关于如何修复模块的帖子。希望这会有所帮助 – Dimanoid 2012-01-19 19:20:36
这似乎工作得很好,谢谢!我肯定是一个Perl新手已经知道了我自己的黑客。对于其他Strawberry Perl用户,这里是HTML :: PrettyPrinter在安装cpanm后显示在我的机器上:C:\ strawberry \ perl \ site \ lib \ HTML \ PrettyPrinter.pm – SenatorForLife 2012-01-19 20:20:31