summaryrefslogtreecommitdiffstats
path: root/lib/Locale/Po4a/Texinfo.pm
blob: ca8b2895bf9e1490753eff1fa803987a9647846a (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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
#!/usr/bin/perl -w

# Copyright © 2004-2007 Nicolas FRANÇOIS <nicolas.francois@centraliens.net>
#
# This file is part of po4a.
#
# This program 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 program 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 po4a; if not, write to the Free Software
# Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
########################################################################

=encoding UTF-8

=head1 NAME

Locale::Po4a::Texinfo - convert Texinfo documents and derivates 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::Texinfo is a module to help the translation of Texinfo documents into
other [human] languages.

This module contains the definitions of common Texinfo commands and environments.

Only the comments starting with 'TRANSLATORS' are added to the PO files to guide the translators.

=head1 STATUS OF THIS MODULE

This module is still beta and not ready for production use.
Please send patches to contribute, not bug reports as we don't know how to deal with them.

=head1 OPTIONS ACCEPTED BY THIS MODULE

These are this module's particular options:

=over 4

=item no-warn

Do not warn about the current state of this module.

=back

=head1 SEE ALSO

L<Locale::Po4a::TeX(3pm)|Locale::Po4a::TeX>,
L<Locale::Po4a::TransTractor(3pm)|Locale::Po4a::TransTractor>,
L<po4a(7)|po4a.7>

=head1 AUTHORS

 Nicolas François <nicolas.francois@centraliens.net>

=head1 COPYRIGHT AND LICENSE

Copyright © 2004-2007 Nicolas FRANÇOIS <nicolas.francois@centraliens.net>.

This program is free software; you may redistribute it and/or modify it
under the terms of GPL v2.0 or later (see the COPYING file).

=cut

package Locale::Po4a::Texinfo;

use 5.16.0;
use strict;
use warnings;

require Exporter;
use vars qw($VERSION @ISA @EXPORT);
$VERSION = $Locale::Po4a::TeX::VERSION;
@ISA     = qw(Locale::Po4a::TeX);
@EXPORT  = qw();

use Locale::Po4a::Common;
use Locale::Po4a::TeX;
use subs qw(
  &parse_definition_file
  &register_generic_command &is_closed &translate_buffer
  &register_verbatim_environment
  &generic_command
  &in_verbatim);
*parse_definition_file         = \&Locale::Po4a::TeX::parse_definition_file;
*register_generic_command      = \&Locale::Po4a::TeX::register_generic_command;
*register_verbatim_environment = \&Locale::Po4a::TeX::register_verbatim_environment;
*generic_command               = \&Locale::Po4a::TeX::generic_command;
*is_closed                     = \&Locale::Po4a::TeX::is_closed;
*in_verbatim                   = \&Locale::Po4a::TeX::in_verbatim;
*translate_buffer              = \&Locale::Po4a::TeX::translate_buffer;
use vars qw($RE_ESCAPE            $ESCAPE
  $RE_VERBATIM
  $RE_COMMENT           $RE_PRE_COMMENT
  $no_wrap_environments $separated_commands
  %commands             %environments
  %command_categories   %separated
  %env_separators       %debug
  %translate_buffer_env
  @exclude_include      @comments);
*RE_ESCAPE            = \$Locale::Po4a::TeX::RE_ESCAPE;
*ESCAPE               = \$Locale::Po4a::TeX::ESCAPE;
*RE_VERBATIM          = \$Locale::Po4a::TeX::RE_VERBATIM;
*RE_COMMENT           = \$Locale::Po4a::TeX::RE_COMMENT;
*RE_PRE_COMMENT       = \$Locale::Po4a::TeX::RE_PRE_COMMENT;
*no_wrap_environments = \$Locale::Po4a::TeX::no_wrap_environments;
*separated_commands   = \$Locale::Po4a::TeX::separated_commands;
*commands             = \%Locale::Po4a::TeX::commands;
*environments         = \%Locale::Po4a::TeX::environments;
*command_categories   = \%Locale::Po4a::TeX::command_categories;
*separated            = \%Locale::Po4a::TeX::separated;
*env_separators       = \%Locale::Po4a::TeX::env_separators;
*debug                = \%Locale::Po4a::TeX::debug;
*translate_buffer_env = \%Locale::Po4a::TeX::translate_buffer_env;
*exclude_include      = \@Locale::Po4a::TeX::exclude_include;
*comments             = \@Locale::Po4a::TeX::comments;

$ESCAPE         = "\@";
$RE_ESCAPE      = "\@";
$RE_VERBATIM    = "\@example";
$RE_COMMENT     = "\\\@(?:c|comment)\\b";
$RE_PRE_COMMENT = "(?<!\@)(?:\@\@)*";

my %break_line = ();

# translate_line_command indicate if the arguments to the command handled
# by line_command() should be translated:
# undefined: arguments are not translated
# 0:         there should be no arguments
# 1:         arguments should be translated
my %translate_line_command = ();

foreach (
    qw/example smallexample tex display smalldisplay verbatim format smallformat
    flushleft flushright lisp smalllisp ignore/
  )
{
    register_verbatim_environment($_);
    $commands{$_}               = \&environment_line_command;
    $translate_line_command{$_} = 0;                            # There should be no arguments
    $break_line{$_}             = 1;
}

my $docheader_pushed = 0;

# The header shall not be written before the Texinfo header (which include
# the \input command that define the texinfo macros)
sub docheader {
    return "";
}

sub push_docheader {
    return if $docheader_pushed;
    my $self = shift;
    $self->pushline(<<END);
\@c ===========================================================================
\@c
\@c This file was generated with po4a. Translate the source file.
\@c
\@c ===========================================================================

END
    $docheader_pushed = 1;
}

sub parse {
    my $self = shift;
    my ( $line, $ref );
    my $paragraph = "";    # Buffer where we put the paragraph while building
    my @env       = ();    # environment stack
    my $t         = "";
    $docheader_pushed = 0;

    print STDERR "The TexInfo module of po4a is not ready for production use, and needs a new maintainer.\n"
      . "Please contact the po4a team if you want to help: send us patches, not bug reports.\n"
      . "(use -o no-warn to remove this message)\n"
      unless $self->{options}{'no-warn'};
  LINE:
    undef $self->{type};
    ( $line, $ref ) = $self->shiftline();

    while ( defined($line) ) {
        chomp($line);
        $self->{ref} = "$ref";

        if ( $line =~ /^\s*@\s*po4a\s*:/ ) {
            parse_definition_line( $self, $line );
            goto LINE;
        }

        my $closed = 1;
        if ( !in_verbatim(@env) ) {
            $closed = is_closed($paragraph);
        }

        #        if (not $closed) {
        #            print "not closed. line: '$line'\n            para: '$paragraph'\n";
        #        }

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

            # An empty line. This indicates the end of the current
            # paragraph.
            $paragraph .= $line . "\n";
            if ( length($paragraph) ) {
                ( $t, @env ) = translate_buffer( $self, $paragraph, undef, @env );
                $self->pushline($t);
                $paragraph = "";
            }
        } elsif ( $line =~ m/^\\input / ) {
            if ( length($paragraph) ) {
                ( $t, @env ) = translate_buffer( $self, $paragraph, undef, @env );
                $self->pushline($t);
                $paragraph = "";
            }
            $self->pushline( $line . "\n" );
            $self->push_docheader();
        } elsif ( $line =~ m/^$RE_COMMENT/ ) {
            if ( $line =~ m/^\@(?:c|comment).*?TRANSLATORS:(.*)$/ ) {
                $self->add_comment($1);
            }
            $self->push_docheader();
            $self->pushline( $line . "\n" );
        } elsif ( $closed
            and ( $line =~ /^@([^ ]*?)(?: +(.*))?$/ )
            and ( defined $commands{$1} )
            and ( $break_line{$1} ) )
        {
            if ( length($paragraph) ) {
                ( $t, @env ) = translate_buffer( $self, $paragraph, undef, @env );
                $self->pushline($t);
                $paragraph = "";
            }
            my $arg  = $2;
            my @args = ();
            if ( defined $arg and length $arg ) {

                # FIXME: keep the spaces ?
                $arg =~ s/\s*$//s;
                @args = ( " ", $arg );
            }
            ( $t, @env ) = &{ $commands{$1} }( $self, $1, "", \@args, \@env, 1 );
            $self->pushline( $t . "\n" );
        } else {

            # continue the same paragraph
            $paragraph .= $line . "\n";
        }

        # Reinit the loop
        ( $line, $ref ) = $self->shiftline();
        undef $self->{type};
    }

    if ( length($paragraph) ) {
        ( $t, @env ) = translate_buffer( $self, $paragraph, undef, @env );
        $self->pushline($t);
        $paragraph = "";
    }
}    # end of parse

