File: TT2.pm

package info (click to toggle)
libtemplate-plugin-gettext-perl 1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 352 kB
  • sloc: perl: 1,317; makefile: 2
file content (379 lines) | stat: -rw-r--r-- 12,598 bytes parent folder | download
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
#! /bin/false

# Copyright (C) 2016-2018 Guido Flohr <guido.flohr@cantanea.com>,
# all rights reserved.

# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU Library General Public License as published
# by the Free Software Foundation; either version 2, 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
# Library General Public License for more details.

# You should have received a copy of the GNU Library General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
# USA.

package Locale::XGettext::TT2;
$Locale::XGettext::TT2::VERSION = '1.0';
use strict;

use Locale::TextDomain qw(Template-Plugin-Gettext);
use Template;

use Locale::XGettext 0.7;
use base qw(Locale::XGettext);

sub versionInformation {
    return __x('{program} (Template-Plugin-Gettext) {version}
Copyright (C) {years} Cantanea EOOD (http://www.cantanea.com/).
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Guido Flohr (http://www.guido-flohr.net/).
',
    program => $0, years => '2016-2018',
    version => $Locale::XGettext::TT2::VERSION);
}

sub fileInformation {
    return __(<<EOF);
The input files should be templates for the Template::Toolkit
(http://www.template-toolkit.org/).  The strings are usually marked and
made translatable with the help of "Template::Plugin::Gettext".  Try the
command "perldoc Template::Plugin::Gettext" for more information.
EOF
}

sub canExtractAll {
    shift;
}

sub canKeywords {
    shift;
}

sub languageSpecificOptions {
    return [
        [
            'plugin|plug-in:s',
            'plug_in',
            '    --plug-in=PLUG-IN, --plugin=PLUG-IN',
            __"the plug-in name (defaults to 'Gettext'), can be an empty string",
        ]
    ];
}

sub defaultKeywords {
    return [
               'gettext:1',
               'ngettext:1,2',
               'pgettext:1c,2',
               'gettextp:1,2c',
               'npgettext:1c,2,3',
               'ngettextp:1,2,3c',
               'xgettext:1',
               'nxgettext:1,2',
               'pxgettext:1c,2',
               'xgettextp:1,2c',
               'npxgettext:1c,2,3',
               'nxgettextp:1,2,3c',
       ];
}

sub defaultFlags {
    return [
               "xgettext:1:perl-brace-format",
               "nxgettext:1:perl-brace-format",
               "nxgettext:2:perl-brace-format",
               "pxgettext:2:perl-brace-format",
               "xgettextp:1:perl-brace-format",
               "npxgettext:2:perl-brace-format",
               "npxgettext:3:perl-brace-format",
               "nxgettextp:1:perl-brace-format",
               "nxgettextp:2:perl-brace-format",
    ];
}

sub readFile {
    my ($self, $filename) = @_;

    my %options = (
        ABSOLUTE => 1,
        # Needed for reading from POTFILES
        RELATIVE => 1
    );

    my $parser = Locale::XGettext::TT2::Parser->new(\%options);

    my $tt = Template->new({
        %options,
        PARSER => $parser,
    });

    my $sink;
    $parser->{__xgettext} = $self;
    $parser->{__xgettext_filename} = $filename;

    $tt->process($filename, {}, \$sink) or die $tt->error;

    return $self;
}

package Locale::XGettext::TT2::Parser;
$Locale::XGettext::TT2::Parser::VERSION = '1.0';
use strict;

use Locale::TextDomain qw(Template-Plugin-Gettext);

use base qw(Template::Parser);

sub split_text {
    my ($self, $text) = @_;

    my $chunks = $self->SUPER::split_text($text) or return;

    my $keywords = $self->{__xgettext}->keywords;
    my $plug_in = $self->{__xgettext}->option('plug_in');
    $plug_in = 'Gettext' if !defined $plug_in;

    my $ident;
    my $lplug_in = length $plug_in;
    while (my $chunk = shift @$chunks) {
        if (!ref $chunk) {
            shift @$chunks;
            next;
        }

        my ($text, $lineno, $tokens) = @$chunk;

        next if !ref $tokens;

        if ($lplug_in) {
            if ('USE' eq $tokens->[0] && 'IDENT' eq $tokens->[2]) {
                if ($plug_in eq $tokens->[3]
                    && (4 == @$tokens
                        || '(' eq $tokens->[4])) {
                    $ident = $plug_in;
                } elsif ('ASSIGN' eq $tokens->[4] && 'IDENT' eq $tokens->[6]
                        && $plug_in eq $tokens->[7]) {
                    $ident = $tokens->[3];
                }
                next;
            }

            next if !defined $ident;
        } else {
            $ident = '';
        }

        for (my $i = 0; $i < @$tokens; $i += 2) {
            # FIXME! It would be better to copy $tokens into an array
            # @tokens because we modify the array reference $tokens.
            # That implies that we iterate over tokens that do ot exist
            # and that is an unnecessary risk.
            if ($lplug_in
                && 'IDENT' eq $tokens->[$i] && $ident eq $tokens->[$i + 1]
                && 'DOT' eq $tokens->[$i + 2] && 'IDENT' eq $tokens->[$i + 4]
                && exists $keywords->{$tokens->[$i + 5]}) {
                my $keyword = $keywords->{$tokens->[$i + 5]};
                $self->__extractEntry($text, $lineno, $keyword,
                                    @$tokens[$i + 6 .. $#$tokens]);
            } elsif ('FILTER' eq $tokens->[$i]
                    && 'IDENT' eq $tokens->[$i + 2]
                    && exists $keywords->{$tokens->[$i + 3]}) {
                my $keyword = $keywords->{$tokens->[$i + 3]};
                # Inject the block contents as the first argument.
                if ($i) {
                    my $first_arg;
                    if ($tokens->[$i - 2] eq 'LITERAL') {
                        $first_arg = $tokens->[$i - 1];
                    } else {
                        next;
                    }
                    # May have been called without parentheses, see
                    # https://github.com/gflohr/Template-Plugin-Gettext/issues/4
                    if (!defined $tokens->[4 + $i]) {
                        $tokens->[4 + $i] = $tokens->[5 + $i] = '(';
                        $tokens->[6 + $i] = $tokens->[7 + $i] = ')';
                        splice @$tokens, 6 + $i, 0, LITERAL => $first_arg;
                    # Or without parentheses and another filter is immediately
                    # following or the value gets dereferenced with a dot.
                    # The latter is kind of nonsense but we support it
                    # elsewhere as well and it is hard to catch.
                    } elsif ('FILTER' eq $tokens->[4 + $i]
                             || 'DOT' eq $tokens->[4 + $i]) {
                        splice @$tokens, 4 + $i, 0,
                               '(', '(', LITERAL => $first_arg, ')', ')';
                    } else {
                        splice @$tokens, 6 + $i, 0,
                            LITERAL => $first_arg, COMMA => ',';
                    }
                } else {
                    next if !@$chunks;
                    my $first_arg;
                    if (ref $chunks->[0]) {
                        next if $chunks->[0]->[2] ne 'ITEXT';
                        $first_arg = $chunks->[0]->[0];
                    } elsif ('TEXT' eq $chunks->[0]) {
                        $first_arg = $chunks->[1];
                    } else {
                        next;
                    }
                    splice @$tokens, 6, 0,
                        'LITERAL', $first_arg, 'COMMA', ',';
                }
                $self->__extractEntry($text, $lineno, $keyword,
                                    @$tokens[$i + 4 .. $#$tokens]);
            } elsif (!$lplug_in && 'IDENT' eq $tokens->[$i]
                     && exists $keywords->{$tokens->[$i + 1]}) {
                my $keyword = $keywords->{$tokens->[$i + 1]};
                $self->__extractEntry($text, $lineno, $keyword,
                                    @$tokens[$i + 2 .. $#$tokens]);
            }
        }
    }

    # Stop processing here, so that for example includes are ignored.
    return [];
}

sub __extractEntry {
    my ($self, $text, $lineno, $keyword, @tokens) = @_;

    my $args = sub {
        my (@tokens) = @_;

        return if '(' ne $tokens[0];

        splice @tokens, 0, 2;

        my @values;
        while (@tokens) {
            if ('LITERAL' eq $tokens[0]) {
                my $string = substr $tokens[1], 1, -1;
                $string =~ s/\\([\\'])/$1/gs;
                push @values, $string;
                splice @tokens, 0, 2;
            } elsif ('"' eq $tokens[0]) {
                if ('TEXT' eq $tokens[2]
                    && '"' eq $tokens[4]
                    && ('COMMA' eq $tokens[6]
                        || ')' eq $tokens[6])) {
                    push @values, $tokens[3];
                    splice @tokens, 6;
                } else {
                      # String containing interpolated variables.
                    my $msg = __"Illegal variable interpolation at \"\$\"!";
                    push @values, \$msg;
                    while (@tokens) {
                        last if 'COMMA' eq $tokens[0];
                        last if ')' eq $tokens[0];
                        shift @tokens;
                    }
                }
            } elsif ('NUMBER' eq $tokens[0]) {
                push @values, $tokens[1];
                splice @tokens, 0, 2;
            } elsif ('IDENT' eq $tokens[0]) {
                # We store undef as the value because we cannot use it
                # anyway.
                push @values, undef;
                splice @tokens, 0, 2;
            } elsif ('(' eq $tokens[0]) {
                splice @tokens, 0, 2;
                my $nested = 1;
                while (@tokens) {
                    if ('(' eq $tokens[0]) {
                        ++$nested;
                        splice @tokens, 0, 2;
                    } elsif (')' eq $tokens[0]) {
                        --$nested;
                        splice @tokens, 0, 2;
                        if (!$nested) {
                            push @values, undef;
                            last;
                        }
                    } else {
                        splice @tokens, 0, 2;
                    }
                }
            } else {
                return @values;
            }

            return @values if !@tokens;

            my $next = shift @tokens;
            if ('COMMA' eq $next) {
                shift @tokens;
                next;
            } elsif ('ASSIGN' eq $next && '=>' eq $tokens[0]) {
                shift @tokens;
                next;
            }

            return @values;
        }

        return @values;
    };

    my $min_args = $keyword->singular;
    my %forms = (msgid => $keyword->singular);
    if ($keyword->plural) {
        $min_args = $keyword->plural if $keyword->plural > $min_args;
        $forms{msgid_plural} = $keyword->plural;
    }

    if ($keyword->context) {
        $min_args = $keyword->context if $keyword->context > $min_args;
        $forms{msgctxt} = $keyword->context;
    }

    my @args = $args->(@tokens);

    # Do we have enough arguments?
    return if $min_args > @args;

    my $entry = {
        keyword => $keyword->{function}
    };
    foreach my $prop (keys %forms) {
        my $argno = $forms{$prop} - 1;

        # We are only interested in literal values.  Whatever is
        # undefined is not parsable or not valid.
        return if !defined $args[$argno];
        if (ref $args[$argno]) {
            my $filename = $self->{__xgettext_filename};
            die "$filename:$lineno: ${$args[$argno]}\n" if ref $args[$argno];
        }
        $entry->{$prop} = $args[$argno];
    }

    my $reference = $self->{__xgettext_filename} . ':' . $lineno;
    $reference =~ s/-[1-9][0-9]*$//;
    $entry->{reference} = $reference;

    if ($text =~ /^#/) {
        my $comment = '';
        my @lines = split /\n/, $text;
        foreach my $line (@lines) {
            last if $line !~ s/^[ \t\r\f\013]*#[ \t\r\f\013]?//;

            $comment .= $line . "\n";
        }
        $entry->{automatic} = $comment;
    }

    $self->{__xgettext}->addEntry($entry);

    return $self;
}

1;