Erik Grinaker is sharing code with you

Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.

Don't show this again

erikg / Revelation

Revelation is a password manager for the GNOME desktop, released under the GNU GPL license. It stores all your accounts and passwords in a single, secure place, and gives you access to it through a user-friendly graphical interface.

Clone this repository (size: 2.1 MB): HTTPS / SSH
hg clone https://bitbucket.org/erikg/revelation
hg clone ssh://hg@bitbucket.org/erikg/revelation

Revelation / intltool-extract.in

commit
6cf4c99aaf31
parent
faf1e560d20a
branch
default
tags
revelation-0.4.8

properly import notes for SplashID files

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