#!/usr/bin/perl -s
#use strict;
#use warnings;
use XML::TMX::Reader;
use XML::DT;
our ($toTrados, $clean, $join, $cat);
# here we must take care that only one of the options is being used.
=head1 NAME
tmx2tmx - utility to convert and filter TMX files
=head1 SYNOPSYS
tmx2tmx -cat file1.tmx ... filen.tmx > file.tmx
tmx2tmx -toTrados file1.tmx > file2.tmx
tmx2tmx -clean file1.tmx > file2.tmx
=cut
# tmx2tmx -join file1.tmx ... filen.tmx > file.tmx
=head1 DESCRIPTION
This utility processes TMX documents and return TMX documents. Tasks
done with this utility include conversion between TMX versions and TMX
cleaning.
=head2 TRADOS conversion
As you maybe know, TRADOS is a company producing computer software for
translators. It includes WorkBench which imports TMX
files. Unfortunately, the version I used do not import TMX version
1.4.
This process is done using the switch C<-toTrados>:
tmx2tmx -toTrados file.tmx > trados.tmx
=cut
if ($toTrados) {
$/ = "";
my $header = <>;
# ->
$header =~ s!]+)/>!!;
#
->
$header =~ s!]+>!!;
# tmx version ->
$header =~ s!]+>!!;
# srclang -> srclang="foo"
if ($header =~ m!(xml:)?lang=(["'])([^"']+)\2!) {
my $lang = $3;
$header =~ s!srclang=(["'])[^"']+\1!srclang="$lang"!;
}
$header =~ s/xml:lang/lang/g;
# xml:lang -> lang
print $header;
while(<>) {
s/xml:lang/lang/g;
print
}
# ugly, but prevents us from forgetting an else somewhere.
exit;
}
=head2 TMX Cleaning
Specially when translation memories are created from anotated text, or
extracted directly from the Internet using any automatic method. This
switch is used to remove junk in translation units.
This option tries to remove junk from TMX files, like empty pairs
where one of the sides is empty, or removing other junk type.
Use it this way:
tmx2tmx -clean file.tmx > file2.tmx
=cut
if ($clean) {
my $file;
my $reader;
if ($file = shift) {
# isto tv fosse mais rápido em sax...
print dt( $file,
-default => sub{ toxml() },
seg => sub{
$c = trim($c); toxml
},
tu => sub{
# remove entries with junk, only
return "" if $c =~ m!(\s|[\|\!@#\$%^&\-])*!;
toxml()
},
);
} else {
die "At the moment, we do not handle files from stdin";
# TODO... aqui fazia jeito que o XML::TMX::Reader lêsse de um
# filehandle já aberto (STDIN, por exemplo);
}
}
#=head2 Joining TMX
#
# tmx2tmx -join file1.tmx ... filen.tmx > file.tmx
#
#=cut
# para juntar temos de saber que linguas cada tmx tem:
#
# 1- só podemos juntar se tiverem línguas em comum?
# 2- temos de ter cuidado com segmentos repetidos
# 2.1- manter tudo em memória?
#
=head2 Concatenating TMX
tmx2tmx -cat file1.tmx ... filen.tmx > file.tmx
ls | grep '.tmx$' | tmx2tmx -cat > file.tmx
=cut
if ($cat) {
my @files ;
if(@ARGV){ @files = @ARGV; }
else { @files = map { chomp; $_ } <> }
my $file = shift @files;
open F, $file or die "Cannot open file: $file\n";
while() {
print;
last if m!\n";
while() {
s!\s*(body|tmx)\s*>!!;
print;
}
close F;
for $file (@files) {
open F, $file or die "Cannot open file: $file\n";
print "\n";
while() {
last if m!]*>//g;
print;
while() {
s!\s*(body|tmx)\s*>!!;
print;
}
close F;
}
print "\n";
print "\n";
}
=head1 SEE ALSO
tmx2html, po2tmx, XML::TMX
=head1 AUTHOR
Alberto Simões, Ealbie@alfarrabio.di.uminho.ptE
=head1 COPYRIGHT AND LICENSE
Copyright 2004 by Projecto Natura
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
#--( AUX FUNCS )-----------
sub trim {
my $x = shift;
for ($x) {
s!^\s+!!;
s!\s+$!!
}
$x
}