From 2bf7f8c4da83e96068507b724e2702e39cb4f4bf Mon Sep 17 00:00:00 2001 From: Kornel Benko Date: Sat, 27 Apr 2013 17:23:58 +0200 Subject: [PATCH] po: Modularisation of the diff_po.pl script --- po/diff_po.pl | 79 +++----------------- po/parsePoLine.pm | 186 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 196 insertions(+), 69 deletions(-) create mode 100644 po/parsePoLine.pm diff --git a/po/diff_po.pl b/po/diff_po.pl index 30ca28dcf2..d5dec02839 100755 --- a/po/diff_po.pl +++ b/po/diff_po.pl @@ -24,14 +24,22 @@ # License along with this software; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # -# Copyright (c) 1010-2011 Kornel Benko, kornel@lyx.org +# Copyright (c) 2010-2013 Kornel Benko, kornel@lyx.org # # TODO: # 1.) Check for ".git" or ".svn" to decide about revisioning # 2.) Search for good correlations of deleted <==> inserted string # using Text::Levenshtein or Algorithm::Diff +BEGIN { + use File::Spec; + my $p = File::Spec->rel2abs( __FILE__ ); + $p =~ s/[\/\\]?diff_po\.pl$//; + unshift(@INC, "$p"); +} + use strict; +use parsePoLine; use Term::ANSIColor qw(:constants); use File::Temp; @@ -74,6 +82,7 @@ if ($ARGV[0] =~ /^-r(.*)/) { print $tmpfile $l; } close(FI); + $tmpfile->seek( 0, SEEK_END ); # Flush() push(@args, $tmpfile->filename, $argf); print "===================================================================\n"; &diff_po(@args); @@ -241,74 +250,6 @@ sub printIfDiff($$$) } } -sub parse_po_file($$) -{ - my ($file, $rMessages) = @_; - if (open(FI, '<', $file)) { - $status = "normal"; - $fuzzy = 0; - my $lineno = 0; - while (my $line = ) { - $lineno++; - &parse_po_line($line, $lineno, $rMessages); - } - &parse_po_line("", $lineno + 1, $rMessages); - close(FI); - } -} - -sub parse_po_line($$$) -{ - my ($line, $lineno, $rMessages) = @_; - chomp($line); - - if ($status eq "normal") { - if ($line =~ /^#, fuzzy/) { - $fuzzy = 1; - } - elsif ($line =~ s/^msgid\s+//) { - $foundline = $lineno; - $status = "msgid"; - $msgid = ""; - &parse_po_line($line); - } - } - elsif ($status eq "msgid") { - if ($line =~ /^\s*"(.*)"\s*/) { - $msgid .= $1; - } - elsif ($line =~ s/^msgstr\s+//) { - $status = "msgstr"; - $msgstr = ""; - &parse_po_line($line); - } - } - elsif ($status eq "msgstr") { - if ($line =~ /^\s*"(.*)"\s*/) { - $msgstr .= $1; - } - else { - if ($msgid ne "") { - $rMessages->{$msgid}->{line} = $foundline; - $rMessages->{$msgid}->{fuzzy} = $fuzzy; - $rMessages->{$msgid}->{msgstr} = $msgstr; - } - $fuzzy = 0; - $status = "normal"; - } - } - else { - die("invalid status"); - } -} - -sub getLineSortedKeys($) -{ - my ($rMessages) = @_; - - return sort {$rMessages->{$a}->{line} <=> $rMessages->{$b}->{line};} keys %{$rMessages}; -} - sub printExtraMessages($$) { my ($type, $rExtra) = @_; diff --git a/po/parsePoLine.pm b/po/parsePoLine.pm new file mode 100644 index 0000000000..fff1418322 --- /dev/null +++ b/po/parsePoLine.pm @@ -0,0 +1,186 @@ +package parsePoLine; + +use strict; + +our(@EXPORT, @ISA); + +BEGIN { + use Exporter (); + @ISA = qw(Exporter); + @EXPORT = qw(parse_po_file getLineSortedKeys); +} + +my ($status, $foundline, $msgid, $msgstr, $fuzzy); + + +my $alternative = 0; +my @entry = (); +my %entries = (); + +sub parse_po_file($$) +{ + $alternative = 0; + @entry = (); + %entries = (); + + my @result = (); + my $resindex = 0; + my ($file, $rMessages) = @_; + if (open(FI, '<', $file)) { + $status = "normal"; + $fuzzy = 0; + my $lineno = 0; + while (my $line = ) { + $lineno++; + &parse_po_line($line, $lineno, $rMessages, \@result, \$resindex); + push(@entry, $line); + + } + &parse_po_line("", $lineno + 1, $rMessages, \@result, \$resindex); + my @entr1 = @entry; + $result[$resindex] = ["zzzzzzzzzzzz", \@entr1]; + close(FI); + } + return(@result); +} + +sub parse_po_line($$$$$) +{ + my ($line, $lineno, $rMessages, $rresult, $rresindex) = @_; + chomp($line); + + if ($status eq "normal") { + if ($line =~ /^#, fuzzy/) { + $fuzzy = 1; + } + elsif ($line =~ s/^msgid\s+//) { + die("line alternate") if ($alternative); + $foundline = $lineno; + $status = "msgid"; + $msgid = ""; + &parse_po_line($line, $lineno, $rMessages, $rresult, $rresindex); + } + elsif ($line =~ s/^\#\~ msgid\s+//) { + $alternative = 1; + $foundline = $lineno; + $status = "msgid"; + $msgid = ""; + &parse_po_line($line, $lineno, $rMessages, $rresult, $rresindex); + } + } + elsif ($status eq "msgid") { + if ($line =~ /^\s*"(.*)"\s*/) { + $msgid .= $1; + } + elsif ($line =~ /^\#\~\s*"(.*)"\s*/) { + die("line not alternate") if (! $alternative); + $msgid .= $1; + } + elsif ($line =~ s/^msgstr\s+//) { + $alternative = 0; + $status = "msgstr"; + $msgstr = ""; + &parse_po_line($line, $lineno, $rMessages, $rresult, $rresindex); + } + elsif ($line =~ s/^\#\~ msgstr\s+//) { + $alternative = 1; + $status = "msgstr"; + $msgstr = ""; + &parse_po_line($line, $lineno, $rMessages, $rresult, $rresindex); + } + } + elsif ($status eq "msgstr") { + if ($line =~ /^\s*"(.*)"\s*/) { + $msgstr .= $1; + } + elsif ($line =~ /^\#\~\s+"(.*)"\s*/) { + die("line not alternate") if (! $alternative); + $msgstr .= $1; + } + else { + if (!defined($entries{$msgid})) { + my @entr1 = @entry; + $rresult->[${$rresindex}] = [$msgid, \@entr1]; + $entries{$msgid} = $msgstr; + } + else { + if ($alternative) { + print "duplicated alternate entry: \"$msgid\"\n"; + } + else { + print "duplicated entry: \"$msgid\"\n"; + } + print " on line: $foundline\n"; + print " original on line: $rMessages->{$msgid}->{line}\n\n"; + } + @entry = (); + $rMessages->{$msgid}->{line} = $foundline; + $rMessages->{$msgid}->{fuzzy} = $fuzzy; + $rMessages->{$msgid}->{msgstr} = $msgstr; + $rMessages->{$msgid}->{alternative} = $alternative; + $rMessages->{$msgid}->{entryidx} = ${$rresindex}; + ${$rresindex} = ${$rresindex}+1; + $fuzzy = 0; + $status = "normal"; + } + } + else { + die("invalid status"); + } +} + +sub getLineSortedKeys($) +{ + my ($rMessages) = @_; + + return sort {$rMessages->{$a}->{line} <=> $rMessages->{$b}->{line};} keys %{$rMessages}; +} + +1; + + +__END__ + +=pod + +=encoding utf8 + +=head1 NAME + +parsePoLine + +=head1 SYNOPSIS + + use parsePoLine; #imports functions 'parse_po_file() and getLineSortedKeys()' + + my %Messages = (); + my @entries = parse_po_file("sk.po", \%Messages); + +=head1 DESCRIPTION + +This is used to parse a single po-file. + +Results: + %Messages The keys in this hash are the msgid-strings + the value is reference to a hash wit following values: + msgstr: the translated string + line: the line-no in the po-file + fuzzy: boolean, if the string is fuzzy + alternative: if set, so this entry is part of help-strings + entryidx: The index in the correspondig @entries array + @entries List of references to 2-valued arrays + [0]: msgid-string + [1]: The sequence of lines from the po-file + belonging to this entry. + +To print the whole po-file: + for my $entry (@entries) { + print @{$entry->[1]}; + } + +To get the index to a known $msgid: + my $entriesidx = $Messages{$msgid}->{entryidx}; + +=head1 AUTHOR + +Kornel Benko -- 2.39.2