Quiq/Tag.pm

filter: Statementstmt 75.9% 79 / 104 Branchbran 60.6% 40 / 66 Conditioncond 54.1% 13 / 24 MC/DCmcdc 21.4% 3 / 14 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 66.9% 148 / 221 CC 42 SCAR 42.4
CC
42
Cov
68%
tag
94.3
cat
9.5
BEGIN
1.0
CRAP 69.7
« Quiq/DestinationTree.pm ↑ Summary Quiq/Mustang.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Tag - Erzeuge Markup-Code gemäß XML-Regeln
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 SYNOPSIS
14  
15 =head3 Modul laden und Objekt instantiieren
16  
17 use Quiq::Tag;
18
19 my $p = Quiq::Tag->new;
20  
21 =head3 Tag ohne Content
22  
23 $code = $p->tag('person',
24 firstname => 'Lieschen',
25 lastname => 'Müller',
26 );
27  
28 liefert
29  
30 <person firstname="Lieschen" lastname="Müller" />
31  
32 =head3 Tag mit Content
33  
34 $code = $p->tag('bold','sehr schön');
35  
36 liefert
37  
38 <bold>sehr schön</bold>
39  
40 Enthält der Content, wie hier, keine Zeilenumbrüche, werden Begin-
41 und End-Tag unmittelbar um den Content gesetzt. Andernfalls wird
42 der Content eingerückt mehrzeilig zwischen Begin- und End-Tag
43 gesetzt. Siehe nächstes Beispiel.
44  
45 =head3 Tag mit Unterstruktur
46  
47 $code = $p->tag('person','-',
48 $p->tag('firstname','Lieschen'),
49 $p->tag('lastname','Müller'),
50 );
51  
52 erzeugt
53  
54 <person>
55 <firstname>Lieschen</firstname>
56 <lastname>Müller</lastname>
57 </person>
58  
59 Das Bindestrich-Argument (C<'-'>) bewirkt, dass die nachfolgenden
60 Argumente zum Content des Tag konkateniert werden. Die umständlichere
61 Formulierung wäre:
62  
63 $code = $p->tag('person',$p->cat(
64 $p->tag('firstname','Lieschen'),
65 $p->tag('lastname','Müller'),
66 ));
67  
68 =head1 DESCRIPTION
69  
70 Ein Objekt der Klasse erzeugt Markup-Code gemäß den Regeln von XML.
71 Mit den beiden Methoden L<tag|"tag() - Erzeuge Tag-Code">() und L<cat|"cat() - Füge Sequenz zusammen">() kann Markup-Code
72 beliebiger Komplexität erzeugt werden.
73  
74 =cut
75  
76 # -----------------------------------------------------------------------------
77  
78 package Quiq::Tag;
79 2 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
80  
81 2 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
84  
85 2 our $VERSION = '1.240';
86  
87 2 ▶use Quiq::Unindent;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 2 ▶use Quiq::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
89 2 ▶use Quiq::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91 # -----------------------------------------------------------------------------
92  
93 =head1 METHODS
94  
95 =head2 Instantiierung
96  
97 =head3 new() - Konstruktor
98  
99 =head4 Synopsis
100  
101 $p = $class->new;
102  
103 =head4 Returns
104  
105 Objekt
106  
107 =head4 Description
108  
109 Instantiiere ein Objekt der Klasse und liefere eine Referenz
110 auf dieses Objekt zurück.
111  
112 =cut
113  
114 # -----------------------------------------------------------------------------
115  
116 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
117 2 my $class = shift;
118 2 return $class->SUPER::new;
119 }
120  
121 # -----------------------------------------------------------------------------
122  
123 =head2 Generierung
124  
125 =head3 tag() - Erzeuge Tag-Code
126  
127 =head4 Synopsis
128  
129 $code = $p->tag($elem,@opts,@attrs);
130 $code = $p->tag($elem,@opts,@attrs,$content);
131 $code = $p->tag($elem,@opts,@attrs,'-',@content);
132  
133 =head4 Arguments
134  
135 =over 4
136  
137 =item $elem
138  
139 Name des Elements.
140  
141 =item @opts
142  
143 Optionen. Siehe unten.
144  
145 =item @attrs
146  
147 Element-Attribute und ihre Werte.
148  
149 =item $content
150  
151 Inhalt des Tag.
152  
153 =item @contents
154  
155 Sequenz von Inhalten.
156  
157 =back
158  
159 =head4 Options
160  
161 =over 4
162  
163 =item -defaults => \@keyVals (Default: undef)
164  
165 Liste der Default-Attribute und ihrer Werte. Ein Attribut in
166 @keyVals, das nicht unter den Attributen @attrs des Aufrufs
167 vorkommt, wird auf den angegebenen Defaultwert gesetzt.
168  
169 =item -elements => \%elements (Default: undef)
170  
171 Hash, der die Default-Formatierung und Default-Attribute von
172 Elementen definiert. Aufbau:
173  
174 %elements = (
175 $elem => [$fmt,\@keyVals],
176 ...
177 )
178  
179 Der Hash muss nicht jedes Element definieren. Nicht-vorkommende
180 Elemente gemäß Default-Formatierung formatiert (siehe -fmt)
181 besitzen keine Default-Attribute.
182  
183 =item -fmt => 'c'|'e'|'E'|'i'|'m'|'p'|'P'|'v' (Default: gemäß $elem)
184  
185 Art der Content-Formatierung.
186  
187 =over 4
188  
189 =item 'c' (cdata):
190  
191 Wie 'm', nur dass der Content in CDATA eingefasst wird
192 (in HTML: script):
193  
194 <TAG ...>
195 // <![CDATA[
196 CONTENT
197 // ]]>
198 </TAG>\n
199  
200 =item 'e' (empty):
201  
202 Element hat keinen Content (in HTML: br, hr, ...):
203  
204 <TAG ... />\n
205  
206 =item 'E' (empty, kein Newline):
207  
208 Wie 'e', nur ohne Newline am Ende (in HTML: img, input, ...):
209  
210 <TAG ... />
211  
212 =item 'i' (inline):
213  
214 Der Content wird belassen wie er ist. Dies ist nützlich für
215 Tags, die in Fließtext eingesetzt werden. Ein Newline wird
216 nicht angehängt.
217  
218 Text Text <TAG ...>Text Text
219 Text</TAG> Text Text
220  
221 (in HTML: a, b, span, ...)
222  
223 =item 'm' (multiline):
224  
225 Content wird auf eigene Zeile(n) zwischen Begin- und End-Tag
226 gesetzt und um -ind=>$n Leerzeichen eingerückt:
227  
228 <TAG ...>
229 CONTENT
230 </TAG>\n
231  
232 Ist der Content leer, wird nur ein End-Tag gesetzt:
233  
234 <TAG ... />\n
235  
236 =item 'M' (multiline, ohne Einrückung):
237  
238 Wie 'm', nur ohne Einrückung (in HTML: html, ...):
239  
240 <TAG ...>
241 CONTENT
242 </TAG>\n
243  
244 =item 'p' (protect):
245  
246 Der Content wird geschützt, indem dieser einzeilig gemacht
247 (LF und CR werden durch &#10; und &#13; ersetzt) und unmittelbar
248 zwischen Begin- und End-Tag gesetzt wird
249 (in HTML: pre, textarea, ...):
250  
251 <TAG ...>CONTENT</TAG>\n
252  
253 =item 'P' (protect, Einrückung entfernen):
254  
255 Wie 'p', nur dass die Einrückung des Content entfernt wird.
256  
257 =item 'v' (variable) = Default-Formatierung:
258  
259 Ist der Content einzeilig, wird er unmittelbar zwischen Begin-
260 und End-Tag gesetzt:
261  
262 <TAG ...>CONTENT</TAG>\n
263  
264 Ist der Content mehrzeilig, wird er eingerückt:
265  
266 <TAG ...>
267 CONTENT
268 </TAG>\n
269  
270 (in HTML: title, h1-h6, ...)
271  
272 =back
273  
274 =item -nl => $n (Default: 1)
275  
276 Anzahl Newlines am Ende.
277  
278 -nl => 0 (kein Newline):
279  
280 <TAG>CONTENT</TAG>
281  
282 -nl => 1 (ein Newline):
283  
284 <TAG>CONTENT</TAG>\n
285  
286 -nl => 2 (zwei Newlines):
287  
288 <TAG>CONTENT</TAG>\n\n
289  
290 usw.
291  
292 =item -placeholders => \@keyVal (Default: undef)
293  
294 Ersetze im erzeugten Code die angegebenen Platzhalter
295 durch ihre Werte.
296  
297 =back
298  
299 =head4 Description
300  
301 Erzeuge den Code eines Tag und liefere diesen zurück.
302  
303 =cut
304  
305 # -----------------------------------------------------------------------------
306  
307 9 ▶sub tag {
Subroutine tag: called CC=37 SCAR=45.5
Pod documented
308 9 my ($self,$elem) = splice @_,0,2;
309  
310 # MEMO: Dies ist eine abgespeckte Variante der Methode tag() in
311 # Quiq::Html::Tag. Etwaige Ergänzungen, die hier gebraucht
312 # werden, von dort übernehmen.
313  
314 # Optionen
315  
316 9 my $contentInd = undef;
317 9 my $defaultA = undef;
318 9 my $fmt = 'v';
319 9 my $ind = 2;
320 9 my $nl = 1;
321 9 my $placeholderA = undef;
322  
323 # Attribute
324 9 my @attrs;
325  
326 # Parameter verarbeiten
327  
328 9 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 19 3 22
329 19 ▶ if (@_ == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 5 14 19
elsif (defined $_[0] and $_[0] eq '-') { } 1 13 14
330 # Letzter Parameter ist Content. Dieser Test muss als erstes
331 # kommen, damit '-' als Content nicht unter den Tisch fällt.
332 5 last;
333 }
334 ▶ elsif (defined $_[0] && $_[0] eq '-') {
Condition66%2/3
defined $_[0] and $_[0] eq "-"
!ll&&!rl&&r
0131
MC/DC50%1/2
defined $_[0] and $_[0] eq "-"
defined $_[0] $_[0] eq "-"
Truth table66%2/3
A and B
A= defined $_[0]B= $_[0] eq "-"
ABresult
0X0
100
111
335 # Explizites Ende der Options- und Attributliste
336 1 shift; # '-' konsumieren
337 1 last;
338 }
339  
340 # Optionen
341  
342 13 my $key = shift;
343 13 ▶ if (substr($key,0,1) eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($key, 0, 1) eq "-") 3 10 13
344 3 ▶ if ($key eq '-elements') {
Branch: 5 branches
BranchTrueFalseTotal
if ($key eq '-elements') { } 3 0 3
elsif ($key eq '-defaults') { } 0 0 0
elsif ($key eq '-fmt') { } 0 0 0
elsif ($key eq '-nl') { } 0 0 0
elsif ($key eq '-placeholders') { } 0 0 0
345 3 ▶ if (my $defA = shift->{$elem}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $defA = +(shift())->{$elem}) 2 1 3
346 2 ($fmt,$defaultA) = @$defA;
347 }
348 }
349 elsif ($key eq '-defaults') {
350 0 $defaultA = shift;
351 }
352 elsif ($key eq '-fmt') {
353 0 $fmt = shift;
354 }
355 elsif ($key eq '-nl') {
356 0 $nl = shift;
357 }
358 elsif ($key eq '-placeholders') {
359 0 $placeholderA = shift;
360 }
361 else {
362 0 $self->throw(
363 'TAG-00001: Unknown option',
364 Option => $key,
365 );
366 }
367 3 next;
368 }
369  
370 # Attribute
371 10 push @attrs,$key,shift;
372 }
373  
374 # Defaultattribute setzen
375  
376 9 ▶ if ($defaultA) {
Branch: 1 branch
BranchTrueFalseTotal
if ($defaultA) 2 7 9
377 # Hash der Defaultattribute erstellen
378 2 my %defaults = @$defaultA;
379  
380 # Gesetzte Attribute aus der Betrachtung nehmen
381  
382 2 ▶ for (my $i = 0; $i < @attrs; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @attrs) 4 2 6
383 4 delete $defaults{$attrs[$i]};
384 }
385  
386 # Defaultattribute setzen
387  
388 2 ▶ for (my $i = 0; $i < @$defaultA; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$defaultA) 6 2 8
389 6 ▶ if (exists $defaults{$defaultA->[$i]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $defaults{$defaultA->[$i]}) 6 0 6
390 6 push @attrs,$defaultA->[$i],$defaultA->[$i+1];
391 }
392 }
393 }
394  
395 # Content
396  
397 9 my $content = $self->cat(@_);
398  
399 9 ▶ if ($fmt eq 'p' || $fmt eq 'P') {
Branch: 4 branches
BranchTrueFalseTotal
if ($fmt eq 'p' or $fmt eq 'P') { } 0 9 9
elsif ($fmt eq 'e' or $fmt eq 'E') { } 0 9 9
elsif ($fmt eq 'i' or $fmt eq 'v' and not $content =~ /\n/) { } 5 4 9
elsif ($fmt eq 'v' or $fmt eq 'm' or $fmt eq 'c') { } 4 0 4
Condition33%1/3
$fmt eq "p" or $fmt eq "P"
l!l&&r!l&&!r
009
MC/DC0%0/2
$fmt eq "p" or $fmt eq "P"
$fmt eq "p" $fmt eq "P"
Truth table33%1/3
A or B
A= $fmt eq "p"B= $fmt eq "P"
ABresult
1X1
011
000
400 0 ▶ if ($fmt eq 'P') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fmt eq "P") 0 0 0
401 0 $content = Quiq::Unindent->trim($content);
402 }
403 0 $content =~ s/\x0a/&#10;/g;
404 0 $content =~ s/\x0d/&#13;/g;
405 }
406 ▶ elsif ($fmt eq 'e' || $fmt eq 'E') {
Condition33%1/3
$fmt eq "e" or $fmt eq "E"
l!l&&r!l&&!r
009
MC/DC0%0/2
$fmt eq "e" or $fmt eq "E"
$fmt eq "e" $fmt eq "E"
Truth table33%1/3
A or B
A= $fmt eq "e"B= $fmt eq "E"
ABresult
1X1
011
000
407 0 ▶ if (length $content) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $content) 0 0 0
408 0 $self->throw(
409 'TAG-00003: No content expected',
410 Content => $content,
411 );
412 }
413 }
414 ▶ elsif ($fmt eq 'i' || $fmt eq 'v' && $content !~ /\n/) {
Condition83%5/6
$fmt eq "i" or $fmt eq "v" and not $content =~ /\n/
l!l&&r!l&&!r
054
$fmt eq "v" and not $content =~ /\n/
!ll&&!rl&&r
225
MC/DC66%2/3
$fmt eq "i" or $fmt eq "v" and not $content =~ /\n/
$fmt eq "i" $fmt eq "v" not $content =~ /\n/
Truth table75%3/4
A or B and not(C)
A= $fmt eq "i"B= $fmt eq "v"C= not $content =~ /\n/
ABCresult
1XX1
0111
00X0
0100
415 # nichts tun
416 }
417 ▶ elsif ($fmt eq 'v' || $fmt eq 'm' || $fmt eq 'c') {
Condition50%3/6
$fmt eq "v" or $fmt eq "m" or $fmt eq "c"
l!l&&r!l&&!r
400
$fmt eq "v" or $fmt eq "m"
l!l&&r!l&&!r
220
MC/DC0%0/3
$fmt eq "v" or $fmt eq "m" or $fmt eq "c"
$fmt eq "v" $fmt eq "m" $fmt eq "c"
Truth table50%2/4
A or B or C
A= $fmt eq "v"B= $fmt eq "m"C= $fmt eq "c"
ABCresult
1XX1
01X1
0011
0000
418 4 $content = Quiq::Unindent->trim($content);
419 4 ▶ if ($contentInd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($contentInd) 0 4 4
420 # Bringe Einrückung des Content auf Tiefe $contendInd
421 0 Quiq::String->reduceIndentation($contentInd,\$content);
422 }
423 4 ▶ if ($fmt eq 'c' && $content =~ tr/&<>//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fmt eq "c" and $content =~ tr/&<>//) 0 4 4
Condition33%1/3
$fmt eq "c" and $content =~ tr/&<>//
!ll&&!rl&&r
400
MC/DC0%0/2
$fmt eq "c" and $content =~ tr/&<>//
$fmt eq "c" $content =~ tr/&<>//
Truth table33%1/3
A and B
A= $fmt eq "c"B= $content =~ tr/&<>//
ABresult
0X0
100
111
424 # Script-Code in CDATA einfassen, wenn &, < oder > enthalten sind
425 0 $content = "// <![CDATA[\n$content\n// ]]>";
426 }
427 4 ▶ if ($ind) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ind) 4 0 4
428 4 my $space = ' ' x $ind;
429 4 $content =~ s/^(?!$)/$space/gm; # Leerzeilen nicht einrücken
430 }
431 4 ▶ if ($content ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($content ne "") 3 1 4
432 3 $content = "\n$content\n";
433 }
434 }
435 else {
436 0 $self->throw(
437 'TAG-00004: Unknown format (-fmt)',
438 Value => $fmt,
439 );
440 }
441  
442 # Tag erzeugen
443  
444 9 my $code = "<$elem";
445 9 ▶ while (@attrs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@attrs) 16 9 25
446 16 my ($key,$val) = splice @attrs,0,2;
447 16 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 16 0 16
448 16 $val =~ s/"/&quot;/g;
449 16 $code .= qq| $key="$val"|;
450 }
451 }
452 9 ▶ $code .= $content ne ''? ">$content</$elem>": ' />';
Branch: 1 branch
BranchTrueFalseTotal
$content ne '' ? : 6 3 9
453  
454 # Platzhalter ersetzen
455  
456 9 ▶ if ($placeholderA) {
Branch: 1 branch
BranchTrueFalseTotal
if ($placeholderA) 0 9 9
457 0 my $tpl = Quiq::Template->new('text',\$code);
458 0 $tpl->replace(@$placeholderA);
459 0 $code = $tpl->asString;
460 }
461  
462 # Newlinw
463 9 $code .= "\n" x $nl;
464  
465 9 return $code;
466 }
467  
468 # -----------------------------------------------------------------------------
469  
470 =head3 cat() - Füge Sequenz zusammen
471  
472 =head4 Synopsis
473  
474 $code = $p->cat(@opt,@args);
475  
476 =head4 Arguments
477  
478 =over 4
479  
480 =item @args
481  
482 Sequenz von Werten.
483  
484 =back
485  
486 =head4 Options
487  
488 =over 4
489  
490 =item -placeholders => \@keyVal
491  
492 Ersetze im generierten Code die angegebenen Platzhalter durch
493 die angegebenen Werte.
494  
495 =back
496  
497 =head4 Description
498  
499 Füge die Arguments @args zusammen und liefere den resultierenden
500 Code zurück.
501  
502 =cut
503  
504 # -----------------------------------------------------------------------------
505  
506 10 ▶sub cat {
Subroutine cat: called CC=6 SCAR=22.6
Pod documented
507 10 my $self = shift;
508  
509 # Optionen
510  
511 10 my $placeholderA = undef;
512  
513 10 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 7 3 10
514 7 ▶ if (!defined $_[0]) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $_[0]) { } 0 7 7
elsif ($_[0] eq '-') { } 0 7 7
elsif ($_[0] eq '-placeholders') { } 0 7 7
515 0 last;
516 }
517 elsif ($_[0] eq '-') {
518 # explizites Ende der Optionen
519 0 shift; # '-' konsumieren
520 0 last;
521 }
522 elsif ($_[0] eq '-placeholders') {
523 0 $placeholderA = splice @_,0,2;
524 0 next;
525 }
526 7 last;
527 }
528  
529 # Argumente konkatenieren
530  
531 10 my $code = join '',@_;
532  
533 # Platzhalter ersetzen
534  
535 10 ▶ if ($placeholderA) {
Branch: 1 branch
BranchTrueFalseTotal
if ($placeholderA) 0 10 10
536 0 my $tpl = Quiq::Template->new('text',\$code);
537 0 $tpl->replace(@$placeholderA);
538 0 $code = $tpl->asStringNL;
539 }
540  
541 10 return $code;
542 }
543  
544 # -----------------------------------------------------------------------------
545  
546 =head1 VERSION
547  
548 1.240
549  
550 =head1 AUTHOR
551  
552 Frank Seitz, L<http://fseitz.de/>
553  
554 =head1 COPYRIGHT
555  
556 Copyright (C) 2026 Frank Seitz
557  
558 =head1 LICENSE
559  
560 This code is free software; you can redistribute it and/or modify
561 it under the same terms as Perl itself.
562  
563 =cut
564  
565 # -----------------------------------------------------------------------------
566  
567 2 1;
568  
569 # eof
« Quiq/DestinationTree.pm ↑ Summary Quiq/Mustang.pm »