summaryrefslogtreecommitdiffstats
path: root/lib/Locale/Po4a/RubyDoc.pm
blob: 6780ca143e8be670e3e1395f4bc51baa9f4af8df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# Locale::Po4a::RubyDoc -- Convert Ruby Document data to PO file, for translation
#
# Copyright © 2016-2017 Francesco Poli <invernomuto@paranoici.org>
#
# This program is free software; you may redistribute it and/or modify it
# under the terms of GPL v2.0 or later (see COPYING).
#
# This work is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This work is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this work; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
#
# Parts of the code (such as many regular expressions) were adapted
# from the source of rdtool, under the terms of the GNU General Public
# License, version 2 or later.
# These parts are originally:
# Copyright © 2004       MoonWolf <moonwolf@moonwolf.com>
# Copyright © 2011-2012  Youhei SASAKI <uwabami@gfd-dennou.org>
#
# The initialize code was adapted from the source of Locale::Po4a::Text,
# under the terms of the GNU General Public License, version 2 or later.
# This code was originally:
# Copyright © 2005-2008  Nicolas FRANÇOIS <nicolas.francois@centraliens.net>
#
############################################################################
#
# This module converts Ruby Document (RD) format to PO files, so that Ruby
# Document formatted texts may be translated. See gettext documentation
# for more details about PO files.
#
############################################################################

package Locale::Po4a::RubyDoc;

use Locale::Po4a::TransTractor qw(process new);
use Locale::Po4a::Common;

use 5.16.0;
use strict;
use warnings;

require Exporter;

use vars qw(@ISA);
@ISA = qw(Locale::Po4a::TransTractor);

######################
#  Global variables  #
######################

my $insiderubydoc = 0;

#############
#  Methods  #
#############

sub initialize {
    my $self    = shift;
    my %options = @_;

    $self->{options}{'debug'}   = 1;
    $self->{options}{'verbose'} = 1;
    $self->{options}{'puredoc'} = 0;

    foreach my $opt ( keys %options ) {
        die wrap_mod( "po4a::rubydoc", dgettext( "po4a", "Unknown option: %s" ), $opt )
          unless exists $self->{options}{$opt};
        $self->{options}{$opt} = $options{$opt};
    }

    if ( defined $options{'puredoc'} ) {

        # initially assume to be already inside the Ruby Document
        $insiderubydoc = 1;
    } else {

        # initially assume to be outside the Ruby Document
        $insiderubydoc = 0;
    }
}

sub docheader {
    return <<EOT;
#
#       *****************************************************
#       *           GENERATED FILE, DO NOT EDIT             *
#       * THIS IS NO SOURCE FILE, BUT RESULT OF COMPILATION *
#       *****************************************************
#
# This file was generated by po4a-translate(1). Do not store it (in VCS,
# for example), but store the PO file used as source file by po4a-translate.
#
# In fact, consider this as a binary, and the PO file as a regular source file:
# If the PO gets lost, keeping this translation up-to-date will be harder.
#
EOT
}

