# $Id: File.pm,v 1.19 2003/10/07 19:38:30 clajac Exp $ package CPANXR::Apache::File; use CPANXR::Config; use IO::File; use File::Spec::Functions qw(rel2abs catdir catfile); use strict; sub show { my ($self, $r, $q) = @_; my $v = \&Foo'bar::cat; # Fetch id of file to show my $file_id = $q->param('id'); my $files = CPANXR::Database->select_files(file_id => $file_id); if(@$files) { my $id = $files->[0]->[0]; my $file_rel_path = $files->[0]->[2]; my $dist_root = CPANXR::Config->get("XrRoot"); my $file_abs_path = rel2abs($file_rel_path, $dist_root); $self->cat($r, $q, $file_abs_path, $file_rel_path, $id); } else { $r->print("Can't find file\n"); } } sub cat { my ($self, $r, $q, $path, $show_path, $file_id) = @_; my $hl = $q->param('hl'); $hl = $hl =~ /^\d+$/ ? $hl : -1; my $hide_pod = $q->param('pod') || 0; my $connections = CPANXR::Database->select_connections(file_id => $file_id); my %lines; for(@$connections) { my ($symbol_id, $symbol, $line_no, $offset, $path, $file_id, $pkg_id) = @$_; $lines{$line_no} = [] unless(exists $lines{$line_no}); push @{$lines{$line_no}}, [$symbol_id, $symbol, $offset, $pkg_id]; } my $io = IO::File->new($path, "r") || return; $r->print("Showing: "); $r->print($show_path); $r->print("

\n"); $r->print("Visualize  |  "); $r->print("= 0) { $r->print("&hl=$hl#l$hl"); } $r->print("\">" . ($hide_pod ? "Show" : "Hide") . " POD sections
"); $r->print("
\n");

  my $line_no = 1;

  while(defined(my $line = <$io>)) {
    next if($hide_pod && $line =~ /^\=\w+/ .. $line =~ /^\=cut/);

    if(exists $lines{$line_no}) {
      my $offset = 0;
      for my $replace (sort { $a->[2] <=> $b->[2] } @{$lines{$line_no}}) {
	my $width = "[0]";
	$width .= "&pkg=$replace->[3]" if($replace->[3]);
	$width .= "\" class=\"sym\">$replace->[1]";
	my $pos = $replace->[2] + $offset;
	substr($line, $pos, length($replace->[1])) = $width;
	$offset += length($width) - length($replace->[1]);
      }
    }

    # Fix bad stuff
    $line =~ s//>/g;
    $line =~ s/\<(a href=\"find\?.*?\" class=\"sym\")\>/<$1>/g;
    $line =~ s/\<\/a\>/<\/a>/g;

    
    my $pre = sprintf("% 6d: ", $line_no, $line_no);
    chomp $line;

    if($hl == $line_no) {
      $r->print($pre);
      $r->print("");
      $r->print($line);
      $r->print("\n");
    } else {
      $r->print($pre);
      $r->print($line);
      $r->print("\n");
      }
    

  } continue {
    $line_no++;
  }
  
  $r->print("
"); $io->close; } sub graph { my ($self, $r, $q) = @_; # Fetch id of file to show my $file_id = $q->param('file'); my $type = $q->param('type') || 'svg'; my $files = CPANXR::Database->select_files(file_id => $file_id); if(@$files) { my $path = $files->[0]->[2]; $r->print("Visualizing file: $path as "); $r->print(qq{SVG | }); $r->print(qq{PNG}); $r->print("\n

"); if($type eq 'svg') { $r->print("Click on a edge label to switch to source mode.
\n"); } $r->print("
\n"); if($type eq 'svg') { $r->print(qq{}); } elsif($type eq 'png') { $r->print(qq{}); } $r->print("
\n"); } else { $r->print("No such file\n"); } } 1;