Quiq/Html/Verbatim.pm

filter: Statementstmt 93.0% 40 / 43 Branchbran 58.3% 7 / 12 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 82.6% 57 / 69 CC 8 SCAR 17.3
CC
8
Cov
83%
html
9.2
BEGIN
1.0
BEGIN
1.0
CRAP 5.7
« Quiq/TeX/Code.pm ↑ Summary Quiq/Logger.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Html::Verbatim - Verbatim-Block in HTML
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 DESCRIPTION
14  
15 Ein Objekt der Klasse repräsentiert einen Verbatim-Block in
16 HTML. Ein Verbatim-Block gibt einen Text TEXT in
17 Festbreitenschrift mit allen Leerzeichen und Zeilenumbrüchen
18 identisch wieder. Innerhalb von TEXT ist HTML erlaubt. Der
19 Verbatim-Block kann mit Zeilennummern ausgestattet werden.
20 In dem Fall wird das HTML-Konstrukt um eine <table> ergänzt
21 und dadurch komplexer.
22  
23 Aufbau eines Verbatim-Blocks I<ohne> Zeilennummern:
24  
25 <div class="CLASS" [id="ID"] [style="STYLE"]>
26 <pre>TEXT</pre>
27 </div>
28  
29 Aufbau eines Verbatim-Blocks I<mit> Zeilennummern:
30  
31 <div class="CLASS" [id="ID"] [style="STYLE"]>
32 <table>
33 <tr>
34 <td class="ln">
35 <pre>ZEILENNUMMERN</pre>
36 </td>
37 <td class="margin"></td>
38 <td class="text">
39 <pre>TEXT</pre>
40 </td>
41 </tr>
42 </table>
43 </div>
44  
45 Die in eckige Klammern eingefassten Bestandteile ([...]) sind
46 optional.
47  
48 Das umgebende C<div> klammert das gesamte Konstrukt und ermöglicht
49 auch im Falle von Zeilennummer, dass der Hintergrund des Blocks
50 über die gesamte Breite der Seite farbig hinterlegt werden kann.
51  
52 Das Aussehen des Verbatim-Block kann via CSS gestaltet werden.
53 Hier die Selektoren, mit denen einzelne Bestandteile des
54 Konstrukts in CSS angesprochen werden können:
55  
56 =over 4
57  
58 =item .CLASS
59  
60 Der gesamte Block.
61  
62 =item .CLASS > pre
63  
64 Der Verbatim-Text im Falle einer Darstellung ohne Zeilennummern.
65  
66 =item .CLASS table
67  
68 Die Tabelle im Falle von Zeilennummern.
69  
70 =item .CLASS .ln
71  
72 Die Zeilennummern-Spalte.
73  
74 =item .CLASS .margin
75  
76 Die Trenn-Spalte zwischen Zeilennummer- und Text-Spalte.
77  
78 =item .CLASS .text
79  
80 Die Text-Spalte.
81  
82 =back
83  
84 Hierbei ist CLASS der über das Attribut C<class> änderbare
85 CSS-Klassenname. Default-Klassenname ist 'verbatim'.
86  
87 =head1 ATTRIBUTES
88  
89 =over 4
90  
91 =item class => $name (Default: 'verbatim')
92  
93 CSS-Klasse des Verbatim-Blocks.
94  
95 =item id => $id
96  
97 Die CSS-Id des Verbatim-Blocks.
98  
99 =item ln => $n (Default: 0)
100  
101 Wenn ungleich 0, wird jeder Zeile eine Zeilennummer vorangestellt,
102 beginnend Zeilennummer $n.
103  
104 =item style => $style
105  
106 CSS-Properties des Verbatim-Blocks (<div>).
107  
108 =item text => $text
109  
110 Der dargestellte Text. Ist $text leer (C<undef> oder Leerstring),
111 wird kein Verbatim-Block erzeugt, d.h. die Methode $obj->html()
112 liefert einen Leerstring.
113  
114 =back
115  
116 =cut
117  
118 # -----------------------------------------------------------------------------
119  
120 package Quiq::Html::Verbatim;
121 1 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
122  
123 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
124 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
125 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
126  
127 1 our $VERSION = '1.240';
128  
129 1 ▶use Quiq::Html::Table::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
130  
131 # -----------------------------------------------------------------------------
132  
133 =head1 METHODS
134  
135 =head2 Konstruktor
136  
137 =head3 new() - Instantiiere Verbatim-Block-Objekt
138  
139 =head4 Synopsis
140  
141 $e = $class->new(@keyVal);
142  
143 =head4 Arguments
144  
145 =over 4
146  
147 =item @keyVal
148  
149 Liste von Attribut/Wert-Paaren. Die Werte werden auf dem Objekt
150 gesetzt. Siehe Abschnitt ATTRIBUTES.
151  
152 =back
153  
154 =head4 Returns
155  
156 =over 4
157  
158 =item $e
159  
160 Verbatim-Abschnitts-Objekt (Referenz)
161  
162 =back
163  
164 =head4 Description
165  
166 Instantiiere ein Verbatim-Block-Objekt und liefere eine
167 Referenz auf dieses Objekt zurück.
168  
169 =cut
170  
171 # -----------------------------------------------------------------------------
172  
173 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
174 1 my $class = shift;
175 # @_: @keyVal
176  
177 1 my $self = $class->SUPER::new(
178 class => 'verbatim',
179 id => undef,
180 ln => 0,
181 style => undef,
182 text => undef,
183 );
184 1 $self->set(@_);
185  
186 1 return $self;
187 }
188  
189 # -----------------------------------------------------------------------------
190  
191 =head2 Objektmethoden
192  
193 =head3 html() - Generiere HTML-Code
194  
195 =head4 Synopsis
196  
197 $html = $e->html($h);
198 $html = $class->html($h,@keyVal);
199  
200 =head4 Arguments
201  
202 =over 4
203  
204 =item $h
205  
206 Objekt für die HTML-Generierung, d.h. eine Instanz der Klasse
207 Quiq::Html::Tag.
208  
209 =item @keyVal
210  
211 Siehe Konstruktor.
212  
213 =back
214  
215 =head4 Returns
216  
217 HTML-Code (String)
218  
219 =head4 Description
220  
221 Generiere den HTML-Code des Verbatim-Blocks und liefere diesen
222 zurück. Als Klassenmethode gerufen, wird das Objekt intern
223 mit den Attributen @keyVal instantiiert.
224  
225 =cut
226  
227 # -----------------------------------------------------------------------------
228  
229 1 ▶sub html {
Subroutine html: called CC=8 SCAR=22.2
Pod documented
230 1 my $this = shift;
231 1 my $h = shift;
232  
233 1 ▶ my $self = ref $this? $this: $this->new(@_);
Branch: 1 branch
BranchTrueFalseTotal
ref $this ? : 0 1 1
234  
235 1 my ($class,$id,$ln,$style,$text) = $self->get(qw/class id ln style text/);
236  
237 1 ▶ if (!defined($text) || $text eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $text or $text eq "") 0 1 1
Condition33%1/3
not defined $text or $text eq ""
l!l&&r!l&&!r
001
MC/DC0%0/2
not defined $text or $text eq ""
not defined $text $text eq ""
Truth table33%1/3
not(A) or B
A= not defined $textB= $text eq ""
ABresult
1X1
011
000
238 # Wenn kein Text gegeben ist, wird kein Code generiert
239 0 return '';
240 }
241  
242 1 my $html;
243 1 ▶ if ($ln) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ln) { } 1 0 1
244 # Zeilennummern-, Margin, Code-Kolumne
245  
246 1 my $lnCount = $text =~ tr/\n//;
247 1 ▶ if (substr($text,-1,1) ne "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($text, -1, 1) ne "\n") 1 0 1
248 # Wenn die letzte Zeile nicht mit einem Newline endet,
249 # haben wir eine Zeilennummer mehr
250 1 $lnCount++;
251 }
252 1 my $lnLast = $ln + $lnCount - 1;
253  
254 1 my $tmp;
255 1 my $lnMaxWidth = length $lnLast;
256 1 ▶ for (my $i = $ln; $i <= $lnLast; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $lnLast) 1 1 2
257 1 ▶ if ($tmp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp) 0 1 1
258 0 $tmp .= "\n";
259 }
260 1 $tmp .= sprintf '%*d',$lnMaxWidth,$i;
261 }
262 1 push my @cols,
263 [class=>"ln",$h->tag('pre',style=>'margin: 0',$tmp)],
264 [class=>"margin",''],
265 ;
266  
267 # Text-Kolumne
268 1 push @cols,[class=>"text",$h->tag('pre',style=>'margin: 0',$text)];
269  
270 # Erzeuge Tabelle
271  
272 1 $html = Quiq::Html::Table::Simple->html($h,
273 border => 0,
274 cellpadding => undef,
275 cellspacing => undef,
276 rows => [
277 [@cols],
278 ],
279 );
280 }
281 else {
282 0 $html = $h->tag('pre',
283 $text
284 );
285 }
286  
287 1 return $h->tag('div',
288 class => $class,
289 id => $id,
290 style => $style,
291 '-',
292 $html
293 );
294 }
295  
296 # -----------------------------------------------------------------------------
297  
298 =head1 VERSION
299  
300 1.240
301  
302 =head1 AUTHOR
303  
304 Frank Seitz, L<http://fseitz.de/>
305  
306 =head1 COPYRIGHT
307  
308 Copyright (C) 2026 Frank Seitz
309  
310 =head1 LICENSE
311  
312 This code is free software; you can redistribute it and/or modify
313 it under the same terms as Perl itself.
314  
315 =cut
316  
317 # -----------------------------------------------------------------------------
318  
319 1 1;
320  
321 # eof
« Quiq/TeX/Code.pm ↑ Summary Quiq/Logger.pm »