sub line_command {
    my $self = shift;
    my ( $command, $variant, $args, $env ) = ( shift, shift, shift, shift );
    my $no_wrap = shift;
    print "line_command($command,$variant,@$args,@$env,$no_wrap)="
      if ( $debug{'commands'} );

    my $translated = $ESCAPE . $command;
    my $line       = $args->[1];
    if ( defined $line and length $line ) {
        if ( defined $translate_line_command{$command}
            and $translate_line_command{$command} )
        {
            # $no_wrap could be forced to 1, but it should already be set
            my ( $t, $e ) = $self->translate_buffer( $line, $no_wrap, @$env, $command );
            $translated .= " " . $t;
        } else {
            $translated .= " " . $line;
        }
    }
    print "($translated,@$env)\n"
      if ( $debug{'commands'} );
    return ( $translated, @$env );
}

sub defindex_line_command {
    my $self = shift;
    my ( $command, $variant, $args, $env ) = ( shift, shift, shift, shift );
    my $no_wrap = shift;
    print "line_command($command,$variant,@$args,@$env,$no_wrap)="
      if ( $debug{'commands'} );
    my $idx = $$args[1] . "index";
    $commands{$idx}               = \&line_command;
    $break_line{$idx}             = 1;
    $translate_line_command{$idx} = 1;

    return line_command( $self, $command, $variant, $args, $env, $no_wrap );
}

