Chameleon

Chameleon Svn Source Tree

Root/branches/ErmaC/Trunk/package/bin/po4a/lib/Locale/Po4a/Pod.pm

1# Locale::Po4a::Pod -- Convert POD data to PO file, for translation.
2#
3# This program is free software; you may redistribute it and/or modify it
4# under the terms of GPL (see COPYING file).
5#
6# This module converts POD to PO file, so that it becomes possible to
7# translate POD formatted documentation. See gettext documentation for
8# more info about PO files.
9
10############################################################################
11# Modules and declarations
12############################################################################
13
14use Pod::Parser;
15use Locale::Po4a::TransTractor qw(process new get_out_charset);
16
17package Locale::Po4a::Pod;
18
19use 5.006;
20use strict;
21use warnings;
22
23require Exporter;
24
25use vars qw(@ISA);
26@ISA = qw(Locale::Po4a::TransTractor Pod::Parser);
27
28sub initialize {}
29
30sub translate {
31 my ($self,$str,$ref,$type) = @_;
32 my (%options)=@_;
33
34 $str = $self->pre_trans($str,$ref,$type);
35 $str = $self->SUPER::translate($str, $ref, $type, %options);
36 $str = $self->post_trans($str,$ref,$type);
37
38 return $str;
39}
40
41sub pre_trans {
42 my ($self,$str,$ref,$type)=@_;
43
44 return $str;
45}
46
47sub post_trans {
48 my ($self,$str,$ref,$type)=@_;
49
50 # Change ascii non-breaking space to POD one
51 my $nbs_out = "\xA0";
52 my $enc_length = Encode::from_to($nbs_out, "latin1",
53 $self->get_out_charset);
54 if (defined $enc_length) {
55 while ($str =~ m/(^|.*\s)(\S+?)\Q$nbs_out\E(\S+?)(\s.*$|$)/s) {
56 my ($begin, $m1, $m2, $end) = ($1, $2, $3, $4);
57 $str = (defined $begin)?$begin:"";
58 # Remove the non-breaking spaces in the string that will be
59 # between S<...>
60 $m2 =~ s/\Q$nbs_out\E/ /g;
61 $str .= "S<$m1 $m2>";
62 $str .= (defined $end)?$end:"";
63 }
64 }
65
66 return $str;
67}
68
69
70sub command {
71 my ($self, $command, $paragraph, $line_num) = @_;
72# print STDOUT "cmd: '$command' '$paragraph' at $line_num\n";
73 if ($command eq 'back'
74|| $command eq 'cut'
75|| $command eq 'pod') {
76$self->pushline("=$command\n\n");
77 } elsif ($command eq 'over') {
78$self->pushline("=$command $paragraph".(length($paragraph)?"":"\n\n"));
79 } elsif ($command eq 'encoding') {
80my $charset = $paragraph;
81$charset =~ s/^\s*(.*?)\s*$/$1/s;
82$self->detected_charset($charset)
83# The =encoding line will be added by docheader
84 } else {
85$paragraph=$self->translate($paragraph,
86 $self->input_file().":$line_num",
87 "=$command",
88 "wrap"=>1);
89$self->pushline("=$command $paragraph\n\n");
90 }
91}
92
93sub verbatim {
94 my ($self, $paragraph, $line_num) = @_;
95# print "verb: '$paragraph' at $line_num\n";
96
97 if ($paragraph eq "\n") {
98$self->pushline("$paragraph\n");
99return;
100 }
101 $paragraph=$self->translate($paragraph,
102$self->input_file().":$line_num",
103"verbatim");
104 $paragraph =~ s/\n$//m;
105 $self->pushline("$paragraph\n");
106}
107
108sub textblock {
109 my ($self, $paragraph, $line_num) = @_;
110# print "text: '$paragraph' at $line_num\n";
111
112 # Fix a pretty damned bug.
113 # Podlators don't wrap explicitelly the text, and groff won't seem to
114 # wrap any line begining with a space. So, we have to consider as
115 # verbatim not only the paragraphs whose first line is indented, but
116 # the paragraph containing an indented line.
117 # That way, we'll declare more paragraphs as verbatim than needed, but
118 # that's harmless (only less confortable for translators).
119
120 if ($paragraph eq "\n") {
121$self->pushline("$paragraph\n");
122return;
123 }
124 if ($paragraph =~ m/^[ \t]/m) {
125$self->verbatim($paragraph, $line_num) ;
126return;
127 }
128
129 $paragraph=$self->translate($paragraph,
130$self->input_file().":$line_num",
131'textblock',
132"wrap"=>1);
133 $paragraph=~ s/ +\n/\n/gm;
134 $self->pushline("$paragraph\n\n");
135}
136
137sub end_pod {}
138
139sub read {
140 my ($self,$filename)=@_;
141
142 push @{$self->{DOCPOD}{infile}}, $filename;
143 $self->Locale::Po4a::TransTractor::read($filename);
144}
145
146sub parse {
147 my $self=shift;
148 map {$self->parse_from_file($_)} @{$self->{DOCPOD}{infile}};
149}
150
151sub docheader {
152 my $self=shift;
153 my $encoding = $self->get_out_charset();
154 if ( (defined $encoding)
155 and (length $encoding)
156 and ($encoding ne "ascii")) {
157 $encoding = "\n=encoding $encoding\n";
158 } else {
159 $encoding = "";
160 }
161
162 return <<EOT;
163
164 *****************************************************
165 * GENERATED FILE, DO NOT EDIT *
166 * THIS IS NO SOURCE FILE, BUT RESULT OF COMPILATION *
167 *****************************************************
168
169This file was generated by po4a(7). Do not store it (in VCS, for example),
170but store the PO file used as source file by po4a-translate.
171
172In fact, consider this as a binary, and the PO file as a regular .c file:
173If the PO get lost, keeping this translation up-to-date will be harder.
174$encoding
175EOT
176}
1771;
178
179##############################################################################
180# Module return value and documentation
181##############################################################################
182
1831;
184__END__
185
186=encoding UTF-8
187
188=head1 NAME
189
190Locale::Po4a::Pod - convert POD data from/to PO files
191
192=head1 SYNOPSIS
193
194 use Locale::Po4a::Pod;
195 my $parser = Locale::Po4a::Pod->new (sentence => 0, width => 78);
196
197 # Read POD from STDIN and write to STDOUT.
198 $parser->parse_from_filehandle;
199
200 # Read POD from file.pod and write to file.txt.
201 $parser->parse_from_file ('file.pod', 'file.txt');
202
203=head1 DESCRIPTION
204
205Locale::Po4a::Pod is a module to help the translation of documentation in
206the POD format (the preferred language for documenting Perl) into other
207[human] languages.
208
209=head1 STATUS OF THIS MODULE
210
211I think that this module is rock stable, and there is only one known bug
212with F</usr/lib/perl5/Tk/MainWindow.pod> (and some other
213pages, see below) which contains:
214
215 C<" #n">
216
217Lack of luck, in the po4a version, this was split on the space by the
218wrapping. As result, in the original version, the man page contains
219
220 " #n"
221
222and mine contains
223
224 "" #n""
225
226which is logic since CE<lt>foobarE<gt> is rewritten "foobar".
227
228Complete list of pages having this problem on my box (from 564 pages; note
229that it depends on the chosen wrapping column):
230/usr/lib/perl5/Tk/MainWindow.pod
231/usr/share/perl/5.8.0/overload.pod
232/usr/share/perl/5.8.0/pod/perlapi.pod
233/usr/share/perl/5.8.0/pod/perldelta.pod
234/usr/share/perl/5.8.0/pod/perlfaq5.pod
235/usr/share/perl/5.8.0/pod/perlpod.pod
236/usr/share/perl/5.8.0/pod/perlre.pod
237/usr/share/perl/5.8.0/pod/perlretut.pod
238
239
240
241=head1 INTERNALS
242
243As a derived class from Pod::Parser, Locale::Po4a::Pod supports the same
244methods and interfaces. See L<Pod::Parser> for all the details; briefly,
245one creates a new parser with C<< Locale::Po4a::Pod->new() >> and then
246calls either parse_from_filehandle() or parse_from_file().
247
248new() can take options, in the form of key/value pairs, that control the
249behavior of the parser. The recognized options common to all Pod::Parser
250children are:
251
252=over 4
253
254=item B<alt>
255
256If set to a true value, selects an alternate output format that, among other
257things, uses a different heading style and marks B<=item> entries with a
258colon in the left margin. Defaults to false.
259
260=item B<code>
261
262If set to a true value, the non-POD parts of the input file will be included
263in the output. Useful for viewing code documented with POD blocks with the
264POD rendered and the code left intact.
265
266=item B<indent>
267
268The number of spaces to indent regular text, and the default indentation for
269B<=over> blocks. Defaults to 4.
270
271=item B<loose>
272
273If set to a true value, a blank line is printed after a B<=head1> heading.
274If set to false (the default), no blank line is printed after B<=head1>,
275although one is still printed after B<=head2>. This is the default because
276it's the expected formatting for manual pages; if you're formatting
277arbitrary text documents, setting this to true may result in more pleasing
278output.
279
280=item B<quotes>
281
282Sets the quote marks used to surround CE<lt>> text. If the value is a
283single character, it is used as both the left and right quote; if it is two
284characters, the first character is used as the left quote and the second as
285the right quote; and if it is four characters, the first two are used as
286the left quote and the second two as the right quote.
287
288This may also be set to the special value B<none>, in which case no quote
289marks are added around CE<lt>> text.
290
291=item B<sentence>
292
293If set to a true value, Locale::Po4a::Pod will assume that each sentence
294ends in two spaces, and will try to preserve that spacing. If set to
295false, all consecutive whitespace in non-verbatim paragraphs is compressed
296into a single space. Defaults to true.
297
298=item B<width>
299
300The column at which to wrap text on the right-hand side. Defaults to 76.
301
302=back
303
304=head1 SEE ALSO
305
306L<Pod::Parser>,
307L<Locale::Po4a::Man(3pm)>,
308L<Locale::Po4a::TransTractor(3pm)>,
309L<po4a(7)|po4a.7>
310
311=head1 AUTHORS
312
313 Denis Barbier <barbier@linuxfr.org>
314 Martin Quinson (mquinson#debian.org)
315
316=head1 COPYRIGHT AND LICENSE
317
318Copyright 2002 by SPI, inc.
319
320This program is free software; you may redistribute it and/or modify it
321under the terms of GPL (see the COPYING file).
322
323=cut
324

Archive Download this file

Revision: 1847