267
271
### 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));
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
488
my $data = $expat->original_string();
489
my $clist = $expat->{Curlist};
494
push @$clist, 1 => $data;
497
# Verbatim copy from intltool-merge.in.in
498
sub intltool_tree_cdatastart
501
my $clist = $expat->{Curlist};
504
push @$clist, 0 => $expat->original_string();
507
# Verbatim copy from intltool-merge.in.in
508
sub intltool_tree_cdataend
511
my $clist = $expat->{Curlist};
514
$clist->[$pos] .= $expat->original_string();
517
# Verbatim copy from intltool-merge.in.in
518
sub intltool_tree_char
522
my $clist = $expat->{Curlist};
525
# Use original_string so that we retain escaped entities
528
if ($pos > 0 and $clist->[$pos - 1] eq '0') {
529
$clist->[$pos] .= $expat->original_string();
531
push @$clist, 0 => $expat->original_string();
535
# Verbatim copy from intltool-merge.in.in
536
sub intltool_tree_start
542
# Use original_string so that we retain escaped entities
543
# in attribute values. We must convert the string to an
544
# @origlist array to conform to the structure of the Tree
547
my @original_array = split /\x/, $expat->original_string();
548
my $source = $expat->original_string();
550
# Remove leading tag.
552
$source =~ s|^\s*<\s*(\S+)||s;
554
# Grab attribute key/value pairs and push onto @origlist array.
558
if ($source =~ /^\s*([\w:-]+)\s*[=]\s*["]/)
560
$source =~ s|^\s*([\w:-]+)\s*[=]\s*["]([^"]*)["]||s;
562
push @origlist, '"' . $2 . '"';
564
elsif ($source =~ /^\s*([\w:-]+)\s*[=]\s*[']/)
566
$source =~ s|^\s*([\w:-]+)\s*[=]\s*[']([^']*)[']||s;
568
push @origlist, "'" . $2 . "'";
576
my $ol = [ { @origlist } ];
578
push @{ $expat->{Lists} }, $expat->{Curlist};
579
push @{ $expat->{Curlist} }, $tag => $ol;
580
$expat->{Curlist} = $ol;
583
# Copied from intltool-merge.in.in and added comment handler.
586
my $xmldoc = shift || return;
587
my $ret = eval 'require XML::Parser';
589
die "You must have XML::Parser installed to run $0\n\n";
591
my $xp = new XML::Parser(Style => 'Tree');
592
$xp->setHandlers(Char => \&intltool_tree_char);
593
$xp->setHandlers(Start => \&intltool_tree_start);
594
$xp->setHandlers(CdataStart => \&intltool_tree_cdatastart);
595
$xp->setHandlers(CdataEnd => \&intltool_tree_cdataend);
597
## differences from intltool-merge.in.in
598
$xp->setHandlers(Comment => \&intltool_tree_comment);
599
## differences end here from intltool-merge.in.in
601
my $tree = $xp->parse($xmldoc);
604
# <foo><!-- comment --><head id="a">Hello <em>there</em></head><bar>Howdy<ref/></bar>do</foo>
606
# [foo, [{}, 1, "comment", head, [{id => "a"}, 0, "Hello ", em, [{}, 0, "there"]], bar,
607
# [{}, 0, "Howdy", ref, [{}]], 0, "do" ] ]
286
612
sub type_schemas {
415
744
sub type_scheme {
416
while ($input =~ /_\w*\(?"((?:[^"\\]+|\\.)*)"\)?/sg) {
745
my ($line, $i, $state, $str, $trcomment, $char);
746
for $line (split(/\n/, $input)) {
748
$state = 0; # 0 - nothing, 1 - string, 2 - translatable string
749
while ($i < length($line)) {
750
if (substr($line,$i,1) eq "\"") {
752
$comments{$str} = $trcomment if ($trcomment);
753
$messages{$str} = [];
755
$state = 0; $trcomment = "";
756
} elsif ($state == 1) {
758
$state = 0; $trcomment = "";
762
if ($i>0 && substr($line,$i-1,1) eq '_') {
767
if (substr($line,$i,1) eq ";") {
768
$trcomment = substr($line,$i+1);
769
$trcomment =~ s/^;*\s*//;
771
} elsif ($trcomment && substr($line,$i,1) !~ /\s|\(|\)|_/) {
775
if (substr($line,$i,1) eq "\\") {
776
$char = substr($line,$i+1,1);
777
if ($char ne "\"" && $char ne "\\") {
782
$str = $str . substr($line,$i,1);