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

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

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