blib/lib/Apache/PrettyPerl.pm untested

filter: Statementstmt 0.0% 0 / 317 Branchbran 0.0% 0 / 98 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 467 CC 50 SCAR 60.4
CC
50
Cov
0%
perl2html
870.0
handler
156.0
get_head
42.0
CRAP 418.7
1 package Apache::PrettyPerl;
2  
3 use strict;
4 use warnings;
5 use vars qw/$VERSION/;
6  
7 use Apache::Const qw/:common/;
8 use Apache::RequestRec;
9 use Apache::RequestIO;
10 use Apache::RequestUtil;
11 use Apache::Response;
12 use File::Basename qw/basename/;
13  
14 $VERSION = '2.10';
15  
16 # You can set colors here. Use HTML color names or codes
17 # (like #ff0000 being red).
18 our %Colors =
19 (
20 foreground => 'silver',
21 background => 'black',
22 links => 'white',
23
24 comment => 'navy',
25 escaped => 'purple',
26 keyword => 'yellow',
27 number => 'red',
28 pod => 'navy',
29 regex => 'red',
30 string => 'red',
31 variable=> 'aqua'
32 );
33  
34 our @KeyWords =
35 qw(
36 while until for foreach unless if elsif else do
37 package use no require import and or eq ne cmp
38 abs accept alarm atan2 bind binmode bless
39 caller chdir chmod chomp chop chown chr
40 chroot close closedir connect continue cos
41 crypt dbmclose dbmopen defined delete die
42 dump each endgrent endhostent endnetent
43 endprotoent endpwent endservent eof eval
44 exec exists exit exp fcntl fileno flock
45 fork format formline getc getgrent getgrgid
46 getgrnam gethostbyaddr gethostbyname gethostent
47 getlogin getnetbyaddr getnetbyname getnetent
48 getpeername getpgrp getppid getpriority
49 getprotobyname getprotobynumber getprotoent
50 getpwent getpwnam getpwuid getservbyname
51 getservbyport getservent getsockname
52 getsockopt glob gmtime goto grep hex index
53 int ioctl join keys kill last lc lcfirst
54 length link listen local localtime log
55 lstat map map mkdir msgctl msgget msgrcv
56 msgsnd my next oct open opendir ord our pack
57 pipe pop pos print printf prototype push
58 quotemeta rand read readdir readline
59 readlink readpipe recv redo ref rename
60 reset return reverse rewinddir rindex
61 rmdir scalar seek seekdir select semctl
62 semget semop send setgrent sethostent
63 setnetent setpgrp setpriority setprotoent
64 setpwent setservent setsockopt shift shmctl
65 shmget shmread shmwrite shutdown sin sleep
66 socket socketpair sort splice split sprintf
67 sqrt srand stat study sub substr symlink
68 syscall sysopen sysread sysread sysseek
69 system syswrite tell telldir tie tied
70 time times truncate uc ucfirst umask undef
71 unlink unpack unshift untie utime values
72 vec wait waitpid wantarray warn write
73 );
74  
75 our @Buffer = ();
76 our $BufferFill = 0;
77 our $alrm = chr (7);
78 our $Tabsize = 8;
79  
80 sub handler
81 {
82 my $req = shift;
83 my $filename = $req->filename ();
84 my $dl = 0;
85 my $dl_ok = 0;
86 my $data;
87 my $mtime;
88  
89 if (!-e $filename)
90 {
91 return (NOT_FOUND);
92 }
93  
94 if (!-r $filename)
95 {
96 return (FORBIDDEN);
97 }
98  
99 $mtime = (stat ($filename))[9] or return (SERVER_ERROR);
100  
101 if ($req->dir_config ('AllowDownload'))
102 {
103 my $conf = lc ($req->dir_config ('AllowDownload'));
104  
105 if (($conf eq 'on') or ($conf eq 'true')
106 or ($conf eq 'yes'))
107 {
108 $dl_ok = 1;
109 }
110 }
111  
112 if ($req->args ())
113 {
114 my %args = $req->args ();
115  
116 if (exists ($args{'download'})
117 and ($dl_ok))
118 {
119 $req->content_type ("text/perl-script");
120 $dl = 1;
121 }
122 else
123 {
124 $req->content_type ("text/html");
125 }
126 }
127 else
128 {
129 $req->content_type ("text/html");
130
131 if ($req->dir_config ('TabSize'))
132 {
133 my $tmp = $req->dir_config ('TabSize');
134 $tmp =~ s/\D//g;
135  
136 if ($tmp)
137 {
138 $Tabsize = $tmp;
139 }
140 }
141 }
142  
143 $req->set_last_modified ($mtime);
144 $req->set_etag ();
145  
146 if ($req->header_only ())
147 {
148 return (OK);
149 }
150  
151 $data = $req->slurp_filename ();
152  
153 if ($dl)
154 {
155 $req->print ($$data);
156 }
157 else
158 {
159 $req->print (get_head ($req));
160  
161 if ($dl_ok)
162 {
163 $req->print (get_dl_link ($req));
164 }
165 $req->print (perl2html ($$data));
166 $req->print (get_foot ());
167 }
168  
169 return (OK);
170 }
171  
172 sub get_head
173 {
174 my $req = shift;
175 my $uri = $req->uri ();
176 my $file = basename ($uri);
177 my $temp;
178
179 my $retval = <<EOF;
180 <?xml version="1.0" encoding="iso-8859-1"?>
181 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
182 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
183  
184 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
185 <head>
186 <title>Source of $file</title>
187 <meta name="generator" content="Apahe::PrettyPerl" />
188 <style type="text/css">
189 <!--
190 EOF
191 $temp = 'white';
192 if (defined ($Colors{'links'}))
193 {
194 $temp = $Colors{'links'};
195 }
196  
197 $retval .= <<EOF;
198 a
199 {
200 color: $temp;
201 background-color: transparent;
202 text-decoration: none;
203 font-weight: bold;
204 }
205  
206 a:hover
207 {
208 text-decoration: underline;
209 }
210  
211 EOF
212  
213 {
214 my $bg = 'black';
215 my $fg = 'silver';
216  
217 if (defined ($Colors{'background'}))
218 {
219 $bg = $Colors{'background'};
220 }
221 if (defined ($Colors{'foreground'}))
222 {
223 $fg = $Colors{'foreground'};
224 }
225  
226 $retval .= <<EOF;
227 body
228 {
229 color: $fg;
230 background-color: $bg;
231 }
232  
233 div.source
234 {
235 font-family: monospace;
236 border: 1px solid gray;
237 padding: 1em;
238 }
239  
240 p.generator
241 {
242 text-align: right;
243 font-size: smaller;
244 }
245  
246 EOF
247 }
248  
249 $temp = 'navy';
250 if (defined ($Colors{'comment'}))
251 {
252 $temp = $Colors{'comment'};
253 }
254  
255 $retval .= <<EOF;
256 span.comment
257 {
258 color: $temp;
259 background-color: transparent;
260 }
261  
262 span.error
263 {
264 color: red;
265 background-color: yellow;
266 }
267 EOF
268  
269 my %defaults =
270 (
271 escaped => 'purple',
272 keyword => 'yellow',
273 number => 'red',
274 pod => 'navy',
275 regex => 'red',
276 string => 'red',
277 variable=> 'aqua'
278 );
279  
280 for (sort (keys (%defaults)))
281 {
282 $temp = $defaults{$_};
283  
284 if (defined ($Colors{$_}))
285 {
286 $temp = $Colors{$_};
287 }
288  
289 $retval .= <<EOF;
290  
291 span.$_
292 {
293 color: $temp;
294 background-color: transparent;
295 }
296 EOF
297 }
298  
299 $retval .= <<EOF;
300 //-->
301 </style>
302 </head>
303 <body>
304 <h1>Source of <code>$file</code></h1>
305 EOF
306 return ($retval);
307 }
308  
309 sub get_foot
310 {
311 my $retval = <<EOF;
312 <p class="generator">Generated by <a href="http://amk.lg.ua/~ra/PrettyPerl/">Apache::PrettyPerl $VERSION</a></p>
313  
314 </body>
315 </html>
316 EOF
317 return ($retval);
318 }
319  
320 sub get_dl_link
321 {
322 my $req = shift;
323 my $uri = $req->uri ();
324 my $file = basename ($uri);
325  
326 my $retval = qq#\n<p><a href="$uri?download">Download <code>$file</code></a></p>\n#;
327 return ($retval);
328 }
329  
330 sub html_escape
331 {
332 $_ = shift;
333  
334 s/&/&amp;/g;
335 s/>/&gt;/g;
336 s/</&lt;/g;
337 s/"/&quot;/g;
338  
339 s/ +/'&nbsp;' x length ($&)/ge;
340 s/\t/'&nbsp;' x $Tabsize/ge;
341 s#\n#<br />\n#sg;
342  
343 return ($_);
344 }
345
346  
347 sub string2html
348 {
349 my $string = shift;
350 my $retval = '';
351  
352 $string = html_escape ($string);
353  
354 if ($string =~ m/^(&quot;|&lt;&lt;[^']|qq.)/)
355 {
356 $retval = $&;
357 $string = $';
358  
359 while ($string =~ m/\\(?:[^&]|&[a-z]+;)|[\@\%\&]?\$*\$(?:&[a-z]+;|[^A-Za-z:]|(?:::)?[A-Za-z](?:\w|::)*)/)
360 {
361 my $match = $&;
362 $retval .= $`;
363 $string = $';
364  
365 if ($match =~ m/^[\$\@\%\&]/)
366 {
367 $retval .= qq#<span class="variable">$match</span>#;
368 }
369 else
370 {
371 $retval .= qq#<span class="escaped">$match</span>#;
372 }
373 }
374
375 $retval .= $string;
376 $retval = qq#<span class="string">$retval</span>#;
377 }
378 elsif ($string =~ m/^('|&lt;&lt;'|q[^qxr])/)
379 {
380 $retval = $string;
381 $retval =~ s#\\[\\']#<span class="escaped">$&</span>#g;
382 $retval = qq#<span class="string">$retval</span>#;
383 }
384 elsif ($string =~ m/^#/)
385 {
386 $retval = qq#<span class="comment">$string</span>#;
387 }
388 elsif ($string =~ m/^=/)
389 {
390 $retval = qq#<span class="pod">$string</span>#;
391 }
392 else
393 {
394 $retval = qq#<span class="string">$string</span>#;
395 }
396
397 return ($retval);
398 }
399  
400 sub regex2html
401 {
402 $_ = shift;
403 $_ = html_escape ($_);
404  
405 s#
406 \((?:\?(?:[=!:]|&lt;[=!]|&gt;))?
407 | \[\^?
408 | \\(?:\&\w+;|.)
409 | [\*\+\?\)\]\|]
410 #<span class="escaped">$&</span>#gx;
411  
412 $_ = qq#<span class="regex">$_</span>#;
413
414 return ($_);
415 }
416  
417 sub perl2html
418 {
419 my $yet_to_process = join ("\n", @_);
420 my $processed = '';
421  
422 my @Buffer = ();
423 $BufferFill = 0;
424  
425 $yet_to_process =~ s/$alrm//g;
426 $yet_to_process =~ s/\r//g;
427  
428 while ($yet_to_process =~ m!
429 (
430 ["'\#] # normal strings and comments
431 | [\@\%\$\&] # variables
432 | \b\d+\b # numbers
433 | \b(?:m|s|y|tr)[^A-Za-z0-9\s] # regexen
434 | [\!=]~\s*/ # regex short form
435 | q[qwxr]?[^A-Za-z0-9\s] # more strings
436 | \w+\s*=> # hashes
437 | <<(['"]?)\w+\2;?\n # multi-line strings
438 | \n=\w+ # pod
439 | <\w+> # filehandles
440 )!xso)
441 {
442 my $match = $&;
443 $processed .= $`;
444 $yet_to_process = $';
445
446 # normal strings
447 if ($match eq '"')
448 {
449 if ($yet_to_process =~ m/^((?:\\.|[^"\\])*)"/s)
450 {
451 $processed .= qq#$alrm!STRING!$alrm$BufferFill$alrm#;
452 $yet_to_process = $';
453  
454 $Buffer[$BufferFill] = qq#"$1"#;
455 $BufferFill++;
456 }
457 else
458 {
459 $processed .= qq#$alrm!ERROR!$alrm$BufferFill$alrm#;
460  
461 $Buffer[$BufferFill] = qq#$match#;
462 $BufferFill++;
463 }
464 }
465 elsif ($match eq "'")
466 {
467 if ($yet_to_process =~ m/^((?:\\'|[^'])*)'/s)
468 {
469 $processed .= qq#$alrm!STRING!$alrm$BufferFill$alrm#;
470 $yet_to_process = $';
471  
472 $Buffer[$BufferFill] = qq#'$1'#;
473 $BufferFill++;
474 }
475 else
476 {
477 $processed .= qq#$alrm!ERROR!$alrm$BufferFill$alrm#;
478  
479 $Buffer[$BufferFill] = qq#$match#;
480 $BufferFill++;
481 }
482 }
483 # comments
484 elsif ($match eq '#')
485 {
486 if ($yet_to_process =~ m/.*/m)
487 {
488 $processed .= qq#$alrm!COMMENT!$alrm$BufferFill$alrm#;
489 $yet_to_process = $';
490  
491 $Buffer[$BufferFill] = '#' . $&;
492 $BufferFill++;
493 }
494 else
495 {
496 die 'You should NEVER get here!';
497 }
498 }
499 # variables
500 elsif ($match =~ m/^[\@\%\$\&]/)
501 {
502 if (($match eq '&') and ($yet_to_process =~ m/^\&/))
503 {
504 $processed .= '&&';
505 $yet_to_process = $';
506 }
507 elsif ($yet_to_process =~ m#\$*(?:[^A-Za-z:\s]|(?:::)?[A-Za-z](?:\w|::)*)#)
508 {
509 $processed .= qq#$alrm!VARIABLE!$alrm$BufferFill$alrm#;
510 $yet_to_process = $';
511  
512 $Buffer[$BufferFill] = $match . $&;
513 $BufferFill++;
514 }
515 else
516 {
517 $processed .= qq#$alrm!ERROR!$alrm$BufferFill$alrm#;
518  
519 $Buffer[$BufferFill] = qq#$match#;
520 $BufferFill++;
521 }
522 }
523 # numbers
524 elsif ($match =~ m/^\d+$/)
525 {
526 $processed .= qq#$alrm!NUMBER!$alrm$BufferFill$alrm#;
527
528 $Buffer[$BufferFill] = $match;
529 $BufferFill++;
530 }
531
532 # other strings
533 elsif ($match =~ m/^(q[qwxr]?)(.)/)
534 {
535 my $type = $1;
536 my $delim = $2;
537 $delim =~ tr/([{</)]}>/;
538 $delim = quotemeta ($delim);
539  
540 my $tmp = "^((?:\\\\$delim|[^$delim])*)($delim)";
541  
542 if ($yet_to_process =~ m/$tmp/s)
543 {
544 $yet_to_process = $';
545  
546 if ($type eq 'qr')
547 {
548 $processed .= qq#$alrm!KEYWORD!$alrm$BufferFill$alrm#;
549 $Buffer[$BufferFill] = $match;
550 $BufferFill++;
551  
552 $processed .= qq#$alrm!REGEX!$alrm$BufferFill$alrm#;
553 $Buffer[$BufferFill] = $1;
554 $BufferFill++;
555  
556 $processed .= qq#$alrm!KEYWORD!$alrm$BufferFill$alrm#;
557 $Buffer[$BufferFill] = $2;
558 $BufferFill++;
559 }
560 else
561 {
562 $processed .= qq#$alrm!STRING!$alrm$BufferFill$alrm#;
563 $Buffer[$BufferFill] = $match . $&;
564 $BufferFill++;
565 }
566 }
567 else
568 {
569 $processed .= qq#$alrm!ERROR!$alrm$BufferFill$alrm#;
570  
571 $Buffer[$BufferFill] = qq#$match#;
572 $BufferFill++;
573 }
574 }
575 elsif ($match =~ m/^<<(['"]?)(\w+)/)
576 {
577 my $type = $1;
578 my $delim = $2;
579
580 if ($yet_to_process =~ m/(.*?\n$delim)([\$\n])/s)
581 {
582 $processed .= qq#$alrm!STRING!$alrm$BufferFill$alrm#;
583 $yet_to_process = $2 . $';
584  
585 $Buffer[$BufferFill] = $match . $1;
586 $BufferFill++;
587 }
588 else
589 {
590 $processed .= qq#$alrm!ERROR!$alrm$BufferFill$alrm#;
591  
592 $Buffer[$BufferFill] = qq#$match#;
593 $BufferFill++;
594 }
595 }
596 elsif ($match =~ m#^(?:[=!]~\s*/|m[^A-Za-z0-9\s])#s)
597 {
598 my $delim;
599  
600 if ($match =~ m#^([=!]~\s*)/#)
601 {
602 $processed .= $1;
603 $delim = '/';
604 }
605 elsif ($match =~ m/^m(.)/)
606 {
607 $delim = $1;
608 }
609 else
610 {
611 die 'You should NEVER get here!';
612 }
613  
614 $processed .= qq#$alrm!KEYWORD!$alrm$BufferFill$alrm#;
615 $Buffer[$BufferFill] = $match;
616 $BufferFill++;
617  
618 $delim =~ tr/([{</)]}>/;
619  
620
621 $delim = quotemeta ($delim);
622  
623 my $tmp = "^((?:\\\\$delim|[^$delim])*)($delim" . '[gcimosx]*)';
624  
625 if ($yet_to_process =~ m/$tmp/s)
626 {
627 $yet_to_process = $';
628 $processed .= qq#$alrm!REGEX!$alrm$BufferFill$alrm#;
629 $Buffer[$BufferFill] = $1;
630 $BufferFill++;
631  
632 $processed .= qq#$alrm!KEYWORD!$alrm$BufferFill$alrm#;
633 $Buffer[$BufferFill] = $2;
634 $BufferFill++;
635 }
636 else
637 {
638 $processed .= qq#$alrm!ERROR!$alrm$BufferFill$alrm#;
639  
640 $Buffer[$BufferFill] = qq#$match#;
641 $BufferFill++;
642 }
643 }
644 elsif ($match =~ m#^(?:s|y|tr)([^A-Za-z0-9\s])#)
645 {
646 my $delim = $1;
647 $delim =~ tr/([{</)]}>/;
648 $delim = quotemeta ($delim);
649  
650 $processed .= qq#$alrm!KEYWORD!$alrm$BufferFill$alrm#;
651 $Buffer[$BufferFill] = $match;
652 $BufferFill++;
653  
654 my $tmp = "^((?:\\\\$delim|[^$delim])*)($delim)";
655  
656 if ($yet_to_process =~ m/$tmp/s)
657 {
658 $yet_to_process = $';
659 $processed .= qq#$alrm!REGEX!$alrm$BufferFill$alrm#;
660 $Buffer[$BufferFill] = $1;
661 $BufferFill++;
662  
663 $processed .= qq#$alrm!KEYWORD!$alrm$BufferFill$alrm#;
664 $Buffer[$BufferFill] = $2;
665  
666 $tmp = '^';
667  
668 if ($delim =~ m/^[\)\]\}>]$/)
669 {
670 if ($yet_to_process =~ m/[\(\[\{<]/)
671 {
672 $Buffer[$BufferFill] .= $&;
673  
674 $tmp .= quotemeta ($&);
675 $delim = $&;
676 $delim =~ tr/([{</)]}>/;
677 $delim = quotemeta ($delim);
678 }
679 else
680 {
681 $delim = '';
682 }
683 }
684 $BufferFill++;
685  
686 $tmp .= "((?:\\\\$delim|[^$delim])*)($delim" . '[egimosx]*)';
687  
688 if ($delim and ($yet_to_process =~ m/$tmp/s))
689 {
690 $yet_to_process = $';
691 $processed .= qq#$alrm!STRING!$alrm$BufferFill$alrm#;
692 $Buffer[$BufferFill] = $1;
693 $BufferFill++;
694  
695 $processed .= qq#$alrm!KEYWORD!$alrm$BufferFill$alrm#;
696 $Buffer[$BufferFill] = $2;
697 $BufferFill++;
698 }
699 else
700 {
701 # No warning here..
702 }
703 }
704 else
705 {
706 $processed .= qq#$alrm!ERROR!$alrm$BufferFill$alrm#;
707  
708 $Buffer[$BufferFill] = qq#$match#;
709 $BufferFill++;
710 }
711 }
712 # pod
713 elsif ($match =~ m#\n=(.*)#m)
714 {
715 my $type = $1;
716 if ($yet_to_process =~ m/(.*?)\n=cut\n/s)
717 {
718 $processed .= qq#\n$alrm!POD!$alrm$BufferFill$alrm\n#;
719 $yet_to_process = $';
720  
721 $Buffer[$BufferFill] = "=$type\n$1";
722 $BufferFill++;
723 }
724 else
725 {
726 $processed .= qq#$alrm!POD!$alrm$BufferFill$alrm#;
727  
728 $Buffer[$BufferFill] = $match . $yet_to_process;
729 $BufferFill++;
730  
731 $yet_to_process = '';
732 }
733 }
734 elsif ($match =~ m/^(\w+)(\s*=>)$/)
735 {
736 $processed .= qq#$alrm!STRING!$alrm$BufferFill$alrm# . $2;
737  
738 $Buffer[$BufferFill] = $1;
739 $BufferFill++;
740 }
741 elsif ($match =~ m#<\w+>#)
742 {
743 $processed .= qq#$alrm!FILEHANDLE!$alrm$BufferFill$alrm#;
744 $Buffer[$BufferFill] = $&;
745 $BufferFill++;
746 }
747 else
748 {
749 $processed .= qq#$alrm!ERROR!$alrm$BufferFill$alrm#;
750  
751 $Buffer[$BufferFill] = qq#$match#;
752 $BufferFill++;
753 }
754 }
755  
756 $_ = html_escape ($processed . $yet_to_process);
757  
758 my $re;
759 {
760 my $temp = '';
761 $temp = join ('|', map { quotemeta ($_) } (@KeyWords));
762  
763 $re = qr#$temp#;
764 }
765  
766 s#\b($re)\b#<span class="keyword">$1</span>#g;
767
768 s#$alrm!STRING!$alrm(\d+)$alrm#string2html ($Buffer[$1])#ge;
769 s#$alrm!REGEX!$alrm(\d+)$alrm#regex2html ($Buffer[$1])#ge;
770 s#$alrm!(\w+)!$alrm(\d+)$alrm#"<span class=\"\L$1\E\">" . html_escape ($Buffer[$2]) . '</span>'#ge;
771  
772 return (qq#\n<div class="source">\n$_</div>\n#);
773 }
774  
775 __END__