真是太好了,本來想自己寫一個的說…
不過有些處理還是可以再調整一下,我調整過的版如下
use feature qw(say);
use HTML::TreeBuilder;
if ($#ARGV != 1) {
say "/n/nusage: hhc2idx.pl your.hhc your.htm";
exit;
}
{
my $tree = HTML::TreeBuilder->new; # build empty tree
$tree->parse_file($ARGV[0]); #parse hhc file as HTML
$tree->elementify(); #optional prevent reparse
@objects = $tree->look_down("_tag","object"); #all object
#say "there ar $#objects ob";
for my $ob (@objects) {
my ($el, $dst, $name);
$el = $ob->look_down("name", "Local"); #Local val as href URL
if (defined($el)) {
$dst = $el->attr('value')
}
$el = $ob->look_down("name", "Name"); #Name val as text
if (defined($el)) {
$name = $el->attr('value')
}
#say sprintf('%s', $dst, $name);
if (defined($dst) && defined($name)) {
$ob->replace_with(
sprintf('%s', $dst)
);
}
elsif(defined($name)) {
$ob->replace_with(
sprintf('
%s', $name)
);
}else{
$ob->replace_with("");
}
}
my $outfile;
open $outfile, ">", $ARGV[1];
say $outfile $tree->as_HTML("", " "); #say out the new html text
# Now that we're done with it, we must destroy it.
$tree = $tree->delete;
}
http://blog.csdn.net/DelphiNew/article/details/4510436
上文作者另一作品,"支持自动TOC超文本目录生成的chm转换器(perl脚本) "
你好博主,非常感谢你的方法,但是我有一个困扰,我发现“sprintf('%s', $dst, $name)”,只能显示超链接名称(没有超链接)或者标题名称其中一个,不懂这是为什么,原文作者的文件试了很多遍都无法用提示第五行错误
回覆刪除----perl的小白
很久沒用 perl 了,感覺應改成 sprintf('%s', $dst)
刪除