File:  [LON-CAPA] / loncom / interface / printout.pl
Revision 1.39: download - view: text, annotated - select for diffs
Mon Oct 20 17:39:30 2003 UTC (20 years, 6 months ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
Cookie mechanism is implemented. Working on cleanup.

    1: #!/usr/bin/perl
    2: # CGI-script to run LaTeX, dvips, ps2ps, ps2pdf etc.
    3: #
    4: # $Id: printout.pl,v 1.39 2003/10/20 17:39:30 sakharuk Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: 
   29: use lib '/home/httpd/lib/perl';
   30: use LONCAPA::loncgi();
   31: use File::Path;
   32: use IO::File;
   33: use Image::Magick;
   34: 
   35: if (! &LONCAPA::loncgi::check_cookie_and_load_env()) {
   36:     print <<END;
   37: Content-type: text/html
   38: 
   39: <html>
   40: <head><title>Bad Cookie</title></head>
   41: <body>
   42: Your cookie information is incorrect.  What\'s up with that?
   43: </body>
   44: </html>
   45: END
   46:     return;
   47: }
   48: 
   49:  print "Content-type: text/html\n\n";
   50:  print "<body bgcolor=\"#FFFFFF\">\n";
   51: ####  my ($texfile,$laystyle,$numberofcolumns,$selectionmade) = split(/&/,$ENV{'QUERY_STRING'});
   52: 
   53: my $identifier = $ENV{'QUERY_STRING'};
   54: my $texfile = $ENV{$identifier.'.file'};
   55: my $laystyle = $ENV{$identifier.'.layout'};
   56: my $numberofcolumns = $ENV{$identifier.'.numcol'};
   57: my $selectionmade = $ENV{$identifier.'.selection'};
   58: 
   59:   my $advans_role=0;
   60:   if ($selectionmade>=10000) {$selectionmade=$selectionmade/10000; $advans_role=1;}
   61:   my $figfile = $texfile;
   62:   $figfile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.dat/;
   63:   my $duefile = $texfile;
   64:   $duefile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.due/;
   65:   #do we have figures?
   66:   if (-e $figfile) {
   67:       my $temporary_file=IO::File->new($figfile) || die "Couldn't open file for reading: $!\n";
   68:       my @content_of_file = <$temporary_file>;
   69:       close $temporary_file;  
   70:       my $noteps;
   71:       foreach $not_eps (@content_of_file) {
   72: 	  if ($not_eps ne '') {
   73: 	      my $eps_f = $not_eps;
   74: 	      $eps_f =~ s/\.[^.]*$/\.eps/i;
   75: 	      $_ = $eps_f;
   76: 	      m/\/home\/httpd\/html\/res\/(.+)/;
   77: 	      $eps_f = '/home/httpd/prtspool/'.$1;
   78: 	      my $path=$eps_f;
   79: 	      $path=~s/\/([^\/]+)\.eps$//; 
   80: 	      File::Path::mkpath($path,0,0777);
   81: 	      my $image = Image::Magick->new;
   82: 	      $not_eps =~ s/^\s+//;
   83: 	      $not_eps =~ s/\s+$//;
   84: 	      $status = $image->Read($not_eps);
   85: 	      if ($status) {print "  $status  ";}
   86: 	      $image->Set(page => '+100+200'); 
   87: 	      $status = $image->Write($eps_f);	    
   88: 	      if ($status) {print "  $status  ";}
   89:               #check is eps exist in prtspool
   90:               if(not -e $eps_f) {
   91: 		  for (my $i=0;$i<10000;$i++) {
   92: 		      if (-e $eps_f.'.'.$i) {
   93: 			  rename $eps_f.'.'.$i, $eps_f;
   94: 			  last;
   95: 		      }
   96: 		  }
   97: 	      }  
   98: 	  }
   99:       }
  100:   }
  101:   #print "$texfile\n"; #name of the tex file for debugging only
  102:   if (-e $texfile) {
  103:       $texfile =~ m/^(.*)\/([^\/]+)$/; 
  104:       my $name_file = $2;
  105:       my $path_file = $1.'/';
  106:       chdir $path_file;
  107:       system("latex $name_file 1>/dev/null 2>/dev/null");
  108:       if ($selectionmade>=10) {system("latex $name_file 1>/dev/null 2>/dev/null");} #to create table of contents
  109:       my $idxname=$name_file;
  110:       $idxname=~s/\.tex$/\.idx/;
  111:       if ($selectionmade>=100) {
  112: 	  system("makeindex $idxname");
  113: 	  system("latex $name_file 1>/dev/null 2>/dev/null");
  114:       } #to create index
  115:       #Do we have a latex error in the log file?
  116:       my $logfilename = $texfile;
  117:       $logfilename =~ s/\.tex$/\.log/;
  118:       my $temporary_file=IO::File->new($logfilename) || die "Couldn't open file for reading: $!\n";
  119:       my @content_of_file = <$temporary_file>;
  120:       close $temporary_file; 
  121:       my $body_log_file = join(' ',@content_of_file);
  122:       $logfilename =~ s/\.log$/\.html/;
  123:       $temporary_file = IO::File->new('>'.$logfilename); 
  124:       print $temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_log_file.'</pre></body></html>'."\n";
  125:       if ($body_log_file=~m/!\s+Emergency stop/) {
  126: 	  #LaTeX failed to parse tex file 
  127: 	  print "<h2>LaTeX could not successfully parse your tex file.</h2>";
  128: 	  print "It probably has errors in it.<br />";
  129: 	  my $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART';
  130: 	  my $whereitends = rindex $body_log_file,'STAMPOFPASSEDRESOURCEEND';
  131: 	  if ($whereitbegins!=-1 and $whereitends!=-1) {
  132: 	      print "With very high probability this error occured in ".substr($body_log_file,$whereitbegins+26,$whereitends-$whereitbegins-26)."<br /><br />";
  133: 	  }
  134: 	  print "Here are the error messages in the LaTeX log file</br><br />";
  135: 	  my $sygnal = 0;
  136: 	  for (my $i=0;$i<=$#content_of_file;$i++) {
  137: 	      if ($content_of_file[$i]=~m/^Runaway argument?/ or $content_of_file[$i]=~m/^!/) {
  138: 		  $sygnal = 1;
  139: 	      } 
  140: 	      if ($content_of_file[$i]=~m/Here is how much of/) {
  141: 		  $sygnal = 0;
  142: 	      } 
  143: 	      if ($sygnal) {
  144: 		  print "$content_of_file[$i]<br />";
  145: 	      }  
  146: 	  }
  147:           if ($advans_role) {  
  148:               print "<b><big>The link to ";
  149:               $logfilename=~s/\/home\/httpd//;
  150: 	      print "<a href=\"$logfilename\">Your log file </a></big></b>";
  151: 	      print "\n";
  152:               #link tooriginal LaTeX file (included according Michael Hamlin desire)
  153: 	      my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open file for reading: $!\n";
  154: 	      my @tex_content_of_file = <$tex_temporary_file>;
  155: 	      close $tex_temporary_file; 
  156: 	      my $body_tex_file = join(' ',@tex_content_of_file);
  157: 	      $texfile =~ s/\.tex$/aaaaa\.html/;
  158: 	      $tex_temporary_file = IO::File->new('>'.$texfile); 
  159: 	      print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
  160: 	      print "<br /><br />";
  161: 	      print "<b><big>The link to ";
  162: 	      $texfile=~s/\/home\/httpd//;
  163: 	      print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
  164: 	      print "\n";
  165: 	  }
  166: 
  167:       } elsif ($body_log_file=~m/<inserted text>/) {
  168: 	  my $whereitbegins = index $body_log_file,'<inserted text>';
  169: 	  print "You are running LaTeX in the <b>batch mode</b>.";
  170: 	  while ($whereitbegins != -1) {
  171: 	      my $tempobegin=$whereitbegins;
  172: 	      $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART',$whereitbegins;
  173: 	      my $whereitends = index $body_log_file,'STAMPOFPASSEDRESOURCEEND',$whereitbegins;
  174: 	      print "<br />It has found an error in".substr($body_log_file,$whereitbegins+26,$whereitends-$whereitbegins-26)." <br /> and corrected it.\n";
  175: 	      print "Usually this correction is valid but you probably need to check the indicated resource one more time and implement neccessary corrections by yourself.\n";
  176: 	      $whereitbegins = index $body_log_file,'<inserted text>',$tempobegin+10;
  177: 	  }
  178: 	  $name_file =~ s/\.tex/\.dvi/;
  179: 	  my $new_name_file = $name_file;
  180: 	  $new_name_file =~ s/\.dvi/\.ps/;
  181: 	  my $comma = "dvips -Ppdf -G0 -o $new_name_file";
  182: 	  system("$comma $name_file 1>/dev/null 2>/dev/null");
  183: 	  if (-e $new_name_file) {
  184: 	      print "<h1>PDF output file (see link below)</h1>\n";
  185: 	      $new_name_file =~ m/^(.*)\./;
  186: 	      my $tempo_file = $1.'temporar.ps';
  187: 	      my $name_file = $1.'.pdf';
  188: 	      if ($laystyle eq 'album' and $numberofcolumns eq '2') {
  189: 		  $comma = "psnup -2 -s1.0 $new_name_file";
  190: 		  system("$comma $tempo_file 1>/dev/null 2>/dev/null"); 
  191: 		  system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
  192: 	      } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
  193: 		  $comma = 'pstops -pletter "2:0+1(0.48w,0)"';
  194: 		  system("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null");
  195: 		  system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
  196: 	      } else {
  197: 		  system("ps2pdf $new_name_file $name_file 1>/dev/null 2>/dev/null");
  198: 	      }	    
  199: 	      my $texlog = $texfile;
  200: 	      my $texaux = $texfile;
  201: 	      my $texdvi = $texfile;
  202: 	      my $texps = $texfile;
  203: 	      $texlog =~ s/\.tex/\.log/;
  204: 	      $texaux =~ s/\.tex/\.aux/;
  205: 	      $texdvi =~ s/\.tex/\.dvi/;
  206: 	      $texps =~ s/\.tex/\.ps/;
  207: 	      my @garb = ($texaux,$texdvi,$texps);
  208: #	  unlink @garb;
  209: 	      unlink $duefile;
  210: 	      print "<a href=\"/prtspool/$name_file\">Your PDF document</a>";
  211: 	      if ($advans_role) {  
  212: 		  print "<br /><br />";
  213: 		  print "<b><big>The link to ";
  214: 		  $logfilename=~s/\/home\/httpd//;
  215: 		  print "<a href=\"$logfilename\">Your log file </a></big></b>";
  216: 		  print "\n";
  217: 		  #link tooriginal LaTeX file (included according Michael Hamlin desire)
  218: 		  my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open file for reading: $!\n";
  219: 		  my @tex_content_of_file = <$tex_temporary_file>;
  220: 		  close $tex_temporary_file; 
  221: 		  my $body_tex_file = join(' ',@tex_content_of_file);
  222: 		  $texfile =~ s/\.tex$/aaaaa\.html/;
  223: 		  $tex_temporary_file = IO::File->new('>'.$texfile); 
  224: 		  print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
  225: 		  print "<br /><br />";
  226: 		  print "<b><big>The link to ";
  227: 		  $texfile=~s/\/home\/httpd//;
  228: 		  print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
  229: 		  print "\n";
  230: 	      }
  231: 	  }
  232:       } else {
  233: 	  #LaTeX successfully parsed tex file 
  234: 	  $name_file =~ s/\.tex/\.dvi/;
  235: 	  my $new_name_file = $name_file;
  236: 	  $new_name_file =~ s/\.dvi/\.ps/;
  237: 	  my $comma = "dvips -Ppdf -G0 -o $new_name_file";
  238: 	  system("$comma $name_file 1>/dev/null 2>/dev/null");
  239: 	  if (-e $new_name_file) {
  240: 	      print "<h1>Successfully created PDF output file (see link below)</h1>\n";
  241: 	      $new_name_file =~ m/^(.*)\./;
  242: 	      my $tempo_file = $1.'temporar.ps';
  243: 	      my $name_file = $1.'.pdf';
  244: 	      if ($laystyle eq 'album' and $numberofcolumns eq '2') {
  245: 		  $comma = "psnup -2 -s1.0 $new_name_file";
  246: 		  system("$comma $tempo_file 1>/dev/null 2>/dev/null"); 
  247: 		  system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
  248: 	      } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
  249: 		  $comma = 'pstops -pletter "2:0+1(0.48w,0)"';
  250: 		  system("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null");
  251: 		  system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
  252: 	      } else {
  253: 		  system("ps2pdf $new_name_file $name_file 1>/dev/null 2>/dev/null");
  254: 	      }	    
  255: 	      my $texlog = $texfile;
  256: 	      my $texaux = $texfile;
  257: 	      my $texdvi = $texfile;
  258: 	      my $texps = $texfile;
  259: 	      $texlog =~ s/\.tex/\.log/;
  260: 	      $texaux =~ s/\.tex/\.aux/;
  261: 	      $texdvi =~ s/\.tex/\.dvi/;
  262: 	      $texps =~ s/\.tex/\.ps/;
  263: 	      my @garb = ($texlog,$texaux,$texdvi,$texps);
  264: #	  unlink @garb;
  265: 	      unlink $duefile;
  266: 	      print "<a href=\"/prtspool/$name_file\">Your PDF document</a>";
  267: 	      print "\n";
  268: 	  }
  269:       }
  270:   } else {
  271:       print "LaTeX file $texfile was not created successfully";
  272:   }
  273: 
  274: 
  275:   
  276: 
  277: 
  278: 
  279: 
  280: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>