sub parse {
    my $self = shift;

    # start with baseline and firstindent corresponding to no indentation
    my $baseline    = 0;
    my $firstindent = 0;

    # start in non-verbatim mode
    my $verbmode = 0;

    # we have not yet seen any Term, hence we are not yet waiting for a
    # Description
    my $waitfordesc = 0;
    my $methodterm  = "";

    # flag to remember that we have reached the end of the document
    my $eof = 0;

  PARAGRAPH: while () {

        # start accumulating a new paragraph and corresponding variables
        my ( $para, $pref, $ptype, $pwrap, $symbol, $tail ) = ( "", "", "", 1, "", "" );

      LINE: while () {

            # fetch next line and its reference
            my ( $line, $lref ) = $self->shiftline();

            unless ( defined($line) ) {

                # we reached the end of the document
                $eof = 1;
                last LINE;
            }

            if ( $line =~ /^=begin\s*(\bRD\b.*)?\s*$/ ) {

                # we are entering a Ruby Document part
                $insiderubydoc = 1;
                $baseline      = 0;
                $verbmode      = 0;
                $waitfordesc   = 0;
                $self->pushline($line);
                next PARAGRAPH;
            }

            if ( $line =~ /^=end/ ) {

                # we are exiting a Ruby Document part
                $insiderubydoc = 0;
                $baseline      = 0;
                $verbmode      = 0;
                $waitfordesc   = 0;
                $tail          = $line;
                last LINE;
            }

            # do nothing while outside the Ruby Document
            next PARAGRAPH unless ($insiderubydoc);

            # we encountered a Comment: ignore it entirely
            next LINE if ( $line =~ /^#/ );

            if (   $line =~ /^(={1,4})(?!=)\s*(?=\S)(.*)/
                or $line =~ /^(\+{1,2})(?!\+)\s*(?=\S)(.*)/ )
            {
                # we encountered a Headline: this is a paragraph on its own
                if ( length($para) ) {

                    # we already have some paragraph to be processed:
                    # reput the current line in input and end paragraph
                    $self->unshiftline( $line, $lref );
                    last LINE;
                } else {

                    # we are at the beginning of a paragraph, but a Headline
                    # is a single-line paragraph: define the variables
                    # and end paragraph
                    $symbol      = "$1 ";
                    $para        = $2;
                    $pref        = $lref;
                    $ptype       = "Headline $1";
                    $baseline    = 0;
                    $verbmode    = 0;
                    $waitfordesc = 0;
                    last LINE;
                }
            }

            if ( $line =~ /^<<<\s*(\S+)/ ) {

                # we encountered an Include line: end paragraph
                $tail = $line;
                last LINE;
            }

            # compute indentation
            $line =~ /^(\s*)/;
            my $indent = length($1);

            if ($verbmode) {

                # use verbatim mode rules
                # -----------------------

                if ( $indent >= $firstindent ) {

                    # indentation matches first line or is deeper:
                    # the Verbatim goes on
                    $para .= $line;
                    next LINE;
                } else {

                    # indentation is shallower than first line:
                    # reput the current line in input, exit verbatim mode
                    # and end paragraph
                    $self->unshiftline( $line, $lref );
                    $verbmode    = 0;
                    $waitfordesc = 0;
                    last LINE;
                }
            } else {

                # use non-verbatim mode rules
                # ---------------------------

                if ( $line =~ /^\s*$/ ) {

                    # we encountered a WHITELINE: end paragraph
                    $tail = $line;
                    last LINE;
                }

                if ( $line =~ /^(\s*)\*(\s*)(.*)/ ) {

                    # we encountered the first line of a ItemListItem
                    if ( length($para) ) {

                        # we already have some paragraph to be processed:
                        # reput the current line in input and end paragraph
                        $self->unshiftline( $line, $lref );
                        last LINE;
                    } else {

                        # we are at the beginning of a paragraph:
                        # define the variables
                        $symbol = "$1*$2";
                        $para .= $3;
                        $pref        = $lref;
                        $ptype       = "ItemListItem *";
                        $baseline    = length($symbol);
                        $waitfordesc = 0;
                        next LINE;
                    }
                }

                if ( $line =~ /^(\s*)(\(\d+\))(\s*)(.*)/ ) {

                    # we encountered the first line of an EnumListItem
                    if ( length($para) ) {

                        # we already have some paragraph to be processed:
                        # reput the current line in input and end paragraph
                        $self->unshiftline( $line, $lref );
                        last LINE;
                    } else {

                        # we are at the beginning of a paragraph:
                        # define the variables
                        $symbol = "$1$2$3";
                        $para .= $4;
                        $pref        = $lref;
                        $ptype       = "EnumListItem $2";
                        $baseline    = length($symbol);
                        $waitfordesc = 0;
                        next LINE;
                    }
                }

                if ( $line =~ /^(\s*):(\s*)(.*)/ ) {

                    # we encountered the Term line of a DescListItem
                    if ( length($para) ) {

                        # we already have some paragraph to be processed:
                        # reput the current line in input and end paragraph
                        $self->unshiftline( $line, $lref );
                        last LINE;
                    } else {

                        # we are at the beginning of a paragraph, but the Term
                        # part of a DescListItem is a single-line paragraph:
                        # define the variables and end paragraph
                        $symbol      = "$1:$2";
                        $para        = $3;
                        $pref        = $lref;
                        $ptype       = "DescListItem Term :";
                        $baseline    = length($symbol);
                        $waitfordesc = 1;
                        last LINE;
                    }
                }

                if ( $line =~ /^(\s*)---(?!-|\s*$)(\s*)(.*)/ ) {

                    # we encountered the Term line of a MethodListItem
                    if ( length($para) ) {

                        # we already have some paragraph to be processed:
                        # reput the current line in input and end paragraph
                        $self->unshiftline( $line, $lref );
                        last LINE;
                    } else {

                        # we are at the beginning of a paragraph, but the Term
                        # part of a MethodListItem is a single-line paragraph;
                        # moreover, it's not translatable: end paragraph
                        $baseline    = length("$1---$2");
                        $waitfordesc = 2;
                        $tail        = $line;
                        $methodterm  = "--- $3";
                        last LINE;
                    }
                }

                # we apparently encountered a STRINGLINE
                if ( length($para) ) {

                    # we already have some paragraph to be processed:
                    if ( $indent == $baseline ) {

                        # indentation matches baseline:
                        # append the STRINGLINE to the paragraph
                        $para .= $line;
                    } else {

                        # indentation differs from baseline:
                        # reput the current line in input and end paragraph
                        $self->unshiftline( $line, $lref );
                        last LINE;
                    }
                } else {

                    # we are at the beginning of a paragraph:
                    # define the variables
                    if ($waitfordesc) {

                        # we were waiting for a DescListItem Description:
                        # we have just found it
                        if ( $waitfordesc == 1 ) {
                            $ptype = "DescListItem Description";
                        } else {
                            $ptype = "MethodListItem Description $methodterm";
                        }
                        $baseline    = $indent;
                        $waitfordesc = 0;

                        # reproduce the original indentation
                        $symbol = " " x $indent;
                    } else {
                        if ( $indent > $baseline ) {

                            # indentation is deeper than baseline:
                            # we are entering a Verbatim
                            $verbmode    = 1;
                            $ptype       = "Verbatim";
                            $pwrap       = 0;
                            $firstindent = $indent;
                        } else {

                            # indentation is not deeper than baseline:
                            # this is a TextBlock
                            $ptype    = "TextBlock";
                            $baseline = $indent;

                            # reproduce the original indentation
                            $symbol = " " x $indent;
                        }
                    }
                    $para .= $line;
                    $pref = $lref;
                }
            }

        }

        if ( length($para) ) {

            # set wrap column at 76 - identation, but never less than 26
            my $ni = length($symbol);
            my $wc = 76 - $ni;
            $wc = 26 if ( $wc < 26 );

            # get the translated paragraph
            my $translated = $self->translate(
                $para,
                $pref,
                $ptype,
                'wrap'    => $pwrap,
                'wrapcol' => $wc
            );

            if ($pwrap) {

                # reformat the translated paragraph
                my $is = " " x $ni;
                chomp $translated;
                $translated =~ s/\n/\n$is/g;
                $translated .= "\n";
            }

            # push the paragraph to the translated document
            $self->pushline( $symbol . $translated );
        }

        if ( length($tail) ) {

            # push the non translatable tail to the translated document
            $self->pushline($tail);
        }

        # stop processing, if we have already reached the end of the document
        return if ($eof);
    }
}

##########################
#  Module documentation  #
##########################

1;
__END__

=encoding UTF-8

=head1 NAME

Locale::Po4a::RubyDoc -- Convert Ruby Document data from/to PO files

=head1 DESCRIPTION

The po4a (PO for anything) project goal is to ease translations (and more
interestingly, the maintenance of translations) using gettext tools on
areas where they were not expected like documentation.

Locale::Po4a::RubyDoc is a module to help the translation of documentation in
the Ruby Document (RD) format (a language used to document Ruby) into other
[human] languages.

=head1 STATUS OF THIS MODULE

This module has been successfully tested on simple Ruby Document files
covering a good part of the format syntax.

A known limitation is that it fails to properly recognize the stacked
structure of input Ruby Document: this implies that when, for instance,
an EnumListItem consists of more than one Block, only the first Block
is actually recognized as EnumListItem, while the subsequent ones are
considered just as TextBlocks...

=head1 OPTIONS ACCEPTED BY THIS MODULE

This module supports the following option:

=over

=item B<puredoc>

Handle files entirely made of Ruby Document formatted text (without
any "=begin" line).

By default, this module only handles Ruby Document formatted text
between "=begin" and "=end" lines (hence ignoring, among other things,
everything that precedes the first "=begin" line).

=back

=head1 SEE ALSO

L<Locale::Po4a::TransTractor(3pm)>

=head1 AUTHORS

Francesco Poli <invernomuto@paranoici.org>

=head1 COPYRIGHT AND LICENSE

 Copyright © 2016-2017 Francesco Poli <invernomuto@paranoici.org>

This work is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This work is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this work; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


Parts of the code (such as many regular expressions) were adapted
from the source of rdtool, under the terms of the GNU General Public
License, version 2 or later.
These parts are originally:

 Copyright © 2004      MoonWolf <moonwolf@moonwolf.com>
 Copyright © 2011-2012 Youhei SASAKI <uwabami@gfd-dennou.org>

The initialize code was adapted from the source of Locale::Po4a::Text,
under the terms of the GNU General Public License, version 2 or later.
This code was originally:

 Copyright © 2005-2008 Nicolas FRANÇOIS <nicolas.francois@centraliens.net>

=cut