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

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

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