| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package XML::TMX::Writer; |
|
2
|
|
|
|
|
|
|
$XML::TMX::Writer::VERSION = '0.39'; |
|
3
|
|
|
|
|
|
|
# ABSTRACT: Perl extension for writing TMX files |
|
4
|
|
|
|
|
|
|
|
|
5
|
6
|
|
|
6
|
|
96801
|
use 5.010; |
|
|
6
|
|
|
|
|
28
|
|
|
6
|
6
|
|
|
6
|
|
31
|
use warnings; |
|
|
6
|
|
|
|
|
10
|
|
|
|
6
|
|
|
|
|
147
|
|
|
7
|
6
|
|
|
6
|
|
25
|
use strict; |
|
|
6
|
|
|
|
|
12
|
|
|
|
6
|
|
|
|
|
118
|
|
|
8
|
6
|
|
|
6
|
|
23
|
use Exporter (); |
|
|
6
|
|
|
|
|
11
|
|
|
|
6
|
|
|
|
|
9447
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our @ISA = 'Exporter'; |
|
11
|
|
|
|
|
|
|
our @EXPORT_OK = qw(); |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new { |
|
15
|
5
|
|
|
5
|
1
|
642
|
my $proto = shift; |
|
16
|
5
|
|
33
|
|
|
24
|
my $class = ref($proto) || $proto; |
|
17
|
5
|
|
|
|
|
12
|
my %ops = @_; |
|
18
|
5
|
|
|
|
|
16
|
my $self = { OUTPUT => \*STDOUT }; |
|
19
|
5
|
50
|
33
|
|
|
33
|
binmode $self->{OUTPUT}, ":utf8" unless exists $ops{-encoding} and $ops{-encoding} !~ /utf.?8/i; |
|
20
|
5
|
|
|
|
|
12
|
bless($self, $class); |
|
21
|
5
|
|
|
|
|
14
|
return($self); |
|
22
|
|
|
|
|
|
|
} |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub start_tmx { |
|
26
|
5
|
|
|
5
|
1
|
376
|
my $self = shift; |
|
27
|
5
|
|
|
|
|
38
|
my %options = @_; |
|
28
|
5
|
|
|
|
|
8
|
my %o; |
|
29
|
|
|
|
|
|
|
|
|
30
|
5
|
|
|
|
|
73
|
my @time = gmtime(time); |
|
31
|
5
|
|
|
|
|
55
|
$o{'creationdate'} = sprintf("%d%02d%02dT%02d%02d%02dZ", $time[5]+1900, |
|
32
|
|
|
|
|
|
|
$time[4]+1, $time[3], $time[2], $time[1], $time[0]); |
|
33
|
|
|
|
|
|
|
|
|
34
|
5
|
|
100
|
|
|
35
|
my $encoding = $options{encoding} || "UTF-8"; |
|
35
|
|
|
|
|
|
|
|
|
36
|
5
|
50
|
|
|
|
45
|
if (defined($options{'-output'})) { |
|
37
|
5
|
|
|
|
|
31
|
delete $self->{OUTPUT}; # because it is a glob |
|
38
|
5
|
50
|
|
|
|
435
|
open $self->{OUTPUT}, ">", $options{'-output'} |
|
39
|
|
|
|
|
|
|
or die "Cannot open file '$options{'-output'}': $!\n"; |
|
40
|
|
|
|
|
|
|
} |
|
41
|
|
|
|
|
|
|
|
|
42
|
5
|
50
|
|
|
|
49
|
if ($encoding =~ m!utf.?8!i) { |
|
43
|
5
|
|
|
|
|
25
|
binmode $self->{OUTPUT}, ":utf8" |
|
44
|
|
|
|
|
|
|
} |
|
45
|
5
|
|
|
|
|
31
|
$self->_write("\n"); |
|
46
|
|
|
|
|
|
|
|
|
47
|
5
|
|
|
|
|
16
|
my @valid_segtype = qw'block sentence paragraph phrase'; |
|
48
|
5
|
50
|
33
|
|
|
21
|
if(defined($options{SEGTYPE}) && grep { $_ eq $options{SEGTYPE} } @valid_segtype) { |
|
|
0
|
|
|
|
|
0
|
|
|
49
|
0
|
|
|
|
|
0
|
$o{segtype} = $options{SEGTYPE}; |
|
50
|
|
|
|
|
|
|
} else { |
|
51
|
5
|
|
|
|
|
42
|
$o{segtype} = 'sentence' |
|
52
|
|
|
|
|
|
|
} |
|
53
|
|
|
|
|
|
|
|
|
54
|
5
|
|
50
|
|
|
26
|
$o{'creationtool'} = $options{tool} || 'XML::TMX::Writer'; |
|
55
|
5
|
|
33
|
|
|
28
|
$o{'creationtoolversion'} = $options{toolversion} || $XML::TMX::Writer::VERSION; |
|
56
|
5
|
|
50
|
|
|
19
|
$o{'o-tmf'} = $options{srctmf} || 'plain text'; |
|
57
|
5
|
|
100
|
|
|
18
|
$o{'adminlang'} = $options{adminlang} || 'en'; |
|
58
|
5
|
|
100
|
|
|
15
|
$o{'srclang'} = $options{srclang} || 'en'; |
|
59
|
5
|
|
100
|
|
|
16
|
$o{'datatype'} = $options{datatype} || 'plaintext'; |
|
60
|
|
|
|
|
|
|
|
|
61
|
5
|
50
|
|
|
|
18
|
defined($options{srcencoding}) and $o{'o-encoding'} = $options{srcencoding}; |
|
62
|
5
|
100
|
|
|
|
12
|
defined($options{id}) and $o{'creationid'} = $options{id}; |
|
63
|
|
|
|
|
|
|
|
|
64
|
5
|
|
|
|
|
17
|
$self->_startTag(0, 'tmx', 'version' => 1.4)->_nl; |
|
65
|
5
|
|
|
|
|
20
|
$self->_startTag(1, 'header', %o)->_nl; |
|
66
|
|
|
|
|
|
|
|
|
67
|
5
|
50
|
|
|
|
36
|
$self->_write_props(2, $options{'-prop'}) if defined $options{'-prop'}; |
|
68
|
5
|
50
|
|
|
|
30
|
$self->_write_notes(2, $options{'-note'}) if defined $options{'-note'}; |
|
69
|
|
|
|
|
|
|
|
|
70
|
5
|
|
|
|
|
21
|
$self->_indent(1)->_endTag('header')->_nl; |
|
71
|
|
|
|
|
|
|
|
|
72
|
5
|
|
|
|
|
11
|
$self->_startTag(0,'body')->_nl->_nl; |
|
73
|
|
|
|
|
|
|
} |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub _write_props { |
|
76
|
33
|
|
|
33
|
|
59
|
my ($self, $indent, $props) = @_; |
|
77
|
33
|
50
|
|
|
|
69
|
return unless ref($props) eq "HASH"; |
|
78
|
33
|
|
|
|
|
88
|
for my $key (sort keys %$props) { |
|
79
|
50
|
100
|
|
|
|
99
|
if (ref($props->{$key}) eq "ARRAY") { |
|
80
|
24
|
|
|
|
|
26
|
for my $val (@{$props->{$key}}) { |
|
|
24
|
|
|
|
|
40
|
|
|
81
|
31
|
50
|
|
|
|
52
|
if ($key eq "_") { |
|
82
|
0
|
|
|
|
|
0
|
$self->_startTag($indent, 'prop'); |
|
83
|
|
|
|
|
|
|
} else { |
|
84
|
31
|
|
|
|
|
50
|
$self->_startTag($indent, prop => (type => $key)); |
|
85
|
|
|
|
|
|
|
} |
|
86
|
31
|
|
|
|
|
61
|
$self->_characters($val); |
|
87
|
31
|
|
|
|
|
50
|
$self->_endTag('prop')->_nl; |
|
88
|
|
|
|
|
|
|
} |
|
89
|
|
|
|
|
|
|
} else { |
|
90
|
26
|
50
|
|
|
|
44
|
if ($key eq "_") { |
|
91
|
0
|
|
|
|
|
0
|
$self->_startTag($indent, 'prop'); |
|
92
|
|
|
|
|
|
|
} else { |
|
93
|
26
|
|
|
|
|
44
|
$self->_startTag($indent, prop => (type => $key)); |
|
94
|
|
|
|
|
|
|
} |
|
95
|
26
|
|
|
|
|
61
|
$self->_characters($props->{$key}); |
|
96
|
26
|
|
|
|
|
41
|
$self->_endTag('prop')->_nl; |
|
97
|
|
|
|
|
|
|
} |
|
98
|
|
|
|
|
|
|
} |
|
99
|
|
|
|
|
|
|
} |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub _write_notes { |
|
102
|
33
|
|
|
33
|
|
50
|
my ($self, $indent, $notes) = @_; |
|
103
|
33
|
50
|
|
|
|
61
|
return unless ref($notes) eq "ARRAY"; |
|
104
|
33
|
|
|
|
|
37
|
for my $p (@{$notes}) { |
|
|
33
|
|
|
|
|
52
|
|
|
105
|
82
|
|
|
|
|
150
|
$self->_startTag($indent, 'note'); |
|
106
|
82
|
|
|
|
|
143
|
$self->_characters($p); |
|
107
|
82
|
|
|
|
|
117
|
$self->_endTag('note')->_nl; |
|
108
|
|
|
|
|
|
|
} |
|
109
|
|
|
|
|
|
|
} |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub add_tu { |
|
113
|
21
|
|
|
21
|
1
|
39
|
my $self = shift; |
|
114
|
21
|
|
|
|
|
83
|
my %tuv = @_; |
|
115
|
21
|
|
|
|
|
28
|
my %prop = (); |
|
116
|
21
|
|
|
|
|
27
|
my @note = (); |
|
117
|
21
|
|
|
|
|
25
|
my %opt; |
|
118
|
|
|
|
|
|
|
|
|
119
|
21
|
|
|
|
|
22
|
my $verbatim = 0; |
|
120
|
21
|
|
|
|
|
22
|
my $cdata = 0; |
|
121
|
|
|
|
|
|
|
|
|
122
|
21
|
50
|
|
|
|
44
|
if (exists($tuv{-raw})) { |
|
123
|
|
|
|
|
|
|
# value already includes tags, hopefully, at least! |
|
124
|
|
|
|
|
|
|
# so we will not mess with it. |
|
125
|
0
|
|
|
|
|
0
|
$self->_write($tuv{-raw}); |
|
126
|
0
|
|
|
|
|
0
|
return; |
|
127
|
|
|
|
|
|
|
} |
|
128
|
|
|
|
|
|
|
|
|
129
|
21
|
|
|
|
|
45
|
for my $key (qw'id datatype segtype srclang creationid creationdate changedate changeid') { |
|
130
|
168
|
100
|
|
|
|
242
|
if (exists($tuv{$key})) { |
|
131
|
41
|
|
|
|
|
76
|
$opt{$key} = $tuv{$key}; |
|
132
|
41
|
|
|
|
|
70
|
delete $tuv{$key}; |
|
133
|
|
|
|
|
|
|
} |
|
134
|
|
|
|
|
|
|
} |
|
135
|
21
|
50
|
|
|
|
40
|
if (defined($tuv{srcencoding})) { |
|
136
|
0
|
|
|
|
|
0
|
$opt{'o-encoding'} = $tuv{srcencoding}; |
|
137
|
0
|
|
|
|
|
0
|
delete $tuv{srcencoding}; |
|
138
|
|
|
|
|
|
|
} |
|
139
|
21
|
50
|
|
|
|
41
|
$verbatim++ if defined $tuv{-verbatim}; |
|
140
|
21
|
100
|
|
|
|
42
|
delete $tuv{-verbatim} if exists $tuv{-verbatim}; |
|
141
|
|
|
|
|
|
|
|
|
142
|
21
|
50
|
|
|
|
43
|
if (defined($tuv{"-prop"})) { |
|
143
|
21
|
|
|
|
|
30
|
%prop = %{$tuv{"-prop"}}; |
|
|
21
|
|
|
|
|
52
|
|
|
144
|
21
|
|
|
|
|
35
|
delete $tuv{"-prop"}; |
|
145
|
|
|
|
|
|
|
} |
|
146
|
21
|
50
|
|
|
|
36
|
if (defined($tuv{"-note"})) { |
|
147
|
21
|
|
|
|
|
24
|
@note = @{$tuv{"-note"}}; |
|
|
21
|
|
|
|
|
47
|
|
|
148
|
21
|
|
|
|
|
30
|
delete $tuv{"-note"}; |
|
149
|
|
|
|
|
|
|
} |
|
150
|
21
|
50
|
|
|
|
38
|
if (defined($tuv{"-n"})) { |
|
151
|
0
|
|
|
|
|
0
|
$opt{id}=$tuv{"-n"}; |
|
152
|
0
|
|
|
|
|
0
|
delete $tuv{"-n"}; |
|
153
|
|
|
|
|
|
|
} |
|
154
|
|
|
|
|
|
|
|
|
155
|
21
|
|
|
|
|
100
|
$self->_startTag(0,'tu', %opt)->_nl; |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
### write the prop s problemas 23 |
|
158
|
21
|
|
|
|
|
47
|
$self->_write_props(3, \%prop); |
|
159
|
21
|
|
|
|
|
51
|
$self->_write_notes(3, \@note); |
|
160
|
|
|
|
|
|
|
|
|
161
|
21
|
|
|
|
|
65
|
for my $lang (sort keys %tuv) { |
|
162
|
42
|
|
|
|
|
46
|
my $cdata = 0; |
|
163
|
42
|
|
|
|
|
77
|
$self->_startTag(1, 'tuv', 'xml:lang' => $lang); |
|
164
|
42
|
100
|
|
|
|
144
|
if (ref($tuv{$lang}) eq "HASH") { |
|
165
|
41
|
50
|
|
|
|
91
|
$cdata++ if defined($tuv{$lang}{-iscdata}); |
|
166
|
41
|
50
|
|
|
|
83
|
delete($tuv{$lang}{-iscdata}) if exists($tuv{$lang}{-iscdata}); |
|
167
|
|
|
|
|
|
|
|
|
168
|
41
|
100
|
|
|
|
75
|
$self->_write_props(2, $tuv{$lang}{-prop}) if exists $tuv{$lang}{-prop}; |
|
169
|
41
|
100
|
|
|
|
78
|
$self->_write_notes(2, $tuv{$lang}{-note}) if exists $tuv{$lang}{-note}; |
|
170
|
41
|
|
50
|
|
|
106
|
$tuv{$lang} = $tuv{$lang}{-seg} || ""; |
|
171
|
|
|
|
|
|
|
} |
|
172
|
42
|
|
|
|
|
84
|
$self->_startTag(0, 'seg'); |
|
173
|
42
|
50
|
|
|
|
81
|
if ($verbatim) { |
|
|
|
50
|
|
|
|
|
|
|
174
|
0
|
|
|
|
|
0
|
$self->_write($tuv{$lang}); |
|
175
|
|
|
|
|
|
|
} elsif ($cdata) { |
|
176
|
0
|
|
|
|
|
0
|
$self->_write("
|
|
177
|
0
|
|
|
|
|
0
|
$self->_write($tuv{$lang}); |
|
178
|
0
|
|
|
|
|
0
|
$self->_write("]]>"); |
|
179
|
|
|
|
|
|
|
} else { |
|
180
|
42
|
|
|
|
|
70
|
$self->_characters($tuv{$lang}); |
|
181
|
|
|
|
|
|
|
} |
|
182
|
42
|
|
|
|
|
82
|
$self->_endTag('seg'); |
|
183
|
42
|
|
|
|
|
65
|
$self->_endTag('tuv')->_nl; |
|
184
|
|
|
|
|
|
|
} |
|
185
|
21
|
|
|
|
|
40
|
$self->_endTag('tu')->_nl->_nl; |
|
186
|
|
|
|
|
|
|
} |
|
187
|
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
sub end_tmx { |
|
191
|
5
|
|
|
5
|
1
|
24
|
my $self = shift(); |
|
192
|
5
|
|
|
|
|
17
|
$self->_endTag('body')->_nl; |
|
193
|
5
|
|
|
|
|
11
|
$self->_endTag('tmx')->_nl; |
|
194
|
5
|
|
|
|
|
562
|
close($self->{OUTPUT}); |
|
195
|
|
|
|
|
|
|
} |
|
196
|
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
sub _write { |
|
199
|
1247
|
|
|
1247
|
|
1264
|
my $self = shift; |
|
200
|
1247
|
|
|
|
|
1177
|
print {$self->{OUTPUT}} @_; |
|
|
1247
|
|
|
|
|
1929
|
|
|
201
|
1247
|
|
|
|
|
2206
|
return $self; |
|
202
|
|
|
|
|
|
|
} |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
sub _nl { |
|
205
|
279
|
|
|
279
|
|
300
|
my $self = shift; |
|
206
|
279
|
|
|
|
|
342
|
$self->_write("\n"); |
|
207
|
|
|
|
|
|
|
} |
|
208
|
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
sub _startTag { |
|
210
|
259
|
|
|
259
|
|
458
|
my ($self, $indent, $tagName, %attributes) = @_; |
|
211
|
259
|
|
|
|
|
280
|
my $attributes = ""; |
|
212
|
259
|
100
|
|
|
|
518
|
$attributes = " ".join(" ",map {"$_=\"$attributes{$_}\""} sort keys %attributes) if %attributes; |
|
|
186
|
|
|
|
|
536
|
|
|
213
|
259
|
|
|
|
|
405
|
$self->_indent($indent)->_write("<$tagName$attributes>"); |
|
214
|
|
|
|
|
|
|
} |
|
215
|
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
sub _indent { |
|
217
|
264
|
|
|
264
|
|
312
|
my ($self, $indent) = @_; |
|
218
|
264
|
|
|
|
|
309
|
$indent = " " x $indent; |
|
219
|
264
|
|
|
|
|
338
|
$self->_write($indent); |
|
220
|
|
|
|
|
|
|
} |
|
221
|
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
sub _characters { |
|
223
|
181
|
|
|
181
|
|
231
|
my ($self, $text) = @_; |
|
224
|
|
|
|
|
|
|
|
|
225
|
181
|
50
|
|
|
|
298
|
$text = "" unless defined $text; |
|
226
|
181
|
|
|
|
|
346
|
$text =~ s/\n/ /g; |
|
227
|
181
|
|
|
|
|
325
|
$text =~ s/ +/ /g; |
|
228
|
181
|
|
|
|
|
231
|
$text =~ s/&/&/g; |
|
229
|
181
|
|
|
|
|
210
|
$text =~ s/</g; |
|
230
|
181
|
|
|
|
|
203
|
$text =~ s/>/>/g; |
|
231
|
181
|
|
|
|
|
231
|
$text =~ s!<(b|emph)>(.+?)</\1>!<$1>$2$1>!gs; |
|
232
|
|
|
|
|
|
|
|
|
233
|
181
|
|
|
|
|
258
|
$self->_write($text); |
|
234
|
|
|
|
|
|
|
} |
|
235
|
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
sub _endTag { |
|
237
|
259
|
|
|
259
|
|
352
|
my ($self, $tagName) = @_; |
|
238
|
|
|
|
|
|
|
|
|
239
|
259
|
|
|
|
|
430
|
$self->_write("$tagName>"); |
|
240
|
|
|
|
|
|
|
} |
|
241
|
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
1; |
|
243
|
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
__END__ |