blib/lib/Apache/Syntax/Highlight/Perl.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Apache::Syntax::Highlight::Perl;
2  
3 require 5.005;
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 $VERSION = '1.01';
7  
8 0 ▶use mod_perl;
Subroutine BEGIN: called CC=1 SCAR=2.6
9 use constant MP2 => ($mod_perl::VERSION >= 1.99);
10  
11 use Syntax::Highlight::Perl;
12 use IO::File;
13  
14 my $can_cache;
15 my %stat;
16 BEGIN {
17 # Tests mod_perl version and uses the appropriate components
18 if (MP2) {
19 require Apache::Const;
20 Apache::Const->import(-compile => qw(DECLINED OK));
21 require Apache::RequestRec;
22 require Apache::RequestIO;
23 require Apache::RequestUtil;
24 }
25 else {
26 require Apache::Constants;
27 Apache::Constants->import(qw(DECLINED OK));
28 }
29  
30 # Test caching necessaries modules
31 eval { require Digest::MD5; Digest::MD5->can('md5_hex') };
32 $can_cache = $@ ? 0 : 1;
33 }
34  
35 my %default_styles = (
36 'Comment_Normal' => 'color:#006699;font-style:italic;',
37 'Comment_POD' => 'color:#001144;font-style:italic;',
38 'Directive' => 'color:#339999;font-style:italic;',
39 'Label' => 'color:#993399;font-style:italic;',
40 'Quote' => 'color:#0000aa;',
41 'String' => 'color:#0000aa;',
42 'Subroutine' => 'color:#998800;',
43 'Variable_Scalar' => 'color:#008800;',
44 'Variable_Array' => 'color:#ff7700;',
45 'Variable_Hash' => 'color:#8800ff;',
46 'Variable_Typeglob' => 'color:#ff0033;',
47 'Whitespace' => '',
48 'Character' => 'color:#880000;',
49 'Keyword' => 'color:#000000;',
50 'Builtin_Operator' => 'color:#330000;',
51 'Builtin_Function' => 'color:#000011;',
52 'Operator' => 'color:#000000;',
53 'Bareword' => 'color:#33AA33;',
54 'Package' => 'color:#990000;',
55 'Number' => 'color:#ff00ff;',
56 'Symbol' => 'color:#000000;',
57 'CodeTerm' => 'color:#000000;',
58 'DATA' => 'color:#000000;',
59 'LineNumber' => 'color:#CCCCCC;'
60 );
61  
62 sub handler {
63 my $r = shift;
64 my $str; # buffered output
65 my $mtime;
66 my $have_to_cache = 0;
67
68 return (MP2 ? Apache::DECLINED : Apache::Constants::DECLINED) if $r->args =~ /download/i;
69  
70 my $sln = ($r->dir_config('HighlightShowLineNumbers') =~ /^on$/i || $r->args =~ /ShowLineNumbers/i) ? 1 : 0;
71 my $key = $r->filename . $sln;
72 my $debug = $r->dir_config('HighlightDebug') eq 'On' ? 1 : 0;
73
74 # Cache feature
75 if ( $can_cache && $r->dir_config('HighlightCache') =~ /^on$/i ) {
76 $mtime = (stat $r->filename)[9];
77 # File needs to be processed
78 if ( ! defined $stat{$key} || $mtime > $stat{$key} ) {
79 $stat{$key} = $mtime;
80 $have_to_cache = 1;
81 print STDERR "[$$] We have to cache!\n" if $debug;
82 }
83 # We have already in cache
84 else {
85 $str = get_cache( file => $key, dir => $r->dir_config('HighlightCacheDir') || '/tmp', debug => $debug );
86 }
87 use Data::Dumper;
88 print STDERR ("[$$] " . $r->filename . "\n" . Dumper(\%stat)) if $debug;
89 }
90  
91 # When we must highlight?
92 if ( $have_to_cache || ! $str ) {
93
94 print STDERR "[$$] Generating highlight...\n" if $debug;
95  
96 my $formatter = new Syntax::Highlight::Perl;
97
98 # Open file to highlight
99 my $fh = new IO::File($r->filename);
100
101 # Escapes HTML
102 $formatter->define_substitution('<' => '&lt;', '>' => '&gt;', '&' => '&amp;');
103  
104 # Install the formats
105 if ( $r->dir_config('HighlightCSS') ) {
106 foreach (keys %default_styles) {
107 $formatter->set_format($_, [ "<span class=\"$_\">",'</span>' ] );
108 }
109 $str = '<LINK HREF="' . $r->dir_config('HighlightCSS') . '" REL="stylesheet" TYPE="text/css"><PRE>';
110 }
111 else {
112 while ( my($type,$style) = each %default_styles ) {
113 $formatter->set_format($type, [ "<span style=\"$style\">",'</span>' ] );
114 $str = '<PRE style="font-size:10pt;color:#333366;">';
115 }
116 }
117 my @lines = $formatter->format_string(<$fh>);
118 undef $fh;
119  
120 # Adds line numbers
121 if ( $sln ) {
122 my $line_number = 1;
123 my $max_space = length($formatter->line_count) + 1;
124 @lines = map { '&nbsp;' x ($max_space - length($line_number)) . '<span class="LineNumber">' . $line_number++ . '</span>&nbsp;' . $_ } @lines;
125 }
126 $str .= join('',@lines) . '</PRE>';
127 }
128  
129 if ( $have_to_cache ) {
130 put_cache( file => $key, content => $str, dir => $r->dir_config('HighlightCacheDir') || '/tmp', debug => $debug );
131 }
132  
133 # Output code to client
134 $r->content_type('text/html');
135 MP2 ? 1 : $r->send_http_header;
136 $r->print($str);
137 return MP2 ? Apache::OK : Apache::Constants::OK;
138 }
139  
140 sub get_cache {
141 my %args = @_;
142 $args{'key'} ||= Digest::MD5->md5_hex($args{'file'});
143 return undef if ! $args{'file'};
144 print STDERR "[$$] Opening file: $args{'dir'}/$args{'key'}\n" if $args{'debug'};
145 my $fh = new IO::File("$args{'dir'}/$args{'key'}");
146 my $slurp = do { local $/; <$fh> };
147 return $slurp;
148 }
149  
150 sub put_cache {
151 my %args = @_;
152 return 0 if ( $args{'dir'} !~ /^\/tmp/ );
153 $args{'key'} ||= Digest::MD5->md5_hex($args{'file'});
154 return 0 if ( ! $args{'key'} || ! $args{'content'} );
155 print STDERR "[$$] Writing file: $args{'dir'}/$args{'key'}\n" if $args{'debug'};
156 my $fh;
157 if ( open($fh,">$args{'dir'}/$args{'key'}") ) {
158 flock($fh,2) if $^O !~ /win32/i;
159 print $fh $args{'content'};
160 flock($fh,8) if $^O !~ /win32/i;
161 close($fh);
162 return 1;
163 }
164 return 0;
165 }
166  
167 1;
168 __END__
↑ Summary