sub translate_buffer_menu {
    my ( $self, $buffer, $no_wrap, @env ) = ( shift, shift, shift, @_ );
    print STDERR "translate_buffer_menu($buffer,$no_wrap,@env)="
      if ( $debug{'translate_buffer'} );

    my $translated_buffer = "";
    my $spaces            = "";
    if ( $buffer =~ m/(\s*)$/s ) {
        $spaces = $1;
    }

    while ( $buffer =~ m/^(.*?)((?:\n|^)\* )(.*)$/s ) {
        my $sep = $2;
        $buffer = $3;
        my ( $t, @e ) = $self->translate_buffer_menuentry( $1, $no_wrap, @env, "menuentry" );
        $translated_buffer .= $t . $sep;
    }
    my ( $t, @e ) = $self->translate_buffer_menuentry( $buffer, $no_wrap, @env, "menuentry" );
    $translated_buffer .= $t;

    $translated_buffer .= $spaces;

    print STDERR "($translated_buffer,@env)\n"
      if ( $debug{'translate_buffer'} );
    return ( $translated_buffer, @env );
}
$translate_buffer_env{"menu"}       = \&translate_buffer_menu;
$translate_buffer_env{"detailmenu"} = \&translate_buffer_menu;
$translate_buffer_env{"direntry"}   = \&translate_buffer_menu;

my $menu_width     = 78;
my $menu_sep_width = 30;

