267
271
### For generic translatable XML files ###
269
while ($input =~ /\s_$w+\s*=\s*\"([^"]+)\"/sg) { # "
270
$messages{entity_decode_minimal($1)} = [];
273
while ($input =~ /(?:<!--([^>]*?)-->\s*)?<_($w+)(?: xml:space="($w+)")?>(.+?)<\/_\2>/sg) {
275
if (!defined($3) || $3 ne "preserve") {
281
$comments{$_} = $1 if (defined($1));
272
my $tree = readXml($input);
278
my $vartype = ref $var;
280
if ($vartype =~ /ARRAY/) {
283
foreach my $el (@arr) {
288
} elsif ($vartype =~ /HASH/) {
291
foreach my $key (keys %hash) {
293
print_var($hash{$key});
302
# Same syntax as getAttributeString in intltool-merge.in.in, similar logic (look for ## differences comment)
303
sub getAttributeString
306
my $do_translate = shift || 1;
307
my $language = shift || "";
308
my $translate = shift;
310
foreach my $e (reverse(sort(keys %{ $sub }))) {
312
my $string = $sub->{$e};
315
$string =~ s/^[\s]+//;
316
$string =~ s/[\s]+$//;
318
if ($string =~ /^'.*'$/)
322
$string =~ s/^['"]//g;
323
$string =~ s/['"]$//g;
325
## differences from intltool-merge.in.in
327
$comments{entity_decode($string)} = $XMLCOMMENT if $XMLCOMMENT;
328
$messages{entity_decode($string)} = [];
331
## differences end here from intltool-merge.in.in
332
$result .= " $key=$quote$string$quote";
337
# Verbatim copy from intltool-merge.in.in
341
my $spacepreserve = shift || 0;
342
my @list = @{ $ref };
345
my $count = scalar(@list);
346
my $attrs = $list[0];
349
$spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
350
$spacepreserve = 0 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?default["']?$/));
352
while ($index < $count) {
353
my $type = $list[$index];
354
my $content = $list[$index+1];
358
# lets strip the whitespace here, and *ONLY* here
359
$content =~ s/\s+/ /gs if (!$spacepreserve);
362
} elsif ( "$type" ne "1" ) {
363
# We've got another element
365
$result .= getAttributeString(@{$content}[0], 0); # no nested translatable elements
367
my $subresult = getXMLstring($content, $spacepreserve);
369
$result .= ">".$subresult . "</$type>";
382
# Verbatim copy from intltool-merge.in.in, except for MULTIPLE_OUTPUT handling removed
383
# Translate list of nodes if necessary
384
sub translate_subnodes
388
my $language = shift || "";
389
my $singlelang = shift || 0;
390
my $spacepreserve = shift || 0;
392
my @nodes = @{ $content };
394
my $count = scalar(@nodes);
396
while ($index < $count) {
397
my $type = $nodes[$index];
398
my $rest = $nodes[$index+1];
399
traverse($fh, $type, $rest, $language, $spacepreserve);
404
# Based on traverse() in intltool-merge.in.in
407
my $fh = shift; # unused, to allow us to sync code between -merge and -extract
408
my $nodename = shift;
410
my $language = shift || "";
411
my $spacepreserve = shift || 0;
413
if ($nodename && "$nodename" eq "1") {
414
$XMLCOMMENT = $content;
415
} elsif ($nodename) {
417
my @all = @{ $content };
418
my $attrs = shift @all;
420
my $outattr = getAttributeString($attrs, 1, $language, \$translate);
422
if ($nodename =~ /^_/) {
428
$spacepreserve = 0 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?default["']?$/));
429
$spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
432
$lookup = getXMLstring($content, $spacepreserve);
433
if (!$spacepreserve) {
434
$lookup =~ s/^\s+//s;
435
$lookup =~ s/\s+$//s;
438
if ($lookup && $translate != 2) {
439
$comments{$lookup} = $XMLCOMMENT if $XMLCOMMENT;
440
$messages{$lookup} = [];
441
} elsif ($translate == 2) {
442
translate_subnodes($fh, \@all, $language, 1, $spacepreserve);
446
my $count = scalar(@all);
449
while ($index < $count) {
450
my $type = $all[$index];
451
my $rest = $all[$index+1];
452
traverse($fh, $type, $rest, $language, $spacepreserve);
462
# Verbatim copy from intltool-merge.in.in, $fh for compatibility
467
my $language = shift || "";
469
my $name = shift @{ $ref };
470
my $cont = shift @{ $ref };
472
while (!$name || "$name" eq "1") {
473
$name = shift @{ $ref };
474
$cont = shift @{ $ref };
477
my $spacepreserve = 0;
478
my $attrs = @{$cont}[0];
479
$spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
481
traverse($fh, $name, $cont, $language, $spacepreserve);
484
# Verbatim copy from intltool-merge.in.in
485
sub intltool_tree_comment
489
my $clist = $expat->{Curlist};
492
push @$clist, 1 => $data;
495
# Verbatim copy from intltool-merge.in.in
496
sub intltool_tree_cdatastart
499
my $clist = $expat->{Curlist};
502
push @$clist, 0 => $expat->original_string();
505
# Verbatim copy from intltool-merge.in.in
506
sub intltool_tree_cdataend
509
my $clist = $expat->{Curlist};
512
$clist->[$pos] .= $expat->original_string();
515
# Verbatim copy from intltool-merge.in.in
516
sub intltool_tree_char
520
my $clist = $expat->{Curlist};
523
# Use original_string so that we retain escaped entities
526
if ($pos > 0 and $clist->[$pos - 1] eq '0') {
527
$clist->[$pos] .= $expat->original_string();
529
push @$clist, 0 => $expat->original_string();
533
# Verbatim copy from intltool-merge.in.in
534
sub intltool_tree_start
540
# Use original_string so that we retain escaped entities
541
# in attribute values. We must convert the string to an
542
# @origlist array to conform to the structure of the Tree
545
my @original_array = split /\x/, $expat->original_string();
546
my $source = $expat->original_string();
548
# Remove leading tag.
550
$source =~ s|^\s*<\s*(\S+)||s;
552
# Grab attribute key/value pairs and push onto @origlist array.
556
if ($source =~ /^\s*([\w:-]+)\s*[=]\s*["]/)
558
$source =~ s|^\s*([\w:-]+)\s*[=]\s*["]([^"]*)["]||s;
560
push @origlist, '"' . $2 . '"';
562
elsif ($source =~ /^\s*([\w:-]+)\s*[=]\s*[']/)
564
$source =~ s|^\s*([\w:-]+)\s*[=]\s*[']([^']*)[']||s;
566
push @origlist, "'" . $2 . "'";
574
my $ol = [ { @origlist } ];
576
push @{ $expat->{Lists} }, $expat->{Curlist};
577
push @{ $expat->{Curlist} }, $tag => $ol;
578
$expat->{Curlist} = $ol;
581
# Copied from intltool-merge.in.in and added comment handler.
584
my $xmldoc = shift || return;
585
my $ret = eval 'require XML::Parser';
587
die "You must have XML::Parser installed to run $0\n\n";
589
my $xp = new XML::Parser(Style => 'Tree');
590
$xp->setHandlers(Char => \&intltool_tree_char);
591
$xp->setHandlers(Start => \&intltool_tree_start);
592
$xp->setHandlers(CdataStart => \&intltool_tree_cdatastart);
593
$xp->setHandlers(CdataEnd => \&intltool_tree_cdataend);
595
## differences from intltool-merge.in.in
596
$xp->setHandlers(Comment => \&intltool_tree_comment);
597
## differences end here from intltool-merge.in.in
599
my $tree = $xp->parse($xmldoc);
602
# <foo><!-- comment --><head id="a">Hello <em>there</em></head><bar>Howdy<ref/></bar>do</foo>
604
# [foo, [{}, 1, "comment", head, [{id => "a"}, 0, "Hello ", em, [{}, 0, "there"]], bar,
605
# [{}, 0, "Howdy", ref, [{}]], 0, "do" ] ]
285
610
sub type_schemas {
414
742
sub type_scheme {
415
while ($input =~ /_\w*\(?"((?:[^"\\]+|\\.)*)"\)?/sg) {
743
my ($line, $i, $state, $str, $trcomment, $char);
744
for $line (split(/\n/, $input)) {
746
$state = 0; # 0 - nothing, 1 - string, 2 - translatable string
747
while ($i < length($line)) {
748
if (substr($line,$i,1) eq "\"") {
750
$comments{$str} = $trcomment if ($trcomment);
751
$messages{$str} = [];
753
$state = 0; $trcomment = "";
754
} elsif ($state == 1) {
756
$state = 0; $trcomment = "";
760
if ($i>0 && substr($line,$i-1,1) eq '_') {
765
if (substr($line,$i,1) eq ";") {
766
$trcomment = substr($line,$i+1);
767
$trcomment =~ s/^;*\s*//;
769
} elsif ($trcomment && substr($line,$i,1) !~ /\s|\(|\)|_/) {
773
if (substr($line,$i,1) eq "\\") {
774
$char = substr($line,$i+1,1);
775
if ($char ne "\"" && $char ne "\\") {
780
$str = $str . substr($line,$i,1);