1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
# Copyrights 2007-2011 by Mark Overmeer.
# For other contributors see ChangeLog.
# See the manual pages for details on the licensing terms.
# Pod stripped from pm file by OODoc 2.00.
use warnings;
use strict;
package Log::Report::Extract::PerlPPI;
use vars '$VERSION';
$VERSION = '0.94';
use Log::Report 'log-report', syntax => 'SHORT';
use Log::Report::Lexicon::Index ();
use Log::Report::Lexicon::POT ();
use PPI;
# See Log::Report translation markup functions
my %msgids =
# MSGIDs COUNT OPTS VARS SPLIT
( __ => [1, 0, 0, 0, 0]
, __x => [1, 0, 1, 1, 0]
, __xn => [2, 1, 1, 1, 0]
, __n => [2, 1, 1, 0, 0]
, N__ => [1, 0, 1, 1, 0] # may be used with opts/vars
, N__n => [2, 0, 1, 1, 0] # idem
, N__w => [1, 0, 0, 0, 1]
);
sub new(@)
{ my $class = shift;
(bless {}, $class)->init( {@_} );
}
sub init($)
{ my ($self, $args) = @_;
my $lexi = $args->{lexicon}
or error __"PerlPPI requires explicit lexicon directory";
-d $lexi or mkdir $lexi
or fault __x"cannot create lexicon directory {dir}", dir => $lexi;
$self->{index} = Log::Report::Lexicon::Index->new($lexi);
$self->{charset} = $args->{charset} || 'utf-8';
$self;
}
sub index() {shift->{index}}
sub charset() {shift->{charset}}
sub domains() {sort keys %{shift->{domains}}}
sub process($@)
{ my ($self, $fn, %opts) = @_;
my $charset = $opts{charset} || 'iso-8859-1';
info __x"processing file {fn} in {charset}", fn=> $fn, charset => $charset;
$charset eq 'iso-8859-1'
or error __x"PPI only supports iso-8859-1 (latin-1) on the moment";
my $doc = PPI::Document->new($fn, readonly => 1)
or fault __x"cannot read from file {filename}", filename => $fn;
my ($pkg, $include, $domain) = ('main', 0, undef);
NODE:
foreach my $node ($doc->schildren)
{ if($node->isa('PPI::Statement::Package'))
{ $pkg = $node->namespace;
# special hack for module Log::Report itself
if($pkg eq 'Log::Report')
{ ($include, $domain) = (1, 'log-report');
$self->_reset($domain, $fn);
}
else { ($include, $domain) = (0, undef) }
next NODE;
}
if($node->isa('PPI::Statement::Include'))
{ next NODE if $node->type ne 'use' || $node->module ne 'Log::Report';
$include++;
my $dom = ($node->schildren)[2];
$domain = $dom->isa('PPI::Token::Quote') ? $dom->string : undef;
$self->_reset($domain, $fn);
}
$node->find_any
( sub { # look for the special translation markers
$_[1]->isa('PPI::Token::Word') or return 0;
my $node = $_[1];
my $def = $msgids{$node->content}
or return 0;
my @msgids = $self->_get($node, @$def)
or return 0;
my $line = $node->location->[0];
unless($domain)
{ mistake __x
"no textdomain for translatable at {fn} line {line}"
, fn => $fn, line => $line;
return 0;
}
if($def->[4]) # split
{ $self->_store($domain, $fn, $line, $_)
for map {split} @msgids;
}
else
{ $self->_store($domain, $fn, $line, @msgids);
}
0; # don't collect
}
);
}
}
sub _get($$$$$)
{ my ($self, $node, $msgids, $count, $opts, $vars, $split) = @_;
my $list_only = ($msgids > 1) || $count || $opts || $vars;
my $expand = $opts || $vars;
my @msgids;
my $first = $node->snext_sibling;
$first = $first->schild(0)
if $first->isa('PPI::Structure::List');
$first = $first->schild(0)
if $first->isa('PPI::Statement::Expression');
while(defined $first && $msgids > @msgids)
{ my $msgid;
my $next = $first->snext_sibling;
my $sep = $next && $next->isa('PPI::Token::Operator') ? $next : '';
my $line = $first->location->[0];
if($first->isa('PPI::Token::Quote'))
{ last if $sep !~ m/^ (?: | \=\> | [,;:] ) $/x;
$msgid = $first->string;
if( $first->isa("PPI::Token::Quote::Double")
|| $first->isa("PPI::Token::Quote::Interpolate"))
{ mistake __x
"do not interpolate in msgid (found '{var}' in line {line})"
, var => $1, line => $line
if $first->string =~ m/(?<!\\)(\$\w+)/;
# content string is uninterpreted, warnings to screen
$msgid = eval "qq{$msgid}";
error __x "string is incorrect at line {line}: {error}"
, line => $line, error => $@ if $@;
}
}
elsif($first->isa('PPI::Token::Word'))
{ last if $sep ne '=>';
$msgid = $first->content;
}
else {last}
mistake __x "new-line is added automatically (found in line {line})"
, line => $line if $msgid =~ s/(?<!\\)\n$//;
push @msgids, $msgid;
last if $msgids==@msgids || !$sep;
$first = $sep->snext_sibling;
}
@msgids;
}
sub showStats(;$)
{ dispatcher needs => 'INFO'
or return;
my $self = shift;
my @domains = @_ ? @_ : $self->domains;
foreach my $domain (@domains)
{ my $pots = $self->{domains}{$domain} or next;
my ($msgids, $fuzzy, $inactive) = (0, 0, 0);
foreach my $pot (@$pots)
{ my $stats = $pot->stats;
next unless $stats->{fuzzy} || $stats->{inactive};
$msgids = $stats->{msgids};
next if $msgids == $stats->{fuzzy}; # ignore the template
notice __x
"{domain}: {fuzzy%3d} fuzzy, {inact%3d} inactive in {filename}"
, domain => $domain, fuzzy => $stats->{fuzzy}
, inact => $stats->{inactive}, filename => $pot->filename;
$fuzzy += $stats->{fuzzy};
$inactive += $stats->{inactive};
}
if($fuzzy || $inactive)
{ info __xn
"{domain}: one file with {ids} msgids, {f} fuzzy and {i} inactive translations"
, "{domain}: {_count} files each {ids} msgids, {f} fuzzy and {i} inactive translations in total"
, scalar(@$pots), domain => $domain
, f => $fuzzy, ids => $msgids, i => $inactive
}
else
{ info __xn
"{domain}: one file with {ids} msgids"
, "{domain}: {_count} files with each {ids} msgids"
, scalar(@$pots), domain => $domain, ids => $msgids;
}
}
}
sub write(;$)
{ my ($self, $domain) = @_;
unless(defined $domain) # write all
{ $self->write($_) for keys %{$self->{domains}};
return;
}
my $pots = delete $self->{domains}{$domain}
or return; # nothing found
for my $pot (@$pots)
{ $pot->updated;
$pot->write;
}
$self;
}
sub DESTROY() {shift->write}
sub _reset($$)
{ my ($self, $domain, $fn) = @_;
my $pots = $self->{domains}{$domain}
||= $self->_read_pots($domain);
$_->removeReferencesTo($fn) for @$pots;
}
sub _read_pots($)
{ my ($self, $domain) = @_;
my $index = $self->index;
my $charset = $self->charset;
my @pots = map {Log::Report::Lexicon::POT->read($_, charset=> $charset)}
$index->list($domain);
trace __xn "found one pot file for domain {domain}"
, "found {_count} pot files for domain {domain}"
, @pots, domain => $domain;
@pots && return \@pots;
# new textdomain
my $fn = $index->addFile("$domain.$charset.po");
info __x"starting new textdomain {domain}, template in {filename}"
, domain => $domain, filename => $fn;
my $pot = Log::Report::Lexicon::POT->new
( textdomain => $domain
, filename => $fn
, charset => $charset
, version => 0.01
);
[ $pot ];
}
sub _store($$$$;$)
{ my ($self, $domain, $fn, $linenr, $msgid, $plural) = @_;
foreach my $pot ( @{$self->{domains}{$domain}} )
{ if(my $po = $pot->msgid($msgid))
{ $po->addReferences( ["$fn:$linenr"]);
$po->plural($plural) if $plural;
next;
}
my $format = $msgid =~ m/\{/ ? 'perl-brace' : 'perl';
my $po = Log::Report::Lexicon::PO->new
( msgid => $msgid
, msgid_plural => $plural
, fuzzy => 1
, format => $format
, references => [ "$fn:$linenr" ]
);
$pot->add($po);
}
}
1;
|