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

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

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