this, that and the other thing. ...rest of footnote code maybe more text here.
####
#!/usr/local/bin/perl
use strict;
use warnings;
use OpenOffice::OODoc;
my $document = ooDocument( file => "testdoc.sxw");
## search recursively for footnote elements
## (I think both XML::Twig and OpenOffice::OODoc
## have methods for doing this, but this is working for me now...
sub foot_find {
my ( $elem, $arrayref ) = @_;
unless ( ref $elem ) {
return;
}
if ( $elem->att("text:style-name") &&
($elem->att("text:style-name") eq "footnote reference" )) {
push( @$arrayref, $elem );
}
my @entities = $elem->children("text:span");
foreach ( @entities ) {
foot_find( $_ , $arrayref );
}
}
# This returns a list of XML::Twig::Elt objects
my @all = $document->getTextElementList;
# build list of footnote elements
my @foots;
foreach ( @all ) {
foot_find( $_, \@foots );
}
#select footnotes with punctuation problems
grep { $_->prev_sibling_text =~ m<([,.])\s*$> } @foots;
foreach ( @foots ) {
my $pre = $_->sibling(-1);
my $post = $_->sibling(1);
unless ( $pre && $post ) {
print "problem with pre or post\n";
next;
}
### this is a problem, because sometimes
### the next element does not contain text
unless ( $pre->text && $post->text ){
print "No text around before and-or after\n";
next;
}
### this is the crux of the whole thing, and is what I would
### like to improve. The subs_text method seems like a hack in
### this case.
my $replace;
my $simple_punct = qr/([,.])\s*$/;
if ( $pre->text =~ $simple_punct ) {
$replace = $1;
}
print $replace."\n" if $replace;
$pre->subs_text( $simple_punct, '' );
$post->subs_text( qr/^/,
"$replace" );
}
$document->save("output.sxw");
##
##
sub jf { print substr($_[0], -1);
jf( substr($_[0], 0, length($_[0])-1))
if length $_[0] > 1; }
jf('gro.alubaf@yehaf');