#!/usr/bin/perl
use strict;
use Getopt::Long;
use RTF::Tokenizer;
my %opts = ();
GetOptions (\%opts, 'from=s', 'to=s');
my $filename = shift;
die "Please provide an rtf file to parse.\n" unless $filename;
my $tokenizer = RTF::Tokenizer->new( file => $filename);
while( my ( $type, $arg, $param ) = $tokenizer->get_token() ){
last if $type eq 'eof';
if($type eq 'control' and $arg eq 'revtbl') {
my $match = 0;
put($type, $arg, $param) if $opts{from} and $opts{to};
my $brace = 1;
while($brace > 0){
my @attr = $tokenizer->get_token();
$brace++ if $attr[0] eq 'group' and $attr[1] == 1;
$brace-- if $attr[0] eq 'group' and $attr[1] == 0;
if( $attr[0] eq 'text') {
$attr[1] =~ s/;$//;
if( $opts{from} and $opts{to} ){
if( $opts{from} eq $attr[1] ) {
$attr[1] = $opts{to};
$match = 1;
}
$attr[1] .= ';';
put( @attr);
} else {
print $attr[1], "\n" unless $attr[1] eq 'Unknown';
}
} else {
put(@attr) if $opts{from} and $opts{to};
}
}
if($opts{from} and $opts{to} and !$match) {
print STDERR "The author $opts{from} was not found in the document!\n";
}
} else {
put($type, $arg, $param) if $opts{from} and $opts{to};
}
}
sub put {
my ($type, $arg, $param) = @_;
if( $type eq 'group' ) {
print $arg == 1 ? '{' : '}';
} elsif( $type eq 'control' ) {
print "\\$arg$param";
} elsif( $type eq 'text' ) {
print "\n$arg";
}
}
=head1 NAME
=head1 SYNOPSIS
=head1 OPTIONS
=over4
=item B<-from>
=item B<-to>
=back
=head1 DESCRIPTION
=head1 AUTHOR
@qnot
=head1 COPYRIGHT
=cut