line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::Translate::Format; |
2
|
|
|
|
|
|
|
BEGIN { |
3
|
4
|
|
|
4
|
|
87497
|
$Text::Translate::Format::VERSION = '0.1.0_01'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
# ABSTRACT: basic format conversions for Text::Translate |
6
|
|
|
|
|
|
|
|
7
|
4
|
|
|
4
|
|
34
|
use strict; |
|
4
|
|
|
|
|
11
|
|
|
4
|
|
|
|
|
126
|
|
8
|
4
|
|
|
4
|
|
17
|
use warnings; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
105
|
|
9
|
4
|
|
|
4
|
|
3241
|
use English qw( -no_match_vars ); |
|
4
|
|
|
|
|
18039
|
|
|
4
|
|
|
|
|
22
|
|
10
|
4
|
|
|
4
|
|
1766
|
use Carp; |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
3341
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub init { |
13
|
2
|
|
|
2
|
1
|
6
|
my $self = shift; |
14
|
2
|
50
|
|
|
|
11
|
my %config = ref $_[0] ? %{$_[0]} : @_; |
|
0
|
|
|
|
|
0
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# clear previous stuff |
17
|
2
|
|
|
|
|
14
|
%$self = (); |
18
|
|
|
|
|
|
|
|
19
|
2
|
50
|
|
|
|
8
|
if (exists $config{text}) { |
20
|
0
|
0
|
|
|
|
0
|
croak "no filename if text is provided" |
21
|
|
|
|
|
|
|
if exists $config{filename}; |
22
|
0
|
0
|
|
|
|
0
|
croak "no paragraphs if text is provided" |
23
|
|
|
|
|
|
|
if exists $config{paragraphs}; |
24
|
0
|
|
|
|
|
0
|
$self->text($config{text}); |
25
|
|
|
|
|
|
|
} |
26
|
2
|
50
|
|
|
|
8
|
if (exists $config{filename}) { |
27
|
0
|
0
|
|
|
|
0
|
croak "no paragraphs if filename is provided" |
28
|
|
|
|
|
|
|
if exists $config{paragraphs}; |
29
|
0
|
|
|
|
|
0
|
$self->text_from_file($config{filename}); |
30
|
|
|
|
|
|
|
} |
31
|
2
|
50
|
|
|
|
8
|
if (exists $config{paragraphs}) { |
32
|
0
|
|
|
|
|
0
|
$self->paragraphs($config{paragraphs}); |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
2
|
|
|
|
|
5
|
return $self; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub new { |
39
|
2
|
|
|
2
|
1
|
5
|
my $package = shift; |
40
|
|
|
|
|
|
|
|
41
|
2
|
50
|
|
|
|
8
|
croak "don't call new() on $package, call create() instead" |
42
|
|
|
|
|
|
|
if $package eq __PACKAGE__; |
43
|
|
|
|
|
|
|
|
44
|
2
|
|
|
|
|
7
|
my $self = bless {}, $package; |
45
|
2
|
|
|
|
|
13
|
$self->init(@_); |
46
|
2
|
|
|
|
|
3
|
return $self; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub create { |
50
|
3
|
|
|
3
|
1
|
45
|
my $package = shift; |
51
|
3
|
50
|
|
|
|
13
|
croak "don't call create() on $package, call new() instead or " |
52
|
|
|
|
|
|
|
. "create() on " . __PACKAGE__ |
53
|
|
|
|
|
|
|
if $package ne __PACKAGE__; |
54
|
3
|
|
|
|
|
8
|
my $format = shift; |
55
|
3
|
50
|
|
|
|
26
|
croak "invalid format '$format'" |
56
|
|
|
|
|
|
|
unless $format =~ m{\A \w[\w\d]* \z}mxs; |
57
|
3
|
|
|
|
|
11
|
my $class = __PACKAGE__ . '::' . $format; |
58
|
3
|
100
|
|
3
|
|
278
|
eval "use $class; 1;" |
|
3
|
|
|
|
|
2036
|
|
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
40
|
|
59
|
|
|
|
|
|
|
or croak "unknown format '$format'"; |
60
|
2
|
|
|
|
|
17
|
my $self = $class->new(@_); |
61
|
2
|
|
|
|
|
8
|
return $self; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
#----------- ACCESSORS ------------------------------------------------ |
65
|
|
|
|
|
|
|
sub paragraphs { |
66
|
6
|
|
|
6
|
1
|
22
|
my $self = shift; |
67
|
6
|
100
|
|
|
|
18
|
if (@_) { |
68
|
2
|
50
|
|
|
|
10
|
$self->{paragraphs} = ref $_[0] ? [ @{$_[0]} ] : @_; |
|
2
|
|
|
|
|
8
|
|
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
$self->text_to_paragraphs() |
71
|
6
|
100
|
66
|
|
|
33
|
if (! exists $self->{paragraphs}) && (exists $self->{text}); |
72
|
6
|
100
|
|
|
|
16
|
return @{$self->{paragraphs}} if wantarray(); |
|
2
|
|
|
|
|
13
|
|
73
|
4
|
|
|
|
|
6
|
return [@{$self->{paragraphs}}]; |
|
4
|
|
|
|
|
17
|
|
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub text { |
77
|
8
|
|
|
8
|
1
|
1866
|
my $self = shift; |
78
|
8
|
100
|
|
|
|
25
|
$self->{text} = shift if @_; |
79
|
8
|
50
|
33
|
|
|
27
|
$self->paragraphs_to_text() |
80
|
|
|
|
|
|
|
if (! exists $self->{text}) && (exists $self->{paragraphs}); |
81
|
8
|
|
|
|
|
72
|
return $self->{text}; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
#----------- SETTERS -------------------------------------------------- |
86
|
|
|
|
|
|
|
sub text_from_file { |
87
|
0
|
|
|
0
|
1
|
0
|
my ($self, $filename) = @_; |
88
|
0
|
|
|
|
|
0
|
my $text; |
89
|
0
|
|
|
|
|
0
|
local $/; |
90
|
0
|
0
|
|
|
|
0
|
if (ref $filename) { |
91
|
0
|
|
|
|
|
0
|
$text = <$filename>; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
else { |
94
|
0
|
0
|
|
|
|
0
|
open my $fh, '<', $filename or croak "open('$filename'): $OS_ERROR"; |
95
|
0
|
|
|
|
|
0
|
my $text = <$fh>; |
96
|
0
|
|
|
|
|
0
|
close $fh; |
97
|
|
|
|
|
|
|
} |
98
|
0
|
|
|
|
|
0
|
return $self->text($text); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
#----------- TRANSFORMERS --------------------------------------------- |
103
|
0
|
|
|
0
|
1
|
0
|
sub paragraphs_to_text { croak "paragraphs_to_text is not implemented" } |
104
|
0
|
|
|
0
|
1
|
0
|
sub text_to_paragraphs { croak "text_to_paragraphs is not implemented" } |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
1; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=pod |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head1 NAME |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
Text::Translate::Format - basic format conversions for Text::Translate |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 VERSION |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
version 0.1.0_01 |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head1 SYNOPSIS |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
my $obj = Text::Translate::Format->new( |
122
|
|
|
|
|
|
|
Pod => filename => '/opt/perl-5.12.2/lib/5.12.2/pod/perlfunc.pod' |
123
|
|
|
|
|
|
|
); |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
my @paragraphs = $obj->paragraphs(); |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
my @translated = translate(@paragraphs); |
128
|
|
|
|
|
|
|
my $trans_obj = Text::Translate::Format->new( |
129
|
|
|
|
|
|
|
Pod => paragraphs => \@translated |
130
|
|
|
|
|
|
|
); |
131
|
|
|
|
|
|
|
print "Translated POD:\n", $trans_obj->text(); |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head1 DESCRIPTION |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
This module allows transforming a text into paragraphs (suitable for |
136
|
|
|
|
|
|
|
handling through ) and vice-versa. This module |
137
|
|
|
|
|
|
|
is actually a base class for format-specific subclasses, and acts as |
138
|
|
|
|
|
|
|
a factory to generate objects of these subclasses as well. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
The typical life cycle will be the following: |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=over |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=item * |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
load the source text from a file (or from a filehandle, or directly |
147
|
|
|
|
|
|
|
from a scalar variable): |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
my $src = Text::Translate::Format->new( |
150
|
|
|
|
|
|
|
Pod => filename => '/path/to/file.pod' |
151
|
|
|
|
|
|
|
); |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=item * |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
get the paragraphs: |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
my @src_paragraphs = $src->paragraphs(); |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=item * |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
translate the paragraphs: |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
my @dst_paragraphs = translate(@src_paragraphs); |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=item * |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
create an object for the translated document: |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
my $dst = Text::Translate::Format->new( |
170
|
|
|
|
|
|
|
Pod => paragraphs => \@dst_paragraphs |
171
|
|
|
|
|
|
|
); |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=item * |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
get the textual rendition, e.g. for saving it or displaying: |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
my $translated = $dst->text(); |
178
|
|
|
|
|
|
|
print "Translated text:\n", $translated; |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=back |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head1 METHODS |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head2 init |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
$obj->init(%args); |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
Object method. |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
(re)-initialise the object. You can pass one (and only one) of the |
191
|
|
|
|
|
|
|
following parameters in the C<%args>: |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=over |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=item C |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
the text to be converted into paragraphs; |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=item C |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
the name of a file containing the text to convert into paragraphs; |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=item C |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
an array reference to the ordered list of paragraphs. |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=back |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
You can pass a reference to a hash instead of a hash. |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
Returns the input C<$obj>. |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=head2 new |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
Text::Translate::Format::Whatever->new(%args); |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
Class method, to be called on derived classes (croaks when called on |
218
|
|
|
|
|
|
|
L). |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
Accepts the same arguments as L. |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
Returns a reference to the new object. |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=head2 create |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
Text::Translate::Format->create($format => %args) |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
Class method, to be called on the L class (by |
229
|
|
|
|
|
|
|
default, croaks when called in derived classes). |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
This is a factory method to generate an object of the right C<$format>. |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
Apart from the first parameter - that represents the format - all the |
234
|
|
|
|
|
|
|
following parameters are the same as L. |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
Returns a reference to the newly created object. |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=head2 paragraphs |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
# setter |
241
|
|
|
|
|
|
|
$obj->paragraphs(@paragraphs); |
242
|
|
|
|
|
|
|
$obj->paragraphs(\@pars); |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
# getter |
245
|
|
|
|
|
|
|
my @paragraphs = $obj->paragraphs(); |
246
|
|
|
|
|
|
|
my $ref_to_pars = $obj->paragraphs(); |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
Object method, can be used as both a setter and a getter. |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
This method allows setting or getting the list of paragraphs. |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
If there are input parameters, they are set as the paragraphs list. It can |
253
|
|
|
|
|
|
|
be either a straight list, or a reference to an array containing the list. |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
Always returns the current list of paragraphs (in the setter case, returns |
256
|
|
|
|
|
|
|
the newly set list) in list context, a reference to an anonymous array |
257
|
|
|
|
|
|
|
containing the list in scalar context. |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=head2 text |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
# setter |
262
|
|
|
|
|
|
|
$obj->text($text); |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
# getter |
265
|
|
|
|
|
|
|
my $text = $obj->text(); |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
Object method, can be used as both a setter and a getter. |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
This method allows setting or getting a text rendition of the paragraphs. |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
If there is an input parameter, it is considered as the text to be set. |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
Always returns the current text (i.e. the newly set text in case of the |
274
|
|
|
|
|
|
|
setter). |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
=head2 text_from_file |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
# Can pass either a filename or an open filehandle to read from |
279
|
|
|
|
|
|
|
my $text = $obj->text_from_file($filename); |
280
|
|
|
|
|
|
|
my $text = $obj->text_from_file($FILEHANDLE); |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
Object method. |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
This method allows setting the text rendition of the paragraphs, reading |
285
|
|
|
|
|
|
|
it from either a file (provided through its filename) or from a filehandle. |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
Accepts either a filehandle or a filename. |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
Returns the text read from the file and set as L. |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
=head2 paragraphs_to_text |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
$obj->paragraphs_to_text(); |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
Object method. |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
This method MUST be overridden in the derived classes; it takes the |
298
|
|
|
|
|
|
|
list of paragraphs (via L) and generate the textual rendition |
299
|
|
|
|
|
|
|
(setting it through L). |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
=head2 text_to_paragraphs |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
$obj->text_to_paragraphs(); |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
Object method. |
306
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
This method MUST be overridden in the derived classes; it takes the |
308
|
|
|
|
|
|
|
textual representation (via L) and generates the list of |
309
|
|
|
|
|
|
|
paragraphs (setting it through L). |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
=head1 AUTHOR |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
Flavio Poletti |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
316
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
Copyright (C) 2010 by Flavio Poletti. |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
This module is free software. You can redistribute it and/or |
320
|
|
|
|
|
|
|
modify it under the terms of the Artistic License 2.0. |
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, |
323
|
|
|
|
|
|
|
but without any warranty; without even the implied warranty of |
324
|
|
|
|
|
|
|
merchantability or fitness for a particular purpose. |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
=cut |
327
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
__END__ |