File:  [LON-CAPA] / loncom / interface / printout.pl
Revision 1.37: download - view: text, annotated - select for diffs
Mon Sep 1 03:30:57 2003 UTC (20 years, 8 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- missing GPL header added

    1: #!/usr/bin/perl
    2: # CGI-script to run LaTeX, dvips, ps2ps, ps2pdf etc.
    3: #
    4: # $Id: printout.pl,v 1.37 2003/09/01 03:30:57 albertel 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 IO::File;
   30: use Image::Magick;
   31:  print "Content-type: text/html\n\n";
   32:  print "<body bgcolor=\"#FFFFFF\">\n";
   33:   my ($texfile,$laystyle,$numberofcolumns,$selectionmade) = split(/&/,$ENV{'QUERY_STRING'});
   34:   my $advans_role=0;
   35:   if ($selectionmade>=10000) {$selectionmade=$selectionmade/10000; $advans_role=1;}
   36:   my $figfile = $texfile;
   37:   $figfile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.dat/;
   38:   my $duefile = $texfile;
   39:   $duefile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.due/;
   40:   #do we have figures?
   41:   if (-e $figfile) {
   42:       my $temporary_file=IO::File->new($figfile) || die "Couldn't open file for reading: $!\n";
   43:       my @content_of_file = <$temporary_file>;
   44:       close $temporary_file;  
   45:       my $noteps;
   46:       foreach $not_eps (@content_of_file) {
   47: 	  if ($not_eps ne '') {
   48: 	      my $eps_f = $not_eps;
   49: 	      $eps_f =~ s/\.[^.]*$/\.eps/i;
   50: 	      $_ = $eps_f;
   51: 	      m/\/([^\/]+)$/;
   52: 	      $eps_f = '/home/httpd/prtspool/'.$1;
   53: 	      my $image = Image::Magick->new;
   54: 	      $not_eps =~ s/^\s+//;
   55: 	      $not_eps =~ s/\s+$//;
   56: 	      $status = $image->Read($not_eps);
   57: 	      if ($status) {print "  $status  ";}
   58: 	      $image->Set(page => '+100+200'); 
   59: 	      $status = $image->Write($eps_f);	    
   60: 	      if ($status) {print "  $status  ";}
   61:               #check is eps exist in prtspool
   62:               if(not -e $eps_f) {
   63: 		  for (my $i=0;$i<10000;$i++) {
   64: 		      if (-e $eps_f.'.'.$i) {
   65: 			  rename $eps_f.'.'.$i, $eps_f;
   66: 			  last;
   67: 		      }
   68: 		  }
   69: 	      }  
   70: 	  }
   71:       }
   72:       unlink $figfile;
   73:   }
   74:   #print "$texfile\n"; #name of the tex file for debugging only
   75:   if (-e $texfile) {
   76:       $texfile =~ m/^(.*)\/([^\/]+)$/; 
   77:       my $name_file = $2;
   78:       my $path_file = $1.'/';
   79:       chdir $path_file;
   80:       system("latex $name_file 1>/dev/null 2>/dev/null");
   81:       if ($selectionmade>=10) {system("latex $name_file 1>/dev/null 2>/dev/null");} #to create table of contents
   82:       my $idxname=$name_file;
   83:       $idxname=~s/\.tex$/\.idx/;
   84:       if ($selectionmade>=100) {
   85: 	  system("makeindex $idxname");
   86: 	  system("latex $name_file 1>/dev/null 2>/dev/null");
   87:       } #to create index
   88:       #Do we have a latex error in the log file?
   89:       my $logfilename = $texfile;
   90:       $logfilename =~ s/\.tex$/\.log/;
   91:       my $temporary_file=IO::File->new($logfilename) || die "Couldn't open file for reading: $!\n";
   92:       my @content_of_file = <$temporary_file>;
   93:       close $temporary_file; 
   94:       my $body_log_file = join(' ',@content_of_file);
   95:       $logfilename =~ s/\.log$/\.html/;
   96:       $temporary_file = IO::File->new('>'.$logfilename); 
   97:       print $temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_log_file.'</pre></body></html>'."\n";
   98:       if ($body_log_file=~m/!\s+Emergency stop/) {
   99: 	  #LaTeX failed to parse tex file 
  100: 	  print "<h2>LaTeX could not successfully parse your tex file.</h2>";
  101: 	  print "It probably has errors in it.<br />";
  102: 	  my $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART';
  103: 	  my $whereitends = rindex $body_log_file,'STAMPOFPASSEDRESOURCEEND';
  104: 	  if ($whereitbegins!=-1 and $whereitends!=-1) {
  105: 	      print "With very high probability this error occured in ".substr($body_log_file,$whereitbegins+26,$whereitends-$whereitbegins-26)."<br /><br />";
  106: 	  }
  107: 	  print "Here are the error messages in the LaTeX log file</br><br />";
  108: 	  my $sygnal = 0;
  109: 	  for (my $i=0;$i<=$#content_of_file;$i++) {
  110: 	      if ($content_of_file[$i]=~m/^Runaway argument?/ or $content_of_file[$i]=~m/^!/) {
  111: 		  $sygnal = 1;
  112: 	      } 
  113: 	      if ($content_of_file[$i]=~m/Here is how much of/) {
  114: 		  $sygnal = 0;
  115: 	      } 
  116: 	      if ($sygnal) {
  117: 		  print "$content_of_file[$i]<br />";
  118: 	      }  
  119: 	  }
  120:           if ($advans_role) {  
  121:               print "<b><big>The link to ";
  122:               $logfilename=~s/\/home\/httpd//;
  123: 	      print "<a href=\"$logfilename\">Your log file </a></big></b>";
  124: 	      print "\n";
  125:               #link tooriginal LaTeX file (included according Michael Hamlin desire)
  126: 	      my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open file for reading: $!\n";
  127: 	      my @tex_content_of_file = <$tex_temporary_file>;
  128: 	      close $tex_temporary_file; 
  129: 	      my $body_tex_file = join(' ',@tex_content_of_file);
  130: 	      $texfile =~ s/\.tex$/aaaaa\.html/;
  131: 	      $tex_temporary_file = IO::File->new('>'.$texfile); 
  132: 	      print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
  133: 	      print "<br /><br />";
  134: 	      print "<b><big>The link to ";
  135: 	      $texfile=~s/\/home\/httpd//;
  136: 	      print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
  137: 	      print "\n";
  138: 	  }
  139: 
  140:       } elsif ($body_log_file=~m/<inserted text>/) {
  141: 	  my $whereitbegins = index $body_log_file,'<inserted text>';
  142: 	  print "You are running LaTeX in the <b>batch mode</b>.";
  143: 	  while ($whereitbegins != -1) {
  144: 	      my $tempobegin=$whereitbegins;
  145: 	      $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART',$whereitbegins;
  146: 	      my $whereitends = index $body_log_file,'STAMPOFPASSEDRESOURCEEND',$whereitbegins;
  147: 	      print "<br />It has found an error in".substr($body_log_file,$whereitbegins+26,$whereitends-$whereitbegins-26)." <br /> and corrected it.\n";
  148: 	      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";
  149: 	      $whereitbegins = index $body_log_file,'<inserted text>',$tempobegin+10;
  150: 	  }
  151: 	  $name_file =~ s/\.tex/\.dvi/;
  152: 	  my $new_name_file = $name_file;
  153: 	  $new_name_file =~ s/\.dvi/\.ps/;
  154: 	  my $comma = "dvips -Ppdf -G0 -o $new_name_file";
  155: 	  system("$comma $name_file 1>/dev/null 2>/dev/null");
  156: 	  if (-e $new_name_file) {
  157: 	      print "<h1>PDF output file (see link below)</h1>\n";
  158: 	      $new_name_file =~ m/^(.*)\./;
  159: 	      my $tempo_file = $1.'temporar.ps';
  160: 	      my $name_file = $1.'.pdf';
  161: 	      if ($laystyle eq 'album' and $numberofcolumns eq '2') {
  162: 		  $comma = "psnup -2 -s1.0 $new_name_file";
  163: 		  system("$comma $tempo_file 1>/dev/null 2>/dev/null"); 
  164: 		  system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
  165: 	      } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
  166: 		  $comma = 'pstops -pletter "2:0+1(0.48w,0)"';
  167: 		  system("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null");
  168: 		  system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
  169: 	      } else {
  170: 		  system("ps2pdf $new_name_file $name_file 1>/dev/null 2>/dev/null");
  171: 	      }	    
  172: 	      my $texlog = $texfile;
  173: 	      my $texaux = $texfile;
  174: 	      my $texdvi = $texfile;
  175: 	      my $texps = $texfile;
  176: 	      $texlog =~ s/\.tex/\.log/;
  177: 	      $texaux =~ s/\.tex/\.aux/;
  178: 	      $texdvi =~ s/\.tex/\.dvi/;
  179: 	      $texps =~ s/\.tex/\.ps/;
  180: 	      my @garb = ($texaux,$texdvi,$texps);
  181: #	  unlink @garb;
  182: 	      unlink $duefile;
  183: 	      print "<a href=\"/prtspool/$name_file\">Your PDF document</a>";
  184: 	      if ($advans_role) {  
  185: 		  print "<br /><br />";
  186: 		  print "<b><big>The link to ";
  187: 		  $logfilename=~s/\/home\/httpd//;
  188: 		  print "<a href=\"$logfilename\">Your log file </a></big></b>";
  189: 		  print "\n";
  190: 		  #link tooriginal LaTeX file (included according Michael Hamlin desire)
  191: 		  my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open file for reading: $!\n";
  192: 		  my @tex_content_of_file = <$tex_temporary_file>;
  193: 		  close $tex_temporary_file; 
  194: 		  my $body_tex_file = join(' ',@tex_content_of_file);
  195: 		  $texfile =~ s/\.tex$/aaaaa\.html/;
  196: 		  $tex_temporary_file = IO::File->new('>'.$texfile); 
  197: 		  print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
  198: 		  print "<br /><br />";
  199: 		  print "<b><big>The link to ";
  200: 		  $texfile=~s/\/home\/httpd//;
  201: 		  print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
  202: 		  print "\n";
  203: 	      }
  204: 	  }
  205:       } else {
  206: 	  #LaTeX successfully parsed tex file 
  207: 	  $name_file =~ s/\.tex/\.dvi/;
  208: 	  my $new_name_file = $name_file;
  209: 	  $new_name_file =~ s/\.dvi/\.ps/;
  210: 	  my $comma = "dvips -Ppdf -G0 -o $new_name_file";
  211: 	  system("$comma $name_file 1>/dev/null 2>/dev/null");
  212: 	  if (-e $new_name_file) {
  213: 	      print "<h1>Successfully created PDF output file (see link below)</h1>\n";
  214: 	      $new_name_file =~ m/^(.*)\./;
  215: 	      my $tempo_file = $1.'temporar.ps';
  216: 	      my $name_file = $1.'.pdf';
  217: 	      if ($laystyle eq 'album' and $numberofcolumns eq '2') {
  218: 		  $comma = "psnup -2 -s1.0 $new_name_file";
  219: 		  system("$comma $tempo_file 1>/dev/null 2>/dev/null"); 
  220: 		  system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
  221: 	      } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
  222: 		  $comma = 'pstops -pletter "2:0+1(0.48w,0)"';
  223: 		  system("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null");
  224: 		  system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
  225: 	      } else {
  226: 		  system("ps2pdf $new_name_file $name_file 1>/dev/null 2>/dev/null");
  227: 	      }	    
  228: 	      my $texlog = $texfile;
  229: 	      my $texaux = $texfile;
  230: 	      my $texdvi = $texfile;
  231: 	      my $texps = $texfile;
  232: 	      $texlog =~ s/\.tex/\.log/;
  233: 	      $texaux =~ s/\.tex/\.aux/;
  234: 	      $texdvi =~ s/\.tex/\.dvi/;
  235: 	      $texps =~ s/\.tex/\.ps/;
  236: 	      my @garb = ($texlog,$texaux,$texdvi,$texps);
  237: #	  unlink @garb;
  238: 	      unlink $duefile;
  239: 	      print "<a href=\"/prtspool/$name_file\">Your PDF document</a>";
  240: 	      print "\n";
  241: 	  }
  242:       }
  243:   } else {
  244:       print "LaTeX file $texfile was not created successfully";
  245:   }
  246: 
  247: 
  248:   
  249: 
  250: 
  251: 
  252: 
  253: 

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