[system] / trunk / webwork2 / htdocs / show-source.cgi Repository:
ViewVC logotype

View of /trunk/webwork2/htdocs/show-source.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6070 - (download) (annotate)
Fri Jun 26 00:58:52 2009 UTC (3 years, 10 months ago) by gage
File size: 1732 byte(s)
syncing HEAD with webwork2.2.4.7 on 6/25/2009

    1 #!/usr/bin/env perl
    2 
    3 print "Content-type:  text/HTML\n\n";
    4 
    5 $file = $ENV{PATH_INFO};
    6 
    7 Error("Can't load specified file") if ($file =~ m!(\.\./|//|:|~)!);
    8 Error("Can't load specified file") if ($file !~ m!\.p[lg]$!);
    9 
   10 
   11 #$root = '/usr/local/WeBWorK';
   12 $root = '/opt/webwork/';
   13 $filedir = $file; $filedir =~ s!/[^/]+$!!;
   14 $file =~ s!.*/!!;
   15 
   16 @PGdirs = (
   17   "../templates$filedir",
   18   "../templates/macros",
   19 #  "$root/local/macros",
   20   "$root/pg/macros",
   21 );
   22 
   23 foreach $dir (@PGdirs) {ShowSource("$dir/$file") if (-e "$dir/$file")}
   24 
   25 Error("Can't find specified file",join(",",@PGdirs).": ".$file);
   26 
   27 sub Error {
   28   print "<HTML>\n<HEAD>\n<TITLE>Show-Source Error</TITLE>\n</HEAD>\n";
   29   print "<BODY>\n\n<H1>Show-Source Error:</H1>\n\n";
   30   print join("\n",@_),"\n";
   31   print "</BODY>\n</HTML>";
   32   exit;
   33 }
   34 
   35 sub ShowSource {
   36   my $file = shift;
   37   my $name = $file; $name =~ s!.*/!!;
   38 
   39   open(PGFILE,$file);
   40   $program = join('',<PGFILE>);
   41   close(PGFILE);
   42 
   43   $program =~ s/&/\&amp;/g;
   44   $program =~ s/</\&lt;/g;
   45   $program =~ s/>/\&gt;/g;
   46   $program =~ s/\t/        /g;
   47   print "<HTML>\n<HEAD>\n<TITLE>Problem Source Code</TITLE>\n</HEAD>\n";
   48   print "<BODY>\n\n<H1>Source Code for <CODE>$name</CODE>:</H1>\n\n";
   49   print "<HR>\n<BLOCKQUOTE>\n";
   50   print "<PRE>";
   51   print MarkSource($program);
   52   print "</PRE>\n";
   53   print "</BLOCKQUOTE>\n<HR>\n";
   54   print "</BODY>\n</HTML>\n";
   55   exit;
   56 }
   57 
   58 sub MarkSource {
   59   my $program = shift;
   60   local $cgi = $ENV{SCRIPT_NAME};
   61   $program =~ s/loadMacros *\(([^\)]*)\)/MakeLinks($1)/ge;
   62   return $program;
   63 }
   64 
   65 sub MakeLinks {
   66   my $macros = shift;
   67   $macros =~ s!"([^\"<]*)"!"<A HREF="$cgi$filedir/\1">\1</A>"!g;
   68   $macros =~ s!'([^\'<]*)'!'<A HREF="$cgi$filedir/\1">\1</A>'!g;
   69   return 'loadMacros('.$macros.')';
   70 }
   71 
   72 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9