File:  [LON-CAPA] / loncom / interface / printout.pl
Revision 1.40: download - view: text, annotated - select for diffs
Mon Oct 20 18:34:20 2003 UTC (20 years, 6 months ago) by sakharuk
Branches: MAIN
CVS tags: version_1_1_X, version_1_1_1, version_1_1_0, version_1_0_99_3, version_1_0_99_2, version_1_0_99_1, version_1_0_99, HEAD
Some cleanup has been done to use cookie mechanism.

    1: #!/usr/bin/perl
    2: # CGI-script to run LaTeX, dvips, ps2ps, ps2pdf etc.
    3: #
    4: # $Id: printout.pl,v 1.40 2003/10/20 18:34:20 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.
   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: 
   52:   my $identifier = $ENV{'QUERY_STRING'};
   53:   my $texfile = $ENV{'cgi.'.$identifier.'.file'};
   54:   my $laystyle = $ENV{'cgi.'.$identifier.'.layout'};
   55:   my $numberofcolumns = $ENV{'cgi.'.$identifier.'.numcol'};
   56:   my $selectionmade = $ENV{'cgi.'.$identifier.'.selection'};
   57:   my $tableofcontents = $ENV{'cgi.'.$identifier.'tableofcontents'};
   58:   my $tableofindex = $ENV{'cgi.'.$identifier.'tableofindex'};
   59:   my $advans_role = $ENV{'cgi.'.$identifier.'role'};
   60: 
   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 ($tableofcontents eq 'yes') {
  109: 	  system("latex $name_file 1>/dev/null 2>/dev/null");
  110:       } #to create table of contents
  111:       my $idxname=$name_file;
  112:       $idxname=~s/\.tex$/\.idx/;
  113:       if ($tableofindex eq 'yes') {
  114: 	  system("makeindex $idxname");
  115: 	  system("latex $name_file 1>/dev/null 2>/dev/null");
  116:       } #to create index
  117:       #Do we have a latex error in the log file?
  118:       my $logfilename = $texfile;
  119:       $logfilename =~ s/\.tex$/\.log/;
  120:       my $temporary_file=IO::File->new($logfilename) || die "Couldn't open file for reading: $!\n";
  121:       my @content_of_file = <$temporary_file>;
  122:       close $temporary_file; 
  123:       my $body_log_file = join(' ',@content_of_file);
  124:       $logfilename =~ s/\.log$/\.html/;
  125:       $temporary_file = IO::File->new('>'.$logfilename); 
  126:       print $temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_log_file.'</pre></body></html>'."\n";
  127:       if ($body_log_file=~m/!\s+Emergency stop/) {
  128: 	  #LaTeX failed to parse tex file 
  129: 	  print "<h2>LaTeX could not successfully parse your tex file.</h2>";
  130: 	  print "It probably has errors in it.<br />";
  131: 	  my $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART';
  132: 	  my $whereitends = rindex $body_log_file,'STAMPOFPASSEDRESOURCEEND';
  133: 	  if ($whereitbegins!=-1 and $whereitends!=-1) {
  134: 	      print "With very high probability this error occured in ".substr($body_log_file,$whereitbegins+26,$whereitends-$whereitbegins-26)."<br /><br />";
  135: 	  }
  136: 	  print "Here are the error messages in the LaTeX log file</br><br />";
  137: 	  my $sygnal = 0;
  138: 	  for (my $i=0;$i<=$#content_of_file;$i++) {
  139: 	      if ($content_of_file[$i]=~m/^Runaway argument?/ or $content_of_file[$i]=~m/^!/) {
  140: 		  $sygnal = 1;
  141: 	      } 
  142: 	      if ($content_of_file[$i]=~m/Here is how much of/) {
  143: 		  $sygnal = 0;
  144: 	      } 
  145: 	      if ($sygnal) {
  146: 		  print "$content_of_file[$i]<br />";
  147: 	      }  
  148: 	  }
  149:           if ($advans_role) {  
  150:               print "<b><big>The link to ";
  151:               $logfilename=~s/\/home\/httpd//;
  152: 	      print "<a href=\"$logfilename\">Your log file </a></big></b>";
  153: 	      print "\n";
  154:               #link tooriginal LaTeX file (included according Michael Hamlin desire)
  155: 	      my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open file for reading: $!\n";
  156: 	      my @tex_content_of_file = <$tex_temporary_file>;
  157: 	      close $tex_temporary_file; 
  158: 	      my $body_tex_file = join(' ',@tex_content_of_file);
  159: 	      $texfile =~ s/\.tex$/aaaaa\.html/;
  160: 	      $tex_temporary_file = IO::File->new('>'.$texfile); 
  161: 	      print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
  162: 	      print "<br /><br />";
  163: 	      print "<b><big>The link to ";
  164: 	      $texfile=~s/\/home\/httpd//;
  165: 	      print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
  166: 	      print "\n";
  167: 	  }
  168: 
  169:       } elsif ($body_log_file=~m/<inserted text>/) {
  170: 	  my $whereitbegins = index $body_log_file,'<inserted text>';
  171: 	  print "You are running LaTeX in the <b>batch mode</b>.";
  172: 	  while ($whereitbegins != -1) {
  173: 	      my $tempobegin=$whereitbegins;
  174: 	      $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART',$whereitbegins;
  175: 	      my $whereitends = index $body_log_file,'STAMPOFPASSEDRESOURCEEND',$whereitbegins;
  176: 	      print "<br />It has found an error in".substr($body_log_file,$whereitbegins+26,$whereitends-$whereitbegins-26)." <br /> and corrected it.\n";
  177: 	      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";
  178: 	      $whereitbegins = index $body_log_file,'<inserted text>',$tempobegin+10;
  179: 	  }
  180: 	  $name_file =~ s/\.tex/\.dvi/;
  181: 	  my $new_name_file = $name_file;
  182: 	  $new_name_file =~ s/\.dvi/\.ps/;
  183: 	  my $comma = "dvips -Ppdf -G0 -o $new_name_file";
  184: 	  system("$comma $name_file 1>/dev/null 2>/dev/null");
  185: 	  if (-e $new_name_file) {
  186: 	      print "<h1>PDF output file (see link below)</h1>\n";
  187: 	      $new_name_file =~ m/^(.*)\./;
  188: 	      my $tempo_file = $1.'temporar.ps';
  189: 	      my $name_file = $1.'.pdf';
  190: 	      if ($laystyle eq 'album' and $numberofcolumns eq '2') {
  191: 		  $comma = "psnup -2 -s1.0 $new_name_file";
  192: 		  system("$comma $tempo_file 1>/dev/null 2>/dev/null"); 
  193: 		  system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
  194: 	      } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
  195: 		  $comma = 'pstops -pletter "2:0+1(0.48w,0)"';
  196: 		  system("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null");
  197: 		  system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
  198: 	      } else {
  199: 		  system("ps2pdf $new_name_file $name_file 1>/dev/null 2>/dev/null");
  200: 	      }	    
  201: 	      my $texlog = $texfile;
  202: 	      my $texaux = $texfile;
  203: 	      my $texdvi = $texfile;
  204: 	      my $texps = $texfile;
  205: 	      $texlog =~ s/\.tex/\.log/;
  206: 	      $texaux =~ s/\.tex/\.aux/;
  207: 	      $texdvi =~ s/\.tex/\.dvi/;
  208: 	      $texps =~ s/\.tex/\.ps/;
  209: 	      my @garb = ($texaux,$texdvi,$texps);
  210: #	  unlink @garb;
  211: 	      unlink $duefile;
  212: 	      print "<a href=\"/prtspool/$name_file\">Your PDF document</a>";
  213: 	      if ($advans_role) {  
  214: 		  print "<br /><br />";
  215: 		  print "<b><big>The link to ";
  216: 		  $logfilename=~s/\/home\/httpd//;
  217: 		  print "<a href=\"$logfilename\">Your log file </a></big></b>";
  218: 		  print "\n";
  219: 		  #link tooriginal LaTeX file (included according Michael Hamlin desire)
  220: 		  my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open file for reading: $!\n";
  221: 		  my @tex_content_of_file = <$tex_temporary_file>;
  222: 		  close $tex_temporary_file; 
  223: 		  my $body_tex_file = join(' ',@tex_content_of_file);
  224: 		  $texfile =~ s/\.tex$/aaaaa\.html/;
  225: 		  $tex_temporary_file = IO::File->new('>'.$texfile); 
  226: 		  print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
  227: 		  print "<br /><br />";
  228: 		  print "<b><big>The link to ";
  229: 		  $texfile=~s/\/home\/httpd//;
  230: 		  print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
  231: 		  print "\n";
  232: 	      }
  233: 	  }
  234:       } else {
  235: 	  #LaTeX successfully parsed tex file 
  236: 	  $name_file =~ s/\.tex/\.dvi/;
  237: 	  my $new_name_file = $name_file;
  238: 	  $new_name_file =~ s/\.dvi/\.ps/;
  239: 	  my $comma = "dvips -Ppdf -G0 -o $new_name_file";
  240: 	  system("$comma $name_file 1>/dev/null 2>/dev/null");
  241: 	  if (-e $new_name_file) {
  242: 	      print "<h1>Successfully created PDF output file (see link below)</h1>\n";
  243: 	      $new_name_file =~ m/^(.*)\./;
  244: 	      my $tempo_file = $1.'temporar.ps';
  245: 	      my $name_file = $1.'.pdf';
  246: 	      if ($laystyle eq 'album' and $numberofcolumns eq '2') {
  247: 		  $comma = "psnup -2 -s1.0 $new_name_file";
  248: 		  system("$comma $tempo_file 1>/dev/null 2>/dev/null"); 
  249: 		  system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
  250: 	      } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
  251: 		  $comma = 'pstops -pletter "2:0+1(0.48w,0)"';
  252: 		  system("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null");
  253: 		  system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
  254: 	      } else {
  255: 		  system("ps2pdf $new_name_file $name_file 1>/dev/null 2>/dev/null");
  256: 	      }	    
  257: 	      my $texlog = $texfile;
  258: 	      my $texaux = $texfile;
  259: 	      my $texdvi = $texfile;
  260: 	      my $texps = $texfile;
  261: 	      $texlog =~ s/\.tex/\.log/;
  262: 	      $texaux =~ s/\.tex/\.aux/;
  263: 	      $texdvi =~ s/\.tex/\.dvi/;
  264: 	      $texps =~ s/\.tex/\.ps/;
  265: 	      my @garb = ($texlog,$texaux,$texdvi,$texps);
  266: #	  unlink @garb;
  267: 	      unlink $duefile;
  268: 	      print "<a href=\"/prtspool/$name_file\">Your PDF document</a>";
  269: 	      print "\n";
  270: 	  }
  271:       }
  272:   } else {
  273:       print "LaTeX file $texfile was not created successfully";
  274:   }
  275: 
  276: 
  277:   
  278: 
  279: 
  280: 
  281: 
  282: 

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