#!/usr/bin/perl
use strict;
use XML::Twig;
my @xmltst = ("Doc 1Some text.footnote text",
"Doc 2(More text.)next footnote QED",
"Doc 3\"More text?\"3rd footnote QED",
"Doc 4('More text!')4th note QED.Finis.",
);
for my $doc ( @xmltst ) {
my $twig = XML::Twig->new();
$twig->parse( $doc );
my @b_elts = $twig->get_xpath('//b');
print "\ndoc contains ",scalar @b_elts," footnotes:\n";
$twig->print;
for my $b ( @b_elts ) {
my $prev = $b->prev_sibling;
my $prev_text = $prev->text;
if ( $prev_text =~ /([.,?!\)\"\']+)$/ ) {
my $punct = $1;
my $offset = length( $prev_text ) - length( $punct );
if ( $b->next_sibling ) {
my $next = $b->next_sibling;
$prev->set_text( substr( $prev_text, 0, $offset ));
$next->set_text( $punct . $next->text );
}
else {
my $next = $prev->split_at( $offset );
$next->cut;
print "\n created new elt containing: ".$next->text;
$next->paste( 'after', $b );
}
}
}
print "\n AFTER EDITING:\n";
$twig->print;
$twig->dispose;
print "\n";
}
__OUTPUT__
doc contains 1 footnotes:
Doc 1Some text.footnote text
created new elt containing: .
AFTER EDITING:
Doc 1Some textfootnote text.
doc contains 1 footnotes:
Doc 2(More text.)next footnote QED
AFTER EDITING:
Doc 2(More textnext footnote.) QED
doc contains 1 footnotes:
Doc 3"More text?"3rd footnote QED
created new elt containing: ?"
AFTER EDITING:
Doc 3"More text3rd footnote?" QED
doc contains 1 footnotes:
Doc 4('More text!')4th note QED.Finis.
AFTER EDITING:
Doc 4('More text4th note!') QED.Finis.