lib/Serge/Engine/Plugin/parse_php_xhtml.pm

filter: Statementstmt 16.9% 19 / 112 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 41.1% 7 / 17 Podpod n/a totaltotal 12.8% 26 / 202 SCAR 65.7
CC
31
Cov
11%
fix_php_blocks
342.0
report_errors
110.0
validate_output
12.0
CRAP 711.7
« lib/Serge/Engine/Plugin/control_commands.pm ↑ Summary lib/Serge/Engine/Plugin/parse_rc.pm »
1 package Serge::Engine::Plugin::parse_php_xhtml;
2 1 ▶use parent Serge::Engine::Plugin::Base::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Serge::Mail;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Serge::Util qw(locale_from_lang xml_escape_strref);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub name {
13 0 ▶ return 'PHP/XHTML static content parser plugin';
Subroutine name: not called CC=1 SCAR=6.9
14 }
15  
16 sub init {
17 0 ▶ my $self = shift;
Subroutine init: not called CC=1 SCAR=6.9
18  
19 0 $self->SUPER::init(@_);
20  
21 0 $self->{errors} = {};
22 0 $self->{php_blocks} = [];
23  
24 0 $self->merge_schema({
25 expand_entities => 'BOOLEAN',
26 validate_output => 'BOOLEAN',
27  
28 email_from => 'STRING',
29 email_to => 'ARRAY',
30 email_subject => 'STRING',
31 });
32  
33 0 $self->add('can_save_localized_file', \&validate_output);
34 0 $self->add('after_job', \&report_errors);
35 }
36  
37 sub validate_output {
38 0 ▶ my ($self, $phase, $file, $lang, $textref) = @_;
Subroutine validate_output: not called CC=3 SCAR=24.8
39  
40 # return values:
41 # 0 - prohibit saving the file
42 # 1 - allow saving the file
43  
44 0 ▶ return 1 unless $self->{data}->{validate_output};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"data"}{"validate_output"} 0 0 0
45  
46 0 $self->{current_file_rel} = "$file:$lang";
47 0 eval {
48 0 ▶ $self->parse($textref, sub {});
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 };
50 0 delete $self->{current_file_rel};
51 0 ▶ return $@ ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
52 }
53  
54 sub report_errors {
55 0 ▶ my ($self, $phase) = @_;
Subroutine report_errors: not called CC=10 SCAR=47.0
56  
57 0 ▶ return if !scalar keys %{$self->{errors}};
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %{$$self{"errors"};} 0 0 0
58  
59 0 my $email_from = $self->{data}->{email_from};
60 0 my $email_to = $self->{data}->{email_to};
61  
62 0 ▶ if (!$email_from || !$email_to) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($email_from and $email_to) 0 0 0
Condition0%0/3
$email_from and $email_to
!ll&&!rl&&r
000
MC/DC0%0/2
$email_from and $email_to
$email_from $email_to
Truth table0%0/3
A and B
A= $email_fromB= $email_to
ABresult
0X0
100
111
63 0 my @a;
64 0 ▶ push @a, "'email_from'" unless $email_from;
Branch: 1 branch
BranchTrueFalseTotal
unless $email_from 0 0 0
65 0 ▶ push @a, "'email_to'" unless $email_to;
Branch: 1 branch
BranchTrueFalseTotal
unless $email_to 0 0 0
66 0 my $fields = join(' and ', @a);
67 0 ▶ my $are = scalar @a > 1 ? 'are' : 'is';
Branch: 1 branch
BranchTrueFalseTotal
scalar @a > 1 ? : 0 0 0
68 0 print "WARNING: there are some parsing errors, but $fields $are not defined, so can't send an email.\n";
69 0 $self->{errors} = {};
70 0 return;
71 }
72  
73 0 ▶ my $email_subject = $self->{data}->{email_subject} || ("[".$self->{parent}->{id}.']: PHP/XHTML Parse Errors');
Condition0%0/2
$self->{'data'}{'email_subject'} || '[' . $self->{'parent'}{'id'} . ']: PHP/XHTML Parse Errors'
l!l
00
MC/DC0%0/1
$self->{'data'}{'email_subject'} || '[' . $self->{'parent'}{'id'} . ']: PHP/XHTML Parse Errors'
$self->{'data'}{'email_subject'}
Truth table0%0/2
A
A= $self->{'data'}{'email_subject'}
Aresult
11
00
74  
75 0 my $text;
76 0 foreach my $key (sort keys %{$self->{errors}}) {
77 0 my $pre_contents = $self->{errors}->{$key};
78 0 xml_escape_strref(\$pre_contents);
79 0 $text .= "<hr />\n<p><b style='color: red'>$key</b> <pre>".$pre_contents."</pre></p>\n";
80 }
81  
82 0 $self->{errors} = {};
83  
84 0 ▶ if ($text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text) 0 0 0
85 0 $text = qq|
86 <html>
87 <head>
88 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
89 </head>
90 <body style="font-family: sans-serif; font-size: 120%">
91  
92 <p>
93 # This is an automatically generated message.
94  
95 The following parsing errors were found when attempting to localize resource files.
96 </p>
97  
98 $text
99  
100 </body>
101 </html>
102 |;
103 0 print 'Errors found. Sending error report to '.join(', ', @$email_to)."\n";
104  
105 0 Serge::Mail::send_html_message(
106 $email_from, # from
107 $email_to, # to (list)
108 $email_subject, # subject
109 $text # message body
110 );
111 }
112  
113 }
114  
115 sub add_php_block {
116 0 ▶ my ($self, $text) = @_;
Subroutine add_php_block: not called CC=1 SCAR=6.9
117 0 push @{$self->{php_blocks}}, $text;
118 0 return scalar(@{$self->{php_blocks}});
119 }
120  
121 sub reconstruct_xml {
122 0 ▶ my ($self, $strref) = @_;
Subroutine reconstruct_xml: not called CC=2 SCAR=17.9
123  
124 # Substituting PHP blocks
125  
126 0 ▶ for (my $i = 1; $i <= scalar(@{$self->{php_blocks}}); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= scalar @{$self->{"php_blocks"};}) 0 0 0
127 0 my $block = $self->{php_blocks}->[$i - 1];
128 0 $$strref =~ s/__PHP__BLOCK__($i)__/<\?$block\?>/;
129 }
130  
131 # Substituting symbolic entities
132  
133 0 $$strref =~ s/__HTML__ENTITY__(\w+?)__/&$1;/g;
134  
135 # Recreating DOCTYPE declaration
136  
137 0 $$strref =~ s/__DOCTYPE__(.*?)__END_DOCTYPE__/<!DOCTYPE$1>/sg;
138  
139 }
140  
141 sub get_current_file_rel {
142 0 ▶ my ($self) = @_;
Subroutine get_current_file_rel: not called CC=2 SCAR=17.9
143 0 ▶ return $self->{current_file_rel} || $self->{parent}->{engine}->{current_file_rel};
Condition0%0/3
$self->{'current_file_rel'} || $self->{'parent'}{'engine'}{'current_file_rel'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'current_file_rel'} || $self->{'parent'}{'engine'}{'current_file_rel'}
$self->{'current_file_rel'} $self->{'parent'}{'engine'}{'current_file_rel'}
Truth table0%0/3
A || B
A= $self->{'current_file_rel'}B= $self->{'parent'}{'engine'}{'current_file_rel'}
ABresult
1X1
011
000
144 }
145  
146 sub die_with_error {
147 0 ▶ my ($self, $error, $textref) = @_;
Subroutine die_with_error: not called CC=1 SCAR=6.9
148 0 my $start_pos = $-[0];
149 0 my $end_pos = $+[0];
150 0 my $around = 40;
151 0 my $s = substr($$textref, $start_pos-$around, $end_pos - $start_pos + $around * 2);
152 0 $s =~ s/\n/ /sg;
153 0 my $message = $error.":\n".
154 "$s\n".
155 ('-' x $around)."^\n";
156  
157 0 $self->{errors}->{$self->get_current_file_rel} = $message;
158 0 die $message;
159 }
160  
161 my $in_cdata;
162 my $in_tag;
163 my $in_attr;
164 sub fix_php_blocks {
165 0 ▶ my ($self, $s, $textref) = @_;
Subroutine fix_php_blocks: not called CC=18 SCAR=58.3
166  
167 #print $-[0].':'.$+[0]."\n";
168  
169 0 ▶ if ($s eq '<![CDATA[') {
Branch: 5 branches
BranchTrueFalseTotal
if ($s eq '<![CDATA[') { } 0 0 0
elsif ($s eq ']]>') { } 0 0 0
elsif ($s eq '<') { } 0 0 0
elsif ($s eq '>') { } 0 0 0
elsif ($s eq '"') { } 0 0 0
170 0 ▶ if ($in_cdata) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_cdata) { } 0 0 0
171 0 my $error = "Premature '<![CDATA['";
172 0 $self->die_with_error($error, $textref);
173 } else {
174 0 $in_cdata = 1;
175 }
176 } elsif ($s eq ']]>') {
177 0 ▶ if (!$in_cdata) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_cdata) { } 0 0 0
178 0 my $error = "Premature ']]>'";
179 0 $self->die_with_error($error, $textref);
180 } else {
181 0 $in_cdata = undef;
182 }
183 } elsif ($s eq '<') {
184 0 ▶ if (!$in_cdata) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_cdata) 0 0 0
185 0 ▶ if ($in_tag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_tag) { } 0 0 0
186 0 my $error = "Premature '<'";
187 0 $self->die_with_error($error, $textref);
188 } else {
189 0 $in_tag = 1;
190 }
191 }
192 } elsif ($s eq '>') {
193 0 ▶ if (!$in_cdata) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_cdata) 0 0 0
194 0 ▶ if (!$in_tag) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $in_tag) { } 0 0 0
elsif ($in_attr) { } 0 0 0
195 0 my $error = "Premature '>'";
196 0 $self->die_with_error($error, $textref);
197 } elsif ($in_attr) {
198 0 my $error = "Premature '>' before '\"'";
199 0 $self->die_with_error($error, $textref);
200 } else {
201 0 $in_tag = undef;
202 }
203 }
204 } elsif ($s eq '"') {
205 0 ▶ if (!$in_cdata && $in_tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_cdata and $in_tag) 0 0 0
Condition0%0/3
not $in_cdata and $in_tag
!ll&&!rl&&r
000
MC/DC0%0/2
not $in_cdata and $in_tag
not $in_cdata $in_tag
Truth table0%0/3
not(A) and B
A= not $in_cdataB= $in_tag
ABresult
0X0
100
111
206 0 $in_attr = !$in_attr;
207 }
208 } else { # PHP block
209 0 ▶ if (!$in_cdata && $in_tag && !$in_attr) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_cdata and $in_tag and not $in_attr) 0 0 0
Condition0%0/6
not $in_cdata and $in_tag and not $in_attr
!ll&&!rl&&r
000
not $in_cdata and $in_tag
!ll&&!rl&&r
000
MC/DC0%0/3
not $in_cdata and $in_tag and not $in_attr
not $in_cdata $in_tag not $in_attr
Truth table0%0/4
not(A) and B and not(C)
A= not $in_cdataB= $in_tagC= not $in_attr
ABCresult
0XX0
10X0
1100
1111
210 0 $s = " $s=\"\" ";
211 }
212 }
213  
214 0 return $s;
215 }
216  
217 sub parse {
218 my ($self, $textref, $callbackref, $lang) = @_;
219  
220 die 'callbackref not specified' unless $callbackref;
221  
222 # Making a copy of the string as we will change it
223  
224 my $text = $$textref;
225  
226 # Clearing the php_blocks array
227  
228 $self->{php_blocks} = [];
229  
230 # Replacing the '<?' and '?>' with different markers to make XML valid,
231 # and wrapping the PHP code into CDATA block
232  
233 $text =~ s/<\?(.*?)\?>/'__PHP__BLOCK__'.$self->add_php_block($1).'__'/sge;
234  
235 # Temporarily replacing DOCTYPE declaration
236  
237 $text =~ s/<\!DOCTYPE(.*?)>/'__DOCTYPE__'.$1.'__END_DOCTYPE__'/sge;
238  
239 # Replacing the symbolic entities as we are not going to expand them
240  
241 $text =~ s/&(\w+);/'__HTML__ENTITY__'.$1.'__'/ge;
242  
243 # Wrapping CDATA blocks inside special '__CDATA' tag
244  
245 $text =~ s/(<\!\[CDATA\[.*?\]\]>)/<__CDATA>$1<\/__CDATA>/sg;
246  
247 # Wrapping HTML comments inside special '__COMMENT' tag
248  
249 $text =~ s/<\!--(.*?)-->/<__COMMENT><\!\[CDATA\[$1\]\]><\/__COMMENT>/sg;
250  
251 # Now we should properly handle the situation when PHP blocks are inside the <...>
252 # This violates the XML rules, so we should dance around this, converting
253 # all '__PHP__BLOCK__#__' to ' __PHP__BLOCK__#__="" '. Yeah, weird.
254  
255 $in_cdata = undef;
256 $in_tag = undef;
257 $in_attr = undef;
258  
259 $text =~ s/(<\!\[CDATA\[|\]\]>|<|>|"|__PHP__BLOCK__\d+__)/$self->fix_php_blocks($1, \$text)/ge;
260  
261 # Extracting strings out of PHP blocks
262  
263 foreach my $block (@{$self->{php_blocks}}) {
264  
265 # Parsing the $pageTitle variable value
266 $block =~ s/(\$pageTitle = ")(.*?)(";)/$1.&$callbackref($self->expand_entities($2), undef, 'Page title', undef, $lang).$3/ge;
267  
268 # Parsing underscore functions
269  
270 $self->parse_underscore_functions(\$block, $callbackref, $lang);
271 }
272  
273 # Adding the dummy root tag for XML to be valid
274 # (no extra line breaks should be added around these dummy tags,
275 # as they will appear in the output before any opening PHP script blocks
276 # and may break sending headers from the script)
277  
278 $text = "<root>".$text."</root>";
279  
280 # Creating XML parser object
281  
282 1 ▶ use XML::Parser;
Subroutine BEGIN: called CC=1 SCAR=2.6
283 my $parser = new XML::Parser(Style => 'IxTree', ErrorContext => 4);
284  
285 # Parsing XML
286  
287 my $tree;
288 eval {
289 $tree = $parser->parse($text);
290 };
291 if ($@) {
292 my $error_text = $@;
293 $error_text =~ s/\t/ /g;
294 $error_text =~ s/^\s+//s;
295  
296 $self->{errors}->{$self->get_current_file_rel} = $error_text;
297 die $error_text;
298 }
299  
300 # Adding the empty attributes hash to the root tag (for uniform processing)
301  
302 unshift @$tree, {};
303  
304 # For dummy root tag, reset this tag name to empty value, so it won't get exported
305  
306 $tree->[1] = '';
307  
308 # Analyze all tags recursively to decide which ones require localization
309  
310 $self->analyze_tag_recursively('', $tree);
311  
312 # Now, in a second pass, export all localizable strings and generate the localized output
313  
314 my $out = $self->render_tag_recursively('', $tree, $callbackref, $lang);
315  
316 #TODO: substitute symbolic entities
317 #TODO: substitute PHP blocks
318  
319 #print "\n******************\n$out\n******************\n";
320  
321 return $lang ? $out : undef;
322 }
323  
324 sub parse_underscore_functions {
325 my ($self, $strref, $callbackref, $lang) = @_;
326  
327 # Looking for _("..."), __("...") and ___("...") calls and substituting them with static translated strings: "..."
328  
329 $$strref =~ s/(^|[^\w_])_{1,3}\("(.+?)"\)/$1.'"'._escape(&$callbackref($self->expand_entities(_unescape($2)), undef, undef, undef, $lang)).'"'/ge;
330  
331 # Same for _('...'), __('...') and ___('...')
332  
333 $$strref =~ s/(^|[^\w_])_{1,3}\('(.+?)'\)/$1.'"'._escape(&$callbackref($self->expand_entities(_unescape($2)), undef, undef, undef, $lang)).'"'/ge;
334 }
335  
336  
337 sub expand_entities {
338 my ($self, $s) = @_;
339  
340 if ($self->{data}->{expand_entities}) {
341 # preserve basic XML entities (to ensure final XML/HTML is still valid
342 $s =~ s/&(gt|lt|amp|quot);/\001$1\002/g;
343 $s =~ s/&nbsp;/__HTML__ENTITY__nbsp__/g;
344 # decode all numeric and named entities using HTML::Entities
345 $s = decode_entities($s, {nbsp => '&nbsp;'});
346 # restore basic XML entities back
347 $s =~ s/__HTML__ENTITY__nbsp__/&nbsp;/g;
348 $s =~ s/\001(gt|lt|amp|quot)\002/&$1;/g;
349 }
350  
351 return $s;
352 }
353  
354 sub _unescape {
355 my $s = shift;
356  
357 $s =~ s/\\"/"/g;
358 $s =~ s/\\'/'/g;
359  
360 return $s;
361 }
362  
363 sub _escape {
364 my $s = shift;
365  
366 $s =~ s/"/\\"/g;
367 #$s =~ s/'/\\'/g; # not needed, as output strings are enclosed in "..."
368  
369 return $s;
370 }
371  
372 sub analyze_tag_recursively {
373 my ($self, $name, $subtree, $prohibit_translation) = @_;
374 my $attrs = $subtree->[0];
375  
376 my $can_translate = undef;
377 my $will_translate = undef;
378 my $contains_translatables = undef;
379 my $prohibit_children_translation = undef;
380  
381 # By default, headings, paragraphs, labels, options, list items, definition terms and definition descriptions are translated;
382 # bare HTML (content of the root node) is also considered translated by default (provided there are no inner tags
383 # that override the segmentation)
384  
385 if ($name =~ m/^(?:|h[1-7]|p|li|dt|dd|label|option)$/) {
386 $can_translate = !$prohibit_translation;
387 }
388  
389 if (exists $attrs->{lang}) {
390 if ($attrs->{lang} eq 'en') {
391 $prohibit_translation = undef;
392 $will_translate = 1;
393 }
394  
395 if ($attrs->{lang} ne 'en') {
396 $prohibit_translation = 1;
397 $will_translate = undef;
398 }
399 }
400  
401 my $some_child_will_translate = undef;
402  
403 #print "[1][$name: proh=$prohibit_translation, can=$can_translate, some=$some_child_will_translate, will=$will_translate]\n" if $self->{parent}->{debug};
404  
405 if (!$will_translate) {
406 for (my $i = 0; $i < (scalar(@$subtree) - 1) / 2; $i++) {
407 my $tagname = $subtree->[1 + $i*2];
408 my $tagtree = $subtree->[1 + $i*2 + 1];
409  
410 if ($tagname ne '0') {
411 my ($child_will_translate, $child_contains_translatables) =
412 $self->analyze_tag_recursively($tagname, $tagtree, $prohibit_translation);
413 $contains_translatables = 1 if ($child_contains_translatables && !$child_will_translate);
414 $some_child_will_translate = 1 if $child_will_translate;
415 } else {
416 my $str = $tagtree; # this is a string for text nodes
417  
418 # Trim the string
419  
420 $str =~ s/^[\r\n\t ]+//sg;
421 $str =~ s/[\r\n\t ]+$//sg;
422  
423 # Only non-empty strings which do not contain php blocks can be translated by default
424  
425 $contains_translatables = 1 if $str ne '';
426 $prohibit_children_translation = 1 if $str =~ m/\b__PHP__BLOCK__(\d+)__\b/;
427  
428 #print "** [$name: can=$can_translate, ctr=$contains_translatables] [$str]\n" if $self->{parent}->{debug};
429 }
430 }
431 }
432  
433 $will_translate = 1 if $can_translate && $contains_translatables;
434 $will_translate = undef if $prohibit_translation or $prohibit_children_translation or $some_child_will_translate;
435  
436 #print "[2][$name: can=$can_translate, ctr=$contains_translatables, some=$some_child_will_translate, will=$will_translate]\n" if $self->{parent}->{debug};
437  
438 if ($will_translate) {
439 $attrs->{'.translate'} = 1;
440 }
441  
442 if ($prohibit_translation) {
443 $attrs->{'.prohibit'} = 1;
444 }
445  
446 return ($will_translate or $some_child_will_translate or $prohibit_translation or $prohibit_children_translation, $contains_translatables);
447 }
448  
449 sub render_tag_recursively {
450 my ($self, $name, $subtree, $callbackref, $lang, $prohibit, $cdata, $context) = @_;
451 my $attrs = $subtree->[0];
452  
453 my $translate = (exists $attrs->{'.translate'}) && (!exists $attrs->{'.prohibit'}) && !$prohibit;
454  
455 # if translation is prohibited for an entire subtree, or if the node is going to be translated
456 # as a whole, then prohibit translation of children
457 my $prohibit_children = $prohibit || $translate;
458  
459 $cdata = 1 if (($name eq '__CDATA') || ($name eq '__COMMENT'));
460  
461 # if context or hint attribute is defined, use that instead of current value, even if the new value is empty;
462 # for values that represent empty strings, use `undef`
463  
464 if (exists $attrs->{context}) {
465 $context = $attrs->{context} ne '' ? $attrs->{context} : undef;
466 }
467  
468 if (exists $attrs->{'data-l10n-context'}) {
469 $context = $attrs->{'data-l10n-context'} ne '' ? $attrs->{'data-l10n-context'} : undef;
470 }
471  
472 my $hint;
473  
474 if (exists $attrs->{hint}) {
475 $hint = $attrs->{hint} ne '' ? $attrs->{hint} : undef;
476 }
477  
478 if (exists $attrs->{'data-l10n-hint'}) {
479 $hint = $attrs->{'data-l10n-hint'} ne '' ? $attrs->{'data-l10n-hint'} : undef;
480 }
481  
482 my $inner_xml = '';
483  
484 my $subnodes_count = (scalar(@$subtree) - 1) / 2;
485 for (my $i = 0; $i < $subnodes_count; $i++) {
486 my $tagname = $subtree->[1 + $i*2];
487 my $tagtree = $subtree->[1 + $i*2 + 1];
488  
489 if ($tagname ne '0') {
490 # if we are going to translate this tag as a whole, then prohibit translation for the entire subtree
491 $inner_xml .= $self->render_tag_recursively($tagname, $tagtree, $callbackref, $lang, $prohibit_children, $cdata, $context);
492 } else {
493 # tagtree holds a string for text nodes
494  
495 my $str = $tagtree;
496  
497 # Escaping unsafe xml chars (excluding quotes)
498  
499 xml_escape_strref(\$str, 1) unless $cdata;
500  
501 # Reconstructing original XML with PHP blocks and symbolic entities
502  
503 $self->reconstruct_xml(\$str);
504  
505 # Add the string to a resulting xml
506  
507 $inner_xml .= $str;
508 }
509 }
510  
511 # Once the inner html is prepared, pass it through localizer if necessary
512 # (We do one exception for <object> tag which we extract as a whole)
513  
514 if ($translate && ($name ne 'object') && ($inner_xml ne '')) {
515 $inner_xml = &$callbackref($self->expand_entities($inner_xml), $context, $hint, undef, $lang);
516 }
517  
518 # If this is a <script> tag which is not a part of a translatable string,
519 # then extract _("...") style strings from it
520  
521 if (!$translate && ($name eq 'script')) {
522 $self->parse_underscore_functions(\$inner_xml, $callbackref, $lang);
523 }
524  
525 #print "::<$name>: $translate\n===\n$inner_xml\n===\n";
526  
527 # Determine if attributes require localization.
528 # This happens when this is not prohibited explicitly,
529 # and there is no explicit instruction to localize the tag
530 # or there is an explicit instruction to localize the non-terminal tag
531 # (as terminal localizable tags will be extracted later as a whole, with all attributes,
532 # so there is no need to extract attributes separately)
533  
534 my $e = exists $attrs->{'.translate'};
535 my $translate_attrs = (!exists $attrs->{'.prohibit'}) && !$prohibit && (!$e || ($e && $inner_xml));
536  
537 # Deleting temporary attributes and special 'lang' and 'context' attributes
538  
539 delete $attrs->{'.translate'};
540 delete $attrs->{'.prohibit'};
541 if (!$self->{leave_attrs}) {
542 delete $attrs->{lang};
543 delete $attrs->{context};
544 }
545  
546 # Adjusting <meta http-equiv="Content-Language" content="..." /> (if exists)
547 # to have the proper content value, e.g. "pt-br"
548  
549 if ((lc($name) eq 'meta') && (lc($attrs->{'http-equiv'}) eq 'content-language')) {
550 $attrs->{content} = $lang;
551 }
552  
553 # Generating the string consisting of [attr="value"] pairs
554  
555 my $locale = locale_from_lang($lang);
556 my $attrs_text;
557  
558 foreach my $key (keys %$attrs) {
559  
560 if ($key =~ m/^__PHP__BLOCK__\d+__$/) {
561  
562 # Reconstruct PHP block
563  
564 $self->reconstruct_xml(\$key);
565  
566 $attrs_text .= " $key";
567 } else {
568 my $val = $attrs->{$key};
569  
570 my $val_contains_php = ($val =~ m/__PHP__BLOCK__\d+__/);
571  
572 # Escaping unsafe xml chars
573  
574 xml_escape_strref(\$val);
575  
576 # Reconstructing original XML with PHP blocks and symbolic entities
577  
578 $self->reconstruct_xml(\$val);
579  
580 # Translate absolute in-site hrefs:
581 # /about/... -> /about/%LOCALE%/...
582  
583 # (!) disabled as this functionality is moved to the string replacement plugin
584  
585 #if (($name eq 'a') && ($key eq 'href')) {
586 # $val =~ s|^/about/(.*)$|/about/$locale/$1|;
587 #}
588  
589 my $can_translate_attr;
590  
591 # Localize `alt' and 'title' attributes if allowed (and if there are no php blocks inside)
592  
593 if ($translate_attrs
594 && ($key =~ m/^(alt|title)$/)) {
595 $can_translate_attr = 1;
596 }
597  
598 # Localize 'value' attribute for specific <input> tags
599  
600 if ($translate_attrs
601 && (lc($name) eq 'input')
602 && (lc($attrs->{type}) =~ m/^(text|search|email|submit|reset|button)$/)
603 && ($key eq 'value')) {
604 $can_translate_attr = 1;
605 }
606  
607 # Localize 'placeholder' attribute for <input> and <textarea> tags
608  
609 if ($translate_attrs
610 && (lc($name) =~ m/^(input|textarea)$/)
611 && ($key eq 'placeholder')) {
612 $can_translate_attr = 1;
613 }
614  
615 # do the translation if the value is not empty and doesn't contain php blocks
616  
617 if ($can_translate_attr && ($val ne '') && (!$val_contains_php)) {
618 $val = &$callbackref($self->expand_entities($val), $context, "$key attribute", undef, $lang);
619 }
620  
621 $attrs_text .= " $key=\"$val\"";
622 }
623 }
624  
625 # Construct and return the tag string with its inner xml
626  
627 if ($name ne '') {
628 my $xml;
629 if ($name eq '__CDATA') {
630 $xml = '<![CDATA['.$inner_xml.']]>';
631 } elsif ($name eq '__COMMENT') {
632 $xml = '<!--'.$inner_xml.'-->';
633 } else {
634 $xml = (($inner_xml ne '') || ($name =~ m/^(a|div|iframe|p|script|span|td|textarea|title|h\d)$/i)) ? "<$name$attrs_text>$inner_xml</$name>" : "<$name$attrs_text \/>";
635 }
636  
637 # If this is a terminal tag (or <object> tag) that requires localization, extract itself
638  
639 if ($translate && (($subnodes_count == 0) || ($name eq 'object'))) {
640 $xml = &$callbackref($self->expand_entities($xml), $context, undef, undef, $lang);
641 }
642 return $xml;
643 } else {
644 return $inner_xml; # for root tag, return just its inner contents
645 }
646 }
647  
648 1;
« lib/Serge/Engine/Plugin/control_commands.pm ↑ Summary lib/Serge/Engine/Plugin/parse_rc.pm »