Annotation of loncom/interface/printout.pl, revision 1.40.2.3

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

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