The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.
#!/usr/bin/perl -w -s

## use utf8::all;
use Data::Dumper;

our ($all,$w,$max,$pdf, $o, $n, $a,$debug, $i);

my ($ab,$ae); ## emphatise marker
if($a){
  if($a eq "1"){
    $ab="=(";
    $ae=")=";
  }
  elsif($a){
    $ab="<$a>";
    $ae="</$a>";
  }
}


$max //= 500;
$max = 1000000000 if ($max == 0 or $all);

use strict;
use warnings;

my $p = shift;

if($i and $w){ $p = qr{(?i)\b$p\b} }
elsif($i)    { $p = qr{(?i)$p} }
elsif($w)    { $p = qr{\b$p\b} }

use XML::TMX;
use XML::TMX::Reader;

$o //= "__.tmx" if $pdf;

our ($icons);

my $tmx = shift;
my $tmx_obj = XML::TMX::Reader->new($tmx);

 $tmx_obj->for_tu( 
    { patt => $p, 
      gen_tu=> $max, 
      n => $n || 0,
      output => $o || undef }, #### "__tmxgrep.tmx" 
    sub { my($tu,$at) = @_;
          print STDERR  Dumper($tu,$at) if $main::debug; 
#         print STDERR  "."; 
          if($main::a){
            for my $k(keys %$tu){
              for(keys %{$tu->{$k}}){
                $tu->{$k}{$_} =~ s!($p)!$ab$1$ae!g;
              }
            }
          }
          return $tu;
    }
 );

if($pdf){ system("tmx2pdf-prince -v  $o");}


__END__

=head1 NAME

tmxgrep - grep translation units in a TMX file

=head1 SYNOPSIS

 tmxgrep <perlregexp> file.tmx
 options:
   -pdf        -- output is transformed in a PDF file 
   -max=300    -- extract up to 300 TU (def=500)
   -max=0      -- all matches
   -all        -- all matches
   -a          -- annotate matches with '=(...)='
   -o=out.tmx  -- define output file (defaut= STDOUT)
   -n          -- print original TU number (eg: <tu n="35">)
   -i          -- ignore case

=head1 DESCRIPTION

Creates a TMX file with the translation units that macth the provided
regular expression.

=head1 AUTHOR

J.Joao Almeida, jj@di.uminho.pt

=head1 SEE ALSO

perl(1).

xpdf-tmx

XML::TMX

TMX

=cut