267
277
### For generic translatable XML files ###
269
while ($input =~ /(?:<!--([^>]*?)-->[^\n]*\n?[^\n]*)?\s_$w+\s*=\s*\"([^"]+)\"/sg) { # "
270
$messages{entity_decode_minimal($2)} = [];
271
$comments{entity_decode_minimal($2)} = $1 if (defined($1));
274
while ($input =~ /(?:<!--([^>]*?)-->\s*)?<_($w+)(?: xml:space="($w+)")?[^>]*>(.+?)<\/_\2>/sg) {
276
if (!defined($3) || $3 ne "preserve") {
282
$comments{$_} = $1 if (defined($1));
278
my $tree = readXml($input);
284
my $vartype = ref $var;
286
if ($vartype =~ /ARRAY/) {
289
foreach my $el (@arr) {
294
} elsif ($vartype =~ /HASH/) {
297
foreach my $key (keys %hash) {
299
print_var($hash{$key});
308
# Same syntax as getAttributeString in intltool-merge.in.in, similar logic (look for ## differences comment)
309
sub getAttributeString
312
my $do_translate = shift || 1;
313
my $language = shift || "";
314
my $translate = shift;
316
foreach my $e (reverse(sort(keys %{ $sub }))) {
318
my $string = $sub->{$e};
321
$string =~ s/^[\s]+//;
322
$string =~ s/[\s]+$//;
324
if ($string =~ /^'.*'$/)
328
$string =~ s/^['"]//g;
329
$string =~ s/['"]$//g;
331
## differences from intltool-merge.in.in
333
$comments{entity_decode($string)} = $XMLCOMMENT if $XMLCOMMENT;
334
$messages{entity_decode($string)} = [];
337
## differences end here from intltool-merge.in.in
338
$result .= " $key=$quote$string$quote";
343
# Verbatim copy from intltool-merge.in.in
347
my $spacepreserve = shift || 0;
348
my @list = @{ $ref };
351
my $count = scalar(@list);
352
my $attrs = $list[0];
355
$spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
356
$spacepreserve = 0 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?default["']?$/));
358
while ($index < $count) {
359
my $type = $list[$index];
360
my $content = $list[$index+1];
364
# lets strip the whitespace here, and *ONLY* here
365
$content =~ s/\s+/ /gs if (!$spacepreserve);
368
} elsif ( "$type" ne "1" ) {
369
# We've got another element
371
$result .= getAttributeString(@{$content}[0], 0); # no nested translatable elements
373
my $subresult = getXMLstring($content, $spacepreserve);
375
$result .= ">".$subresult . "</$type>";
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
394
my $language = shift || "";
395
my $singlelang = shift || 0;
396
my $spacepreserve = shift || 0;
398
my @nodes = @{ $content };
400
my $count = scalar(@nodes);
402
while ($index < $count) {
403
my $type = $nodes[$index];
404
my $rest = $nodes[$index+1];
405
traverse($fh, $type, $rest, $language, $spacepreserve);
410
# Based on traverse() in intltool-merge.in.in
413
my $fh = shift; # unused, to allow us to sync code between -merge and -extract
414
my $nodename = shift;
416
my $language = shift || "";
417
my $spacepreserve = shift || 0;
419
if ($nodename && "$nodename" eq "1") {
420
$XMLCOMMENT = $content;
421
} elsif ($nodename) {
423
my @all = @{ $content };
424
my $attrs = shift @all;
426
my $outattr = getAttributeString($attrs, 1, $language, \$translate);
428
if ($nodename =~ /^_/) {
434
$spacepreserve = 0 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?default["']?$/));
435
$spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
438
$lookup = getXMLstring($content, $spacepreserve);
439
if (!$spacepreserve) {
440
$lookup =~ s/^\s+//s;
441
$lookup =~ s/\s+$//s;
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);
452
my $count = scalar(@all);
455
while ($index < $count) {
456
my $type = $all[$index];
457
my $rest = $all[$index+1];
458
traverse($fh, $type, $rest, $language, $spacepreserve);
468
# Verbatim copy from intltool-merge.in.in, $fh for compatibility
473
my $language = shift || "";
475
my $name = shift @{ $ref };
476
my $cont = shift @{ $ref };
478
while (!$name || "$name" eq "1") {
479
$name = shift @{ $ref };
480
$cont = shift @{ $ref };
483
my $spacepreserve = 0;
484
my $attrs = @{$cont}[0];
485
$spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/));
487
traverse($fh, $name, $cont, $language, $spacepreserve);
490
# Verbatim copy from intltool-merge.in.in
491
sub intltool_tree_comment
494
my $data = $expat->original_string();
495
my $clist = $expat->{Curlist};
500
push @$clist, 1 => $data;
503
# Verbatim copy from intltool-merge.in.in
504
sub intltool_tree_cdatastart
507
my $clist = $expat->{Curlist};
510
push @$clist, 0 => $expat->original_string();
513
# Verbatim copy from intltool-merge.in.in
514
sub intltool_tree_cdataend
517
my $clist = $expat->{Curlist};
520
$clist->[$pos] .= $expat->original_string();
523
# Verbatim copy from intltool-merge.in.in
524
sub intltool_tree_char
528
my $clist = $expat->{Curlist};
531
# Use original_string so that we retain escaped entities
534
if ($pos > 0 and $clist->[$pos - 1] eq '0') {
535
$clist->[$pos] .= $expat->original_string();
537
push @$clist, 0 => $expat->original_string();
541
# Verbatim copy from intltool-merge.in.in
542
sub intltool_tree_start
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
553
my @original_array = split /\x/, $expat->original_string();
554
my $source = $expat->original_string();
556
# Remove leading tag.
558
$source =~ s|^\s*<\s*(\S+)||s;
560
# Grab attribute key/value pairs and push onto @origlist array.
564
if ($source =~ /^\s*([\w:-]+)\s*[=]\s*["]/)
566
$source =~ s|^\s*([\w:-]+)\s*[=]\s*["]([^"]*)["]||s;
568
push @origlist, '"' . $2 . '"';
570
elsif ($source =~ /^\s*([\w:-]+)\s*[=]\s*[']/)
572
$source =~ s|^\s*([\w:-]+)\s*[=]\s*[']([^']*)[']||s;
574
push @origlist, "'" . $2 . "'";
582
my $ol = [ { @origlist } ];
584
push @{ $expat->{Lists} }, $expat->{Curlist};
585
push @{ $expat->{Curlist} }, $tag => $ol;
586
$expat->{Curlist} = $ol;
589
# Copied from intltool-merge.in.in and added comment handler.
592
my $xmldoc = shift || return;
593
my $ret = eval 'require XML::Parser';
595
die "You must have XML::Parser installed to run $0\n\n";
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);
603
## differences from intltool-merge.in.in
604
$xp->setHandlers(Comment => \&intltool_tree_comment);
605
## differences end here from intltool-merge.in.in
607
my $tree = $xp->parse($xmldoc);
610
# <foo><!-- comment --><head id="a">Hello <em>there</em></head><bar>Howdy<ref/></bar>do</foo>
612
# [foo, [{}, 1, "comment", head, [{id => "a"}, 0, "Hello ", em, [{}, 0, "there"]], bar,
613
# [{}, 0, "Howdy", ref, [{}]], 0, "do" ] ]
286
618
sub type_schemas {
415
761
sub type_scheme {
416
while ($input =~ /_\w*\(?"((?:[^"\\]+|\\.)*)"\)?/sg) {
762
my ($line, $i, $state, $str, $trcomment, $char);
763
for $line (split(/\n/, $input)) {
765
$state = 0; # 0 - nothing, 1 - string, 2 - translatable string
766
while ($i < length($line)) {
767
if (substr($line,$i,1) eq "\"") {
769
$comments{$str} = $trcomment if ($trcomment);
770
$messages{$str} = [];
772
$state = 0; $trcomment = "";
773
} elsif ($state == 1) {
775
$state = 0; $trcomment = "";
779
if ($i>0 && substr($line,$i-1,1) eq '_') {
784
if (substr($line,$i,1) eq ";") {
785
$trcomment = substr($line,$i+1);
786
$trcomment =~ s/^;*\s*//;
788
} elsif ($trcomment && substr($line,$i,1) !~ /\s|\(|\)|_/) {
792
if (substr($line,$i,1) eq "\\") {
793
$char = substr($line,$i+1,1);
794
if ($char ne "\"" && $char ne "\\") {
799
$str = $str . substr($line,$i,1);