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

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

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