Debian Codegraph @

scripts/tmxsplit

raw
1 #!/usr/bin/perl -s
2
3 # PODNAME: tmxsplit
4 # ABSTRACT: Splits a TMX file several files, one for each language
5
6 use strict;
7 use utf8;
8 use XML::DT;
9 #use bytes;
10
11 our ($twente,$latin1,$utf8,$q,$cutmaxlen);
12
13 $utf8 = 1 unless $latin1;
14
15 my $filename = $ARGV[0];
16
17 my %files;
18 my $data;
19 my %h = (
20 # '-outputenc' => "ISO-8859-1",
21 'seg' => sub{
22 for ($c){
23 # s/\&/\&/g;
24 # s/</\&lt;/g;
25 # s/>/\&gt;/g;
26 s/\s\s+|^\s+|\s+$/ /g;
27 };
28 $c
29 },
30 'ut' => sub{" "},
31 'tu' => sub{$c},
32 'tuv' => sub{
33 $c =~ s/^[\s\n]*//;
34 $c =~ s/[\s\n]*$//;
35 $data->{$v{lang}||$v{"xml:lang"}} =
36 $cutmaxlen && length($c) > $cutmaxlen
37 ? substr($c,0,$cutmaxlen)."||" : $c},
38 );
39
40 $h{-outputenc} = "ISO-8859-1" if $twente || $latin1;
41 undef $h{-outputenc} if $utf8;
42
43 my $i = 0;
44 $| = 1;
45 my $f;
46 for $f (@ARGV){
47 # print "\n$f" unless $q;
48 print STDERR "\n$f";
49 $/ = "\n";
50 open X, $f or die "cannot open file $f";
51 do {
52 if(/encoding=.ISO-8859-1./i){$h{-outputenc}=$h{-inputenc}="ISO-8859-1";}
53 } while ($_ = <X> and $_ !~ /<body\b/);
54
55 my $resto = "";
56 m!<body.*?>!s and $resto = $';
57 $/ = "</tu>";
58
59 while(<X>) {
60 ($_ = $resto . $_ and $resto = "" ) if $resto;
61 $i++;
62 last if /<\/body>/;
63 #print "." if (!$q && $i%500==0);
64 print STDERR "." if ($i % 1000==0);
65 s/\>\s+/>/;
66 undef($data);
67 eval {dtstring($_, %h)} ; ## don't die in invalid XML
68 if($@){warn($@)}
69 else{
70 for my $k (keys %$data) {
71 if (exists($files{"$filename-$k"})) {
72 myprint($files{"$filename-$k"}, $data->{$k},$i);
73 } else {
74 my $x;
75 open $x, ">$filename-$k" or die("cant >$filename-$k\n");
76 binmode($x,":utf8") if $utf8;
77 myprint($x, $data->{$k},$i);
78 $files{"$filename-$k"} = $x;
79 }
80 }
81 }
82 }
83 close X;
84 }
85
86 for my $key (keys %files) {
87 print "$key\n";
88 }
89
90 sub myprint{
91 my($f,$tu,$i)=@_;
92 if ($twente){
93 for ($tu){
94 s/<.*?>/ /gs;
95 s/[\|\$]/ /gs;
96 s/(\w)([.;,!:?«»"])/$1 $2/g;
97 s/([.;,!:?«»"])(\w)/$1 $2/g;
98 s/\s\s+|^\s+|\s+$/ /g;
99 }
100 print {$f} "$tu\n\$\n";
101 } else {
102 print {$f} "<tu id=\"$i\">$tu</tu>\n";
103 }
104 }
105
106 __END__
107
108 =pod
109
110 =encoding utf-8
111
112 =head1 NAME
113
114 tmxsplit - Splits a TMX file several files, one for each language
115
116 =head1 VERSION
117
118 version 0.39
119
120 =head1 SYNOPSIS
121
122 tmxsplit f.tmx f2.tmx ...
123 tmxsplit -twente f.tmx
124
125 =head1 DESCRIPTION
126
127 splits a TMX file in several files (one per language) and puts
128 a tag C<tu id=...> in each translation unit.
129
130 The names for output files is based on the first tmx file supplied.
131
132 =head1 Options
133
134 -twente -- makes a format compatible with twente-aligner
135
136 -latin1 -- a make latin1-encoded output
137 -utf8 -- a make utf8-encoded output
138
139 -q -- don't print filenames and "."
140
141 -cutmaxlen=n -- cut translations by the n character
142
143 =head1 AUTHORS
144
145 =over 4
146
147 =item *
148
149 Alberto Simões <[email protected]>
150
151 =item *
152
153 José João Almeida <[email protected]>
154
155 =back
156
157 =head1 COPYRIGHT AND LICENSE
158
159 This software is copyright (c) 2010-2017 by Projeto Natura <[email protected]>.
160
161 This is free software; you can redistribute it and/or modify it under
162 the same terms as the Perl 5 programming language system itself.
163
164 =cut