sub translate_buffer_menuentry {
    my ( $self, $buffer, $no_wrap, @env ) = ( shift, shift, shift, @_ );
    print STDERR "translate_buffer_menuentry($buffer,$no_wrap,@env)="
      if ( $debug{'translate_buffer'} );

    my $translated_buffer = "";

    if (   $buffer =~ m/^(.*?)(::)(?:\s+(.*))?$/s
        or $buffer =~ m/^(.*?: .*?)(\.)\s+(.*)$/s )
    {
        my ( $name, $sep, $description ) = ( $1, $2, $3 );
        my ( $t, @e ) = $self->translate_buffer( $name, $no_wrap, @env );
        $translated_buffer = $t . $sep . "  ";
        my $l = length($translated_buffer) + 2;
        if ( $l < $menu_sep_width - 1 ) {
            $translated_buffer .= ' ' x ( $menu_sep_width - 1 - $l );
            $l = $menu_sep_width - 1;
        }
        if ($description) {
            ( $t, @e ) = $self->translate_buffer( $description, $no_wrap, @env );
        }

        # Replace newlines with space for proper wrapping
        # See https://github.com/mquinson/po4a/issues/122
        $t =~ s/\n/ /sg;

        # Remove trailing spaces
        $t =~ s/\s*$//;
        $t = Locale::Po4a::Po::wrap( $t, $menu_width - $l - 2 );
        my $spaces = ' ' x ( $l + 2 );
        $t =~ s/\n/\n$spaces/sg;
        $translated_buffer .= $t;
    } else {

        # FIXME: no-wrap if a line start by a space
        my ( $t, @e ) = $self->translate_buffer( $buffer, $no_wrap, @env );
        $translated_buffer = $t;
    }

    print STDERR "($translated_buffer,@env)\n"
      if ( $debug{'translate_buffer'} );
    return ( $translated_buffer, @env );
}

sub translate_buffer_ignore {
    my ( $self, $buffer, $no_wrap, @env ) = ( shift, shift, shift, @_ );
    print STDERR "translate_buffer_ignore($buffer,$no_wrap,@env);\n"
      if ( $debug{'translate_buffer'} );
    return ( $buffer, @env );
}
$translate_buffer_env{"ignore"} = \&translate_buffer_ignore;

foreach (
    qw(appendix section cindex findex kindex opindex pindex tindex vindex subsection
    dircategory subtitle include
    exdent center unnumberedsec
    heading unnumbered unnumberedsubsec
    unnumberedsubsubsec appendixsec appendixsubsec
    appendixsubsubsec majorheading chapheading subheading
    subsubheading shorttitlepage
    subsubsection top item itemx chapter settitle
    title author)
  )
{
    $commands{$_}               = \&line_command;
    $break_line{$_}             = 1;
    $translate_line_command{$_} = 1;
}
foreach (
    qw(c comment clear set setfilename setchapternewpage vskip synindex
    syncodeindex need fonttextsize printindex headings finalout sp
    definfoenclose)
  )
{
    $commands{$_}   = \&line_command;
    $break_line{$_} = 1;
}
foreach (qw(defcodeindex defindex)) {
    $commands{$_}   = \&defindex_line_command;
    $break_line{$_} = 1;
}

# definfoenclose: command definition => translate?
foreach (
    qw(insertcopying page bye summarycontents shortcontents contents
    noindent)
  )
{
    $commands{$_}               = \&line_command;
    $break_line{$_}             = 1;
    $translate_line_command{$_} = 0;
}

foreach (
    qw(defcv deffn
    defivar defmac defmethod defop
    defopt defspec deftp deftypecv
    deftypefn deftypefun
    deftypeivar deftypemethod
    deftypeop deftypevar deftypevr
    defun defvar defvr)
  )
{
    $commands{$_}               = \&environment_line_command;
    $translate_line_command{$_} = 1;
    $break_line{$_}             = 1;
}
foreach (
    qw(defcvx deffnx defivarx defmacx defmethodx defopx defoptx
    defspecx deftpx deftypecvx deftypefnx deftypefunx deftypeivarx
    deftypemethodx deftypeopx deftypevarx deftypevrx defunx
    defvarx defvrx)
  )
{
    $commands{$_}               = \&line_command;
    $translate_line_command{$_} = 1;
    $break_line{$_}             = 1;
}

foreach (
    qw(titlefont w i r b sansserif sc slanted strong t cite email
    footnote indicateurl emph ref xref pxref inforef kbd key
    acronym),

    # The following commands could cause problems since their arguments
    # have a semantic and a translator could decide not to translate code but
    # still translate theses short words if they appear in another context.
    qw(file command dfn dmn option math code samp var)
  )
{
    register_generic_command("-$_,{_}");
}

register_generic_command("*anchor,{_}");
register_generic_command("*refill,");

$translate_line_command{'node'} = 1;
$no_wrap_environments .= " node";
$break_line{'node'} = 1;

