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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.