| 1 | package Apache::VimColor; | ||
| 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 Apache::Log; | ||
| 13 | use Apache::Server; | ||
| 14 | use File::Basename (qw(basename)); | ||
| 15 | use Text::VimColor; | ||
| 16 | |||
| 17 | $VERSION = '2.31'; | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | B<Apache::VimColor> - Apache mod_perl Handler for syntax highlighting in HTML. | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | This apache handler converts text files in syntax highlighted HTML output using | ||
| 26 | L<Text::VimColor|Text::VimColor>. If allowed by the configuration the visitor | ||
| 27 | can also download the text-file without syntax highlighting. | ||
| 28 | |||
| 29 | Since Text::VimColor isn't the fastest module this version can use | ||
| 30 | L<Cache::Cache|Cache::Cache> to cache the parsed files. Also the I<ETag> and | ||
| 31 | I<LastModified> HTTP headers are set to help browsers and proxy servers to | ||
| 32 | cache the URL. | ||
| 33 | |||
| 34 | =head1 SYNOPSIS | ||
| 35 | |||
| 36 | This module requires B<mod_perl2> (see L<http://perl.apache.org/>) and | ||
| 37 | B<Text::VimColor>. | ||
| 38 | |||
| 39 | The apache configuration neccessary might look a bit like this: | ||
| 40 | |||
| 41 | # in httpd.conf (or any other apache configuration file) | ||
| 42 | <Location /source> | ||
| 43 | SetHandler perl-script | ||
| 44 | PerlHandler Apache::VimColor | ||
| 45 | |||
| 46 | # Below here is optional | ||
| 47 | PerlSetVar AllowDownload "True" | ||
| 48 | PerlSetVar CacheType "File" | ||
| 49 | PerlSetVar CacheSize 1048576 # 1 MByte | ||
| 50 | PerlSetVar CacheExpire 7200 # 2 hours | ||
| 51 | PerlSetVar StyleSheet "http://domain.com/stylesheet.css" | ||
| 52 | PerlSetVar TabSize 8 | ||
| 53 | PerlSetVar LineNumbers "True" | ||
| 54 | </Location> | ||
| 55 | |||
| 56 | For a complete list of all options and descriptions see L<below|/"CONFIGURATION DIRECTIVES">. | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | our $Position = 0; | ||
| 61 | our $Cache = {}; | ||
| 62 | |||
| 63 | return (1); | ||
| 64 | |||
| 65 | sub escape_html ($) | ||
| 66 | { | ||
| 67 | $_ = shift; | ||
| 68 | |||
| 69 | s/\&/&/g; | ||
| 70 | s/</</g; | ||
| 71 | s/>/>/g; | ||
| 72 | s/"/"/g; | ||
| 73 | |||
| 74 | s#\n#<br />\n#sg; | ||
| 75 | s/( +)/' ' x length ($1)/ge; | ||
| 76 | |||
| 77 | return ($_); | ||
| 78 | } | ||
| 79 | |||
| 80 | sub escape_tabs ($$) | ||
| 81 | { | ||
| 82 | my $value = shift; | ||
| 83 | my $tabstop = shift; | ||
| 84 | my $retval = ''; | ||
| 85 | |||
| 86 | $value =~ s/\r//g; | ||
| 87 | |||
| 88 | while ($value =~ s/^([^\n\t]*)([\n\t])//s) | ||
| 89 | { | ||
| 90 | $retval .= $1; | ||
| 91 | $Position += length ($1); | ||
| 92 | |||
| 93 | if ($2 eq "\n") | ||
| 94 | { | ||
| 95 | $retval .= "\n"; | ||
| 96 | $Position = 0; | ||
| 97 | } | ||
| 98 | else | ||
| 99 | { | ||
| 100 | my $num = $tabstop - ($Position % $tabstop); | ||
| 101 | $retval .= ' 'x$num; | ||
| 102 | $Position += $num; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | $retval .= $value; | ||
| 107 | $Position += length ($value); | ||
| 108 | |||
| 109 | return ($retval); | ||
| 110 | } | ||
| 111 | |||
| 112 | =head1 CONFIGURATION DIRECTIVES | ||
| 113 | |||
| 114 | All features of the this PerlHandler can be set in the apache configuration | ||
| 115 | using the I<PerlSetVar> directive. For example: | ||
| 116 | |||
| 117 | PerlSetVar AllowDownload true # inside <Files>, <Location>, ... | ||
| 118 | # apache directives | ||
| 119 | |||
| 120 | =over 4 | ||
| 121 | |||
| 122 | =cut | ||
| 123 | |||
| 124 | sub get_config ($) | ||
| 125 | { | ||
| 126 | my $req = shift; | ||
| 127 | my $options = | ||
| 128 | { | ||
| 129 | allow_dl => 0, | ||
| 130 | cssfile => '', | ||
| 131 | tabstop => 8 | ||
| 132 | }; | ||
| 133 | |||
| 134 | =item AllowDownload | ||
| 135 | |||
| 136 | Setting this option to B<true> will allow plaintext downloads of the files. A | ||
| 137 | link will be included in the output. The default is not to allow downloads. | ||
| 138 | |||
| 139 | =cut | ||
| 140 | |||
| 141 | if ($req->dir_config ('AllowDownload')) | ||
| 142 | { | ||
| 143 | my $conf = lc ($req->dir_config ('AllowDownload')); | ||
| 144 | |||
| 145 | if (($conf eq 'on') or ($conf eq 'true') | ||
| 146 | or ($conf eq 'yes')) | ||
| 147 | { | ||
| 148 | $options->{'allow_dl'} = 1; | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | =item CacheType | ||
| 153 | |||
| 154 | Selects the caching method to use. Depending on your choices a | ||
| 155 | L<Cache::Cache|Cache::Cache> module will be loaded and used. The default is not | ||
| 156 | to use any caching. I<CacheType> can be one of: | ||
| 157 | |||
| 158 | Memory | ||
| 159 | SharedMemory | ||
| 160 | File | ||
| 161 | |||
| 162 | Although the default is not to use caching, if I<CacheSize> is given and | ||
| 163 | I<CacheType> is not, then B<Memory> is being used. Obviously these values | ||
| 164 | correspond to the B<Cache::*Cache> modules. | ||
| 165 | |||
| 166 | The modules are loaded at runtime. If errors occur they are logged to Apache's | ||
| 167 | errorlog. | ||
| 168 | |||
| 169 | =item CacheSize | ||
| 170 | |||
| 171 | Sets the maximum size of the cache in bytes. If I<CacheSize> is non-zero the | ||
| 172 | B<Cache::SizeAware*Cache> variants will be used. | ||
| 173 | |||
| 174 | =item CacheExpire | ||
| 175 | |||
| 176 | I<CacheExpire> sets the expiration time. The value must be given in seconds. | ||
| 177 | Defaults to 3600 seconds (one hour). See L<Cache::Cache> for details. | ||
| 178 | |||
| 179 | =cut | ||
| 180 | |||
| 181 | if ($req->dir_config ('CacheType') or $req->dir_config ('CacheSize')) | ||
| 182 | { | ||
| 183 | my $cid = $req->server ()->server_hostname () . ':' . $req->location (); | ||
| 184 | my $cache; | ||
| 185 | |||
| 186 | if (defined ($Cache->{$cid})) | ||
| 187 | { | ||
| 188 | $cache = $Cache->{$cid}; | ||
| 189 | } | ||
| 190 | else | ||
| 191 | { | ||
| 192 | my $type = 'File'; | ||
| 193 | my $size = 0; | ||
| 194 | my $expr = 3600; | ||
| 195 | my $cmd; | ||
| 196 | |||
| 197 | if ($req->dir_config ('CacheType')) | ||
| 198 | { | ||
| 199 | my $tmp = lc ($req->dir_config ('CacheType')); | ||
| 200 | |||
| 201 | if ($tmp =~ m/((?:shared)?memory|file)/) | ||
| 202 | { | ||
| 203 | if ($1 eq 'sharedmemory') { $type = 'SharedMemory'; } | ||
| 204 | elsif ($1 eq 'memory') { $type = 'Memory'; } | ||
| 205 | } | ||
| 206 | else | ||
| 207 | { | ||
| 208 | $req->warn (qq(CacheType "$tmp" is not valid. Will use "File".)); | ||
| 209 | } | ||
| 210 | } | ||
| 211 | |||
| 212 | if ($req->dir_config ('CacheSize')) | ||
| 213 | { | ||
| 214 | my $tmp = $req->dir_config ('CacheSize'); | ||
| 215 | $tmp =~ s/\D//g; | ||
| 216 | |||
| 217 | $size = $tmp if ($tmp); | ||
| 218 | } | ||
| 219 | |||
| 220 | if ($req->dir_config ('CacheExpire')) | ||
| 221 | { | ||
| 222 | my $tmp = $req->dir_config ('CacheExpire'); | ||
| 223 | $tmp =~ s/\D//g; | ||
| 224 | |||
| 225 | $expr = $tmp if ($tmp); | ||
| 226 | } | ||
| 227 | |||
| 228 | if ($size) | ||
| 229 | { | ||
| 230 | $type = "SizeAware$type"; | ||
| 231 | } | ||
| 232 | $type .= 'Cache'; | ||
| 233 | |||
| 234 | $cmd = "require Cache::$type; \$cache = Cache::$type->new ({ namespace => 'Apache::VimColor', default_expires_in => $expr"; | ||
| 235 | if ($size) | ||
| 236 | { | ||
| 237 | $cmd .= ", max_size => $size"; | ||
| 238 | } | ||
| 239 | $cmd .= ' });'; | ||
| 240 | |||
| 241 | eval ($cmd); | ||
| 242 | |||
| 243 | if ($@) | ||
| 244 | { | ||
| 245 | $req->log ()->error (qq(Loading Cache::$type filed: $@")); | ||
| 246 | $cache = undef; # just to make sure ;) | ||
| 247 | } | ||
| 248 | |||
| 249 | $Cache->{$cid} = $cache if (defined ($cache)); | ||
| 250 | } | ||
| 251 | |||
| 252 | $options->{'cache'} = $cache; | ||
| 253 | } | ||
| 254 | |||
| 255 | =item TabStop | ||
| 256 | |||
| 257 | Sets the width of one tab symbol. The default is eight spaces. | ||
| 258 | |||
| 259 | =cut | ||
| 260 | |||
| 261 | if ($req->dir_config ('TabStop')) | ||
| 262 | { | ||
| 263 | my $tmp = $req->dir_config ('TabStop'); | ||
| 264 | $tmp =~ s/\D//g; | ||
| 265 | $options->{'tabstop'} = $tmp if ($tmp); | ||
| 266 | } | ||
| 267 | |||
| 268 | =item StyleSheet | ||
| 269 | |||
| 270 | If you want to include a custom stylesheet you can set this option. The string | ||
| 271 | will be included in the html-output as-is, you will have to take care of | ||
| 272 | relative filenames yourself. | ||
| 273 | |||
| 274 | All highlighted text is withing a C<span>-tag with one of the following | ||
| 275 | classes: | ||
| 276 | |||
| 277 | Comment | ||
| 278 | Constant | ||
| 279 | Error | ||
| 280 | Identifier | ||
| 281 | PreProc | ||
| 282 | Special | ||
| 283 | Statement | ||
| 284 | Todo | ||
| 285 | Type | ||
| 286 | Underlined | ||
| 287 | |||
| 288 | =cut | ||
| 289 | |||
| 290 | if ($req->dir_config ('StyleSheet')) | ||
| 291 | { | ||
| 292 | $options->{'cssfile'} = $req->dir_config ('StyleSheet'); | ||
| 293 | } | ||
| 294 | |||
| 295 | return ($options); | ||
| 296 | } | ||
| 297 | |||
| 298 | sub handler | ||
| 299 | { | ||
| 300 | my $req = shift; | ||
| 301 | my $filename = $req->filename (); | ||
| 302 | my $filename_without_path = basename ($filename); | ||
| 303 | my $options = get_config ($req); | ||
| 304 | my $download = 0; | ||
| 305 | my $mtime; | ||
| 306 | my $vim; | ||
| 307 | my $cache_entry; | ||
| 308 | my $elems; | ||
| 309 | my $output = ''; | ||
| 310 | |||
| 311 | if (!-e $filename or -z $filename) | ||
| 312 | { | ||
| 313 | return (NOT_FOUND); | ||
| 314 | } | ||
| 315 | |||
| 316 | if (!-r $filename) | ||
| 317 | { | ||
| 318 | return (FORBIDDEN); | ||
| 319 | } | ||
| 320 | |||
| 321 | $mtime = (stat ($filename))[9] or return (SERVER_ERROR); | ||
| 322 | |||
| 323 | if ($req->args ()) | ||
| 324 | { | ||
| 325 | my %args = $req->args (); | ||
| 326 | |||
| 327 | if (exists ($args{'download'}) | ||
| 328 | and ($options->{'allow_dl'})) | ||
| 329 | { | ||
| 330 | $download = 1; | ||
| 331 | } | ||
| 332 | } | ||
| 333 | |||
| 334 | # Set up header | ||
| 335 | $req->content_type ($download ? 'text/perl-script' : 'text/html'); | ||
| 336 | $req->set_last_modified ($mtime); | ||
| 337 | $req->set_etag (); | ||
| 338 | |||
| 339 | if ($req->header_only ()) | ||
| 340 | { | ||
| 341 | return (OK); | ||
| 342 | } | ||
| 343 | |||
| 344 | # User wished to download. This is already checked against the | ||
| 345 | # `AllowDownload' option. | ||
| 346 | if ($download) | ||
| 347 | { | ||
| 348 | return ($req->sendfile ($filename)); | ||
| 349 | } | ||
| 350 | |||
| 351 | $req->print (<<HEADER); | ||
| 352 | <?xml version="1.0" encoding="iso-8859-1"?> | ||
| 353 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" | ||
| 354 | "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> | ||
| 355 | |||
| 356 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> | ||
| 357 | <head> | ||
| 358 | <title>$filename_without_path</title> | ||
| 359 | HEADER | ||
| 360 | $req->print ($options->{'cssfile'} ? qq(\t\t<link rel="stylesheet" type="text/css" href=") . $options->{'cssfile'} . qq(" />\n) : <<HEADER); | ||
| 361 | <style type="text/css"> | ||
| 362 | <!-- | ||
| 363 | a { color: inherit; background-color: transparent; } | ||
| 364 | body { background-color: black; color: white; } | ||
| 365 | div.fixed { border: 1px solid silver; font-family: monospace; padding: 1ex; } | ||
| 366 | div.notice { color: silver; background-color: inherit; font-size: smaller; text-align: right; } | ||
| 367 | h1 { font-size: medium; } | ||
| 368 | span.linenumber { white-space: pre; color: yellow; background-color: transparent; } | ||
| 369 | |||
| 370 | span.Comment { color: blue; background-color: transparent; } | ||
| 371 | span.Constant { color: red; background-color: transparent; } | ||
| 372 | span.Identifier { color: aqua; background-color: transparent; } | ||
| 373 | span.Statement { color: yellow; background-color: transparent; } | ||
| 374 | span.PreProc { color: fuchsia; background-color: transparent; } | ||
| 375 | span.Type { color: lime; background-color: transparent; } | ||
| 376 | span.Special { color: fuchsia; background-color: transparent; } | ||
| 377 | span.Underlined { color: fuchsia; background-color: transparent; text-decoration: underline; } | ||
| 378 | span.Error { background-color: red; color: white; font-weight: bold; } | ||
| 379 | span.Todo { background-color: yellow; color: black; } | ||
| 380 | --> | ||
| 381 | </style> | ||
| 382 | HEADER | ||
| 383 | $req->print (<<HEADER); | ||
| 384 | </head> | ||
| 385 | |||
| 386 | <body> | ||
| 387 | HEADER | ||
| 388 | $req->print (qq(\t\t<h1>Source of <code>$filename_without_path</code>) | ||
| 389 | . ($options->{'allow_dl'} ? ' (<a href="' . $req->uri () . '?download">download</a>)' : '') . "</h1>\n"); | ||
| 390 | |||
| 391 | $req->print (qq(\t\t<div class="fixed">\n)); | ||
| 392 | |||
| 393 | if (defined ($options->{'cache'})) | ||
| 394 | { | ||
| 395 | $cache_entry = $options->{'cache'}->get ($filename); | ||
| 396 | |||
| 397 | if (defined ($cache_entry)) | ||
| 398 | { | ||
| 399 | if ($cache_entry->[0] != $mtime) | ||
| 400 | { | ||
| 401 | $cache_entry->[0] = $mtime; | ||
| 402 | $cache_entry->[1] = []; | ||
| 403 | } | ||
| 404 | |||
| 405 | $elems = $cache_entry->[1]; | ||
| 406 | } | ||
| 407 | else | ||
| 408 | { | ||
| 409 | $cache_entry = [$mtime, []]; | ||
| 410 | $elems = $cache_entry->[1]; | ||
| 411 | } | ||
| 412 | } | ||
| 413 | else | ||
| 414 | { | ||
| 415 | $elems = []; | ||
| 416 | } | ||
| 417 | |||
| 418 | # $elems may have been loaded from the cache | ||
| 419 | if (scalar (@$elems) == 0) | ||
| 420 | { | ||
| 421 | my $tmp; | ||
| 422 | |||
| 423 | # This is slow, therefore the caching. | ||
| 424 | $vim = new Text::VimColor (file => $filename); | ||
| 425 | $tmp = $vim->marked (); | ||
| 426 | |||
| 427 | # For loop to prevent aliasing. | ||
| 428 | for (my $i = 0; $i < scalar (@$tmp); $i++) | ||
| 429 | { | ||
| 430 | push (@$elems, [$tmp->[$i][0], $tmp->[$i][1]]); | ||
| 431 | } | ||
| 432 | |||
| 433 | if (defined ($options->{'cache'})) | ||
| 434 | { | ||
| 435 | $options->{'cache'}->set ($filename, $cache_entry); | ||
| 436 | } | ||
| 437 | } | ||
| 438 | |||
| 439 | # For loop to prevent aliasing. | ||
| 440 | for (my $i = 0; $i < scalar (@$elems); $i++) | ||
| 441 | { | ||
| 442 | my $type = $elems->[$i][0]; | ||
| 443 | my $value = $elems->[$i][1]; | ||
| 444 | |||
| 445 | $value = escape_tabs ($value, $options->{'tabstop'}); | ||
| 446 | $value = escape_html ($value); | ||
| 447 | |||
| 448 | if ($type) | ||
| 449 | { | ||
| 450 | $output .= qq(<span class="$type">$value</span>); | ||
| 451 | } | ||
| 452 | else | ||
| 453 | { | ||
| 454 | $output .= $value; | ||
| 455 | } | ||
| 456 | } | ||
| 457 | |||
| 458 | =item LineNumbers | ||
| 459 | |||
| 460 | Sets wether or not line numbers will be displayed. The Default is not to | ||
| 461 | display line numbers. | ||
| 462 | |||
| 463 | =back | ||
| 464 | |||
| 465 | =cut | ||
| 466 | if ($req->dir_config ('LineNumbers') and ($req->dir_config ('LineNumbers') =~ m/^(yes|on|true)$/i)) | ||
| 467 | { | ||
| 468 | my $linenumber = 1; | ||
| 469 | $output =~ s#^#sprintf (q(<span class="linenumber">%7u </span>), $linenumber++)#gem; | ||
| 470 | } | ||
| 471 | $req->print ($output); | ||
| 472 | |||
| 473 | $req->print ("\t\t</div>\n"); | ||
| 474 | $req->print (<<FOOTER); | ||
| 475 | <div class="notice"> | ||
| 476 | Generated with <a href="http://search.cpan.org/perldoc?Apache%3A%3AVimColor">Apache::VimColor $VERSION</a> | ||
| 477 | by <a href="http://verplant.org/">Florian octo Forster</a> | ||
| 478 | </div> | ||
| 479 | </body> | ||
| 480 | </html> | ||
| 481 | FOOTER | ||
| 482 | |||
| 483 | return (OK); | ||
| 484 | } | ||
| 485 | |||
| 486 | =head1 SEE ALSO | ||
| 487 | |||
| 488 | L<perl(1)>, L<mod_perl(3)>, L<Apache(3)>, L<Text::VimColor|Text::VimColor>, | ||
| 489 | L<Cache::Cache> | ||
| 490 | |||
| 491 | =head1 AUTHOR | ||
| 492 | |||
| 493 | Florian octo Forster | ||
| 494 | octo(at)verplant.org | ||
| 495 | http://verplant.org/ | ||
| 496 | |||
| 497 | =head1 COPYRIGHT | ||
| 498 | |||
| 499 | Copyright (c) 2005 Florian Forster. | ||
| 500 | |||
| 501 | All rights reserved. This package is free software; you can redistribute it | ||
| 502 | and/or modify it under the same terms as Perl itself. | ||
| 503 | |||
| 504 | =cut |