# @node     Comments,  Minimum, Conventions, Overview
$commands{'node'} = sub {
    my $self = shift;
    my ( $command, $variant, $args, $env ) = ( shift, shift, shift, shift );
    my $no_wrap = shift;
    print "node($command,$variant,@$args,@$env,$no_wrap)="
      if ( $debug{'commands'} );

    my $translated = $ESCAPE . $command;
    my $line       = $args->[1];
    if ( defined $line and length $line ) {
        my @pointers = split( /, */, $line );
        my @t;
        foreach (@pointers) {
            push @t, $self->translate( $_, $self->{ref}, $command, "wrap" => 0 );
        }
        $translated .= " " . join( ", ", @t );
    }

    print "($translated,@$env)\n"
      if ( $debug{'commands'} );
    return ( $translated, @$env );
};

sub environment_command {
    my $self = shift;
    my ( $command, $variant, $args, $env ) = ( shift, shift, shift, shift );
    my $no_wrap = shift;
    print "environment_command($command,$variant,@$args,@$env,$no_wrap)="
      if ( $debug{'commands'} );
    my ( $t, @e ) = ( "", () );

    ( $t, @e ) = generic_command( $self, $command, $variant, $args, $env, $no_wrap );
    @e = ( @$env, $command );

    print "($t,@e)\n"
      if ( $debug{'commands'} );
    return ( $t, @e );
}

sub environment_line_command {
    my $self = shift;
    my ( $command, $variant, $args, $env ) = ( shift, shift, shift, shift );
    my $no_wrap = shift;
    print "environment_command_line($command,$variant,@$args,@$env,$no_wrap)="
      if ( $debug{'commands'} );
    my ( $t, @e ) = ( "", () );

    ( $t, @e ) = line_command( $self, $command, $variant, $args, $env, $no_wrap );
    @e = ( @$env, $command );

    print "($t,@e)\n"
      if ( $debug{'commands'} );
    return ( $t, @e );
}

## push the environment in the environment stack, and do not translate
## the command
#sub push_environment {
#    my $self = shift;
#    my ($command,$variant,$args,$env) = (shift,shift,shift,shift);
#    print "push_environment($command,$variant,@$args,@$env)="
#        if ($debug{'environments'});
#
#    my ($t,@e) = generic_command($self,$command,$variant,$args,$env);
#
#    print "($t,@e)\n"
#        if ($debug{'environments'});
#    return ($t,@e);
#}
#
foreach (
    qw(detailmenu menu titlepage group copying
    documentdescription cartouche
    direntry
    ifdocbook ifhtml ifinfo ifplaintext iftex ifxml
    ifnotdocbook ifnothtml ifnotinfo ifnotplaintext ifnottex ifnotxml)
  )
{
    $commands{$_}               = \&environment_line_command;
    $translate_line_command{$_} = 0;
    $break_line{$_}             = 1;
}
foreach (qw(enumerate multitable ifclear ifset)) {
    $commands{$_}   = \&environment_line_command;
    $break_line{$_} = 1;
}
foreach (
    qw(quotation smallquotation
    indentedblock smallindentedblock
    raggedright)
  )
{
    $commands{$_}               = \&environment_line_command;
    $translate_line_command{$_} = 1;
    $break_line{$_}             = 1;
}

$env_separators{'format'}     = "(?:(?:^|\n)\\\*|END-INFO-DIR-ENTRY|START-INFO-DIR-ENTRY)";
$env_separators{'multitable'} = "(?:\@item|\@tab)";

my $end_command = $commands{'end'};
register_generic_command("*end,  ");
$commands{'end'}   = $end_command;
$break_line{'end'} = 1;

register_generic_command("*macro,  ");
$commands{'macro'}   = \&environment_command;
$break_line{'macro'} = 1;
register_generic_command("*itemize,  ");
$commands{'itemize'}   = \&environment_command;
$break_line{'itemize'} = 1;
register_generic_command("*table,  ");
$commands{'table'}   = \&environment_command;
$break_line{'table'} = 1;

# TODO: is_closed, use a regexp: \ does not escape the closing brace.
# TBC on LaTeX.
# In Texinfo, it appears with the "code" command. Maybe this command should
# be used as verbatim. (Expressions.texi)

# TODO: @include @ignore

# TBC: node Indices

1;