Quiq/Pod/Generator.pm

filter: Statementstmt 100% 93 / 93 Branchbran 83.3% 20 / 24 Conditioncond 66.6% 4 / 6 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 16 / 16 Podpod 100% 11 / 11 totaltotal 94.7% 145 / 153 CC 17 SCAR 11.6
CC
17
Cov
95%
fmt
6.0
definitionList
5.0
section
3.0
CRAP 3.2
« Quiq/Database/Row.pm ↑ Summary Quiq/Database/Config.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Pod::Generator - POD-Generator
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 DESCRIPTION
14  
15 Dokumentation der POD-Syntax: L<http://perldoc.perl.org/perlpod.html>
16  
17 Ein Objekt der Klasse repräsentiert einen POD-Generator. Mit den
18 Methoden der Klasse können aus einem Perl-Programm heraus
19 POD-Dokumente erzeugt werden, wobei man sich um die Syntaxregeln
20 und die Details der Formatierung nicht zu kümmern braucht.
21  
22 =head1 ATTRIBUTES
23  
24 =over 4
25  
26 =item indentation => $n (Default: 4)
27  
28 Einrücktiefe bei Code-Abschnitten und Listen.
29  
30 =back
31  
32 =cut
33  
34 # -----------------------------------------------------------------------------
35  
36 package Quiq::Pod::Generator;
37 1 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 1 our $VERSION = '1.240';
44  
45 1 ▶use Quiq::Unindent;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 # -----------------------------------------------------------------------------
48  
49 =head1 METHODS
50  
51 =head2 Konstruktor
52  
53 =head3 new() - Instantiiere POD-Generator
54  
55 =head4 Synopsis
56  
57 $pg = $class->new(@keyVal);
58  
59 =head4 Description
60  
61 Instantiiere einen POD-Generator und liefere eine Referenz auf
62 dieses Objekt zurück.
63  
64 =head4 Example
65  
66 Generiere POD mit Einrückung 2:
67  
68 $pg = Quiq::Pod::Generator->new(
69 indentation => 2,
70 );
71  
72 =cut
73  
74 # -----------------------------------------------------------------------------
75  
76 9 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
77 9 my $class = shift;
78 # @_: @keyVal
79  
80 9 my $self = $class->SUPER::new(
81 indentation => 4,
82 );
83 9 $self->set(@_);
84  
85 9 return $self;
86 }
87  
88 # -----------------------------------------------------------------------------
89  
90 =head2 Abschnitts-Kommandos
91  
92 Alle Abschnitts-Methoden ergänzen den generierten POD-Code um eine
93 Leerzeile am Ende, so dass das nächste Konstrukt direkt angefügt
94 werden kann. Diese Leezeile ist in den Beispielen nicht
95 wiedergegeben.
96  
97 =head3 encoding() - Deklaration des Encodings
98  
99 =head4 Synopsis
100  
101 $pod = $pg->encoding($encoding);
102  
103 =head4 Description
104  
105 Erzeuge eine Deklaration des Encodings $encoding und liefere
106 den resultierenden POD-Code zurück.
107  
108 =head4 Example
109  
110 $pg->encoding('utf-8');
111  
112 erzeugt
113  
114 =encoding utf-8
115  
116 =cut
117  
118 # -----------------------------------------------------------------------------
119  
120 1 ▶sub encoding {
Subroutine encoding: called CC=1 SCAR=0.0
Pod documented
121 1 my ($self,$encoding) = @_;
122 1 return "=encoding $encoding\n\n";
123 }
124  
125 # -----------------------------------------------------------------------------
126  
127 =head3 section() - Abschnitt
128  
129 =head4 Synopsis
130  
131 $pod = $pg->section($level,$title);
132 $pod = $pg->section($level,$title,$body);
133  
134 =head4 Description
135  
136 Erzeuge einen Abschnitt der Tiefe $level mit dem Titel $title und
137 dem Abschnitts-Körper $body und liefere den resultierenden
138 POD-Code zurück. Ist $body nicht angegeben oder ein Leerstring,
139 wird nur der Titel erzeugt. Andernfalls wird $body per trim()
140 von einer Einrückung befreit.
141  
142 =head4 Examples
143  
144 =over 2
145  
146 =item *
147  
148 ohne Body
149  
150 $pg->section(1,'Test');
151  
152 erzeugt
153  
154 =head1 Test
155  
156 =item *
157  
158 mit Body
159  
160 $pg->section(1,'Test',"Dies ist\nein Test.");
161  
162 erzeugt
163  
164 =head1 Test
165
166 Dies ist
167 ein Test.
168  
169 =item *
170  
171 eine Einrückung wird automatisch entfernt
172  
173 $pg->section(1,'DESCRIPTION',q~
174 Dies ist
175 ein Test.
176 ~);
177  
178 erzeugt
179  
180 =head1 Test
181
182 Dies ist
183 ein Test.
184  
185 =back
186  
187 =cut
188  
189 # -----------------------------------------------------------------------------
190  
191 2 ▶sub section {
Subroutine section: called CC=3 SCAR=11.0
Pod documented
192 2 my ($self,$level,$title,$body) = @_;
193  
194 2 my $pod = "=head$level $title\n\n";
195 2 ▶ if (defined $body) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body) 1 1 2
196 1 $body = Quiq::Unindent->trim($body);
197 1 ▶ if ($body ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($body ne "") 1 0 1
198 1 $pod .= "$body\n\n";
199 }
200 }
201  
202 2 return $pod;
203 }
204  
205 # -----------------------------------------------------------------------------
206  
207 =head3 code() - Code-Abschnitt
208  
209 =head4 Synopsis
210  
211 $pod = $pg->code($text);
212  
213 =head4 Description
214  
215 Erzeuge einen Code-Abschnitt mit Text $text und liefere den
216 resultierenden POD-Code zurück.
217  
218 =head4 Example
219  
220 $pg->code("sub f {\n return 1;\n}");
221  
222 erzeugt
223  
224 $n Leerzeichen
225 ----
226 sub f {
227 return 1;
228 }
229  
230 Der Code ist um $n Leerzeichen (den Wert des Objekt-Attributs
231 "indentation") eingerückt.
232  
233 =cut
234  
235 # -----------------------------------------------------------------------------
236  
237 1 ▶sub code {
Subroutine code: called CC=1 SCAR=0.0
Pod documented
238 1 my ($self,$text) = @_;
239  
240 1 $text = Quiq::Unindent->trim($text);
241  
242 1 my $indent = ' ' x $self->indentation;
243 1 $text =~ s/^/$indent/mg;
244  
245 1 return "$text\n\n";
246 }
247  
248 # -----------------------------------------------------------------------------
249  
250 =head3 bulletList() - Punkte-Liste
251  
252 =head4 Synopsis
253  
254 $pod = $pg->bulletList(\@items);
255  
256 =head4 Description
257  
258 Erzeuge eine Punkte-Liste mit den Elementen @items und liefere
259 den resultierenden POD-Code zurück.
260  
261 =head4 Example
262  
263 $pg->bulletList(['Eins','Zwei']);
264  
265 erzeugt
266  
267 =over 4
268
269 =item *
270
271 Eins
272
273 =item *
274
275 Zwei
276
277 =back
278  
279 =cut
280  
281 # -----------------------------------------------------------------------------
282  
283 1 ▶sub bulletList {
Subroutine bulletList: called CC=3 SCAR=11.0
Pod documented
284 1 my ($self,$itemA) = @_;
285  
286 1 my $indent = $self->indentation;
287  
288 1 my $pod = '';
289 1 for (@$itemA) {
290 2 my $item = $_;
291 2 $item =~ s/^\n+//;
292 2 $item =~ s/\s+$//;
293 2 $pod .= "=item *\n\n$item\n\n";
294 }
295 1 ▶ if ($pod) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod) 1 0 1
296 1 $pod = "=over $indent\n\n$pod=back\n\n";
297 }
298  
299 1 return $pod;
300 }
301  
302 # -----------------------------------------------------------------------------
303  
304 =head3 orderedList() - Aufzählungs-Liste
305  
306 =head4 Synopsis
307  
308 $pod = $pg->orderedList(\@items);
309  
310 =head4 Description
311  
312 Erzeuge eine Aufzählungs-Liste mit den Elementen @items und liefere
313 den resultierenden POD-Code zurück.
314  
315 =head4 Example
316  
317 $pg->orderedList(['Eins','Zwei']);
318  
319 erzeugt
320  
321 =over 4
322
323 =item 1.
324
325 Eins
326
327 =item 2.
328
329 Zwei
330
331 =back
332  
333 =cut
334  
335 # -----------------------------------------------------------------------------
336  
337 1 ▶sub orderedList {
Subroutine orderedList: called CC=3 SCAR=11.0
Pod documented
338 1 my ($self,$itemA) = @_;
339  
340 1 my $indent = $self->indentation;
341  
342 1 my $pod = '';
343 1 my $i = 1;
344 1 for (@$itemA) {
345 2 my $item = $_;
346 2 $item =~ s/^\n+//;
347 2 $item =~ s/\s+$//;
348 2 $pod .= sprintf "=item %d.\n\n$item\n\n",$i++;
349 }
350 1 ▶ if ($pod) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod) 1 0 1
351 1 $pod = "=over $indent\n\n$pod=back\n\n";
352 }
353  
354 1 return $pod;
355 }
356  
357 # -----------------------------------------------------------------------------
358  
359 =head3 definitionList() - Definitions-Liste
360  
361 =head4 Synopsis
362  
363 $pod = $pg->definitionList(\@items);
364  
365 =head4 Description
366  
367 Erzeuge eine Definitions-Liste mit den Elementen @items und liefere
368 den resultierenden POD-Code zurück.
369  
370 =head4 Example
371  
372 Die Aufrufe
373  
374 $pg->definitionList([A=>'Eins',B=>'Zwei']);
375  
376 oder
377  
378 $pg->definitionList([['A','Eins'],['B','Zwei']]);
379  
380 erzeugen
381  
382 =over 4
383
384 =item A
385
386 Eins
387
388 =item B
389
390 Zwei
391
392 =back
393  
394 =cut
395  
396 # -----------------------------------------------------------------------------
397  
398 2 ▶sub definitionList {
Subroutine definitionList: called CC=5 SCAR=16.1
Pod documented
399 2 my ($self,$itemA) = @_;
400  
401 2 my $step = 2;
402 2 ▶ if (ref $itemA->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $itemA->[0]) 1 1 2
403 1 $step = 1; # zweielementige Listen
404 }
405  
406 2 my $pod = '';
407 2 ▶ for (my $i = 0; $i < @$itemA; $i += $step) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$itemA) 4 2 6
408 4 ▶ my ($key,$val) = $step == 1? @{$itemA->[$i]}: @$itemA[$i,$i+1];
Branch: 1 branch
BranchTrueFalseTotal
$step == 1 ? : 2 2 4
409 4 $val =~ s/^\n+//;
410 4 $val =~ s/\s+$//;
411 4 $pod .= "=item $key\n\n$val\n\n";
412 }
413 2 ▶ if ($pod) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod) 2 0 2
414 2 my $indent = $self->indentation;
415 2 $pod = "=over $indent\n\n$pod=back\n\n";
416 }
417  
418 2 return $pod;
419 }
420  
421 # -----------------------------------------------------------------------------
422  
423 =head3 for() - Formatierer-Code
424  
425 =head4 Synopsis
426  
427 $pod = $pg->for($format,$code);
428  
429 =head4 Description
430  
431 Definiere Code $code für Formatierer des Formats $format und
432 liefere das Resultat zurück. Ist $code einzeilig, wird eine
433 for-Instruktion erzeugt, ansonsten eine begin/end-Instruktion.
434  
435 =head4 Examples
436  
437 =over 2
438  
439 =item *
440  
441 einzeiliger Code
442  
443 $pg->for('html','<img src="figure1.png" />');
444  
445 erzeugt
446  
447 =for html <img src="figure1.png" />
448  
449 =item *
450  
451 mehrzeiliger Code
452  
453 $pg->for('html',qq|Ein Bild:\n<img src="figure1.png" />|);
454  
455 erzeugt
456  
457 =begin html
458
459 Ein Bild:
460 <img src="figure1.png" />
461
462 =end html
463  
464 =back
465  
466 =cut
467  
468 # -----------------------------------------------------------------------------
469  
470 2 ▶sub for {
Subroutine for: called CC=2 SCAR=6.9
Pod documented
471 2 my ($self,$format,$code) = @_;
472  
473 2 $code =~ s/\s+$//;
474  
475 2 my $pod;
476 2 ▶ if ($code =~ tr/\n//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ tr/\n//) { } 1 1 2
477 1 $pod = "=begin $format\n\n$code\n\n=end $format\n\n";
478 }
479 else {
480 1 $pod = "=for $format $code\n\n";
481 }
482
483 2 return $pod;
484 }
485  
486 # -----------------------------------------------------------------------------
487  
488 =head3 pod() - Beginne POD-Block
489  
490 =head4 Synopsis
491  
492 $pod = $pg->pod;
493  
494 =head4 Description
495  
496 Beginne einen POD-Block. Diese Instruktion ist nur nötig, wenn der
497 Block mit einem einfachen Text beginnt, denn I<jede> andere
498 POD-Instruktion beginnt ebenfalls einen POD-Block.
499  
500 =head4 Example
501  
502 $pg->pod;
503  
504 erzeugt
505  
506 =pod
507  
508 =cut
509  
510 # -----------------------------------------------------------------------------
511  
512 1 ▶sub pod {
Subroutine pod: called CC=1 SCAR=0.0
Pod documented
513 1 my $self = shift;
514 1 return "=pod\n\n";
515 }
516  
517 # -----------------------------------------------------------------------------
518  
519 =head3 cut() - Beende POD-Block
520  
521 =head4 Synopsis
522  
523 $pod = $pg->cut;
524  
525 =head4 Description
526  
527 Beende einen POD-Block. Diese Instruktion ist nicht nötig, wenn
528 danach kein Perl-Code folgt.
529  
530 =head4 Example
531  
532 $pg->cut;
533  
534 erzeugt
535  
536 =cut
537  
538 =cut
539  
540 # -----------------------------------------------------------------------------
541  
542 1 ▶sub cut {
Subroutine cut: called CC=1 SCAR=0.0
Pod documented
543 1 my ($self,$encoding) = @_;
544 1 return "=cut\n\n";
545 }
546  
547 # -----------------------------------------------------------------------------
548  
549 =head2 Format-Codes
550  
551 =head3 fmt() - Format-Code
552  
553 =head4 Synopsis
554  
555 $str = $this->fmt($type,$text);
556  
557 =head4 Description
558  
559 Erzeuge Inline-Segment vom Typ $type (B, I, C usw.)
560 und liefere den resultierenden POD-Code dieses zurück.
561  
562 Die Methode sorgt dafür, dass das Segment korrekt generiert wird,
563 wenn in $text die Zeichen '<' oder '>' vorkommen.
564  
565 =head4 Examples
566  
567 Nomal:
568  
569 $pg->fmt('C','$x');
570 =>
571 C<$x>
572  
573 1x > eingebettet:
574  
575 $pg->fmt('C','$class->new()');
576 =>
577 C<< $class->new() >>
578  
579 2x > eingebettet:
580  
581 $pg->fmt('C','$x >> $y');
582 =>
583 C<<< $x >> $y >>>
584  
585 =cut
586  
587 # -----------------------------------------------------------------------------
588  
589 4 ▶sub fmt {
Subroutine fmt: called CC=6 SCAR=18.0
Pod documented
590 4 my ($this,$type,$text) = @_;
591  
592 4 my $maxL = 0;
593 4 ▶ while ($text =~ /(>+|<+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /(>+|<+)/g) 4 4 8
594 4 my $l = length($1);
595 4 ▶ if ($l > $maxL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l > $maxL) 3 1 4
596 3 $maxL = $l;
597 }
598 }
599 4 ▶ if ($maxL or $text =~ /^</ or $text =~ />$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maxL or $text =~ /^</ or $text =~ />$/) 3 1 4
Condition66%4/6
$maxL or $text =~ /^</ or $text =~ />$/
l!l&&r!l&&!r
301
$maxL or $text =~ /^</
l!l&&r!l&&!r
301
MC/DC33%1/3
$maxL or $text =~ /^</ or $text =~ />$/
$maxL $text =~ /^</ $text =~ />$/
Truth table50%2/4
A or B or C
A= $maxLB= $text =~ /^</C= $text =~ />$/
ABCresult
1XX1
01X1
0011
0000
600 3 $text = " $text ";
601 }
602 4 $maxL++;
603  
604 4 return sprintf '%s%s%s%s',$type,'<'x$maxL,$text,'>'x$maxL;
605 }
606  
607 # -----------------------------------------------------------------------------
608  
609 =head1 VERSION
610  
611 1.240
612  
613 =head1 AUTHOR
614  
615 Frank Seitz, L<http://fseitz.de/>
616  
617 =head1 COPYRIGHT
618  
619 Copyright (C) 2026 Frank Seitz
620  
621 =head1 LICENSE
622  
623 This code is free software; you can redistribute it and/or modify
624 it under the same terms as Perl itself.
625  
626 =cut
627  
628 # -----------------------------------------------------------------------------
629  
630 1 1;
631  
632 # eof
« Quiq/Database/Row.pm ↑ Summary Quiq/Database/Config.pm »