Hello Monks --
I am working with OpenOffice::OODoc and therefore XML::Twig to modify a Writer document. I am trying to move footnotes in relation to the surrounding text. The text representation of the XML would be something like this:
Sample text:
this, that and the other thing.<text:span text:style-name="footnote reference"> ...rest of footnote code </text: +span> maybe more text here.
I need to be able to move the <text:span></text:span> element to the left or to the right in the text. (More specifically, this is designed to move the footnote before any punctuation, in accordance with local typographical rules.)
Here is my best shot so far, which works but seems a bit too much like a hack, mostly due to use of the subs_text XML::Twig method in an unexpressive way, making me suspect that there might be a more robust solution:
#!/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");
I am having trouble putting my head around this. It seems like there should be a way to say more clearly that I just want to move the tag up or down within its parent text.
Advance thanks for any insights.
s-t
sub jf { print substr($_[0], -1); jf( substr($_[0], 0, length($_[0])-1)) if length $_[0] > 1; } jf('gro.alubaf@yehaf');
In reply to Moving a tag within text with XML::Twig by skillet-thief
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |