~ubuntu-branches/ubuntu/karmic/feisty-gdm-themes/karmic

« back to all changes in this revision

Viewing changes to intltool-extract.in

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2007-02-26 13:55:15 UTC
  • Revision ID: james.westby@ubuntu.com-20070226135515-ww49mvqi5mo2742k
Tags: 0.13
* New release.
* Use native packaging.
* debian/control, debian/rules:
  - use python-distutils-extras and python 'build system'.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!@INTLTOOL_PERL@ -w 
2
 
# -*- Mode: perl; indent-tabs-mode: nil; c-basic-offset: 4  -*-
3
 
 
4
 
#
5
 
#  The Intltool Message Extractor
6
 
#
7
 
#  Copyright (C) 2000-2001, 2003 Free Software Foundation.
8
 
#
9
 
#  Intltool is free software; you can redistribute it and/or
10
 
#  modify it under the terms of the GNU General Public License as
11
 
#  published by the Free Software Foundation; either version 2 of the
12
 
#  License, or (at your option) any later version.
13
 
#
14
 
#  Intltool is distributed in the hope that it will be useful,
15
 
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 
#  General Public License for more details.
18
 
#
19
 
#  You should have received a copy of the GNU General Public License
20
 
#  along with this program; if not, write to the Free Software
21
 
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
 
#
23
 
#  As a special exception to the GNU General Public License, if you
24
 
#  distribute this file as part of a program that contains a
25
 
#  configuration script generated by Autoconf, you may include it under
26
 
#  the same distribution terms that you use for the rest of that program.
27
 
#
28
 
#  Authors: Kenneth Christiansen <kenneth@gnu.org>
29
 
#           Darin Adler <darin@bentspoon.com>
30
 
#
31
 
 
32
 
## Release information
33
 
my $PROGRAM      = "intltool-extract";
34
 
my $PACKAGE      = "intltool";
35
 
my $VERSION      = "0.35.4";
36
 
 
37
 
## Loaded modules
38
 
use strict; 
39
 
use File::Basename;
40
 
use Getopt::Long;
41
 
 
42
 
## Scalars used by the option stuff
43
 
my $TYPE_ARG    = "0";
44
 
my $LOCAL_ARG   = "0";
45
 
my $HELP_ARG    = "0";
46
 
my $VERSION_ARG = "0";
47
 
my $UPDATE_ARG  = "0";
48
 
my $QUIET_ARG   = "0";
49
 
my $SRCDIR_ARG  = ".";
50
 
 
51
 
my $FILE;
52
 
my $OUTFILE;
53
 
 
54
 
my $gettext_type = "";
55
 
my $input;
56
 
my %messages = ();
57
 
my %loc = ();
58
 
my %count = ();
59
 
my %comments = ();
60
 
my $strcount = 0;
61
 
 
62
 
my $XMLCOMMENT = "";
63
 
 
64
 
## Use this instead of \w for XML files to handle more possible characters.
65
 
my $w = "[-A-Za-z0-9._:]";
66
 
 
67
 
## Always print first
68
 
$| = 1;
69
 
 
70
 
## Handle options
71
 
GetOptions (
72
 
            "type=s"     => \$TYPE_ARG,
73
 
            "local|l"    => \$LOCAL_ARG,
74
 
            "help|h"     => \$HELP_ARG,
75
 
            "version|v"  => \$VERSION_ARG,
76
 
            "update"     => \$UPDATE_ARG,
77
 
            "quiet|q"    => \$QUIET_ARG,
78
 
            "srcdir=s"   => \$SRCDIR_ARG,
79
 
            ) or &error;
80
 
 
81
 
&split_on_argument;
82
 
 
83
 
 
84
 
## Check for options. 
85
 
## This section will check for the different options.
86
 
 
87
 
sub split_on_argument {
88
 
 
89
 
    if ($VERSION_ARG) {
90
 
        &version;
91
 
 
92
 
    } elsif ($HELP_ARG) {
93
 
        &help;
94
 
        
95
 
    } elsif ($LOCAL_ARG) {
96
 
        &place_local;
97
 
        &extract;
98
 
 
99
 
    } elsif ($UPDATE_ARG) {
100
 
        &place_normal;
101
 
        &extract;
102
 
 
103
 
    } elsif (@ARGV > 0) {
104
 
        &place_normal;
105
 
        &message;
106
 
        &extract;
107
 
 
108
 
    } else {
109
 
        &help;
110
 
 
111
 
    }  
112
 
}    
113
 
 
114
 
sub place_normal {
115
 
    $FILE        = $ARGV[0];
116
 
    $OUTFILE     = "$FILE.h";
117
 
 
118
 
    my $dirname = dirname ($OUTFILE);
119
 
    if (! -d "$dirname" && $dirname ne "") {
120
 
        system ("mkdir -p $dirname");
121
 
    }
122
 
}   
123
 
 
124
 
sub place_local {
125
 
    $FILE        = $ARGV[0];
126
 
    $OUTFILE     = fileparse($FILE, ());
127
 
    if (!-e "tmp/") { 
128
 
        system("mkdir tmp/"); 
129
 
    }
130
 
    $OUTFILE     = "./tmp/$OUTFILE.h"
131
 
}
132
 
 
133
 
sub determine_type {
134
 
   if ($TYPE_ARG =~ /^gettext\/(.*)/) {
135
 
        $gettext_type=$1
136
 
   }
137
 
}
138
 
 
139
 
## Sub for printing release information
140
 
sub version{
141
 
    print <<_EOF_;
142
 
${PROGRAM} (${PACKAGE}) $VERSION
143
 
Copyright (C) 2000, 2003 Free Software Foundation, Inc.
144
 
Written by Kenneth Christiansen, 2000.
145
 
 
146
 
This is free software; see the source for copying conditions.  There is NO
147
 
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
148
 
_EOF_
149
 
    exit;
150
 
}
151
 
 
152
 
## Sub for printing usage information
153
 
sub help {
154
 
    print <<_EOF_;
155
 
Usage: ${PROGRAM} [OPTION]... [FILENAME]
156
 
Generates a header file from an XML source file.
157
 
 
158
 
It grabs all strings between <_translatable_node> and its end tag in
159
 
XML files. Read manpage (man ${PROGRAM}) for more info.
160
 
 
161
 
      --type=TYPE   Specify the file type of FILENAME. Currently supports:
162
 
                    "gettext/glade", "gettext/ini", "gettext/keys"
163
 
                    "gettext/rfc822deb", "gettext/schemas",
164
 
                    "gettext/scheme", "gettext/xml", "gettext/quoted"
165
 
  -l, --local       Writes output into current working directory
166
 
                    (conflicts with --update)
167
 
      --update      Writes output into the same directory the source file 
168
 
                    reside (conflicts with --local)
169
 
      --srcdir      Root of the source tree
170
 
  -v, --version     Output version information and exit
171
 
  -h, --help        Display this help and exit
172
 
  -q, --quiet       Quiet mode
173
 
 
174
 
Report bugs to http://bugzilla.gnome.org/ (product name "$PACKAGE")
175
 
or send email to <xml-i18n-tools\@gnome.org>.
176
 
_EOF_
177
 
    exit;
178
 
}
179
 
 
180
 
## Sub for printing error messages
181
 
sub error{
182
 
    print STDERR "Try `${PROGRAM} --help' for more information.\n";
183
 
    exit;
184
 
}
185
 
 
186
 
sub message {
187
 
    print "Generating C format header file for translation.\n" unless $QUIET_ARG;
188
 
}
189
 
 
190
 
sub extract {
191
 
    &determine_type;
192
 
 
193
 
    &convert;
194
 
 
195
 
    open OUT, ">$OUTFILE";
196
 
    binmode (OUT) if $^O eq 'MSWin32';
197
 
    &msg_write;
198
 
    close OUT;
199
 
 
200
 
    print "Wrote $OUTFILE\n" unless $QUIET_ARG;
201
 
}
202
 
 
203
 
sub convert {
204
 
 
205
 
    ## Reading the file
206
 
    {
207
 
        local (*IN);
208
 
        local $/; #slurp mode
209
 
        open (IN, "<$SRCDIR_ARG/$FILE") || die "can't open $SRCDIR_ARG/$FILE: $!";
210
 
        $input = <IN>;
211
 
    }
212
 
 
213
 
    &type_ini if $gettext_type eq "ini";
214
 
    &type_keys if $gettext_type eq "keys";
215
 
    &type_xml if $gettext_type eq "xml";
216
 
    &type_glade if $gettext_type eq "glade";
217
 
    &type_scheme if $gettext_type eq "scheme";
218
 
    &type_schemas  if $gettext_type eq "schemas";
219
 
    &type_rfc822deb  if $gettext_type eq "rfc822deb";
220
 
    &type_quoted if $gettext_type eq "quoted";
221
 
}
222
 
 
223
 
sub entity_decode_minimal
224
 
{
225
 
    local ($_) = @_;
226
 
 
227
 
    s/&apos;/'/g; # '
228
 
    s/&quot;/"/g; # "
229
 
    s/&amp;/&/g;
230
 
 
231
 
    return $_;
232
 
}
233
 
 
234
 
sub entity_decode
235
 
{
236
 
    local ($_) = @_;
237
 
 
238
 
    s/&apos;/'/g; # '
239
 
    s/&quot;/"/g; # "
240
 
    s/&amp;/&/g;
241
 
    s/&lt;/</g;
242
 
    s/&gt;/>/g;
243
 
 
244
 
    return $_;
245
 
}
246
 
 
247
 
sub escape_char
248
 
{
249
 
    return '\"' if $_ eq '"';
250
 
    return '\n' if $_ eq "\n";
251
 
    return '\\\\' if $_ eq '\\';
252
 
 
253
 
    return $_;
254
 
}
255
 
 
256
 
sub escape
257
 
{
258
 
    my ($string) = @_;
259
 
    return join "", map &escape_char, split //, $string;
260
 
}
261
 
 
262
 
sub type_ini {
263
 
    ### For generic translatable desktop files ###
264
 
    while ($input =~ /^_.*=(.*)$/mg) {
265
 
        $messages{$1} = [];
266
 
    }
267
 
}
268
 
 
269
 
sub type_keys {
270
 
    ### For generic translatable mime/keys files ###
271
 
    while ($input =~ /^\s*_\w+=(.*)$/mg) {
272
 
        $messages{$1} = [];
273
 
    }
274
 
}
275
 
 
276
 
sub type_xml {
277
 
    ### For generic translatable XML files ###
278
 
    my $tree = readXml($input);
279
 
    parseTree(0, $tree);
280
 
}
281
 
 
282
 
sub print_var {
283
 
    my $var = shift;
284
 
    my $vartype = ref $var;
285
 
    
286
 
    if ($vartype =~ /ARRAY/) {
287
 
        my @arr = @{$var};
288
 
        print "[ ";
289
 
        foreach my $el (@arr) {
290
 
            print_var($el);
291
 
            print ", ";
292
 
        }
293
 
        print "] ";
294
 
    } elsif ($vartype =~ /HASH/) {
295
 
        my %hash = %{$var};
296
 
        print "{ ";
297
 
        foreach my $key (keys %hash) {
298
 
            print "$key => ";
299
 
            print_var($hash{$key});
300
 
            print ", ";
301
 
        }
302
 
        print "} ";
303
 
    } else {
304
 
        print $var;
305
 
    }
306
 
}
307
 
 
308
 
# Same syntax as getAttributeString in intltool-merge.in.in, similar logic (look for ## differences comment)
309
 
sub getAttributeString
310
 
