lib/PPI/HTML/CodeFolder.pm

filter: Statementstmt 89.8% 239 / 266 Branchbran 61.0% 83 / 136 Conditioncond 36.5% 15 / 41 MC/DCmcdc 11.5% 3 / 26 Subroutinesub 91.3% 21 / 23 Podpod 100% 11 / 11 totaltotal 73.9% 372 / 503 CC 97 SCAR 28.1
CC
97
Cov
76%
html
40.1
_pathAdjust
27.3
_extractXRef
13.4
CRAP 16.5
↑ Summary lib/PPI/HTML/mkhtmlcf.pl »
1 =pod
2  
3 =begin classdoc
4  
5 Subclasses <cpan>PPI::HTML</cpan> to add code folding for POD,
6 comments, and 'use'/'require' statements. Optionally permits abbreviation
7 of standard PPI::HTML class/token names with user specified
8 replacements. For line number output, moves the line numbers
9 from individual &lt;span&gt;'s to a single table column,
10 with the source body in the 2nd column.
11 <p>
12 Copyright&copy; 2007, Presicient Corp., USA
13 All rights reserved.
14 <p>
15 Permission is granted to use this software under the terms of the
16 <a href='http://perldoc.perl.org/perlartistic.html'>Perl Artisitic License</a>.
17  
18 @author D. Arnold
19 @since 2007-01-22
20 @self $self
21  
22  
23 =end classdoc
24  
25 =cut
26  
27  
28 package PPI::HTML::CodeFolder;
29  
30 2 ▶use PPI::HTML;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use base ('PPI::HTML');
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 2 our $VERSION = '1.01';
37  
38 2 our %classabvs = qw(
39  
40 arrayindex ai
41 backtick bt
42 cast cs
43 comment ct
44 core co
45 data dt
46 double db
47 end en
48 heredoc hd
49 heredoc_content hc
50 heredoc_terminator ht
51 interpolate ip
52 keyword kw
53 label lb
54 line_number ln
55 literal ll
56 magic mg
57 match mt
58 number nm
59 operator op
60 pod pd
61 pragma pg
62 prototype pt
63 readline rl
64 regex re
65 regexp re
66 separator sp
67 single sg
68 structure st
69 substitute su
70 symbol sy
71 transliterate tl
72 word wo
73 words wd
74  
75 );
76 #
77 # fold section regular expressions
78 #
79 2 my %foldres = (
80 Whitespace => [
81 qr/\G(?<=<pre>)((?:\s*<br>)+)/,
82 qr/\G.*?<br>((?:\s*<br>)+)/
83 ],
84 Comments => [
85 qr/\G(?<=<pre>)\s*(<span\s+class=['"]comment['"]>.+?<\/span>)(?=<br>)/,
86 qr/\G.*?<br>\s*(<span\s+class=['"]comment['"]>.+?<\/span>)(?=<br>)/
87 ],
88 POD => [
89 qr/\G(?<=<pre>)\s*(<span\s+class=['"]pod['"]>.+?<\/span>)(?=<br>)/,
90 qr/\G.*?<br>\s*(<span\s+class=['"]pod['"]>.+?<\/span>)(?=<br>)/
91 ],
92 Heredocs => [
93 qr/\G(?<=<pre>)\s*(<span\s+class=['"]heredoc_content['"]>.+?<\/span>)(?=<br>)/,
94 qr/\G.*?<br>\s*(<span\s+class=['"]heredoc_content['"]>.+?<\/span>)(?=<br>)/
95 ],
96 Imports => [
97 qr/\G(?<=<pre>)\s*
98 (
99 (?:<span\s+class=['"]keyword['"]>(?:use|require)<\/span>.+?;\s*)+
100 (?:<span\s+class=['"]comment['"]>.+?<\/span>)?
101 )
102 (?=<br>)
103 /x,
104 qr/\G.*?<br>\s*
105 (
106 (?:<span\s+class=['"]keyword['"]>(?:use|require)<\/span>.+?;\s*)+
107 (?:<span\s+class=['"]comment['"]>.+?<\/span>)?
108 )
109 (?=<br>)
110 /x
111 ],
112 );
113  
114 #
115 # folddiv CSS
116 #
117 2 our $ftcss = <<'EOFTCSS';
118  
119 .popupdiv {
120 font-family: fixed, Courier;
121 font-size: 8pt;
122 font-style: normal;
123 /* lineheight: 10pt; */
124 border:solid 1px #666666;
125 padding:4px;
126 position:absolute;
127 z-index:100;
128 visibility: hidden;
129 color: black;
130 top:10px;
131 left:20px;
132 width:auto;
133 height:auto;
134 background-color:#ffffcc;
135 layer-background-color:#ffffcc;
136 /* opacity: .9;
137 filter: alpha(opacity=90); */
138 overflow : hidden; // to keep FF on OS X happy
139 }
140  
141 .folddiv {
142 position: absolute;
143 visibility: hidden;
144 overflow : hidden; // to keep FF on OS X happy
145 }
146  
147 .bodypre {
148 font-family: fixed, Courier;
149 font-size: 9pt;
150 line-height: 13pt;
151 text-align: left;
152 color: black;
153 }
154  
155 .lnpre {
156 font-family: fixed, Courier;
157 font-size: 9pt;
158 line-height: 13pt;
159 text-align: right;
160 color: #666666;
161 }
162  
163 .foldfill {
164 font-family: fixed, Courier;
165 font-size: 8pt;
166 font-style: italic;
167 line-height: 13pt;
168 color: blue;
169 }
170  
171 .foldbtn {
172 font-family: fixed, Courier;
173 font-size: 9pt;
174 line-height: 13pt;
175 color: blue;
176 }
177  
178 -->
179 </style>
180 EOFTCSS
181 #
182 # fold expansion javascript
183 #
184 2 our $ftjs = <<'EOFTJS';
185  
186 function ppiHtmlCF(startlines, endlines)
187 {
188 this.startlines = startlines;
189 this.endlines = endlines;
190 this.foldstate = [];
191 for (var i = 0; i < startlines.length; i++)
192 this.foldstate[startlines[i]] = "closed";
193 }
194  
195 // Clears the cookie
196 ppiHtmlCF.prototype.clearCookie = function()
197 {
198 var now = new Date();
199 var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);
200 this.setCookie('', yesterday);
201 };
202  
203 // Sets value in the cookie
204 ppiHtmlCF.prototype.setCookie = function(cookieValue, expires)
205 {
206 document.cookie =
207 'ppihtmlcf=' + escape(cookieValue) + ' ' +
208 + (expires ? '; expires=' + expires.toGMTString() : '')
209 + ' path=' + location.pathname;
210 };
211  
212 // Gets the cookie
213 ppiHtmlCF.prototype.getCookie = function() {
214 var cookieValue = '';
215 var posName = document.cookie.indexOf('ppihtmlcf=');
216 if (posName != -1) {
217 var posValue = posName + 'ppihtmlcf='.length;
218 var endPos = document.cookie.indexOf(';', posValue);
219 if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));
220 else cookieValue = unescape(document.cookie.substring(posValue));
221 }
222 return (cookieValue);
223 };
224  
225 // updates cookie with current set of unfolded section startlines as a string
226 ppiHtmlCF.prototype.updateCookie = function()
227 {
228 var str = ':';
229 for (var n = 0; n < this.startlines.length; n++) {
230 line = this.startlines[n];
231 if ((this.foldstate[line] != null) && (this.foldstate[line] == "open"))
232 str += line + ':';
233 }
234 if (str == ':')
235 this.setCookie('');
236 else
237 this.setCookie(str);
238 };
239  
240 // forces all folds to current cookie state
241 ppiHtmlCF.prototype.openFromCookie = function()
242 {
243 var opened = this.getCookie();
244 if (opened == '') {
245 /*
246 * no cookie, create one w/ all folds closed
247 */
248 this.setCookie('');
249 }
250 else {
251 for (var n = 0; n < this.startlines.length; n++) {
252 line = this.startlines[n];
253 if (this.foldstate[line] == null)
254 this.foldstate[line] = "closed";
255  
256 if (opened.indexOf(':' + line + ':') >= 0) {
257 if (this.foldstate[line] == "closed")
258 this.accordian(line, this.endlines[n]);
259 }
260 else {
261 if (this.foldstate[line] == "open")
262 this.accordian(line, this.endlines[n]);
263 }
264 }
265 }
266 };
267  
268 /*
269 * renders line number and fold button margins
270 */
271 ppiHtmlCF.prototype.renderMargins = function(lastline)
272 {
273 var start = 1;
274 var lnmargin = '';
275 var btnmargin = '';
276 for (var i = 0; i < this.startlines.length; i++) {
277 if (start != this.startlines[i]) {
278 for (var j = start; j < this.startlines[i]; j++) {
279 lnmargin += j + "\n";
280 btnmargin += "\n";
281 }
282 }
283 start = this.endlines[i] + 1;
284 lnmargin += "<span id='lm" + this.startlines[i] + "' class='lnpre'>" + this.startlines[i] + "</span>\n";
285 btnmargin += "<a id='ll" + this.startlines[i] + "' class='foldbtn' " +
286 "onclick=\"ppihtml.accordian(" + this.startlines[i] + "," + this.endlines[i] + ")\">&oplus;</a>\n";
287 }
288 if (lastline > this.endlines[this.endlines.length - 1]) {
289 for (var j = start; j <= lastline; j++) {
290 lnmargin += j + "\n";
291 btnmargin += "\n";
292 }
293 }
294 lnmargin += "\n";
295 btnmargin += "\n";
296 buttons = document.getElementById("btnmargin");
297 lnno = document.getElementById("lnnomargin");
298 if (navigator.appVersion.indexOf("MSIE")!=-1) {
299 lnno.outerHTML = "<pre id='lnnomargin' class='lnpre'>" + lnmargin + "</pre>";
300 buttons.outerHTML = "<pre id='btnmargin' class='lnpre'>" + btnmargin + "</pre>";
301 }
302 else {
303 lnno.innerHTML = lnmargin;
304 buttons.innerHTML = btnmargin;
305 }
306 }
307  
308 /*
309 * Accordian function for folded code
310 *
311 * if clicked fold is closed
312 * replace contents of specified lineno margin span
313 * with complete lineno list
314 * replace contents of specified link span with end - start + 1 oplus's + linebreaks
315 * replace contents of insert span with contents of src_div
316 * foldstate = open
317 * else
318 * replace contents of specified lineno margin span with start lineno
319 * replace contents of specified link span with single oplus
320 * replace contents of specified insert span with "Folded lines start to end"
321 * foldstate = closed
322 *
323 * For fancier effect, use delay to add/remove a single line at a time, with
324 * delay millsecs between updates
325 */
326 ppiHtmlCF.prototype.accordian = function(startline, endline)
327 {
328 if (document.getElementById) {
329 if (navigator.appVersion.indexOf("MSIE")!=-1) {
330 this.ie_accordian(startline, endline);
331 }
332 else {
333 this.ff_accordian(startline, endline);
334 }
335 }
336 }
337 /*
338 * MSIE is a pile of sh*t, so we have to bend over
339 * backwards and completely rebuild the document elements
340 * Bright bunch of folks up there in Redmond...BTW this bug
341 * exists in IE 4 thru 7, despite people screaming for a solution
342 * for a decade. So MSFT is deaf as well as dumb
343 */
344 ppiHtmlCF.prototype.ie_accordian = function(startline, endline)
345 {
346 src = document.getElementById("preft" + startline);
347 foldbtn = document.getElementById('btnmargin');
348 lineno = document.getElementById('lnnomargin');
349 insert = document.getElementById("src" + startline);
350 linenos = lineno.innerHTML;
351 buttons = foldbtn.innerHTML;
352 if ((this.foldstate[startline] == null) || (this.foldstate[startline] == "closed")) {
353 lnr = "<span[^>]+>" + startline + "[\r\n]*</span>";
354 lnre = new RegExp(lnr, "i");
355 bnr = "id=ll" + startline + "[^<]+</a>";
356 btnre = new RegExp(bnr, "i");
357  
358 lnfill = startline + "\n";
359 btnfill = "&oplus;\n";
360 for (i = startline + 1; i <= endline; i++) {
361 lnfill += i + "\n";
362 btnfill += "&oplus;\n";
363 }
364  
365 linenos = linenos.replace(lnre, "<span id='lm" + startline + "' class='lnpre'>" + lnfill + "</span>");
366 buttons = buttons.replace(btnre, "id='ll" + startline + "' class='foldbtn' style='background-color: yellow' onclick=\"ppihtml.accordian(" +
367 startline + ", " + endline + ")\">" + btnfill + "</a>");
368  
369 foldbtn.outerHTML = "<pre id='btnmargin' class='lnpre'>" + buttons + "</pre>";
370 lineno.outerHTML = "<pre id='lnnomargin' class='lnpre'>" + linenos + "</pre>";
371 insert.outerHTML = "<span id='src" + startline + "'><pre class='bodypre'>" + src.innerHTML + "</pre></span>";
372 this.foldstate[startline] = "open";
373 }
374 else {
375 lnr = "<span[^>]+>" + startline + "[\r\n][^<]*</span>";
376 lnre = new RegExp(lnr, "i");
377 bnr = "id=ll" + startline + "[^<]+</a>";
378 btnre = new RegExp(bnr, "i");
379  
380 if (! linenos.match(lnre))
381 alert("linenos no match");
382 if (! buttons.match(btnre))
383 alert("buttons no match");
384 linenos = linenos.replace(lnre, "<span id='lm" + startline + "' class='lnpre'>" + startline + "\n</span>");
385 buttons = buttons.replace(btnre, "id='ll" + startline + "' class='foldbtn' style='background-color: #E9E9E9' onclick=\"ppihtml.accordian(" +
386 startline + ", " + endline + ")\">&oplus;\n</a>");
387  
388 foldbtn.outerHTML = "<pre id='btnmargin' class='lnpre'>" + buttons + "</pre>";
389 lineno.outerHTML = "<pre id='lnnomargin' class='lnpre'>" + linenos + "</pre>";
390 insert.outerHTML = "<span id='src" + startline + "'><pre class='foldfill'>Folded lines " + startline + " to " + endline + "</pre></span>";
391  
392 this.foldstate[startline] = "closed";
393 }
394 this.updateCookie();
395 }
396  
397 ppiHtmlCF.prototype.ff_accordian = function(startline, endline)
398 {
399 src = document.getElementById("preft" + startline);
400 foldbtn = document.getElementById("ll" + startline);
401 lineno = document.getElementById("lm" + startline);
402 insert = document.getElementById("src" + startline);
403 if ((this.foldstate[startline] == null) || (this.foldstate[startline] == "closed")) {
404 lnfill = startline + "\n";
405 btnfill = "&oplus;\n";
406 for (i = startline + 1; i <= endline; i++) {
407 lnfill += (i < endline) ? i + "\n" : i;
408 btnfill += (i < endline) ? "&oplus;\n" : "&oplus;";
409 }
410 foldbtn.innerHTML = btnfill;
411 lineno.innerHTML = lnfill;
412 foldbtn.style.backgroundColor = "yellow";
413 insert.innerHTML = src.innerHTML;
414 insert.className = "bodypre";
415 this.foldstate[startline] = "open";
416 }
417 else {
418 foldbtn.innerHTML = "&oplus;";
419 foldbtn.style.backgroundColor = "#E9E9E9";
420 lineno.innerHTML = startline;
421 insert.innerHTML = "Folded lines " + startline + " to " + endline;
422 insert.className = "foldfill";
423 this.foldstate[startline] = "closed";
424 }
425 this.updateCookie();
426 }
427  
428 /*
429 * open/close all folds
430 */
431 ppiHtmlCF.prototype.fold_all = function(foldstate)
432 {
433 for (i = 0; i < this.startlines.length; i++) {
434 line = this.startlines[i];
435 if (this.foldstate[line] == null)
436 this.foldstate[line] = "closed";
437  
438 if (this.foldstate[line] != foldstate)
439 this.accordian(line, this.endlines[i]);
440 }
441 this.updateCookie();
442 }
443  
444 ppiHtmlCF.prototype.add_fold = function(startline, endline)
445 {
446 this.startlines[this.startlines.length] = startline;
447 this.endlines[this.endlines.length] = endline;
448 this.foldstate[this.foldstate.length] = "closed";
449 }
450  
451 EOFTJS
452  
453 =pod
454  
455 =begin classdoc
456  
457 Constructor. Uses PPI::HTML base constructor, then installs some
458 additional members based on the <code>fold</code> argument.
459  
460 @optional colors hashref of <b>original</b> PPI::HTML classnames to color codes/names
461 @optional css a <cpan>CSS::Tiny</cpan> object containg additional stylsheet properties
462 @optional fold hashref of code folding properties; if not specified, a default
463 set of properties is applied. Folding properties include:
464 <ul>
465 <li>Abbreviate - hashref mapping full classnames to smaller classnames; useful
466 to provide further output compression; default uses predefined mapping
467 <li>Comments - if true, fold comments; default true
468 <li>Expandable - if true, provide links to unfold lines in place; default false
469 <li>Imports - if true, fold 'use' and 'require' statements; default false
470 <li>Javascript - name of file to reference for the fold expansion javascript in the output HTML;
471 default none, resulting in Javascript embedded in output HTML.<br>
472 Note that the Javascript may be retrieved separately via the <code>foldJavascript()</code> method.
473 <li>MinFoldLines - minimum number of consecutive foldable lines required before folding is applied;
474 default is 4
475 <li>POD - if true, fold POD line; default true
476 <li>Stylesheet - name of file to reference for the CSS for abbreviated classnames and
477 fold DIVs in the output HTML; default none,resulting in CSS embedded in output
478 HTML.<br>
479 Note that the CSS may be retrieved separately via the <code>foldCSS()</code> method.
480 <li>Tabs - size of tabs; default 4
481 </ul>
482  
483 @optional line_numbers if true, include line numbering in the output HTML
484 @optional page if true, wrap the output in a HTML &lt;head&gt; and &lt;body&gt;
485 sections. <b>NOTE: CodeFolder forces this to true.
486 @optional verbose if true, spews various diagnostic info
487  
488 @return a new PPI::HTML::CodeFolder object
489  
490 =end classdoc
491  
492 =cut
493  
494  
495 2 ▶sub new {
Subroutine new: called CC=7 SCAR=20.5
Pod documented
496 2 my ($class, %args) = @_;
497  
498 2 my $fold = delete $args{fold};
499 2 my $verb = delete $args{verbose};
500 #
501 # remove line numbering option since it greatly simplifies the spanning
502 # scan later; we'll apply it after we're done
503 #
504 2 my $needs_ln = delete $args{line_numbers};
505 #
506 # force page wrapping
507 #
508 2 $args{page} = 1;
509 2 my $self = $class->SUPER::new(%args);
510 return undef
511 2 ▶ unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 2 2
512  
513 2 $self->{_needs_ln} = $needs_ln;
514 2 $self->{_verbose} = $verb;
515 2 ▶ $self->{fold} = $fold ?
Branch: 1 branch
BranchTrueFalseTotal
$fold ? : 2 0 2
516 { %$fold } :
517 {
518 Abbreviate => \%classabvs,
519 Comments => 1,
520 Heredocs => 0,
521 Imports => 0,
522 Javascript => undef,
523 Expandable => 0,
524 MinFoldLines => 4,
525 POD => 1,
526 Stylesheet => undef,
527 Tabs => 4,
528 };
529  
530 $self->{fold}{Abbreviate} = \%classabvs
531 2 ▶ if $self->{fold}{Abbreviate} && (! (ref $self->{fold}{Abbreviate}));
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"fold"}{"Abbreviate"} and not ref $self->{"fold"}{"Abbreviate"} 2 0 2
Condition33%1/3
$self->{"fold"}{"Abbreviate"} and not ref $self->{"fold"}{"Abbreviate"}
!ll&&!rl&&r
002
MC/DC0%0/2
$self->{"fold"}{"Abbreviate"} and not ref $self->{"fold"}{"Abbreviate"}
$self->{"fold"}{"Abbreviate"} not ref $self->{"fold"}{"Abbreviate"}
Truth table33%1/3
A and not(B)
A= $self->{"fold"}{"Abbreviate"}B= not ref $self->{"fold"}{"Abbreviate"}
ABresult
0X0
100
111
532  
533 $self->{fold}{MinFoldLines} = 4
534 2 ▶ unless $self->{fold}{MinFoldLines};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"fold"}{"MinFoldLines"} 0 2 2
535  
536 $self->{fold}{Tabs} = 4
537 2 ▶ unless $self->{fold}{Tabs};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"fold"}{"Tabs"} 2 0 2
538 #
539 # keep a running package/method cross reference
540 #
541 2 $self->{_pkgs} = {};
542  
543 2 return $self;
544 }
545  
546 =pod
547  
548 =begin classdoc
549  
550 Returns the Javascript used for fold expansion.
551  
552 @return Javascript for fold expansion, as a string
553  
554 =end classdoc
555  
556 =cut
557  
558 1 ▶sub foldJavascript { return $ftjs; }
Subroutine foldJavascript: called CC=1 SCAR=0.0
Pod documented
559  
560  
561 =pod
562  
563 =begin classdoc
564  
565 Write out the Javascript used for fold expansion.
566  
567 @return 1 on success; undef on failure, with error message in $@
568  
569 =end classdoc
570  
571 =cut
572  
573 0 ▶sub writeJavascript {
Subroutine writeJavascript: not called CC=2 SCAR=17.9
Pod documented
574 0 ▶ $@ = $!,
Branch: 1 branch
BranchTrueFalseTotal
unless open OUTF, ">$_[1]" 0 0 0
575 return undef
576 unless open OUTF, ">$_[1]";
577 0 print OUTF $ftjs;
578 0 close OUTF;
579 0 return 1;
580 }
581  
582 =pod
583  
584 =begin classdoc
585  
586 Write out the CSS used for the sources.
587  
588 @return 1 on success; undef on failure, with error message in $@
589  
590 =end classdoc
591  
592 =cut
593  
594 0 ▶sub writeCSS {
Subroutine writeCSS: not called CC=2 SCAR=17.9
Pod documented
595 0 ▶ $@ = $!,
Branch: 1 branch
BranchTrueFalseTotal
unless open OUTF, ">$_[1]" 0 0 0
596 return undef
597 unless open OUTF, ">$_[1]";
598 0 print OUTF $_[0]->foldCSS();
599 0 close OUTF;
600 0 return 1;
601 }
602  
603 =pod
604  
605 =begin classdoc
606  
607 Returns the CSS used for the abbreviated classes and fold DIVs.
608  
609 @return CSS as a string
610  
611 =end classdoc
612  
613 =cut
614  
615  
616 2 ▶sub foldCSS {
Subroutine foldCSS: called CC=6 SCAR=18.2
Pod documented
617 2 my $self = shift;
618 2 my $orig_colors = exists $self->{colors};
619 2 ▶ my $css = $self->_css_html() || << 'EOCSS';
Condition50%1/2
$self->_css_html || qq[<style type="text/css">\n<!--\nbody {\n font-family: fixed, Courier;\n font-size: 10pt;\n}\n]
l!l
20
MC/DC0%0/1
$self->_css_html || qq[<style type="text/css">\n<!--\nbody {\n font-family: fixed, Courier;\n font-size: 10pt;\n}\n]
$self->_css_html
Truth table50%1/2
A
A= $self->_css_html
Aresult
11
00
620 <style type="text/css">
621 <!--
622 body {
623 font-family: fixed, Courier;
624 font-size: 10pt;
625 }
626 EOCSS
627  
628 2 my $ftc = $ftcss;
629 2 ▶ if ($self->{colors}{line_number}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"colors"}{"line_number"}) 2 0 2
630 2 my $lnc = $self->{colors}{line_number};
631 2 $ftc=~s/(.lnpre\s+.+?color: )#888888;/$1$lnc;/gs;
632 }
633  
634 2 ▶ delete $self->{colors} unless $orig_colors;
Branch: 1 branch
BranchTrueFalseTotal
unless $orig_colors 0 2 2
635 2 $css=~s|-->\s*</style>||s;
636 #
637 # !!!fix for (yet another) Firefox bug: need a dummy class
638 # at front of CSS or firefox ignores the first class...
639 #
640 2 $css=~s/(<!--.*?\n)/$1\n\n.dummy_class_for_firefox { color: white; }\n/;
641 #
642 # replace classes w/ abbreviations
643 #
644 2 ▶ if ($self->{fold}{Abbreviate}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"fold"}{"Abbreviate"}) 2 0 2
645 2 my ($long, $abv);
646 $css=~s/\.$long \{/.$abv {/s
647 2 ▶ while (($long, $abv) = each %{$self->{fold}{Abbreviate}});
Branch: 1 branch
BranchTrueFalseTotal
() = each %{$$self{"fold"}{"Abbreviate"};} and do { $css =~ s/\.$long \{/.$abv {/s } 68 2 70
648 }
649 2 return $css . $ftc;
650 }
651  
652 =pod
653  
654 =begin classdoc
655  
656 Generate folded HTML from source PPI document.
657 Overrides base class <code>html()</code> to apply codefolding support.
658  
659 @param $src a <cpan>PPI::Document</cpan> object, OR the
660 path to the source file, OR a scalarref of the
661 actual source text.
662 @optional $outfile name of the output HTML file; If not specified for a filename $src, the
663 default is "$src.html"; If not specified for either PPI::Document or text $src,
664 defaults to an empty string.
665 @optional $script a name used if source is a script file. Script files might not include
666 any explicit packages or method declarations which would be mapped into the
667 table of contents. By specifying this parameter, an entry is forced into the
668 table of contents for the script, with any "main" package methods within the
669 script reassigned to this script name. If not specified, and <code>$src</code>
670 is not a filename, an error will be issued when the TOC is generated.
671  
672 @return on success, the folded HTML; undef on failure
673
674 @returnlist on success, the folded HTML and a hashref mapping packages to an arrayref of method names;
675 undef on failure
676  
677 =end classdoc
678  
679 =cut
680  
681 2 ▶sub html {
Subroutine html: called CC=30 SCAR=36.9
Pod documented
682 2 my ($self, $src, $outfile, $script) = @_;
683  
684 2 my $orig_colors = exists $self->{colors};
685 2 ▶ my $html = $self->SUPER::html($src)
Branch: 1 branch
BranchTrueFalseTotal
my $html = $self->SUPER::html($src) or return undef 2 0 2
686 or return undef;
687  
688 2 ▶ $outfile = (ref $src) ? '' : "$src.html"
Branch: 2 branches
BranchTrueFalseTotal
unless $outfile 0 2 2
ref $src ? : 0 0 0
689 unless $outfile;
690 2 ▶ $script ||= $src
Branch: 1 branch
BranchTrueFalseTotal
unless ref $src or substr($src, -3) eq ".pm" 0 2 2
Condition16%1/6
ref $src or substr($src, -3) eq ".pm"
l!l&&r!l&&!r
200
$script ||= $src
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $src or substr($src, -3) eq ".pm"
ref $src substr($src, -3) eq ".pm"
MC/DC0%0/2
$script ||= $src
$script $src
Truth table33%1/3
A or B
A= ref $srcB= substr($src, -3) eq ".pm"
ABresult
1X1
011
000
Truth table0%0/3
A ||= B
A= $scriptB= $src
ABresult
1X1
011
000
691 unless ref $src || (substr($src, -3) eq '.pm');
692 #
693 # expand tabs as needed (we use 4 space tabs)
694 # have to adjust some spans that confuse tab processing
695 #
696 2 my @lns = split /\n/, $html;
697 2 my $tabsz = $self->{fold}{Tabs};
698 2 foreach my $line (@lns) {
699 2734 ▶ next if $line=~s/^\s*$//;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ s/^\s*$// 0 2734 2734
700 2734 ▶ next unless $line=~tr/\t//;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ tr/\t// 1550 1184 2734
701 1184 my $offs = 0;
702 1184 my $pad;
703 #
704 # scan for and replace tabs; adjust positions
705 # of extracted tags as needed
706 #
707 1184 pos($line) = 0;
708 1184 ▶ while ($line=~/\G.*?((<[^>]+>)|\t)/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\G.*?((<[^>]+>)|\t)/cg) 7210 1184 8394
709 7210 ▶ $offs += length($2),
Branch: 1 branch
BranchTrueFalseTotal
unless $1 eq "\t" 5234 1976 7210
710 next
711 unless ($1 eq "\t");
712  
713 1976 $pad = $tabsz - ($-[1] - $offs) % $tabsz;
714 1976 substr($line, $-[1], 1, ' ' x $pad);
715 1976 pos($line) = $-[1] + $pad - 1;
716 }
717 }
718 2 $html = join("\n", @lns);
719  
720 2 ▶ delete $self->{colors} unless $orig_colors;
Branch: 1 branch
BranchTrueFalseTotal
unless $orig_colors 0 2 2
721  
722 2 my $opts = $self->{fold};
723 #
724 # extract stylesheet and replace with abbreviated version
725 #
726 my $style = $opts->{Stylesheet} ?
727 "<link type='text/css' rel='stylesheet' href='" .
728 2 ▶ _pathAdjust($outfile, $opts->{Stylesheet}) . "' />" :
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'Stylesheet'} ? : 1 1 2
729 $self->foldCSS();
730  
731 $style .= $opts->{Javascript} ?
732 "\n<script type='text/javascript' src='" .
733 _pathAdjust($outfile, $opts->{Javascript}) . "'></script>\n" :
734 "\n<script type='text/javascript'>\n$ftjs\n</script>\n"
735 2 ▶ if $opts->{Expandable};
Branch: 2 branches
BranchTrueFalseTotal
if $opts->{"Expandable"} 2 0 2
$opts->{'Javascript'} ? : 1 1 2
736 #
737 # original html may have no style, so we've got to add OR replace
738 #
739 2 ▶ $html=~s|</head>|$style</head>|s
Branch: 1 branch
BranchTrueFalseTotal
unless $html =~ s[<style type="text/css">.+</style>][$style]s 0 2 2
740 unless ($html=~s|<style type="text/css">.+</style>|$style|s);
741 #
742 # force spans to end before line endings
743 #
744 2 $html=~s!(<br>\s*)</span>!</span>$1!g;
745 #
746 # split multiline comments into 2 spans: 1st line (in case its midline)
747 # and the remainder; note that the prior substitution avoids
748 # doing this to single line comments
749 #
750 2 $html=~s/(?!<br>\s+)(<span class=['"]comment['"]>[^<]+)<br>\n/$1<\/span><br>\n<span class="comment">/g;
751 #
752 # keep folded fragments here for later insertion
753 # as fold DIVs; key is starting line number,
754 # value is [ number of lines, text ]
755 #
756 2 my %folddivs = ( 1 => [ 0, '', 0, 0 ]);
757 #
758 # count <br> tags, and looks for any of
759 # comment, pod, or use/require keyword (depending on the options);
760 # keeps track of start and end position of foldable segments
761 #
762 2 my $lineno = 1;
763 2 my $lastfold = 1;
764  
765 2 $html=~s/<br>\n/<br>/g;
766 #
767 # now process remainder
768 #
769 2 study $html;
770 2 pos($html) = 0;
771 2 $html=~/^.*?(<body[^>]+><pre>)/s;
772 2 my $startpos = $+[1];
773 #
774 # map linebreak positions to line numbers
775 #
776 2 my @lnmap = (0, $startpos);
777 2 ▶ push @lnmap, $+[1]
Branch: 1 branch
BranchTrueFalseTotal
$html =~ /\G.*?(<br>)/cgs and do { push @lnmap, $+[1] } 2578 2 2580
778 while ($html=~/\G.*?(<br>)/gcs);
779 #
780 # now scan for foldables
781 #
782 2 pos($html) = $startpos;
783 2 my @folds = _extractFolds(\$html, $startpos, \@lnmap, $opts);
784 #
785 # trim small folds;
786 # since its used frequently, create a sorted list of the fold DIV lines;
787 # isolate positions of folds and extract folded content
788 #
789 2 my $ln = 0;
790 2 my @ftsorted = ();
791 2 foreach (@folds) {
792 212 ▶ if ($_->[1] - $_->[0] + 1 >= $opts->{MinFoldLines}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_->[1] - $_->[0] + 1 >= $opts->{'MinFoldLines'}) { } 60 152 212
elsif ($self->{'_verbose'}) { } 0 152 152
793 60 $folddivs{$_->[0]} = [ $_->[1], substr($html, $lnmap[$_->[0]], $lnmap[$_->[1] + 1] - $lnmap[$_->[0]]),
794 $lnmap[$_->[0]], $lnmap[$_->[1] + 1] ];
795 60 push @ftsorted, $_->[0];
796 }
797 elsif ($self->{_verbose}) {
798 # print "*** skipping section at line $_->[0]to $_->[1]\n";
799 # print substr($html, $lnmap[$_->[0]], $lnmap[$_->[1] + 1] - $lnmap[$_->[0]]), "\n";
800 }
801 }
802 #
803 # now remove the folded lines; we work from bottom to top since
804 # we're changing the HTML as we go, which would invalidate the
805 # positional elements we've kept. If fold expansion is enabled, we replace
806 # w/ a hyperlink; otherwise we replace with a simple indication of the fold
807 #
808 substr($html, $folddivs{$_}[2], $folddivs{$_}[3] - $folddivs{$_}[2],
809 "<span id='src$_' class='foldfill'>Folded lines $_ to " . $folddivs{$_}[0] . "</span>\n")
810 2 foreach (reverse @ftsorted);
811 #
812 # abbreviate the default span classes for both the html and fold divs
813 #
814 2 pos($html) = 0;
815 2 my $abvs = $opts->{Abbreviate};
816 2 ▶ if ($abvs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($abvs) 2 0 2
817 2 ▶ $html=~s/(<span\s+class=['"])([^'"]+)(['"])/$1 . ($$abvs{$2} || $2) . $3/egs;
Condition33%1/3
$abvs->{$2} || $2
l!l&&r!l&&!r
277400
MC/DC0%0/2
$abvs->{$2} || $2
$abvs->{$2} $2
Truth table33%1/3
A || B
A= $abvs->{$2}B= $2
ABresult
1X1
011
000
818 2 ▶ if ($opts->{Expandable}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"Expandable"}) 2 0 2
819 108 ▶ $_->[1]=~s/(<span\s+class=['"])([^'"]+)(['"])/$1 . ($$abvs{$2} || $2) . $3/egs
Condition33%1/3
$abvs->{$2} || $2
l!l&&r!l&&!r
10800
MC/DC0%0/2
$abvs->{$2} || $2
$abvs->{$2} $2
Truth table33%1/3
A || B
A= $abvs->{$2}B= $2
ABresult
1X1
011
000
820 2 foreach (values %folddivs);
821 }
822 }
823 #
824 # create and insert fold DIVs if requested
825 #
826 2 ▶ my $expdivs = $opts->{Expandable} ? _addFoldDivs(\%folddivs, \@ftsorted) : '';
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'Expandable'} ? : 2 0 2
827  
828 2 $html=~s/<br>/\n/gs;
829 #
830 # now create the line number table (if requested)
831 # NOTE: this is where having the breakable lines would be really
832 # useful!!!
833 #
834 _addLineNumTable(\$html, \@ftsorted, \%folddivs, \$expdivs, $#lnmap)
835 2 ▶ if $self->{_needs_ln};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_needs_ln"} 2 0 2
836 #
837 # extract a package/method reference list, and add anchors for them
838 #
839 2 $self->_extractXRef(\$html, $outfile, $script);
840 #
841 # report number of spans, for firefox performance report
842 #
843 2 ▶ if ($self->{_verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_verbose"}) 0 2 2
844 0 my $spancnt = $html=~s/<\/span>/<\/span>/gs;
845 0 print "\n***Total spans: $spancnt\n";
846 }
847 #
848 # fix Firefox blank lines inside spans bug: add a single space to
849 # all blank lines
850 #
851 2 $html=~s!\n\n!\n \n!gs;
852  
853 2 return $html;
854 }
855  
856 =pod
857  
858 =begin classdoc
859  
860 Return current package/method cross reference.
861  
862 @return hashref of current package/method cross reference
863  
864 =end classdoc
865  
866 =cut
867  
868 1 ▶sub getCrossReference { return $_[0]->{_pkgs}; }
Subroutine getCrossReference: called CC=1 SCAR=0.0
Pod documented
869  
870 =pod
871  
872 =begin classdoc
873  
874 Write out a table of contents document for the current collection of
875 sources as a nested HTML list. The output filename is 'toc.html'.
876 The caller may optionally specify the order of packages in the menu.
877  
878 @param $path directory to write TOC file
879 @optional Order arrayref of packages in the order in which they should appear in TOC; if a partial list,
880 any remaining packages will be appended to the TOC in alphabetical order
881  
882 @return this object on success, undef on failure, with error message in $@
883  
884 =end classdoc
885  
886 =cut
887  
888 1 ▶sub writeTOC {
Subroutine writeTOC: called CC=2 SCAR=7.0
Pod documented
889 1 my $self = shift;
890 1 my $path = shift;
891 1 ▶ $@ = "Can't open $path/toc.html: $!",
Branch: 1 branch
BranchTrueFalseTotal
unless open OUTF, ">$path/toc.html" 0 1 1
892 return undef
893 unless CORE::open(OUTF, ">$path/toc.html");
894  
895 1 print OUTF $self->getTOC("$path/toc.html", @_);
896 1 close OUTF;
897 1 return $self;
898 }
899  
900 =begin classdoc
901  
902 Generate a table of contents document for the current collection of
903 sources as a nested HTML list. Caller may optionally specify
904 the order of packages in the menu.
905  
906 @param $tocpath path of output TOC file
907 @optional Order arrayref of packages in the order in which they should appear in TOC; if a partial list,
908 any remaining packages will be appended to the TOC in alphabetical order
909  
910 @return the TOC document
911  
912 =end classdoc
913  
914 =cut
915  
916 3 ▶sub getTOC {
Subroutine getTOC: called CC=8 SCAR=20.8
Pod documented
917 3 my $self = shift;
918 3 my $tocpath = shift;
919 3 my %args = @_;
920 3 ▶ my @order = $args{Order} ? @{$args{Order}} : ();
Branch: 1 branch
BranchTrueFalseTotal
$args{'Order'} ? : 2 1 3
921 3 my $sources = $self->{_pkgs};
922 3 my $base;
923 3 my $doc =
924 "<html>
925 <body>
926 <small>
927 <!-- INDEX BEGIN -->
928 <ul>
929 ";
930 3 my %ordered = ();
931 3 $ordered{$_} = 1 foreach (@order);
932 3 foreach (sort keys %$sources) {
933 3 ▶ push @order, $_ unless exists $ordered{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ordered{$_} 1 2 3
934 }
935  
936 3 foreach my $class (@order) {
937 #
938 # due to input @order, we might get classes that don't exist
939 #
940 5 ▶ next unless exists $sources->{$class};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $sources->{$class} 2 3 5
941  
942 3 $base = _pathAdjust($tocpath, $sources->{$class}{URL});
943 3 $doc .= "<li><a href='$base' target='mainframe'>$class</a>
944 <ul>\n";
945 3 my $info = $sources->{$class}{Methods};
946 $doc .= "<li><a href='" . _pathAdjust($tocpath, $info->{$_}) . "' target='mainframe'>$_</a></li>\n"
947 3 foreach (sort keys %$info);
948 3 $doc .= "</ul>\n</li>\n";
949 }
950  
951 3 $doc .= "
952 </ul>
953 <!-- INDEX END -->
954 </small>
955 </body>
956 </html>
957 ";
958  
959 3 return $doc;
960 }
961  
962 =pod
963  
964 =begin classdoc
965  
966 Write out a frame container document to hold the rendered source and TOC.
967 The file is written to "$path/index.html".
968  
969 @param $path directory to write the document.
970 @param $title Title string for resulting document
971 @optional $home the "home" document initially loaded into the main frame; default none
972  
973 @return this object on success, undef on failure, with error message in $@
974  
975 =end classdoc
976  
977 =cut
978  
979 1 ▶sub writeFrameContainer {
Subroutine writeFrameContainer: called CC=2 SCAR=7.0
Pod documented
980 1 my ($self, $path, $title, $home) = @_;
981 1 ▶ $@ = "Can't open $path/index.html: $!",
Branch: 1 branch
BranchTrueFalseTotal
unless open OUTF, ">$path/index.html" 0 1 1
982 return undef
983 unless open(OUTF, ">$path/index.html");
984  
985 1 print OUTF $self->getFrameContainer($title, $home);
986 1 close OUTF;
987 1 return $self;
988 }
989  
990 =begin classdoc
991  
992 Generate a frame container document to hold the rendered source and TOC.
993  
994 @return the frame container document as a string
995  
996 =end classdoc
997  
998 =cut
999  
1000 2 ▶sub getFrameContainer {
Subroutine getFrameContainer: called CC=2 SCAR=7.2
Pod documented
1001 2 my ($self, $title, $home) = @_;
1002 2 ▶ return $home ?
Branch: 1 branch
BranchTrueFalseTotal
$home ? : 2 0 2
1003 "<html><head><title>$title</title></head>
1004 <frameset cols='15%,85%'>
1005 <frame name='navbar' src='toc.html' scrolling=auto frameborder=0>
1006 <frame name='mainframe' src='$home'>
1007 </frameset>
1008 </html>
1009 " :
1010 "<html><head><title>$title</title></head>
1011 <frameset cols='15%,85%'>
1012 <frame name='navbar' src='toc.html' scrolling=auto frameborder=0>
1013 <frame name='mainframe'>
1014 </frameset>
1015 </html>
1016 ";
1017 }
1018 #
1019 # extract a package/method reference list, and add anchors for them
1020 #
1021 2 ▶sub _extractXRef {
Subroutine _extractXRef: called CC=11 SCAR=26.0
1022 2 my ($self, $html, $outfile, $script) = @_;
1023 2 ▶ $self->{_pkgs} = {} unless exists $self->{_pkgs};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_pkgs"} 0 2 2
1024 2 my $pkgs = $self->{_pkgs};
1025 2 my $pkglink;
1026 #
1027 # assume package "main" to start; on exit,
1028 # if we have a script name, then replace all "main"
1029 # entries with $script
1030 #
1031 2 my $curpkg = 'main';
1032 $pkgs->{main} = {
1033 2 ▶ URL => $outfile,
Branch: 1 branch
BranchTrueFalseTotal
if $script 0 2 2
1034 Methods => {}
1035 }
1036 if $script;
1037  
1038 2 ▶ while ($$html=~/\G.*?(<span class=['"]kw['"]>)\s*(package|sub)\s*<\/span>\s*(<span class=['"][^'"]+['"]>\s*)?([\w:]+)/gcs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$html =~ m[\G.*?(<span class=['"]kw['"]>)\s*(package|sub)\s*</span>\s*(<span class=['"][^'"]+['"]>\s*)?([\w:]+)]cgs) 40 2 42
1039 # " to keep Textpad formatting happy
1040 40 my $pkg = $4;
1041 40 my $next = pos($$html);
1042 40 my $insert = $-[1];
1043 40 ▶ if ($2 eq 'package') {
Branch: 1 branch
BranchTrueFalseTotal
if ($2 eq 'package') { } 2 38 40
1044 2 $curpkg = $pkg;
1045 2 ▶ next if exists $pkgs->{$pkg} && $pkgs->{$pkg}{URL}; # only use 1st definition of package
Branch: 1 branch
BranchTrueFalseTotal
if exists $pkgs->{$pkg} and $pkgs->{$pkg}{"URL"} 0 2 2
Condition33%1/3
exists $pkgs->{$pkg} and $pkgs->{$pkg}{"URL"}
!ll&&!rl&&r
200
MC/DC0%0/2
exists $pkgs->{$pkg} and $pkgs->{$pkg}{"URL"}
exists $pkgs->{$pkg} $pkgs->{$pkg}{"URL"}
Truth table33%1/3
A and B
A= exists $pkgs->{$pkg}B= $pkgs->{$pkg}{"URL"}
ABresult
0X0
100
111
1046 2 $pkglink = $pkg;
1047 2 $pkgs->{$pkg} = {
1048 URL => "$outfile#$pkg",
1049 Methods => {}
1050 };
1051 }
1052 else {
1053 38 ▶ if ($pkg=~/^(.+)::(\w+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg =~ /^(.+)::(\w+)$/) { } 0 38 38
1054 #
1055 # fully qualified name, check if we have a pkg entry for it
1056 #
1057 $pkgs->{$1} = {
1058 URL => '',
1059 Methods => {}
1060 }
1061 0 ▶ unless exists $pkgs->{$1};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $pkgs->{$1} 0 0 0
1062 0 $pkgs->{$1}{Methods}{$2} = "$outfile#$pkg";
1063 0 $pkglink = $pkg;
1064 }
1065 else {
1066 38 ▶ $pkglink = ($curpkg eq 'main') ? $pkg : "$curpkg\:\:$pkg";
Branch: 1 branch
BranchTrueFalseTotal
$curpkg eq 'main' ? : 0 38 38
1067 38 $pkgs->{$curpkg}{Methods}{$pkg} = "$outfile#$pkglink";
1068 }
1069 }
1070 40 $pkglink = "<a name='$pkglink'></a>";
1071 40 substr($$html, $insert, 0, $pkglink);
1072 40 $next += length($pkglink);
1073 40 pos($$html) = $next;
1074 }
1075 $pkgs->{$script} = delete $pkgs->{main}
1076 2 ▶ if $script;
Branch: 1 branch
BranchTrueFalseTotal
if $script 0 2 2
1077 2 return $html;
1078 }
1079  
1080 2 ▶sub _extractFolds {
Subroutine _extractFolds: called CC=8 SCAR=20.9
1081 2 my ($html, $startpos, $lnmap, $opts) = @_;
1082 #
1083 # scan for foldables
1084 #
1085 2 pos($$html) = $startpos;
1086 2 my %folded = (
1087 Whitespace => [],
1088 Comments => [],
1089 POD => [],
1090 Heredocs => [],
1091 Imports => [],
1092 );
1093 2 my $whitespace = [];
1094 #
1095 # accumulate foldable sections, including leading/trailing whitespace
1096 #
1097 # my $fre = $foldres{$_}[0];
1098 # push @{$folded{$_}}, [ $-[1], $+[1] - 1 ]
1099 # if ($$html=~/$fre/gcs);
1100  
1101 # push @{$folded{Whitespace}}, [ $-[1], $+[1] - 1 ]
1102 # while ($$html=~/\G.*?<br>((?:\s*<br>)+)/gcs);
1103 # _mergeSection(_cvtToLines($folded{Whitespace}, $lnmap))
1104 # if scalar @{$folded{Whitespace}};
1105  
1106 2 pos($$html) = $startpos;
1107 2 foreach (qw(Whitespace Comments POD Heredocs Imports)) {
1108 10 ▶ next unless ($_ eq 'Whitespace') || $opts->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $_ eq "Whitespace" or $opts->{$_} 0 10 10
Condition66%2/3
$_ eq "Whitespace" or $opts->{$_}
l!l&&r!l&&!r
280
MC/DC0%0/2
$_ eq "Whitespace" or $opts->{$_}
$_ eq "Whitespace" $opts->{$_}
Truth table66%2/3
A or B
A= $_ eq "Whitespace"B= $opts->{$_}
ABresult
1X1
011
000
1109 #
1110 # capture anything at the very beginning
1111 #
1112 10 my $fre = $foldres{$_}[0];
1113 10 ▶ push @{$folded{$_}}, [ $-[1], $+[1] - 1 ]
Branch: 1 branch
BranchTrueFalseTotal
if $$html =~ /$fre/cgs 2 8 10
1114 if ($$html=~/$fre/gcs);
1115
1116 10 $fre = $foldres{$_}[1];
1117 10 ▶ push @{$folded{$_}}, [ $-[1], $+[1] - 1 ]
Branch: 1 branch
BranchTrueFalseTotal
$$html =~ /$fre/cgs and do { push @{$folded{$_};}, [$-[1], $+[1] - 1] } 544 10 554
1118 while ($$html=~/$fre/gcs);
1119 _mergeSection(_cvtToLines($folded{$_}, $lnmap))
1120 10 ▶ if scalar @{$folded{$_}};
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$folded{$_};} 10 0 10
1121 10 pos($$html) = $startpos;
1122 }
1123 #
1124 # now merge different sections
1125 #
1126 2 my $last = 'Whitespace';
1127 2 foreach (qw(Imports POD Heredocs Comments)) {
1128 8 _mergeSections($folded{$_}, $folded{$last});
1129 8 $last = $_;
1130 }
1131 2 return @{$folded{$last}};
1132 }
1133  
1134 10 ▶sub _cvtToLines {
Subroutine _cvtToLines: called CC=6 SCAR=18.0
1135 10 my ($pos, $lnmap) = @_;
1136
1137 10 my $ln = 1;
1138 10 foreach (@$pos) {
1139 546 ▶ $ln++ while ($ln <= $#$lnmap) && ($lnmap->[$ln] <= $_->[0]);
Branch: 1 branch
BranchTrueFalseTotal
$ln <= $lnmap->$#* and $lnmap->[$ln] <= $_->[0] and do { ++$ln } 7244 546 7790
Condition66%2/3
$ln <= $lnmap->$#* and $lnmap->[$ln] <= $_->[0]
!ll&&!rl&&r
05467244
MC/DC50%1/2
$ln <= $lnmap->$#* and $lnmap->[$ln] <= $_->[0]
$ln <= $lnmap->$#* $lnmap->[$ln] <= $_->[0]
Truth table66%2/3
A and B
A= $ln <= $lnmap->$#*B= $lnmap->[$ln] <= $_->[0]
ABresult
0X0
100
111
1140 546 $_->[0] = $ln - 1;
1141 546 ▶ $ln++ while ($ln <= $#$lnmap) && ($lnmap->[$ln] <= $_->[1]);
Branch: 1 branch
BranchTrueFalseTotal
$ln <= $lnmap->$#* and $lnmap->[$ln] <= $_->[1] and do { ++$ln } 1194 546 1740
Condition66%2/3
$ln <= $lnmap->$#* and $lnmap->[$ln] <= $_->[1]
!ll&&!rl&&r
05461194
MC/DC50%1/2
$ln <= $lnmap->$#* and $lnmap->[$ln] <= $_->[1]
$ln <= $lnmap->$#* $lnmap->[$ln] <= $_->[1]
Truth table66%2/3
A and B
A= $ln <= $lnmap->$#*B= $lnmap->[$ln] <= $_->[1]
ABresult
0X0
100
111
1142 546 $_->[1] = $ln - 1;
1143 }
1144 10 return $pos;
1145 }
1146  
1147 18 ▶sub _mergeSection {
Subroutine _mergeSection: called CC=4 SCAR=13.9
1148 18 my $sect = shift;
1149 18 my @temp = shift @$sect;
1150 18 foreach (@$sect) {
1151 1600 ▶ push(@temp, $_),
Branch: 1 branch
BranchTrueFalseTotal
unless $temp[-1][1] + 1 >= $_->[0] 1266 334 1600
1152 next
1153 unless ($temp[-1][1] + 1 >= $_->[0]);
1154 #
1155 # if current surrounds new, the discard new
1156 #
1157 334 ▶ $temp[-1][1] = $_->[1]
Branch: 1 branch
BranchTrueFalseTotal
if $temp[-1][1] < $_->[1] 156 178 334
1158 if ($temp[-1][1] < $_->[1]);
1159 }
1160 18 @$sect = @temp;
1161 18 1;
1162 }
1163  
1164 8 ▶sub _mergeSections {
Subroutine _mergeSections: called CC=7 SCAR=20.5
1165 8 my ($first, $second) = @_;
1166
1167 8 ▶ if ($#$first < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first->$#* < 0) 0 8 8
1168 0 @$first = @$second;
1169 0 return $first;
1170 }
1171  
1172 8 my @temp = ();
1173 8 ▶ push @temp, (($first->[0][0] < $second->[0][0]) ? shift @$first : shift @$second)
Branch: 2 branches
BranchTrueFalseTotal
@$first and @$second and do { push @temp, $first->[0][0] < $second->[0][0] ? shift @$first : shift @$second } 620 8 628
$first->[0][0] < $second->[0][0] ? : 126 494 620
Condition66%2/3
@$first and @$second
!ll&&!rl&&r
80620
MC/DC50%1/2
@$first and @$second
@$first @$second
Truth table66%2/3
A and B
A= @$firstB= @$second
ABresult
0X0
100
111
1174 while (@$first && @$second);
1175  
1176 8 ▶ push @temp, @$first if scalar @$first;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$first 0 8 8
1177 8 ▶ push @temp, @$second if scalar @$second;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$second 8 0 8
1178 8 _mergeSection(\@temp);
1179 8 @$first = @temp;
1180 8 1;
1181 }
1182  
1183 2 ▶sub _addLineNumTable {
Subroutine _addLineNumTable: called CC=2 SCAR=6.9
1184 2 my ($html, $ftsorted, $folddivs, $expdivs, $linecnt) = @_;
1185  
1186 2 $$html=~s/<pre>/<pre class='bodypre'>/;
1187 2 $$html=~/(<body[^>]+>)/s;
1188 2 my $insert = $+[0];
1189 #
1190 # generate JS declaration of fold sections
1191 #
1192 my $startfolds = scalar @$ftsorted ?
1193 2 ▶ '[' . join(',', @$ftsorted) . " ],\n[" . join(',', map $folddivs->{$_}[0], @$ftsorted) . " ]" :
Branch: 1 branch
BranchTrueFalseTotal
scalar @$ftsorted ? : 2 0 2
1194 "[], []";
1195  
1196 2 my $linenos = $$expdivs . "
1197 <table border=0 width='100\%' cellpadding=0 cellspacing=0>
1198 <tr>
1199 <td width=40 bgcolor='#E9E9E9' align=right valign=top>
1200 <pre id='lnnomargin' class='lnpre'>
1201 </pre>
1202 </td>
1203 <td width=8 bgcolor='#E9E9E9' align=right valign=top>
1204 <pre id='btnmargin' class='lnpre'>
1205 </pre>
1206 </td>
1207 <td bgcolor='white' align=left valign=top>
1208 ";
1209 2 substr($$html, $insert, 0, $linenos);
1210 2 substr($$html, index($$html, '</body>'), 0, "
1211 </td></tr></table>
1212  
1213 <script type='text/javascript'>
1214 <!--
1215  
1216 var ppihtml = new ppiHtmlCF($startfolds);
1217 ppihtml.renderMargins($linecnt);
1218 /*
1219 * all rendered, now selectively open from any existing cookie
1220 */
1221 ppihtml.openFromCookie();
1222  
1223 -->
1224 </script>
1225 "
1226 );
1227 2 return 1;
1228 }
1229  
1230 2 ▶sub _addFoldDivs {
Subroutine _addFoldDivs: called CC=8 SCAR=22.9
1231 2 my ($folddivs, $ftsorted) = @_;
1232 2 foreach my $ft (values %$folddivs) {
1233 60 $ft->[1]=~s/<br>/\n/gs;
1234 #
1235 # squeeze out leading whitespace, but keep aligned
1236 #
1237 60 my $shortws = 1000000;
1238 60 my @lns = split /\n/, $ft->[1];
1239 #
1240 # expand tabs as needed (we use 4 space tabs)
1241 #
1242 60 foreach (@lns) {
1243 88 ▶ next if s/^\s*$//;
Branch: 1 branch
BranchTrueFalseTotal
if s/^\s*$// 28 60 88
1244 60 ▶ $shortws = 0, last
Branch: 1 branch
BranchTrueFalseTotal
unless /^(\s+)/ 60 0 60
1245 unless /^(\s+)/;
1246 0 ▶ $shortws = length($1)
Branch: 1 branch
BranchTrueFalseTotal
if $shortws > length $1 0 0 0
1247 if ($shortws > length($1))
1248 }
1249 0 ▶ $ft->[1] = join("\n", map { $_ ? substr($_, $shortws) : ''; } @lns)
Branch: 2 branches
BranchTrueFalseTotal
if $shortws 0 60 60
$_ ? : 0 0 0
1250 if $shortws;
1251 #
1252 # move whitespace inside any leading/trailing spans
1253 #
1254 60 $ft->[1]=~s!(</span>)(\s+)$!$2$1!s;
1255 60 $ft->[1]=~s!^(\s+)(<span [^>]+>)!$2$1!s;
1256 #
1257 # if ends on span, make sure its not creating newline
1258 #
1259 60 $ft->[1]=~s!\n</span>$! </span>!s;
1260 #
1261 # likewise if it doesn't end on a span
1262 #
1263 60 $ft->[1]=~s!\n$!!s;
1264 }
1265 2 return join('', map "\n<div id='ft$_' class='folddiv'><pre id='preft$_'>$folddivs->{$_}[1]</pre></div>", @$ftsorted);
1266 }
1267  
1268 62 ▶sub _pathAdjust {
Subroutine _pathAdjust: called CC=6 SCAR=33.1
1269 62 my ($path, $jspath) = @_;
1270 62 ▶ return $jspath
Branch: 1 branch
BranchTrueFalseTotal
unless substr($jspath, 0, 2) eq "./" and substr($path, 0, 2) eq "./" 62 0 62
Condition33%1/3
substr($jspath, 0, 2) eq "./" and substr($path, 0, 2) eq "./"
!ll&&!rl&&r
6200
MC/DC0%0/2
substr($jspath, 0, 2) eq "./" and substr($path, 0, 2) eq "./"
substr($jspath, 0, 2) eq "./" substr($path, 0, 2) eq "./"
Truth table33%1/3
A and B
A= substr($jspath, 0, 2) eq "./"B= substr($path, 0, 2) eq "./"
ABresult
0X0
100
111
1271 unless (substr($jspath, 0, 2) eq './') && (substr($path, 0, 2) eq './');
1272 #
1273 # relative path, adjust as needed from current base
1274 #
1275 0 my @parts = split /\//, $path;
1276 0 my @jsparts = split /\//, $jspath;
1277 0 my $jsfile = pop @jsparts; # get rid of filename
1278 0 pop @parts; # remove filename
1279 0 shift @parts;
1280 0 shift @jsparts; # and the relative lead
1281 0 my $prefix = '';
1282 0 ▶ shift @parts,
Branch: 1 branch
BranchTrueFalseTotal
@parts and @jsparts and $parts[0] eq $jsparts[0] and do { shift @parts, shift @jsparts } 0 0 0
Condition0%0/6
@parts and @jsparts and $parts[0] eq $jsparts[0]
!ll&&!rl&&r
000
@parts and @jsparts
!ll&&!rl&&r
000
MC/DC0%0/3
@parts and @jsparts and $parts[0] eq $jsparts[0]
@parts @jsparts $parts[0] eq $jsparts[0]
Truth table0%0/4
A and B and C
A= @partsB= @jspartsC= $parts[0] eq $jsparts[0]
ABCresult
0XX0
10X0
1100
1111
1283 shift @jsparts
1284 while @parts && @jsparts && ($parts[0] eq $jsparts[0]);
1285 0 push @jsparts, $jsfile;
1286 0 return ('../' x scalar @parts) . join('/', @jsparts)
1287 }
1288  
1289 2 1;
↑ Summary lib/PPI/HTML/mkhtmlcf.pl »