perl - 使用 Mojo::DOM 替换 HTML 片段中的文本时遇到问题

标签 perl mojolicious

我需要扫描 html 片段,查找文本中的某些字符串(不在元素属性内),并用 <span></span> 包装这些匹配的字符串。 。以下是输出的示例尝试:

use v5.10;
use Mojo::DOM;

my $body = qq|
<div>
<p>Boring Text:</p>
<p>
Highlight Cool whenever we see it.
but not <a href="/Cool.html">here</a>.
<code>
    sub Cool {
        print "Foo\n";
    }
</code>
And here is more Cool.
</p>
</div>
|;
my $dom = Mojo::DOM->new($body);

foreach my $e ($dom->find('*')->each) {
    my $text = $e->text;
    say "e text is:  $text ";
    if ($text =~ /Cool/) {
        (my $newtext = $text ) =~ s/Cool/<span class="fun">Cool<\/span>/g;
        $e->replace_content($newtext);
    }
}

say $dom->root;

输出:

e text is:   
e text is:  Boring Text: 
e text is:  Highlight Cool whenever we see it. but not. And here is more Cool. 
e text is:  here 
e text is:  sub Cool { print "Foo "; } 

<div>
<p>Boring Text:</p>
<p>Highlight <span class="fun">Cool</span> whenever we see it. but not. And here is more <span class="fun">Cool</span>.</p>
</div>

很接近,但我真正想看到的是如下内容:

<div>
<p>Boring Text:</p>
<p>Highlight <span class="fun">Cool</span> whenever we see it. but not <a href="/Cool.html">here</a>. 
<code>
sub <span class="fun">Cool<span> { 
    print "Foo\n"; 
}
</code>  
And here is more <span class="fun">Cool</span>.</p>
</div>

任何帮助/指示将不胜感激。 谢谢, 托德

最佳答案

研究过 XML::Twig 我不太确定它是正确的工具。令人惊讶的是,如此简单的任务竟然如此尴尬。

这是一个使用 HTML::TreeBuilder 的工作程序。不幸的是,它不会产生格式化输出,因此我自己添加了一些空格。

use strict;
use warnings;

use HTML::TreeBuilder;

my $html = HTML::TreeBuilder->new_from_content(<<__HTML__);
<div>
<p>Boring Text:</p>
<p>
Highlight Cool whenever we see it.
but not <a href="/Cool.html">here</a>.
<code>
    sub Cool {
        print "Foo\n";
    }
</code>
And here is more Cool.
</p>
</div>
__HTML__

$html->objectify_text;

for my $text_node ($html->look_down(_tag => '~text')) {

  my $text = $text_node->attr('text');

  if (my @replacement = process_text($text)) {
    my $old_node = $text_node->replace_with(@replacement);
    $old_node->delete;
  }
}

$html->deobjectify_text;

print $html->guts->as_XML;

sub process_text {

  my @nodes = split /\bCool\b/, shift;
  return unless @nodes > 1;

  my $span = HTML::Element->new('span', class => 'fun');
  $span->push_content('Cool');

  for (my $i = 1; $i < @nodes; $i += 2) {
    splice @nodes, $i, 0, $span->clone;
  }

  $span->delete;

  @nodes;
}

输出

<div>
<p>Boring Text:</p>
<p>
Highlight <span class="fun">Cool</span> whenever we see it.
but not <a href="/Cool.html">here</a>.
<code> sub <span class="fun">Cool</span> { print &quot;Foo &quot;; } </code>
And here is more <span class="fun">Cool</span>.
</p>
</div>

关于perl - 使用 Mojo::DOM 替换 HTML 片段中的文本时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17324568/

相关文章:

perl - Mojolicious websocket 使用

apache - 在反向代理 (ProxyPass) 后面的 Mojolicious 中配置带有前缀的 URL

perl - 在 mojolicious 中处理路由权限

perl - 我的 Perl 脚本如何确定 Excel 文件是 XLS 还是 XLSX 格式?

regex - 用空格搜索和替换多行文本

perl - 如何使用 Test::WWW::Mechanize::PSGI 测试 Dancer 应用程序?

mysql - 如何使用 UNIX 命令行将 XML 转换为 MYSQL 插入?

regex - 如何在 perl 替换运算符的替换端使用表达式?

perl - '%p' 和 'my %p' 之间的区别?

perl - 使用 Mojo::DOM 处理 HTML 文档时,如何最可靠地保留 HTML 实体?