Revision 36012e7e
Von Moritz Bunkus vor mehr als 15 Jahren hinzugefügt
SL/Template.pm | ||
---|---|---|
34 | 34 |
$self->{userspath} = shift; |
35 | 35 |
|
36 | 36 |
$self->{error} = undef; |
37 |
$self->{quot_re} = '"'; |
|
37 | 38 |
|
38 | 39 |
$self->set_tag_style('<%', '%>'); |
39 | 40 |
} |
... | ... | |
120 | 121 |
return $text; |
121 | 122 |
} |
122 | 123 |
|
124 |
sub _parse_block_if { |
|
125 |
$main::lxdebug->enter_sub(); |
|
126 |
|
|
127 |
my $self = shift; |
|
128 |
my $contents = shift; |
|
129 |
my $new_contents = shift; |
|
130 |
my $pos_if = shift; |
|
131 |
my @indices = @_; |
|
132 |
|
|
133 |
$$new_contents .= $self->substitute_vars(substr($$contents, 0, $pos_if), @indices); |
|
134 |
substr($$contents, 0, $pos_if) = ""; |
|
135 |
|
|
136 |
if ($$contents !~ m/^$self->{tag_start_qm}if |
|
137 |
\s* |
|
138 |
(not\b|\!)? # $1 -- Eventuelle Negierung |
|
139 |
\s+ |
|
140 |
(\b.+?\b) # $2 -- Name der zu ?berpr?fenden Variablen |
|
141 |
( # $3 -- Beginn des optionalen Vergleiches |
|
142 |
\s* |
|
143 |
([!=]) # $4 -- Negierung des Vergleiches speichern |
|
144 |
([=~]) # $5 -- Art des Vergleiches speichern |
|
145 |
\s* |
|
146 |
( # $6 -- Gequoteter String oder Bareword |
|
147 |
$self->{quot_re} |
|
148 |
(.*?)(?<!\\) # $7 -- Gequoteter String -- direkter Vergleich mit eq bzw. ne oder Patternmatching; Escapete Anf?hrungs als Teil des Strings belassen |
|
149 |
$self->{quot_re} |
|
150 |
| |
|
151 |
(\b.+?\b) # $8 -- Bareword -- als Index f?r $form benutzen |
|
152 |
) |
|
153 |
)? |
|
154 |
\s* |
|
155 |
$self->{tag_end_qm} |
|
156 |
/x) { |
|
157 |
$self->{"error"} = "Malformed $self->{tag_start}if$self->{tag_end}."; |
|
158 |
$main::lxdebug->leave_sub(); |
|
159 |
return undef; |
|
160 |
} |
|
161 |
|
|
162 |
my $not = $1; |
|
163 |
my $var = $2; |
|
164 |
my $operator_neg = $4; # '=' oder '!' oder undef, wenn kein Vergleich erkannt |
|
165 |
my $operator_type = $5; # '=' oder '~' f?r Stringvergleich oder Regex |
|
166 |
my $quoted_word = $7; # nur g?ltig, wenn quoted string angegeben (siehe unten); dann "value" aus <%if var == "value" %> |
|
167 |
my $bareword = $8; # undef, falls quoted string angegeben wurde; andernfalls "othervar" aus <%if var == othervar %> |
|
168 |
|
|
169 |
$not = !$not if ($operator_neg && $operator_neg eq '!'); |
|
170 |
|
|
171 |
substr($$contents, 0, length($&)) = ""; |
|
172 |
|
|
173 |
my $block; |
|
174 |
($block, $$contents) = $self->find_end($$contents, 0, $var, $not); |
|
175 |
if (!$block) { |
|
176 |
$self->{"error"} = "Unclosed $self->{tag_start}if$self->{tag_end}." unless ($self->{"error"}); |
|
177 |
$main::lxdebug->leave_sub(); |
|
178 |
return undef; |
|
179 |
} |
|
180 |
|
|
181 |
my $value = $self->_get_loop_variable($var, 0, @indices); |
|
182 |
my $hit = 0; |
|
183 |
|
|
184 |
if ($operator_type) { |
|
185 |
my $compare_to = $bareword ? $self->_get_loop_variable($bareword, 0, @indices) : $quoted_word; |
|
186 |
if ($operator_type eq '=') { |
|
187 |
$hit = ($not && !($value eq $compare_to)) || (!$not && ($value eq $compare_to)); |
|
188 |
} else { |
|
189 |
$hit = ($not && !($value =~ m/$compare_to/i)) || (!$not && ($value =~ m/$compare_to/i)); |
|
190 |
} |
|
191 |
|
|
192 |
} else { |
|
193 |
$hit = ($not && ! $value) || (!$not && $value); |
|
194 |
} |
|
195 |
|
|
196 |
if ($hit) { |
|
197 |
my $new_text = $self->parse_block($block, @indices); |
|
198 |
if (!defined($new_text)) { |
|
199 |
$main::lxdebug->leave_sub(); |
|
200 |
return undef; |
|
201 |
} |
|
202 |
$$new_contents .= $new_text; |
|
203 |
} |
|
204 |
|
|
205 |
$main::lxdebug->leave_sub(); |
|
206 |
|
|
207 |
return 1; |
|
208 |
} |
|
209 |
|
|
123 | 210 |
1; |
124 | 211 |
|
125 | 212 |
#### |
... | ... | |
333 | 420 |
$new_contents .= $new_text; |
334 | 421 |
|
335 | 422 |
} else { |
336 |
$new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices); |
|
337 |
substr($contents, 0, $pos_if) = ""; |
|
338 |
|
|
339 |
if ($contents !~ m/^$self->{tag_start_qm}if |
|
340 |
\s* |
|
341 |
(not\b|\!)? # $1 -- Eventuelle Negierung |
|
342 |
\s+ |
|
343 |
(\b.+?\b) # $2 -- Name der zu ?berpr?fenden Variablen |
|
344 |
( # $3 -- Beginn des optionalen Vergleiches |
|
345 |
\s* |
|
346 |
([!=]) # $4 -- Negierung des Vergleiches speichern |
|
347 |
([=~]) # $5 -- Art des Vergleiches speichern |
|
348 |
\s* |
|
349 |
( # $6 -- Gequoteter String oder Bareword |
|
350 |
"(.*?)(?<!\\)" # $7 -- Gequoteter String -- direkter Vergleich mit eq bzw. ne oder Patternmatching; Escapete Anf?hrungs als Teil des Strings belassen |
|
351 |
| |
|
352 |
(\b.+?\b) # $8 -- Bareword -- als Index f?r $form benutzen |
|
353 |
) |
|
354 |
)? |
|
355 |
\s* |
|
356 |
$self->{tag_end_qm} |
|
357 |
/x) { |
|
358 |
$self->{"error"} = "Malformed $self->{tag_start}if$self->{tag_end}."; |
|
423 |
if (!$self->_parse_block_if(\$contents, \$new_contents, $pos_if, @indices)) { |
|
359 | 424 |
$main::lxdebug->leave_sub(); |
360 | 425 |
return undef; |
361 | 426 |
} |
362 |
|
|
363 |
my $not = $1; |
|
364 |
my $var = $2; |
|
365 |
my $operator_neg = $4; # '=' oder '!' oder undef, wenn kein Vergleich erkannt |
|
366 |
my $operator_type = $5; # '=' oder '~' f?r Stringvergleich oder Regex |
|
367 |
my $quoted_word = $7; # nur g?ltig, wenn quoted string angegeben (siehe unten); dann "value" aus <%if var == "value" %> |
|
368 |
my $bareword = $8; # undef, falls quoted string angegeben wurde; andernfalls "othervar" aus <%if var == othervar %> |
|
369 |
|
|
370 |
$not = !$not if ($operator_neg && $operator_neg eq '!'); |
|
371 |
|
|
372 |
substr($contents, 0, length($&)) = ""; |
|
373 |
|
|
374 |
($block, $contents) = $self->find_end($contents, 0, $var, $not); |
|
375 |
if (!$block) { |
|
376 |
$self->{"error"} = "Unclosed $self->{tag_start}if${not}$self->{tag_end}." unless ($self->{"error"}); |
|
377 |
$main::lxdebug->leave_sub(); |
|
378 |
return undef; |
|
379 |
} |
|
380 |
|
|
381 |
my $value = $self->_get_loop_variable($var, 0, @indices); |
|
382 |
my $hit = 0; |
|
383 |
|
|
384 |
if ($operator_type) { |
|
385 |
my $compare_to = $bareword ? $self->_get_loop_variable($bareword, 0, @indices) : $quoted_word; |
|
386 |
if ($operator_type eq '=') { |
|
387 |
$hit = ($not && !($value eq $compare_to)) || (!$not && ($value eq $compare_to)); |
|
388 |
} else { |
|
389 |
$hit = ($not && !($value =~ m/$compare_to/i)) || (!$not && ($value =~ m/$compare_to/i)); |
|
390 |
} |
|
391 |
|
|
392 |
} else { |
|
393 |
$hit = ($not && ! $value) || (!$not && $value); |
|
394 |
} |
|
395 |
|
|
396 |
if ($hit) { |
|
397 |
my $new_text = $self->parse_block($block, @indices); |
|
398 |
if (!defined($new_text)) { |
|
399 |
$main::lxdebug->leave_sub(); |
|
400 |
return undef; |
|
401 |
} |
|
402 |
$new_contents .= $new_text; |
|
403 |
} |
|
404 | 427 |
} |
405 | 428 |
} |
406 | 429 |
|
... | ... | |
825 | 848 |
$self->{"iconv"} = Text::Iconv->new($main::dbcharset, "UTF-8"); |
826 | 849 |
|
827 | 850 |
$self->set_tag_style('<%', '%>'); |
851 |
$self->{quot_re} = '"'; |
|
828 | 852 |
|
829 | 853 |
return $self; |
830 | 854 |
} |
... | ... | |
986 | 1010 |
$new_contents .= $new_text; |
987 | 1011 |
|
988 | 1012 |
} else { |
989 |
$new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices); |
|
990 |
substr($contents, 0, $pos_if) = ""; |
|
991 |
|
|
992 |
if ($contents !~ m|^\<\%if\s*(not)?\s+(.*?)\%\>|) { |
|
993 |
$self->{"error"} = "Malformed <\%if\%>."; |
|
1013 |
if (!$self->_parse_block_if(\$contents, \$new_contents, $pos_if, @indices)) { |
|
994 | 1014 |
$main::lxdebug->leave_sub(); |
995 | 1015 |
return undef; |
996 | 1016 |
} |
997 |
|
|
998 |
my ($not, $var) = ($1, $2); |
|
999 |
|
|
1000 |
substr($contents, 0, length($&)) = ""; |
|
1001 |
|
|
1002 |
($block, $contents) = $self->find_end($contents, 0, $var, $not); |
|
1003 |
if (!$block) { |
|
1004 |
$self->{"error"} = "Unclosed <\%if${not}\%>." unless ($self->{"error"}); |
|
1005 |
$main::lxdebug->leave_sub(); |
|
1006 |
return undef; |
|
1007 |
} |
|
1008 |
|
|
1009 |
my $value = $self->{"form"}->{$var}; |
|
1010 |
for (my $i = 0; $i < scalar(@indices); $i++) { |
|
1011 |
last unless (ref($value) eq "ARRAY"); |
|
1012 |
$value = $value->[$indices[$i]]; |
|
1013 |
} |
|
1014 |
|
|
1015 |
if (($not && !$value) || (!$not && $value)) { |
|
1016 |
my $new_text = $self->parse_block($block, @indices); |
|
1017 |
if (!defined($new_text)) { |
|
1018 |
$main::lxdebug->leave_sub(); |
|
1019 |
return undef; |
|
1020 |
} |
|
1021 |
$new_contents .= $new_text; |
|
1022 |
} |
|
1023 | 1017 |
} |
1024 | 1018 |
} |
1025 | 1019 |
} |
Auch abrufbar als: Unified diff
Diverse Arbeiten an <%if%>-Blöcken.
1. Bugfix: <%if%>
Blöcke haben in OpenDocument-Vorlagen nach der>{TEMPLATE_ARRAYS} nichtUmstellung auf die Verwendung von $form
mehr funktioniert.
2. Feautres: <%if%>-Blöcke können nun auch in OpenDocument die
erweiterten Vergleiche benutzen.
3. Refactoring: Das Parsen der <%if%>-Blöcke wurde für alle
Vorlagentypen in gemeinsame Unterfunktion ausgelagert.