, XML:: Twig ( ) . , , mark, .
, , . Regexp:: Assempble , . , mark ( , ), , href ( "" , ).
: , . , , , - , "" "" , "furtykurtle", "furtikurty" "furtijurty", . , , , . , .
use strict;
use warnings;
use XML::Twig;
use Regexp::Assemble;
use Test::More tests => 1;
use autodie qw(open);
my %dictionary = (
frobnitz => 'definitions.html#frobnitz',
crulps => 'definitions.html#crulps',
furtikurty => 'definitions.html#furtikurty',
);
my $match_defs= Regexp::Assemble->new()
->add( keys %dictionary)
->anchor_word
->as_string;
$match_defs= qr/($match_defs)/;
my $in = data_para();
my $expected = data_para();
my $out;
open( my $out_fh, '>', \$out);
XML::Twig->new( twig_roots => { 'description' => sub { tag_defs( @_, $out_fh, $match_defs, \%dictionary); } },
twig_print_outside_roots => $out_fh,
)
->parse( $in);
is( $out, $expected, 'base test');
exit;
sub tag_defs
{ my( $t, $description, $out_fh, $match_defs, $dictionary)= @_;
my @a= $description->mark( $match_defs, 'a' );
my $tagged_in_description;
foreach my $a (@a)
{ my $word= $a->text;
warn "checking a: ", $a->sprint, "\n";
if( $tagged_in_description->{$word})
{ $a->erase; }
else
{ $a->set_att( href => $dictionary->{$word}); }
$tagged_in_description->{$word}++;
}
$t->flush( $out_fh); }
sub def_href
{ my( $word)= @_;
return $dictionary{word};
}
sub data_para
{ local $/="\n\n";
my $para= <DATA>;
return $para;
}
__DATA__
<article>
<author>Smith</author>
<date>2011-10-10</date>
<description>Article about <b>frobnitz</b>, crulps and furtikurty's. Mainly frobnitz</description>
</article>
<article>
<author>Smith</author>
<date>2011-10-10</date>
<description>Article about <b><a href="definitions.html#frobnitz">frobnitz</a></b>, <a href="definitions.html#crulps">crulps</a> and <a href="definitions.html#furtikurty">furtikurty</a>'s. Mainly frobnitz</description>
</article>