Quiq/Html/Listing.pm

filter: Statementstmt 51.4% 52 / 101 Branchbran 24.1% 14 / 58 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 87.5% 7 / 8 Podpod 100% 2 / 2 totaltotal 41.8% 77 / 184 CC 34 SCAR 57.8
CC
34
Cov
40%
html
390.6
__ANON__
2.0
BEGIN
1.0
CRAP 324.1
« Quiq/LaTeX/Figure.pm ↑ Summary Quiq/Axis/Time.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Html::Listing - Programm-Listing in HTML
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Html::Base>
12  
13 =head1 SYNOPSIS
14  
15 use Quiq::Html::Listing;
16
17 my $h = Quiq::Html::Tag->new;
18
19 my $obj = Quiq::Html::Listing->new(
20 language => 'Perl',
21 lineNumbers => 1,
22 colNumbers => 79,
23 source => $file,
24 );
25
26 my $html = $obj->html($h);
27  
28 =head1 ATTRIBUTES
29  
30 =over 4
31  
32 =item anchor => 'doc'|'method'|undef (Default: 'doc')
33  
34 Setze Methodenanker an den Anfang der eingebetteten Dokumentation
35 zur Methode (im Fall von Perl der POD-Dokumentation) oder an
36 den Anfang der Methode selbst. Im Falle von C<undef> wird kein
37 Anker gesetzt.
38  
39 =item colNumbers => $n (Default: 0)
40  
41 Setze eine Zeile mit Kolumnennummern. Die Mindest-Zeilenlänge ist $n
42 (z.B. 79). Bei colNumbers=>0 werden keine Kolumnennummern gesetzt.
43  
44 =item escape => $bool (Default: 1)
45  
46 Schütze &, >, < in den Daten durch HTML-Entities. Wenn die Daten
47 bereits geschützt sind, kann dies mit escape=>0 abgeschaltet werden.
48 In dem Fall sind die ermittelten Zeilenlängen für Option
49 colNumbers u.U. zu groß.
50  
51 =item language => 'Perl' (Default: undef)
52  
53 Sprache. Aktuell nur 'Perl'.
54  
55 =item lineNumbers => $n (Default: 1)
56  
57 Setze die Zeilennummer an den Anfang jeder Zeile, beginnend
58 mit $n. Bei lineNumbers=>0 wird keine Zeilennummer gesetzt.
59  
60 =item minLineNumberWidth => $n (Default: 2)
61  
62 Minimale Breite der Zeilennummern-Spalte in Zeichen. Ungenutzte Stellen
63 werden mit Leerzeichen aufgefüllt.
64  
65 =item source => $filename -or- $strRef (Default: undef)
66  
67 Inhalt. Dieser kann aus einer Datei oder einem String kommen.
68  
69 =back
70  
71 =head1 EXAMPLE
72  
73 Programm:
74  
75 1: require R1::HtmlTag;
76 2: require R1::Html::Listing;
77 3:
78 4: my $h = R1::HtmlTag->new;
79 5:
80 6: my $text = << '__PERL__';
81 7: #!/usr/bin/perl
82 8:
83 9: =encoding utf8
84 10:
85 11: Nur ein Demo-Programm.
86 12:
87 13: =cut
88 14:
89 15: print "Hello world!\n";
90 16:
91 17: # eof
92 18: __PERL__
93 19:
94 20: my $html = R1::Html::Listing->html($h,
95 21: cssPrefix=>'sdoc-code',
96 22: language=>'Perl',
97 23: source=>\$text,
98 24: );
99  
100 Ergebnis:
101  
102 1:
103  
104 Im Browser:
105  
106 =cut
107  
108 # -----------------------------------------------------------------------------
109  
110 package Quiq::Html::Listing;
111 1 ▶use base qw/Quiq::Html::Base/;
Subroutine BEGIN: called CC=1 SCAR=0.0
112  
113 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
114 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
115 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
116  
117 1 our $VERSION = '1.240';
118  
119 1 ▶use Quiq::FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
120  
121 # -----------------------------------------------------------------------------
122  
123 =head1 METHODS
124  
125 =head2 Konstruktor
126  
127 =head3 new() - Konstruktor
128  
129 =head4 Synopsis
130  
131 $obj = $class->new(@keyVal);
132  
133 =cut
134  
135 # -----------------------------------------------------------------------------
136  
137 3 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
138 3 my $class = shift;
139 # @_: @keyVal
140  
141 3 my $self = $class->SUPER::new(
142 anchor => 'doc',
143 colNumbers => 0,
144 cssPrefix => 'listing',
145 escape => 1,
146 language => undef,
147 lineNumbers => 1,
148 minLineNumberWidth => 1,
149 source => undef,
150 );
151 3 $self->set(@_);
152  
153 3 return $self;
154 }
155  
156 # -----------------------------------------------------------------------------
157  
158 =head2 Objektmethoden
159  
160 =head3 html() - Generiere HTML
161  
162 =head4 Synopsis
163  
164 $html = $obj->html($h);
165 $html = $class->html($h,@keyVal);
166  
167 =cut
168  
169 # -----------------------------------------------------------------------------
170  
171 1 ▶sub html {
Subroutine html: called CC=34 SCAR=59.7
Pod documented
172 1 my $this = shift;
173 1 my $h = shift;
174  
175 1 ▶ my $self = ref $this? $this: $this->new(@_);
Branch: 1 branch
BranchTrueFalseTotal
ref $this ? : 0 1 1
176  
177 1 my ($anchor,$colNumbers,$cssPrefix,$escape,$id,$lang,$i,$lnWidth,
178 $source) =
179 $self->get(qw/anchor colNumbers cssPrefix escape id language
180 lineNumbers minLineNumberWidth source/);
181  
182 1 my $noLn;
183 1 ▶ if ($i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i) { } 1 0 1
184 1 ▶ if (ref $source) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $source) 1 0 1
185 1 my $n = $$source =~ tr/\n//;
186 1 my $l = length $n;
187 1 ▶ $lnWidth = $l if $l > $lnWidth;
Branch: 1 branch
BranchTrueFalseTotal
if $l > $lnWidth 0 1 1
188 }
189 }
190 else {
191 0 $noLn = 1;
192 0 $i = 1; # für odd/even-Unterscheidung
193 }
194  
195 # Pattern für Anker-Setzung
196  
197 1 my $anchorPat;
198 1 ▶ if ($lang && $anchor) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang and $anchor) 0 1 1
Condition33%1/3
$lang and $anchor
!ll&&!rl&&r
100
MC/DC0%0/2
$lang and $anchor
$lang $anchor
Truth table33%1/3
A and B
A= $langB= $anchor
ABresult
0X0
100
111
199 0 ▶ if ($lang eq 'Perl') {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang eq "Perl") 0 0 0
200 0 ▶ if ($anchor eq 'doc') {
Branch: 2 branches
BranchTrueFalseTotal
if ($anchor eq 'doc') { } 0 0 0
elsif ($anchor eq 'method') { } 0 0 0
201 0 $anchorPat = qr/^=head\d\s+(\w+)\(\)/;
202 }
203 elsif ($anchor eq 'method') {
204 0 $anchorPat = qr/^sub\s+(\w+)/;
205 }
206 else {
207 0 $self->throw(
208 'LISTING-00002: Unbekannter Anker-Typ',
209 Anchor => $anchor,
210 );
211 }
212 }
213 }
214  
215 1 my $html = '';
216 1 my $maxLen = 0;
217 1 my $isDoc = 0; # Dokumentationsabschnitt (bei Perl POD-Abschnitt)
218  
219 1 my $fh = Quiq::FileHandle->new('<',$source);
220 1 for my $line (<$fh>) {
221 3 chomp $line;
222  
223 # Ermittele längste Zeile
224  
225 3 ▶ if (my $l = length $line) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $l = length $line) 3 0 3
226 3 ▶ if ($l > $maxLen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l > $maxLen) 1 2 3
227 1 $maxLen = $l;
228 }
229 }
230  
231 # Daten schützen
232  
233 3 ▶ if ($escape) {
Branch: 1 branch
BranchTrueFalseTotal
if ($escape) 3 0 3
234 3 $line =~ s/&/&amp;/g;
235 3 $line =~ s/</&lt;/g;
236 3 $line =~ s/>/&gt;/g;
237 }
238  
239 3 ▶ if ($lang && $line ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang and $line ne "") 0 3 3
Condition33%1/3
$lang and $line ne ""
!ll&&!rl&&r
300
MC/DC0%0/2
$lang and $line ne ""
$lang $line ne ""
Truth table33%1/3
A and B
A= $langB= $line ne ""
ABresult
0X0
100
111
240 # <span> für Perl- und CoTeDo-Kommentare
241  
242 0 ▶ my $commentSub = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
243 0 my ($h,$prefix,$str) = @_;
244 0 return $h->tag('span',
245 class => "$prefix-comment",
246 '-',
247 $str
248 );
249 0 };
250  
251 0 ▶ if ($lang eq 'Perl') {
Branch: 2 branches
BranchTrueFalseTotal
if ($lang eq 'Perl') { } 0 0 0
elsif ($lang eq 'CoTeDo') { } 0 0 0
252 0 my $html;
253  
254 0 ▶ if ($line =~ /^=\w/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^=\w/) 0 0 0
255 0 $isDoc = 1;
256 }
257  
258 # Methoden-Anker
259  
260 0 ▶ if ($anchorPat && $line =~ /$anchorPat/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($anchorPat and $line =~ /$anchorPat/) 0 0 0
Condition0%0/3
$anchorPat and $line =~ /$anchorPat/
!ll&&!rl&&r
000
MC/DC0%0/2
$anchorPat and $line =~ /$anchorPat/
$anchorPat $line =~ /$anchorPat/
Truth table0%0/3
A and B
A= $anchorPatB= $line =~ /$anchorPat/
ABresult
0X0
100
111
261 # FIXME: Liste der Links merken um sie gegenüber
262 # Sdoc registrieren zu können. Dann l{} statt U{}.
263  
264 0 $html = $h->tag('a',
265 name => "perl_method_$1",
266 );
267 }
268  
269 # Dokumentationszeilen und Kommentare kennzeichnen
270  
271 0 ▶ if ($isDoc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isDoc) { } 0 0 0
272 # <span> für Dokumentations-Zeile
273  
274 0 ▶ if ($line =~ /^=over/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^=over/) { } 0 0 0
275 # =over-Zeile hat insgesamt keinen Content
276 0 $html .= $line;
277 }
278 else {
279 # Aufteilung von POD-Kommando und Content
280
281 0 $line =~ s/^(=\w+)//;
282 0 my $podCmd = $1;
283 0 ▶ if ($podCmd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($podCmd) 0 0 0
284 0 $html .= $podCmd;
285 }
286 0 $html .= $h->tag('span',
287 class => "$cssPrefix-doc",
288 '-',
289 $line
290 );
291 0 ▶ if ($podCmd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($podCmd) 0 0 0
292 0 $line = "$podCmd$line";
293 }
294 }
295 }
296 else {
297 0 (my $tmp = $line) =~ s/((^|\s)#.*)/
298 0 $commentSub->($h,$cssPrefix,$1)/e;
299 0 $html .= $tmp;
300 }
301  
302 0 ▶ if ($line =~ /^=cut/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^=cut/) 0 0 0
303 0 $isDoc = 0;
304 }
305  
306 0 $line = $html;
307 }
308 elsif ($lang eq 'CoTeDo') {
309 0 ▶ if ($line =~ /^# (\[|\&lt;|eof)/) { # Entity-Zeile
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^# (\[|\&lt;|eof)/) { } 0 0 0
elsif ($line =~ /^\w+:$/) { } 0 0 0
310 0 $line = $h->tag('span',
311 class => "$cssPrefix-entity",
312 '-',
313 $line
314 );
315 }
316 elsif ($line =~ /^\w+:$/) { # keyRegex SectionParser
317 0 $line = $h->tag('span',
318 class => "$cssPrefix-key",
319 '-',
320 $line
321 );
322 }
323 else {
324 0 $line =~ s/((^|\s)\!\!.*)/
325 0 $commentSub->($h,$cssPrefix,$1)/e;
326 }
327 }
328 else {
329 0 $self->throw(
330 'LISTING-00001: Unbekannte Sprache',
331 Language => $lang,
332 );
333 }
334 }
335 3 ▶ if ($line eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq "") 0 3 3
336 0 $line = '&nbsp;';
337 }
338  
339 3 ▶ $html .= $h->tag('tr',
Branch: 1 branch
BranchTrueFalseTotal
$i % 2 ? : 2 1 3
340 class => sprintf("$cssPrefix-tr-%s",$i%2? 'odd': 'even'),
341 '-',
342 $h->tag('td',
343 -ignoreIf => $noLn,
344 class => "$cssPrefix-td-ln",
345 sprintf('%*d',$lnWidth,$i++)
346 ),
347 $h->tag('td',
348 class => "$cssPrefix-td-line",
349 $line
350 )
351 );
352 }
353 1 $fh->close;
354  
355 1 ▶ if ($colNumbers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colNumbers) 0 1 1
356 0 my $str = '1';
357 0 ▶ my $max = $maxLen > $colNumbers? $maxLen: $colNumbers;
Branch: 1 branch
BranchTrueFalseTotal
$maxLen > $colNumbers ? : 0 0 0
358 0 my $limit = int($max/10)*10;
359 0 ▶ for (my $i = 10; $i <= $limit; $i += 10) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $limit) 0 0 0
360 0 $str .= '.' x ($i-length($str)-1);
361 0 $str .= $i;
362 }
363 0 my $l = $max-length($str);
364 0 ▶ if ($l > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l > 0) 0 0 0
365 0 $str .= '.' x $l;
366 }
367  
368 0 $html = $h->tag('tr','-',
369 $h->tag('td',
370 class => "$cssPrefix-td-edge",
371 ),
372 $h->tag('td',
373 class => "$cssPrefix-td-cn",
374 $str
375 )
376 ).
377 $html;
378 }
379  
380 1 $html = $h->tag('table',
381 id => $id,
382 class => "$cssPrefix-table",
383 cellpadding => 0,
384 cellspacing => 0,
385 $html
386 );
387  
388 1 return $html;
389 }
390  
391 # -----------------------------------------------------------------------------
392  
393 =head1 DETAILS
394  
395 =head2 CSS-Klassen
396  
397 PREFIX-table|Das gesamte Konstrukt (Tabelle)
398 PREFIX-tr-odd|Ungerade Zeile
399 PREFIX-tr-even|Gerade Zeile
400 PREFIX-td-ln|Zelle für Zeilennummer
401 PREFIX-td-cn|Zelle für Kolumnennummer
402 PREFIX-td-edge|Eckzelle Kolumnennummer/Zeilennummer
403 PREFIX-td-line|Zelle für Zeileninhalt
404 PREFIX-doc|Kennzeichnung Doku (bei Perl POD)
405 PREFIX-comment|Kennzeichnung Kommentar
406  
407 =head1 VERSION
408  
409 1.240
410  
411 =head1 AUTHOR
412  
413 Frank Seitz, L<http://fseitz.de/>
414  
415 =head1 COPYRIGHT
416  
417 Copyright (C) 2026 Frank Seitz
418  
419 =head1 LICENSE
420  
421 This code is free software; you can redistribute it and/or modify
422 it under the same terms as Perl itself.
423  
424 =cut
425  
426 # -----------------------------------------------------------------------------
427  
428 1 1;
429  
430 # eof
« Quiq/LaTeX/Figure.pm ↑ Summary Quiq/Axis/Time.pm »