{
311
 
    my $sub = shift;
312
 
    my $do_translate = shift || 1;
313
 
    my $language = shift || "";
314
 
    my $translate = shift;
315
 
    my $result = "";
316
 
    foreach my $e (reverse(sort(keys %{ $sub }))) {
317
 
        my $key    = $e;
318
 
        my $string = $sub->{$e};
319
 
        my $quote = '"';
320
 
        
321
 
        $string =~ s/^[\s]+//;
322
 
        $string =~ s/[\s]+$//;
323
 
        
324
 
        if ($string =~ /^'.*'$/)
325
 
        {
326
 
            $quote = "'";
327
 
        }
328
 
        $string =~ s/^['"]//g;
329
 
        $string =~ s/['"]$//g;
330
 
 
331
 
        ## differences from intltool-merge.in.in
332
 
        if ($key =~ /^_/) {
333
 
            $comments{entity_decode($string)} = $XMLCOMMENT if $XMLCOMMENT;
334
 
            $messages{entity_decode($string)} = [];
335
 
            $$translate = 2;
336
 
        }
337
 
        ## differences end here from intltool-merge.in.in
338
 
        $result .= " $key=$quote$string$quote";
339
 
    }
340
 
    return $result;
341
 
}
342
 
 
343
 
# Verbatim copy from intltool-merge.in.in
344
 
sub getXMLstring
345
 
{
346
 
    my $ref = shift;
347
 
    my $spacepreserve = shift || 0;
348
 
    my @list = @{ $ref };
349
 
    my $result = "";
350
 
 
351
 
    my $count = scalar(@list);
352
 
    my $attrs = $list[0];
353
 
    my $index = 1;
354
 
 
355
 
    $spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
356
 
    $spacepreserve = 0 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?default["']?$/));
357
 
 
358
 
    while ($index < $count) {
359
 
        my $type = $list[$index];
360
 
        my $content = $list[$index+1];
361
 
        if (! $type ) {
362
 
            # We've got CDATA
363
 
            if ($content) {
364
 
                # lets strip the whitespace here, and *ONLY* here
365
 
                $content =~ s/\s+/ /gs if (!$spacepreserve);
366
 
                $result .= $content;
367
 
            }
368
 
        } elsif ( "$type" ne "1" ) {
369
 
            # We've got another element
370
 
            $result .= "<$type";
371
 
            $result .= getAttributeString(@{$content}[0], 0); # no nested translatable elements
372
 
            if ($content) {
373
 
                my $subresult = getXMLstring($content, $spacepreserve);
374
 
                if ($subresult) {
375
 
                    $result .= ">".$subresult . "</$type>";
376
 
                } else {
377
 
                    $result .= "/>";
378
 
                }
379
 
            } else {
380
 
                $result .= "/>";
381
 
            }
382
 
        }
383
 
        $index += 2;
384
 
    }
385
 
    return $result;
386
 
}
387
 
 
388
 
# Verbatim copy from intltool-merge.in.in, except for MULTIPLE_OUTPUT handling removed
389
 
# Translate list of nodes if necessary
390
 
sub translate_subnodes
391
 
{
392
 
    my $fh = shift;
393
 
    my $content = shift;
394
 
    my $language = shift || "";
395
 
    my $singlelang = shift || 0;
396
 
    my $spacepreserve = shift || 0;
397
 
 
398
 
    my @nodes = @{ $content };
399
 
 
400
 
    my $count = scalar(@nodes);
401
 
    my $index = 0;
402
 
    while ($index < $count) {
403
 
        my $type = $nodes[$index];
404
 
        my $rest = $nodes[$index+1];
405
 
        traverse($fh, $type, $rest, $language, $spacepreserve);
406
 
        $index += 2;
407
 
    }
408
 
}
409
 
 
410
 
# Based on traverse() in intltool-merge.in.in
411
 
sub traverse
412
 
{
413
 
    my $fh = shift; # unused, to allow us to sync code between -merge and -extract
414
 
    my $nodename = shift;
415
 
    my $content = shift;
416
 
    my $language = shift || "";
417
 
    my $spacepreserve = shift || 0;
418
 
 
419
 
    if ($nodename && "$nodename" eq "1") {
420
 
        $XMLCOMMENT = $content;
421
 
    } elsif ($nodename) {
422
 
        # element
423
 
        my @all = @{ $content };
424
 
        my $attrs = shift @all;
425
 
        my $translate = 0;
426
 
        my $outattr = getAttributeString($attrs, 1, $language, \$translate);
427
 
 
428
 
        if ($nodename =~ /^_/) {
429
 
            $translate = 1;
430
 
            $nodename =~ s/^_//;
431
 
        }
432
 
        my $lookup = '';
433
 
 
434
 
        $spacepreserve = 0 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?default["']?$/));
435
 
        $spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
436
 
 
437
 
        if ($translate) {
438
 
            $lookup = getXMLstring($content, $spacepreserve);
439
 
            if (!$spacepreserve) {
440
 
                $lookup =~ s/^\s+//s;
441
 
                $lookup =~ s/\s+$//s;
442
 
            }
443
 
 
444
 
            if ($lookup && $translate != 2) {
445
 
                $comments{$lookup} = $XMLCOMMENT if $XMLCOMMENT;
446
 
                $messages{$lookup} = [];
447
 
            } elsif ($translate == 2) {
448
 
                translate_subnodes($fh, \@all, $language, 1, $spacepreserve);
449
 
            }
450
 
        } else {
451
 
            $XMLCOMMENT = "";
452
 
            my $count = scalar(@all);
453
 
            if ($count > 0) {
454
 
                my $index = 0;
455
 
                while ($index < $count) {
456
 
                    my $type = $all[$index];
457
 
                    my $rest = $all[$index+1];
458
 
                    traverse($fh, $type, $rest, $language, $spacepreserve);
459
 
                    $index += 2;
460
 
                }
461
 
            }
462
 
        }
463
 
        $XMLCOMMENT = "";
464
 
    }
465
 
}
466
 
 
467
 
 
468
 
# Verbatim copy from intltool-merge.in.in, $fh for compatibility
469
 
sub parseTree
470
 
{
471
 
    my $fh        = shift;
472
 
    my $ref       = shift;
473
 
    my $language  = shift || "";
474
 
 
475
 
    my $name = shift @{ $ref };
476
 
    my $cont = shift @{ $ref };
477
 
 
478
 
    while (!$name || "$name" eq "1") {
479
 
        $name = shift @{ $ref };
480
 
        $cont = shift @{ $ref };
481
 
    }
482
 
 
483
 
    my $spacepreserve = 0;
484
 
    my $attrs = @{$cont}[0];
485
 
    $spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
486
 
 
487
 
    traverse($fh, $name, $cont, $language, $spacepreserve);
488
 
}
489
 
 
490
 
# Verbatim copy from intltool-merge.in.in
491
 
sub intltool_tree_comment
492
 
{
493
 
    my $expat = shift;
494
 
    my $data  = $expat->original_string();
495
 
    my $clist = $expat->{Curlist};
496
 
    my $pos   = $#$clist;
497
 
 
498
 
    $data =~ s/^<!--//s;
499
 
    $data =~ s/-->$//s;
500
 
    push @$clist, 1 => $data;
501
 
}
502
 
 
503
 
# Verbatim copy from intltool-merge.in.in
504
 
sub intltool_tree_cdatastart
505
 
{
506
 
    my $expat    = shift;
507
 
    my $clist = $expat->{Curlist};
508
 
    my $pos   = $#$clist;
509
 
 
510
 
    push @$clist, 0 => $expat->original_string();
511
 
}
512
 
 
513
 
# Verbatim copy from intltool-merge.in.in
514
 
sub intltool_tree_cdataend
515
 
{
516
 
    my $expat    = shift;
517
 
    my $clist = $expat->{Curlist};
518
 
    my $pos   = $#$clist;
519
 
 
520
 
    $clist->[$pos] .= $expat->original_string();
521
 
}
522
 
 
523
 
# Verbatim copy from intltool-merge.in.in
524
 
sub intltool_tree_char
525
 
{
526
 
    my $expat = shift;
527
 
    my $text  = shift;
528
 
    my $clist = $expat->{Curlist};
529
 
    my $pos   = $#$clist;
530
 
 
531
 
    # Use original_string so that we retain escaped entities
532
 
    # in CDATA sections.
533
 
    #
534
 
    if ($pos > 0 and $clist->[$pos - 1] eq '0') {
535
 
        $clist->[$pos] .= $expat->original_string();
536
 
    } else {
537
 
        push @$clist, 0 => $expat->original_string();
538
 
    }
539
 
}
540
 
 
541
 
# Verbatim copy from intltool-merge.in.in
542
 
sub intltool_tree_start
543
 
{
544
 
    my $expat    = shift;
545
 
    my $tag      = shift;
546
 
    my @origlist = ();
547
 
 
548
 
    # Use original_string so that we retain escaped entities
549
 
    # in attribute values.  We must convert the string to an
550
 
    # @origlist array to conform to the structure of the Tree
551
 
    # Style.
552
 
    #
553
 
    my @original_array = split /\x/, $expat->original_string();
554
 
    my $source         = $expat->original_string();
555
 
 
556
 
    # Remove leading tag.
557
 
    #
558
 
    $source =~ s|^\s*<\s*(\S+)||s;
559
 
 
560
 
    # Grab attribute key/value pairs and push onto @origlist array.
561
 
    #
562
 
    while ($source)
563
 
    {
564
 
       if ($source =~ /^\s*([\w:-]+)\s*[=]\s*["]/)
565
 
       {
566
 
           $source =~ s|^\s*([\w:-]+)\s*[=]\s*["]([^"]*)["]||s;
567
 
           push @origlist, $1;
568
 
           push @origlist, '"' . $2 . '"';
569
 
       }
570
 
       elsif ($source =~ /^\s*([\w:-]+)\s*[=]\s*[']/)
571
 
       {
572
 
           $source =~ s|^\s*([\w:-]+)\s*[=]\s*[']([^']*)[']||s;
573
 
           push @origlist, $1;
574
 
           push @origlist, "'" . $2 . "'";
575
 
       }
576
 
       else
577
 
       {
578
 
           last;
579
 
       }
580
 
    }
581
 
 
582
 
    my $ol = [ { @origlist } ];
583
 
 
584
 
    push @{ $expat->{Lists} }, $expat->{Curlist};
585
 
    push @{ $expat->{Curlist} }, $tag => $ol;
586
 
    $expat->{Curlist} = $ol;
587
 
}
588
 
 
589
 
# Copied from intltool-merge.in.in and added comment handler.
590
 
sub readXml
591
 
{
592
 
    my $xmldoc = shift || return;
593
 
    my $ret = eval 'require XML::Parser';
594
 
    if(!$ret) {
595
 
        die "You must have XML::Parser installed to run $0\n\n";
596
 
    }
597
 
    my $xp = new XML::Parser(Style => 'Tree');
598
 
    $xp->setHandlers(Char => \&intltool_tree_char);
599
 
    $xp->setHandlers(Start => \&intltool_tree_start);
600
 
    $xp->setHandlers(CdataStart => \&intltool_tree_cdatastart);
601
 
    $xp->setHandlers(CdataEnd => \&intltool_tree_cdataend);
602
 
 
603
 
    ## differences from intltool-merge.in.in
604
 
    $xp->setHandlers(Comment => \&intltool_tree_comment);
605
 
    ## differences end here from intltool-merge.in.in
606
 
 
607
 
    my $tree = $xp->parse($xmldoc);
608
 
    #print_var($tree);
609
 
 
610
 
# <foo><!-- comment --><head id="a">Hello <em>there</em></head><bar>Howdy<ref/></bar>do</foo>
611
 
# would be:
612
 
# [foo, [{}, 1, "comment", head, [{id => "a"}, 0, "Hello ",  em, [{}, 0, "there"]], bar, 
613
 
# [{}, 0, "Howdy",  ref, [{}]], 0, "do" ] ]
614
 
 
615
 
    return $tree;
616
 
}
617
 
 
618
 
sub type_schemas {
619
 
    ### For schemas XML files ###
620
 
         
621
 
    # FIXME: We should handle escaped < (less than)
622
 
    while ($input =~ /
623
 
                      <locale\ name="C">\s*
624
 
                          (<default>\s*(?:<!--([^>]*?)-->\s*)?(.*?)\s*<\/default>\s*)?
625
 
                          (<short>\s*(?:<!--([^>]*?)-->\s*)?(.*?)\s*<\/short>\s*)?
626
 
                          (<long>\s*(?:<!--([^>]*?)-->\s*)?(.*?)\s*<\/long>\s*)?
627
 
                      <\/locale>
628
 
                     /sgx) {
629
 
        my @totranslate = ($3,$6,$9);
630
 
        my @eachcomment = ($2,$5,$8);
631
 
        foreach (@totranslate) {
632
 
            my $currentcomment = shift @eachcomment;
633
 
            next if !$_;
634
 
            s/\s+/ /g;
635
 
            $messages{entity_decode_minimal($_)} = [];
636
 
            $comments{entity_decode_minimal($_)} = $currentcomment if (defined($currentcomment));
637
 
        }
638
 
    }
639
 
}
640
 
 
641
 
sub type_rfc822deb {
642
 
    ### For rfc822-style Debian configuration files ###
643
 
 
644
 
    my $lineno = 1;
645
 
    my $type = '';
646
 
    while ($input =~ /\G(.*?)(^|\n)(_+)([^:]+):[ \t]*(.*?)(?=\n\S|$)/sg)
647
 
    {
648
 
        my ($pre, $newline, $underscore, $tag, $text) = ($1, $2, $3, $4, $5);
649
 
        while ($pre =~ m/\n/g)
650
 
        {
651
 
            $lineno ++;
652
 
        }
653
 
        $lineno += length($newline);
654
 
        my @str_list = rfc822deb_split(length($underscore), $text);
655
 
        for my $str (@str_list)
656
 
        {
657
 
            $strcount++;
658
 
            $messages{$str} = [];
659
 
            $loc{$str} = $lineno;
660
 
            $count{$str} = $strcount;
661
 
            my $usercomment = '';
662
 
            while($pre =~ s/(^|\n)#([^\n]*)$//s)
663
 
            {
664
 
                $usercomment = "\n" . $2 . $usercomment;
665
 
            }
666
 
            $comments{$str} = $tag . $usercomment;
667
 
        }
668
 
        $lineno += ($text =~ s/\n//g);
669
 
    }
670
 
}
671
 
 
672
 
sub rfc822deb_split {
673
 
    # Debian defines a special way to deal with rfc822-style files:
674
 
    # when a value contain newlines, it consists of
675
 
    #   1.  a short form (first line)
676
 
    #   2.  a long description, all lines begin with a space,
677
 
    #       and paragraphs are separated by a single dot on a line
678
 
    # This routine returns an array of all paragraphs, and reformat
679
 
    # them.
680
 
    # When first argument is 2, the string is a comma separated list of
681
 
    # values.
682
 
    my $type = shift;
683
 
    my $text = shift;
684
 
    $text =~ s/^[ \t]//mg;
685
 
    return (split(/, */, $text, 0)) if $type ne 1;
686
 
    return ($text) if $text !~ /\n/;
687
 
 
688
 
    $text =~ s/([^\n]*)\n//;
689
 
    my @list = ($1);
690
 
    my $str = '';
691
 
    for my $line (split (/\n/, $text))
692
 
    {
693
 
        chomp $line;
694
 
        if ($line =~ /^\.\s*$/)
695
 
        {
696
 
            #  New paragraph
697
 
            $str =~ s/\s*$//;
698
 
            push(@list, $str);
699
 
            $str = '';
700
 
        }
701
 
        elsif ($line =~ /^\s/)
702
 
        {
703
 
            #  Line which must not be reformatted
704
 
            $str .= "\n" if length ($str) && $str !~ /\n$/;
705
 
            $line =~ s/\s+$//;
706
 
            $str .= $line."\n";
707
 
        }
708
 
        else
709
 
        {
710
 
            #  Continuation line, remove newline
711
 
            $str .= " " if length ($str) && $str !~ /\n$/;
712
 
            $str .= $line;
713
 
        }
714
 
    }
715
 
    $str =~ s/\s*$//;
716
 
    push(@list, $str) if length ($str);
717
 
    return @list;
718
 
}
719
 
 
720
 
sub type_quoted {
721
 
    while ($input =~ /\"(([^\"]|\\\")*[^\\\"])\"/g) {
722
 
        my $message = $1;
723
 
        my $before = $`;
724
 
        $message =~ s/\\\"/\"/g;
725
 
        $before =~ s/[^\n]//g;
726
 
        $messages{$message} = [];
727
 
        $loc{$message} = length ($before) + 2;
728
 
    }
729
 
}
730
 
 
731
 
sub type_glade {
732
 
    ### For translatable Glade XML files ###
733
 
 
734
 
    my $tags = "label|title|text|format|copyright|comments|preview_text|tooltip|message";
735
 
 
736
 
    while ($input =~ /<($tags)>([^<]+)<\/($tags)>/sg) {
737
 
        # Glade sometimes uses tags that normally mark translatable things for
738
 
        # little bits of non-translatable content. We work around this by not
739
 
        # translating strings that only includes something like label4 or window1.
740
 
        $messages{entity_decode($2)} = [] unless $2 =~ /^(window|label|dialog)[0-9]+$/;
741
 
    }
742
 
    
743
 
    while ($input =~ /<items>(..[^<]*)<\/items>/sg) {
744
 
        for my $item (split (/\n/, $1)) {
745
 
            $messages{entity_decode($item)} = [];
746
 
        }
747
 
    }
748
 
 
749
 
    ## handle new glade files
750
 
    while ($input =~ /<(property|atkproperty)\s+[^>]*translatable\s*=\s*"yes"(?:\s+[^>]*comments\s*=\s*"([^"]*)")?[^>]*>([^<]+)<\/\1>/sg) {
751
 
        $messages{entity_decode($3)} = [] unless $3 =~ /^(window|label)[0-9]+$/;
752
 
        if (defined($2) and !($3 =~ /^(window|label)[0-9]+$/)) {
753
 
           $comments{entity_decode($3)} = entity_decode($2) ;
754
 
        }
755
 
    }
756
 
    while ($input =~ /<atkaction\s+action_name="([^>]*)"\s+description="([^>]+)"\/>/sg) {
757
 
        $messages{entity_decode_minimal($2)} = [];
758
 
    }
759
 
}
760
 
 
761
 
sub type_scheme {
762
 
    my ($line, $i, $state, $str, $trcomment, $char);
763
 
    for $line (split(/\n/, $input)) {
764
 
        $i = 0;
765
 
        $state = 0; # 0 - nothing, 1 - string, 2 - translatable string
766
 
        while ($i < length($line)) {
767
 
            if (substr($line,$i,1) eq "\"") {
768
 
                if ($state == 2) {
769
 
                    $comments{$str} = $trcomment if ($trcomment);
770
 
                    $messages{$str} = [];
771
 
                    $str = '';
772
 
                    $state = 0; $trcomment = "";
773
 
                } elsif ($state == 1) {
774
 
                    $str = '';
775
 
                    $state = 0; $trcomment = "";
776
 
                } else {
777
 
                    $state = 1;
778
 
                    $str = '';
779
 
                    if ($i>0 && substr($line,$i-1,1) eq '_') {
780
 
                        $state = 2;
781
 
                    }
782
 
                }
783
 
            } elsif (!$state) {
784
 
                if (substr($line,$i,1) eq ";") {
785
 
                    $trcomment = substr($line,$i+1);
786
 
                    $trcomment =~ s/^;*\s*//;
787
 
                    $i = length($line);
788
 
                } elsif ($trcomment && substr($line,$i,1) !~ /\s|\(|\)|_/) {
789
 
                    $trcomment = "";
790
 
                }
791
 
            } else {
792
 
                if (substr($line,$i,1) eq "\\") {
793
 
                    $char = substr($line,$i+1,1);
794
 
                    if ($char ne "\"" && $char ne "\\") {
795
 
                       $str = $str . "\\";
796
 
                    }
797
 
                    $i++;
798
 
                }
799
 
                $str = $str . substr($line,$i,1);
800
 
            }
801
 
            $i++;
802
 
        }
803
 
    }
804
 
}
805
 
 
806
 
sub msg_write {
807
 
    my @msgids;
808
 
    if (%count)
809
 
    {
810
 
        @msgids = sort { $count{$a} <=> $count{$b} } keys %count;
811
 
    }
812
 
    else
813
 
    {
814
 
        @msgids = sort keys %messages;
815
 
    }
816
 
    for my $message (@msgids)
817
 
    {
818
 
        my $offsetlines = 1;
819
 
        $offsetlines++ if $message =~ /%/;
820
 
        if (defined ($comments{$message}))
821
 
        {
822
 
                while ($comments{$message} =~ m/\n/g)
823
 
                {
824
 
                    $offsetlines++;
825
 
                }
826
 
        }
827
 
        print OUT "# ".($loc{$message} - $offsetlines).  " \"$FILE\"\n"
828
 
                if defined $loc{$message};
829
 
        print OUT "/* ".$comments{$message}." */\n"
830
 
                if defined $comments{$message};
831
 
        print OUT "/* xgettext:no-c-format */\n" if $message =~ /%/;
832
 
        
833
 
        my @lines = split (/\n/, $message, -1);
834
 
        for (my $n = 0; $n < @lines; $n++)
835
 
        {
836
 
            if ($n == 0)
837
 
            {
838
 
                print OUT "char *s = N_(\""; 
839
 
            }
840
 
            else
841
 
            {  
842
 
                print OUT "             \""; 
843
 
            }
844
 
 
845
 
            print OUT escape($lines[$n]);
846
 
 
847
 
            if ($n < @lines - 1)
848
 
            {
849
 
                print OUT "\\n\"\n"; 
850
 
            }
851
 
            else
852
 
            {
853
 
                print OUT "\");\n";  
854
 
            }
855
 
        }
856
 
    }
857
 
}
858