Quiq/Template.pm

filter: Statementstmt 62.8% 127 / 202 Branchbran 45.6% 42 / 92 Conditioncond 38.8% 7 / 18 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 90.4% 19 / 21 Podpod 90.9% 10 / 11 totaltotal 58.3% 207 / 355 CC 53 SCAR 37.4
CC
53
Cov
56%
removeOptional_5_10
182.0
removeOptional
26.3
replace
14.8
CRAP 42.3
« Quiq/Zugferd/Tree.pm ↑ Summary Quiq/Gd/Component/BlockDiagram.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Template - Klasse für HTML/XML/Text-Generierung
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 EXAMPLE
14  
15 Template-Datei C<test.html> oder Template-String C<$str>:
16  
17 <html>
18 <head>
19 <title>__TITLE__</title>
20 </head>
21 <body>
22 __BODY__
23 </body>
24  
25 Code:
26  
27 $tpl = Quiq::Template->new('html','test.html');
28 -oder-
29 $tpl = Quiq::Template->new('html',\$str);
30
31 $tpl->replace(
32 __TITLE__ => 'Testseite',
33 __BODY__ => 'Hello World!',
34 );
35 $str = $tpl->asString;
36  
37 Resultat C<$str>:
38  
39 <html>
40 <head>
41 <title>Testseite</title>
42 </head>
43 <body>
44 Hello World!
45 </body>
46  
47 =cut
48  
49 # -----------------------------------------------------------------------------
50  
51 package Quiq::Template;
52 51 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 51 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 51 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 51 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 51 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 51 our $VERSION = '1.240';
60  
61 51 ▶use Quiq::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 51 ▶use Quiq::Option;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 51 ▶use Quiq::Unindent;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 51 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
65 51 ▶use Quiq::Reference;
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 # -----------------------------------------------------------------------------
68  
69 =head1 METHODS
70  
71 =head2 Konstruktor
72  
73 =head3 new() - Instantiiere Template-Objekt
74  
75 =head4 Synopsis
76  
77 $tpl = Quiq::Template->new($type,$file,@opt);
78 $tpl = Quiq::Template->new($type,\$str,@opt);
79  
80 =head4 Options
81  
82 =over 4
83  
84 =item -lineContinuation => $type (Default: undef)
85  
86 Art der Zeilenfortsetzung. Mögliche Werte:
87  
88 =over 4
89  
90 =item undef
91  
92 Keine Zeilenfortsetzung.
93  
94 =item 'backslash'
95  
96 Endet eine Zeile mit einem Backslash, entferne Whitespace am
97 Anfang der Folgezeile und füge den Rest zur Zeile hinzu.
98  
99 Dies kann für eine Zeile unterdrückt werden, indem der Backslash am
100 Ende der Zeile durch einen davorgestellten Backslash maskiert wird.
101 In dem Fall wird statt einer Fortsetzung der Zeile der maskierende
102 Backslash entfernt.
103  
104 Diese Option ist nützlich, wenn ein Template-Text im Editor auf
105 eine bestimmte Breite (z.B. 80 Zeichen/Zeile) begrenzt sein soll,
106 aber der generierte Text breiter sein darf.
107  
108 =back
109  
110 =item -singleReplace => $bool (Default: 0)
111  
112 Ersetze bei replace() immer nur den ersten von mehreren identischen
113 Platzhaltern. Dies ist z.B. in HTML bei Ersetzung von mehreren
114 Checkboxen mit gleichem Namen nützlich.
115  
116 =back
117  
118 =head4 Description
119  
120 Instantiiere ein Template vom Typ $type aus Datei $file oder String $str
121 und liefere eine Referenz auf dieses Objekt zurück.
122  
123 Template-Typen:
124  
125 =over 4
126  
127 =item 'xml'
128  
129 XML-Template. Metazeichen &, < und > in Werten werden durch
130 Entities ersetzt.
131  
132 =item 'html'
133  
134 HTML-Template. Metazeichen &, < und > in Werten werden durch
135 Entities ersetzt.
136  
137 =item 'text'
138  
139 Text-Template. Werte werden unverändert eingesetzt.
140  
141 =back
142  
143 =cut
144  
145 # -----------------------------------------------------------------------------
146  
147 11 ▶sub new {
Subroutine new: called CC=4 SCAR=14.8
Pod documented
148 11 my $class = shift;
149 11 my $type = shift;
150 11 my $arg = shift;
151 # @_: @opt
152  
153 # Optionen
154  
155 11 my $lineContinuation = undef;
156 11 my $singleReplace = 0;
157  
158 11 Quiq::Option->extract(\@_,
159 -lineContinuation => \$lineContinuation,
160 -singleReplace => \$singleReplace,
161 );
162  
163 # Operation ausführen
164  
165 11 ▶ my $str = ref $arg? $$arg: Quiq::Path->read($arg);
Branch: 1 branch
BranchTrueFalseTotal
ref $arg ? : 10 1 11
166 11 $str =~ s/\s+$//; # WS am Ende entfernen
167  
168 11 ▶ if ($lineContinuation) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lineContinuation) 0 11 11
169 0 ▶ if ($lineContinuation eq 'backslash') {
Branch: 1 branch
BranchTrueFalseTotal
if ($lineContinuation eq 'backslash') { } 0 0 0
170 0 $str =~ s/(?<!\\)\\\n[ \t]*//g;
171 }
172 else {
173 0 $class->throw(
174 'TEMPLATE-00001: Ungüliger Wert für Option -lineContinuation',
175 Value => $lineContinuation,
176 );
177 }
178 }
179  
180 11 my $self = $class->SUPER::new(
181 type => $type,
182 string => $str,
183 protect => 1,
184 singleReplace => $singleReplace,
185 );
186 11 $self->set(@_);
187  
188 11 return $self;
189 }
190  
191 # -----------------------------------------------------------------------------
192  
193 =head2 Klassenmethoden
194  
195 =head3 combine() - Ersetze Platzhalter in Text
196  
197 =head4 Synopsis
198  
199 $str = $class->combine(@argVal);
200  
201 =head4 Arguments
202  
203 =over 4
204  
205 =item placeholders => \@keyVal (Default: [])
206  
207 Liste von Platzhalter/Wert-Paaren.
208  
209 =item template => $text (Default: '')
210  
211 Text mit Platzhaltern.
212  
213 =back
214  
215 =head4 Description
216  
217 Ersetze in Template $text die Platzhalter durch die Werte aus
218 @keyVal und liefere den resultierenden Text zurück. Die Methode
219 ist eine Vereinfachung, sie instantiiert intern ein Template-Objekt,
220 wendet darauf die Methode replace() an und liefert den
221 resultierenden Text zurück.
222  
223 Eine Tilde (~) kann als Zeilenfortsetzungszeichen im
224 Template verwendet werden.
225  
226 =head4 Example
227  
228 $js = Quiq::Template->combine(
229 placeholders => [
230 __NAME__ => $name,
231 __CONFIG__ => $config,
232 ],
233 template => q~
234 Chart.defaults.global.defaultFontSize = 12;
235 Chart.defaults.global.animation.duration = 1000;
236 var __NAME__ = new Chart('__NAME__',__CONFIG__);
237 ~);
238  
239 =cut
240  
241 # -----------------------------------------------------------------------------
242  
243 3 ▶sub combine {
Subroutine combine: called CC=1 SCAR=0.0
Pod documented
244 3 my $class = shift;
245 # @_: @keyVal
246  
247 3 my $placeholderA = [];
248 3 my $template = '';
249  
250 3 $class->parameters(\@_,
251 placeholders => \$placeholderA,
252 template => \$template,
253 );
254  
255 3 $template = Quiq::Unindent->string('~',$template);
256 3 my $tpl = $class->new('text',\$template);
257 3 $tpl->replace(@$placeholderA);
258  
259 3 return $tpl->asStringNL;
260 }
261  
262 # -----------------------------------------------------------------------------
263  
264 =head2 Objektmethoden
265  
266 =head3 placeholders() - Liefere Liste der Platzhalter
267  
268 =head4 Synopsis
269  
270 @arr | $arr = $tpl->placeholders;
271  
272 =cut
273  
274 # -----------------------------------------------------------------------------
275  
276 1 ▶sub placeholders {
Subroutine placeholders: called CC=2 SCAR=7.1
Pod documented
277 1 my $self = shift;
278 1 my @arr = $self->{'string'} =~ /(__\w+?__)/g;
279 1 ▶ return wantarray? @arr: \@arr;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
280 }
281  
282 # -----------------------------------------------------------------------------
283  
284 =head3 replace() - Ersetze Platzhalter
285  
286 =head4 Synopsis
287  
288 $tpl = $tpl->replace(@keyVal);
289  
290 =head4 Returns
291  
292 Referenz auf das Template-Objekt (für Method-Chaining)
293  
294 =head4 Description
295  
296 Ersetze alle Platzhalter durch ihre Werte. Platzhalter und
297 Werte werden als Paare @keyVal übergeben.
298  
299 Der Wert kann ein String, eine Arrayrefernz, eine Codereferenz oder
300 ein Template-Objekt sein. Siehe Methode L<value|"value() - Liefere Platzhalter-Wert als Zeichenkette">().
301  
302 Es wird für jeden Platzhalter mit einem Wert ungleich C<undef> geprüft,
303 ob dieser im Template vorkommt. Wenn nicht, wird eine Exception geworfen.
304  
305 =head4 Example
306  
307 Subroutine liefert Platzhalter-Wert:
308  
309 my $tpl = Quiq::Template->new('xml',\$Order);
310 $tpl->replace(
311 __CUSTNR__ => $kundenNr,
312 __LIEFERNAME__ => $vor->{'liefername'},
313 __LIEFERSTRASSE__ => $lieferstrasse,
314 __LIEFERHAUSNR__ => $lieferhausnr,
315 __LIEFERPLZ__ => $vor->{'lieferplz'},
316 __LIEFERORT__ => $vor->{'lieferort'},
317 __LIEFERLAND_ISO__ => $vor->{'lieferland_iso'},
318 __BESTELLDATUM__ => POSIX::strftime('%Y-%m',localtime),
319 __BESTELLNUMMER__ => $vor->{'vorgang_bestellnummer'},
320 __WAEHRUNG__ => $waehrung,
321 __ENVIRONMENT__ => $test? 'T': 'L',
322 __ORDERLINES__ => sub {
323 my @arr;
324 my $i = 0;
325 for my $pos (@$posA) {
326 my $tpl = Quiq::Template->new('xml',\$OrderLine);
327 $tpl->replace(
328 __I__ => $i++,
329 __LIEFERNR__ => $pos->{'posten_liefernr'},
330 __ARTBE__ => $pos->{'posten_artbe'},
331 __ANZAHL__ => $pos->{'posten_anzahl'},
332 __EPREIS__ => $pos->{'posten_epreis'},
333 );
334 push @arr,$tpl;
335 }
336 return \@arr;
337 },
338 );
339  
340 Die Subroutine, die den Wert des Platzhalters __ORDERLINES__ berechnet,
341 liefert keinen String, sondern eine Referenz auf ein Array von
342 Template-Objekten. Wie jeder Platzhalterwert wird dieser von der
343 Methode $tpl->L<value|"value() - Liefere Platzhalter-Wert als Zeichenkette">() in einen String (oder C<undef>) umgesetzt.
344  
345 =cut
346  
347 # -----------------------------------------------------------------------------
348  
349 9 ▶sub replace {
Subroutine replace: called CC=12 SCAR=26.9
Pod documented
350 9 my $self = shift;
351 # @_: @keyVal
352  
353 9 my $sloppy = 0;
354 9 my $singleReplace = $self->{'singleReplace'};
355  
356 9 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 20 8 28
357 20 my $key = $self->key(shift);
358 20 my $val = $self->value(shift); # MEMO: $val ist undef oder ein String
359  
360 20 ▶ if (!defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 0 20 20
361 # Wenn undef, keine Ersetzung. Die Existenz
362 # des Platzhalters wird nicht geprüft.
363 0 next;
364 }
365
366 # wir entfernen Newlines am Ende (evtl. Option hierfür einführen)
367 20 $val =~ s/\n+$//;
368
369 20 my $exists = 0; # Zeigt an, ob Platzhalter im Template vorkommt
370  
371 20 ▶ if ($val =~ tr/\n//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ tr/\n//) { } 6 14 20
372 # Ist der Wert mehrzeilig, gehen wir jede einzelne Fundstelle
373 # durch und rücken jede Zeile des Werts so weit ein wie die
374 # Zeile, in der der Platzhalter vorkommt, eingerückt ist.
375  
376 6 while (1) {
377 # vor 2019-12-08
378 # if ($self->{'string'} !~ /(^[ \t]*)?\Q$key/m) {
379 12 ▶ if ($self->{'string'} !~ /^([ \t]*).*\Q$key/m) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"string"} =~ /^([ \t]*).*\Q$key\E/m) 6 6 12
380 # Ende: Key kommt nicht mehr vor
381 6 last;
382 }
383 6 $exists++;
384  
385 # Wert einrücken
386  
387 6 my $indVal = $val;
388 6 ▶ if ($1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1) 0 6 6
389 0 my $ind = $1;
390 0 $indVal =~ s/^/$ind/mg;
391 0 $indVal =~ s/^$ind//; # Einrückung erste Zeile entfernen
392 }
393  
394 # Platzhalter durch eingerückten Wert ersetzen
395  
396 6 $self->{'string'} =~ s/\Q$key/$indVal/;
397  
398 6 ▶ if ($singleReplace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($singleReplace) 0 6 6
399 # wir führen nur eine Ersetzung durch
400 0 last;
401 }
402 }
403 }
404 else {
405 # Wert ist einzeilig. Wir ersetzen den Platzhalter global,
406 # egal wo er steht.
407  
408 14 ▶ if ($val eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($val eq "") 0 14 14
409 # Steht der Platzhalter allein auf einer Zeile mit
410 # Leerzeilen davor und dahinter, entfernen wir zusätzlich
411 # zum Platzghalter alle folgenden Leerzeilen
412  
413 0 ▶ if ($singleReplace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($singleReplace) { } 0 0 0
414 # wir führen nur eine Ersetzung durch
415 0 $exists += $self->{'string'} =~
416 s/(\n{2,})\Q$key\E\n{2,}/$1/m;
417 }
418 else {
419 0 $exists += $self->{'string'} =~
420 s/(\n{2,})\Q$key\E\n{2,}/$1/mg;
421 }
422 }
423  
424 14 ▶ if ($singleReplace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($singleReplace) { } 5 9 14
425 # wir führen nur eine Ersetzung durch
426 5 $exists += $self->{'string'} =~ s/\Q$key/$val/;
427 }
428 else {
429 9 $exists += $self->{'string'} =~ s/\Q$key/$val/g;
430 }
431 }
432  
433 # Exception, wenn Platzhalter (mit gesetztem Wert) nicht existiert
434  
435 20 ▶ if (!$exists && !$sloppy) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exists or $sloppy) 1 19 20
Condition66%2/3
$exists or $sloppy
l!l&&r!l&&!r
1901
MC/DC50%1/2
$exists or $sloppy
$exists $sloppy
Truth table66%2/3
A or B
A= $existsB= $sloppy
ABresult
1X1
011
000
436 #my $str = $self->{'string'};
437
438 #if ($self->{'type'} eq 'html') {
439 # $str =~ s/&/&amp;/g;
440 # $str =~ s/</&lt;/g;
441 # $str =~ s/>/&gt;/g;
442 #}
443
444 1 $self->throw(
445 'TMPL-00001: Platzhalter existiert nicht',
446 # Template => $str,
447 Placeholder => $key,
448 );
449 }
450 }
451  
452 8 return $self;
453 }
454  
455 # -----------------------------------------------------------------------------
456  
457 =head3 key() - Liefere Schlüssel und Ersetzungsattribut(e)
458  
459 =head4 Synopsis
460  
461 $key = $tpl->key($arg);
462 ($key,@attr) = $tpl->key($arg);
463  
464 =head4 Description
465  
466 Als Schlüssel $arg kann
467  
468 =over 4
469  
470 =item 1.
471  
472 ein String,
473  
474 =item 2.
475  
476 eine Stringreferenz
477  
478 =item 3.
479  
480 eine Arrayreferenz
481  
482 =back
483  
484 angegeben sein.
485  
486 MEMO: Die Methode ist so gestaltet, dass (weitere) Einsetzungsattribute
487 definiert werden können, wenn als Platzhalter eine Arrayreferenz
488 übergeben wird, z.B.
489  
490 ['__ITEMS__',call=>sub {...}]
491  
492 Die Ersetzungsattribute werden im Arraykontext neben dem Schlüssel
493 zurückgegeben und können in replace() den Ersetzungsvorgang in
494 spezieller Weise steuern. Dies wird aber noch nicht genutzt.
495  
496 =cut
497  
498 # -----------------------------------------------------------------------------
499  
500 20 ▶sub key {
Subroutine key: called CC=3 SCAR=16.0
Pod documented
501 20 my ($self,$arg) = @_;
502  
503 20 ▶ if (ref $arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg) 0 20 20
504 0 ▶ if (Quiq::Reference->isArrayRef($arg)) {
Branch: 1 branch
BranchTrueFalseTotal
if ('Quiq::Reference'->isArrayRef($arg)) { } 0 0 0
505 0 return $arg->[0];
506 }
507 else {
508 0 return $$arg;
509 }
510 }
511  
512 20 return $arg;
513 }
514  
515 # -----------------------------------------------------------------------------
516  
517 =head3 value() - Liefere Platzhalter-Wert als Zeichenkette
518  
519 =head4 Synopsis
520  
521 $str = $tpl->value($arg);
522  
523 =head4 Description
524  
525 Die Methode liefert die Zeichenkette $str zum (bei L<replace|"replace() - Ersetze Platzhalter">()
526 angegebenen) Platzhalter-Wert $arg.
527  
528 =over 4
529  
530 =item String:
531  
532 Liefere String.
533  
534 =item Arrayreferenz:
535  
536 Ermittele die Zeichenketten-Werte aller Array-Elemente,
537 konkateniere diese mit "\n" und liefere das Resultat zurück.
538  
539 =item Corereferenz:
540  
541 Rufe Subroutine auf und liefere den Zeichenketten-Wert des
542 Returnwerts.
543  
544 =item Template-Objekt:
545  
546 Rufe Methode L<asString|"asString() - Liefere Inhalt">() des Objekts auf und liefere
547 das Resultat zurück.
548  
549 =back
550  
551 =cut
552  
553 # -----------------------------------------------------------------------------
554  
555 21 ▶sub value {
Subroutine value: called CC=10 SCAR=25.0
Pod documented
556 21 my ($self,$arg) = @_;
557  
558 21 ▶ if (!ref $arg) {
Branch: 4 branches
BranchTrueFalseTotal
if (not ref $arg) { } 19 2 21
elsif (builtin::blessed($arg) and $arg->isa('Quiq::Template')) { } 0 2 2
elsif ('Quiq::Reference'->isArrayRef($arg)) { } 1 1 2
elsif ('Quiq::Reference'->isCodeRef($arg)) { } 0 1 1
559 # Zeichenkette -> liefern
560  
561 19 ▶ if ($self->{'protect'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"protect"}) 19 0 19
562 19 my $type = $self->{'type'};
563  
564 19 ▶ if (defined($arg) && ($type eq 'xml' || $type eq 'html')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg and $type eq "xml" || $type eq "html") 2 17 19
Condition66%4/6
defined $arg and $type eq "xml" || $type eq "html"
!ll&&!rl&&r
0172
$type eq "xml" or $type eq "html"
l!l&&r!l&&!r
0217
MC/DC33%1/3
defined $arg and $type eq "xml" || $type eq "html"
defined $arg $type eq "xml" $type eq "html"
Truth table50%2/4
A and B or C
A= defined $argB= $type eq "xml"C= $type eq "html"
ABCresult
0XX0
1000
11X1
1011
565 # Metazeichen schützen
566 2 $arg =~ s/&/&amp;/g;
567 2 $arg =~ s/</&lt;/g;
568 2 $arg =~ s/>/&gt;/g;
569 }
570 }
571  
572 19 return $arg;
573 }
574 ▶ elsif (Scalar::Util::blessed($arg) && $arg->isa('Quiq::Template')) {
Condition33%1/3
builtin::blessed($arg) and $arg->isa("Quiq::Template")
!ll&&!rl&&r
200
MC/DC0%0/2
builtin::blessed($arg) and $arg->isa("Quiq::Template")
builtin::blessed($arg) $arg->isa("Quiq::Template")
Truth table33%1/3
A and B
A= builtin::blessed($arg)B= $arg->isa("Quiq::Template")
ABresult
0X0
100
111
575 # Referenz auf Template-Objekt -> asString() aufrufen
576 0 return $arg->asString;
577 }
578 elsif (Quiq::Reference->isArrayRef($arg)) {
579 # Array-Referenz -> Werte mit "\n" konkatenieren
580 1 return join "\n",map {$self->value($_)} @$arg;
581 }
582 elsif (Quiq::Reference->isCodeRef($arg)) {
583 # Subroutine-Referenz -> Wert berechnen
584 0 return $self->value($arg->());
585 }
586 else {
587 # Stringreferenz: Wir liefern den Wert unverändert
588 1 return $$arg;
589 }
590  
591 0 $self->throw;
592 }
593  
594 # -----------------------------------------------------------------------------
595  
596 =head3 removePlaceholders() - Entferne unaufgelöste Platzhalter
597  
598 =head4 Synopsis
599  
600 $tpl->removePlaceholders;
601  
602 =head4 Description
603  
604 Entferne alle Attribute und Tags, die unaufgelöste Platzhalter der
605 Form "__...__" enthalten.
606  
607 =cut
608  
609 # -----------------------------------------------------------------------------
610  
611 0 ▶sub removePlaceholders {
Subroutine removePlaceholders: not called CC=1 SCAR=6.9
Pod documented
612 0 my $self = shift;
613  
614 # Kopie, auf der wir ersetzen
615 0 my $str = $self->{'string'};
616  
617 # Entferne Attribute mit unaufgelösten Platzhaltern
618 0 $str =~ s/\s\w+="__.*?__"//g;
619  
620 # Entferne Tags mit unaufgelösten Platzhaltern
621 0 $str =~ s|(^\s*)?<.*?>__.*?__</.*?>(\s*\n)?||gm;
622  
623 # Inhalt wieder herstellen
624 0 $self->{'string'} = $str;
625  
626 0 return;
627 }
628  
629 # -----------------------------------------------------------------------------
630  
631 =head3 removeOptional() - Entferne Optional-Metatags
632  
633 =head4 Synopsis
634  
635 $tpl->removeOptional;
636  
637 =head4 Description
638  
639 Entferne die <optional>-Metatags aus dem Template-Text.
640 Ein <optional>-Metatag hat die Struktur
641  
642 <!--optional-->...<!--/optional-->
643  
644 oder
645  
646 <!--optional-->
647 ...
648 <!--/optional-->
649  
650 <optional>-Konstrukte können geschachtelt sein. Sie werden von innen
651 nach außen aufgelöst.
652  
653 B<Attribute>
654  
655 =over 4
656  
657 =item default="VALUE"
658  
659 Enthält der Inhalt einen unersetzten Platzhalter, ersetze den
660 Inhalt durch VALUE.
661  
662 =item placeholder="NAME"
663  
664 Entferne den Inhalt nur dann, wenn der Platzhalter __NAME__ unersetzt
665 ist. Andere unersetzte Platzhalter werden nicht beachtet.
666  
667 =back
668  
669 =cut
670  
671 # -----------------------------------------------------------------------------
672  
673 6 ▶sub removeOptional { # Perl 5.8.8
Subroutine removeOptional: called CC=14 SCAR=32.7
Pod documented
674 6 my $self = shift;
675  
676 6 my $str = $self->{'string'}; # Kopie, auf der wir ersetzen
677  
678 6 my $skipped = 0; # Anzahl der wegen Einbettung übergangener Tags
679 # Wir kopieren den Wert, da sonst Totschleife unter 5.8.8
680 6 my $tmp = $self->{'string'};
681 6 ▶ while ($tmp =~ m|<!--optional(.*?)-->(.*?)<!--/optional-->|sg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp =~ m[<!--optional(.*?)-->(.*?)<!--/optional-->]gs) 4 6 10
682 4 my $pre = $`;
683 4 my $tag = $&;
684 4 my $post = $';
685 4 my $attr = $1;
686 4 my $content = $2;
687  
688 # Schachtelung prüfen
689  
690 4 ▶ if ($content =~ m|<!--optional(.*?)-->|) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /<!--optional(.*?)-->/) 1 3 4
691 # Schachtelung: Content enthält weiteren Optional-Tag,
692 # wir übergehen das Konstrukt.
693  
694 1 $tmp =~ s|<!--optional|<!--OpTiOnAl|;
695 1 $skipped++;
696 1 next;
697 }
698  
699 # Attribute ermitteln
700  
701 3 my $placeholder = '';
702 3 ▶ if ($attr =~ /placeholder="(.*?)"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr =~ /placeholder="(.*?)"/) 0 3 3
703 # Platzhalter angegeben. Ohne __ am Anfang und am Ende!
704 # Also: placeholder="TEST" statt placeholder="__TEST__"
705 # Der Inhalt wird nur entfernt, wenn dieser Platzhalter
706 # enthalten ist.
707  
708 0 $placeholder = $1;
709 }
710  
711 3 my $default = '';
712 3 ▶ if ($attr =~ /default="(.*?)"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr =~ /default="(.*?)"/) 0 3 3
713 # Defaultwert angegeben
714 0 $default = $1;
715 }
716  
717 # Unerfüllter Platzhalter im Content enthalten?
718  
719 3 my $remove = 0;
720 3 ▶ if ($placeholder) {
Branch: 1 branch
BranchTrueFalseTotal
if ($placeholder) { } 0 3 3
721 # Platzhalter ist vorgegeben
722 0 ▶ $remove = 1 if $content =~ /__${placeholder}__/;
Branch: 1 branch
BranchTrueFalseTotal
if $content =~ /__${placeholder}__/ 0 0 0
723 }
724 else {
725 # beliebiger Platzhalter
726 3 ▶ $remove = 1 if $content =~ /__\w+?__/;
Branch: 1 branch
BranchTrueFalseTotal
if $content =~ /__\w+?__/ 0 3 3
727 }
728  
729 # Optional-Tag ersetzen
730  
731 3 ▶ if ($remove) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remove) { } 0 3 3
732 0 ▶ if ($default eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($default eq '') { } 0 0 0
733 # echtes Entfernen
734  
735 0 my ($indent) = $pre =~ /\n([ \t]*)$/;
736 0 my ($trail) = $post =~ /^([ \t]*\n)/;
737  
738 0 ▶ if ($indent && $trail) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indent and $trail) { } 0 0 0
Condition0%0/3
$indent and $trail
!ll&&!rl&&r
000
MC/DC0%0/2
$indent and $trail
$indent $trail
Truth table0%0/3
A and B
A= $indentB= $trail
ABresult
0X0
100
111
739 0 $str =~ s|\Q$indent$tag$trail||;
740 }
741 else {
742 0 $str =~ s|\Q$tag||;
743 }
744 }
745 else {
746 # durch Defaultwert ersetzen
747 0 $str =~ s|\Q$tag|$default|;
748 }
749 }
750 else {
751 # Durch Content ersetzen.
752
753 3 ▶ if ($content =~ tr/\n//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ tr/\n//) 0 3 3
754 # Bei einem mehrzeiligen Content (der durch optional-Tags auf
755 # eigenen Zeilen eingefasst sein sollte), beginnt und endet
756 # dieser mit Whitespace, den wir hier entfernen.
757  
758 0 $content =~ s/^\s+//g;
759 0 $content =~ s/\s+$//g;
760 }
761  
762 3 $str =~ s|\Q$tag|$content|;
763 }
764 }
765  
766 6 $self->{'string'} = $str;
767  
768 6 ▶ if ($skipped) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skipped) 1 5 6
769 # Es gab Konstrukte, die wir übergangen haben -> rekursiver Aufruf
770  
771 1 $self->{'string'} =~ s|<!--OpTiOnAl|<!--optional|g;
772 1 $self->removeOptional;
773 }
774  
775 6 return;
776 }
777  
778 0 ▶sub removeOptional_5_10 { # moderne Version für Perl 5.10 und höher. while ersetzen!
Subroutine removeOptional_5_10: not called CC=13 SCAR=52.0
Pod undocumented
779 0 my $self = shift;
780  
781 0 my $str = $self->{'string'}; # Kopie, auf der wir ersetzen
782  
783 0 my $skipped = 0; # Anzahl der wegen Einbettung übergangener Tags
784 # while ($self->{'string'} =~
785 # m|<!--optional(.*?)-->(.*?)<!--/optional-->|sgp) {
786 0 while (1) { # durch obiges while ersetzen, wenn Perl 5.10 oder höher
787 0 my $pre = ${^PREMATCH};
788 0 my $tag = ${^MATCH};
789 0 my $post = ${^POSTMATCH};
790 0 my $attr = $1;
791 0 my $content = $2;
792  
793 # Schachtelung prüfen
794  
795 0 ▶ if ($content =~ m|<!--optional(.*?)-->|) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /<!--optional(.*?)-->/) 0 0 0
796 # Schachtelung: Content enthält weiteren Optional-Tag,
797 # wir übergehen das Konstrukt.
798  
799 0 $self->{'string'} =~ s|<!--optional|<!--OpTiOnAl|;
800 0 $skipped++;
801 0 next;
802 }
803  
804 # Attribute ermitteln
805  
806 0 my $placeholder = '';
807 0 ▶ if ($attr =~ /placeholder="(.*?)"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr =~ /placeholder="(.*?)"/) 0 0 0
808 # Platzhalter angegeben. Ohne __ am Anfang und am Ende!
809 # Also: placeholder="TEST" statt placeholder="__TEST__"
810 # Der Inhalt wird nur entfernt, wenn dieser Platzhalter
811 # enthalten ist.
812  
813 0 $placeholder = $1;
814 }
815  
816 0 my $default = '';
817 0 ▶ if ($attr =~ /default="(.*?)"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr =~ /default="(.*?)"/) 0 0 0
818 # Defaultwert angegeben
819 0 $default = $1;
820 }
821  
822 # Unerfüllter Platzhalter im Content enthalten?
823  
824 0 my $remove = 0;
825 0 ▶ if ($placeholder) {
Branch: 1 branch
BranchTrueFalseTotal
if ($placeholder) { } 0 0 0
826 # Platzhalter ist vorgegeben
827 0 ▶ $remove = 1 if $content =~ /__${placeholder}__/;
Branch: 1 branch
BranchTrueFalseTotal
if $content =~ /__${placeholder}__/ 0 0 0
828 }
829 else {
830 # beliebiger Platzhalter
831 0 ▶ $remove = 1 if $content =~ /__\w+?__/;
Branch: 1 branch
BranchTrueFalseTotal
if $content =~ /__\w+?__/ 0 0 0
832 }
833  
834 # Optional-Tag ersetzen
835  
836 0 ▶ if ($remove) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remove) { } 0 0 0
837 0 ▶ if ($default eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($default eq '') { } 0 0 0
838 # echtes Entfernen
839  
840 0 my ($indent) = $pre =~ /\n([ \t]*)$/;
841 0 my ($trail) = $post =~ /^([ \t]*\n)/;
842  
843 0 ▶ if ($indent && $trail) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indent and $trail) { } 0 0 0
Condition0%0/3
$indent and $trail
!ll&&!rl&&r
000
MC/DC0%0/2
$indent and $trail
$indent $trail
Truth table0%0/3
A and B
A= $indentB= $trail
ABresult
0X0
100
111
844 0 $str =~ s|\Q$indent$tag$trail||;
845 }
846 else {
847 0 $str =~ s|\Q$tag||;
848 }
849 }
850 else {
851 # durch Defaultwert ersetzen
852 0 $str =~ s|\Q$tag|$default|;
853 }
854 }
855 else {
856 # durch Content ersetzen
857
858 0 ▶ if ($content =~ tr/\n//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ tr/\n//) 0 0 0
859 # Bei einem mehrzeiligen Content (der durch optional-Tags auf
860 # eigenen Zeilen eingefasst sein sollte), beginnt und endet
861 # dieser mit Whitespace, den wir hier entfernen.
862  
863 0 $content =~ s/^\s+//g;
864 0 $content =~ s/\s+$//g;
865 }
866  
867 0 $str =~ s|\Q$tag|$content|;
868 }
869 }
870  
871 0 $self->{'string'} = $str;
872  
873 0 ▶ if ($skipped) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skipped) 0 0 0
874 # Es gab Konstrukte, die wir übergangen haben -> rekursiver Aufruf
875  
876 0 $self->{'string'} =~ s|<!--OpTiOnAl|<!--optional|g;
877 0 $self->removeOptional;
878 }
879  
880 0 return;
881 }
882  
883 # -----------------------------------------------------------------------------
884  
885 =head3 asString() - Liefere Inhalt
886  
887 =head4 Synopsis
888  
889 $str = $tpl->asString;
890  
891 =cut
892  
893 # -----------------------------------------------------------------------------
894  
895 3 ▶sub asString {
Subroutine asString: called CC=1 SCAR=0.0
Pod documented
896 3 my $self = shift;
897 3 return $self->{'string'};
898 }
899  
900 # -----------------------------------------------------------------------------
901  
902 =head3 asStringNL() - Liefere Inhalt mit Newline am Ende
903  
904 =head4 Synopsis
905  
906 $str = $tpl->asStringNL;
907  
908 =cut
909  
910 # -----------------------------------------------------------------------------
911  
912 7 ▶sub asStringNL {
Subroutine asStringNL: called CC=2 SCAR=6.9
Pod documented
913 7 my $self = shift;
914 7 ▶ return $self->{'string'} eq ''? '': "$self->{'string'}\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{'string'} eq '' ? : 1 6 7
915 }
916  
917 # -----------------------------------------------------------------------------
918  
919 =head1 VERSION
920  
921 1.240
922  
923 =head1 AUTHOR
924  
925 Frank Seitz, L<http://fseitz.de/>
926  
927 =head1 COPYRIGHT
928  
929 Copyright (C) 2026 Frank Seitz
930  
931 =head1 LICENSE
932  
933 This code is free software; you can redistribute it and/or modify
934 it under the same terms as Perl itself.
935  
936 =cut
937  
938 # -----------------------------------------------------------------------------
939  
940 51 1;
941  
942 # eof
« Quiq/Zugferd/Tree.pm ↑ Summary Quiq/Gd/Component/BlockDiagram.pm »