| 1 | #!/usr/bin/perl -s | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # PODNAME: tmx2tmx | ||||||||||||||||||||||||||||
| 4 | # ABSTRACT: Utility to convert and filter TMX files | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use XML::TMX::Reader; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use XML::TMX::Writer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use XML::DT; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | our ( | ||||||||||||||||||||||||||||
| 15 | 1 | $toTrados, ## Fix some issues in the TMX file the old trados software did not cope with | |||||||||||||||||||||||||||
| 16 | $clean, ## Remove empty UTs and UTs with only junk characters | ||||||||||||||||||||||||||||
| 17 | $cat, ## Concatenates TMX files | ||||||||||||||||||||||||||||
| 18 | $select, ## Filters the TMX to the selected languages | ||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # here we must take care that only one of the options is being used. | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | toTrados() if $toTrados; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 1 | ▶ | clean() if $clean; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 1 | ▶ | cat() if $cat; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 0 | ▶ | selectl() if $select; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 0 | die "No option supplied\n"; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | #--( AUX FUNCS )----------- | ||||||||||||||||||||||||||||
| 35 | 0 | ▶ | sub trim { | ||||||||||||||||||||||||||
|
Subroutine
trim: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 36 | 0 | my $x = shift; | |||||||||||||||||||||||||||
| 37 | 0 | for ($x) { | |||||||||||||||||||||||||||
| 38 | 0 | s!^\s+!!; | |||||||||||||||||||||||||||
| 39 | 0 | s!\s+$!! | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | $x | ||||||||||||||||||||||||||||
| 42 | 0 | } | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | sub toTrados { | ||||||||||||||||||||||||||
|
Subroutine
toTrados: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 46 | 0 | $/ = "</tu>"; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | my $header = <>; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # <header/> -> <header></header> | ||||||||||||||||||||||||||||
| 51 | 0 | $header =~ s!<header([^/>]+)/>!<header$1></header>!; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # <body version...> -> <body> | ||||||||||||||||||||||||||||
| 54 | 0 | $header =~ s!<body[^>]+>!<body>!; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # tmx version -> <tmx version="1.1"> | ||||||||||||||||||||||||||||
| 57 | 0 | $header =~ s!<tmx version[^>]+>!<tmx version="1.1">!; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # srclang -> srclang="foo" | ||||||||||||||||||||||||||||
| 60 | 0 | ▶ | if ($header =~ m!(xml:)?lang=(["'])([^"']+)\2!) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | my $lang = $3; | |||||||||||||||||||||||||||
| 62 | 0 | $header =~ s!srclang=(["'])[^"']+\1!srclang="$lang"!; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | $header =~ s/xml:lang/lang/g; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # xml:lang -> lang | ||||||||||||||||||||||||||||
| 68 | 0 | print $header; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | while (<>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 0 | s/xml:lang/lang/g; | |||||||||||||||||||||||||||
| 72 | 0 | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # ugly, but prevents us from forgetting an else somewhere. | ||||||||||||||||||||||||||||
| 76 | 0 | exit; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | sub clean { | ||||||||||||||||||||||||||
|
Subroutine
clean: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 81 | 0 | my $file; | |||||||||||||||||||||||||||
| 82 | my $reader; | ||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if ($file = shift) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | # isto tv fosse mais rápido em sax... | ||||||||||||||||||||||||||||
| 85 | print dt( $file, | ||||||||||||||||||||||||||||
| 86 | 0 | ▶ | -default => sub{ toxml() }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | seg => sub{ | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 88 | 0 | $c = trim($c); toxml | |||||||||||||||||||||||||||
| 89 | }, | ||||||||||||||||||||||||||||
| 90 | 0 | ▶ | tu => sub{ | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 91 | # remove entries with junk, only | ||||||||||||||||||||||||||||
| 92 | 0 | ▶ | return "" if $c =~ m!<seg>(\s|[\|\!@#\$%^&\-])*</seg>!; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | toxml() | |||||||||||||||||||||||||||
| 94 | }, | ||||||||||||||||||||||||||||
| 95 | 0 | ); | |||||||||||||||||||||||||||
| 96 | } else { | ||||||||||||||||||||||||||||
| 97 | 0 | die "At the moment, we do not handle files from stdin"; | |||||||||||||||||||||||||||
| 98 | # TODO... aqui fazia jeito que o XML::TMX::Reader lêsse de um | ||||||||||||||||||||||||||||
| 99 | # filehandle já aberto (STDIN, por exemplo); | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 0 | exit; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 1 | ▶ | sub _join_and_print_headers { | ||||||||||||||||||||||||||
|
Subroutine
_join_and_print_headers: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 105 | 1 | my $header; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 1 | ▶ | while (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 2 | my $file = shift @_; | |||||||||||||||||||||||||||
| 109 | 2 | my $reader = XML::TMX::Reader->new($file); | |||||||||||||||||||||||||||
| 110 | 2 | for my $key (keys %{$reader->{header}}) { | |||||||||||||||||||||||||||
| 111 | 22 | ▶ | if ($key eq "-prop") { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 112 | 2 | for my $type (keys %{$reader->{header}{$key}}) { | |||||||||||||||||||||||||||
| 113 | 6 | push @{$header->{$key}{$type}}, @{$reader->{header}{$key}{$type}} | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | elsif ($key eq "-note") { | ||||||||||||||||||||||||||||
| 117 | 2 | push @{$header->{$key}}, @{$reader->{header}{$key}} | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | else { | ||||||||||||||||||||||||||||
| 120 | 18 | $header->{$key} = $reader->{header}{$key} | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 1 | my $writer = XML::TMX::Writer->new(); | |||||||||||||||||||||||||||
| 126 | 1 | $writer->start_tmx(%$header); | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 2 | ▶ | sub open_tmx_with_correct_encoding { | ||||||||||||||||||||||||||
|
Subroutine
open_tmx_with_correct_encoding: called CC=5 SCAR=18.1
| |||||||||||||||||||||||||||||
| 130 | # in the future it might be a good idea to use the guess_encoding method from Reader.pm | ||||||||||||||||||||||||||||
| 131 | 2 | my $filename = shift; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 2 | ▶ | open my $f, $filename or die "Can't open file $filename"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 2 | my $header = <$f>; | |||||||||||||||||||||||||||
| 135 | 2 | close $f; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 2 | my $fh; | |||||||||||||||||||||||||||
| 138 | 2 | ▶ | if ($header =~ /encoding\s*=\s*["']([^"']+)["']/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 2 | ▶ | open $fh, "<:encoding($1)", $filename or die "Can't open file $filename"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | else { | ||||||||||||||||||||||||||||
| 142 | 0 | ▶ | open $fh, "<:utf8", $filename or die "Can't open file $filename"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | 2 | return $fh; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 1 | ▶ | sub cat { | ||||||||||||||||||||||||||
|
Subroutine
cat: called CC=9 SCAR=22.2
| |||||||||||||||||||||||||||||
| 148 | 1 | my @files ; | |||||||||||||||||||||||||||
| 149 | 1 | ▶ | if (@ARGV) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 1 | @files = @ARGV; | |||||||||||||||||||||||||||
| 151 | } else { | ||||||||||||||||||||||||||||
| 152 | 0 | @files = map { chomp; $_ } <>; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 1 | binmode STDOUT, ":utf8"; | |||||||||||||||||||||||||||
| 156 | 1 | _join_and_print_headers(@files); | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 1 | my $fh; | |||||||||||||||||||||||||||
| 159 | 1 | my $file = shift @files; | |||||||||||||||||||||||||||
| 160 | 1 | $fh = open_tmx_with_correct_encoding($file); | |||||||||||||||||||||||||||
| 161 | # open $fh, "<:utf8", $file or die "Cannot open file: $file\n"; | ||||||||||||||||||||||||||||
| 162 | 1 | ▶ | while (<$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 10 | ▶ | last if m!<body!; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | 1 | print "<!-- FILE: $file -->\n"; | |||||||||||||||||||||||||||
| 166 | 1 | ▶ | while (<$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 19 | s!</\s*(body|tmx)\s*>!!; | |||||||||||||||||||||||||||
| 168 | 19 | print; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | 1 | close $fh; | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 1 | for $file (@files) { | |||||||||||||||||||||||||||
| 173 | 1 | $fh = open_tmx_with_correct_encoding($file); | |||||||||||||||||||||||||||
| 174 | # open $fh, "<:utf8", $file or die "Cannot open file: $file\n"; | ||||||||||||||||||||||||||||
| 175 | 1 | print "<!-- FILE: $file -->\n"; | |||||||||||||||||||||||||||
| 176 | 1 | ▶ | while (<$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | 12 | ▶ | last if m!<body!; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | 1 | s/<body[^>]*>//g; | |||||||||||||||||||||||||||
| 180 | 1 | print; | |||||||||||||||||||||||||||
| 181 | 1 | ▶ | while (<$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 23 | s!</\s*(body|tmx)\s*>!!; | |||||||||||||||||||||||||||
| 183 | 23 | print; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | 1 | close $fh; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | 1 | print "</body>\n"; | |||||||||||||||||||||||||||
| 188 | 1 | print "</tmx>\n"; | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 1 | exit; | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 0 | ▶ | sub selectl { | ||||||||||||||||||||||||||
|
Subroutine
selectl: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 194 | 0 | my @files; | |||||||||||||||||||||||||||
| 195 | 0 | ▶ | if (@ARGV) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 0 | @files = @ARGV; | |||||||||||||||||||||||||||
| 197 | } else { | ||||||||||||||||||||||||||||
| 198 | 0 | @files = map { chomp; $_ } <>; | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 0 | _join_and_print_headers(@files); | |||||||||||||||||||||||||||
| 202 | 0 | my %languages = map { (lc $_ => 1) } split( /,/, $select ); | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 0 | my $tmx; | |||||||||||||||||||||||||||
| 205 | 0 | ▶ | while ($tmx = shift @files) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | 0 | ▶ | die "Can't find $tmx" if (!$tmx || !-f$tmx); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$tmx and -f $tmx
MC/DC0%0/2
$tmx and -f $tmx
$tmx
-f $tmx
Truth table0%0/3
A and B
A= $tmxB= -f $tmx
| |||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 0 | print STDERR "$tmx\n"; | |||||||||||||||||||||||||||
| 209 | 0 | my $reader = XML::TMX::Reader->new($tmx); | |||||||||||||||||||||||||||
| 210 | $reader->for_tu( | ||||||||||||||||||||||||||||
| 211 | { | ||||||||||||||||||||||||||||
| 212 | -verbose => 1, | ||||||||||||||||||||||||||||
| 213 | -output => 1, | ||||||||||||||||||||||||||||
| 214 | -header => 0 | ||||||||||||||||||||||||||||
| 215 | }, | ||||||||||||||||||||||||||||
| 216 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 217 | 0 | my $tu = shift; | |||||||||||||||||||||||||||
| 218 | 0 | my $l = 0; | |||||||||||||||||||||||||||
| 219 | 0 | for my $k (keys %$tu) { | |||||||||||||||||||||||||||
| 220 | 0 | ▶ | next if $k =~ /^-/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | 0 | ▶ | if (exists $languages{lc $k}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 0 | $l++; | |||||||||||||||||||||||||||
| 223 | } else { | ||||||||||||||||||||||||||||
| 224 | 0 | delete $tu->{$k} | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | 0 | ▶ | if ($l == keys %languages) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 228 | 0 | return $tu; | |||||||||||||||||||||||||||
| 229 | } else { | ||||||||||||||||||||||||||||
| 230 | 0 | return undef; | |||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | 0 | }); | |||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | 0 | print "</body>\n</tmx>\n"; | |||||||||||||||||||||||||||
| 235 | 0 | exit; | |||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | __END__ | ||||||||||||||||||||||||||||