File:  [LON-CAPA] / loncom / interface / printout.pl
Revision 1.34: download - view: text, annotated - select for diffs
Thu Aug 28 13:52:22 2003 UTC (20 years, 9 months ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
Bug 2076 (which is not a bug but rather enhancement) is partly fixed: in the batch mode I added a link to original LaTeX source file (michael hamlin wanted to have this). Continue to work with the case of plane LaTeX processing (without batch mode).

    1: #!/usr/bin/perl
    2: use IO::File;
    3: use Image::Magick;
    4:  print "Content-type: text/html\n\n";
    5:  print "<body bgcolor=\"#FFFFFF\">\n";
    6:   my ($texfile,$laystyle,$numberofcolumns,$selectionmade) = split(/&/,$ENV{'QUERY_STRING'});
    7:   my $figfile = $texfile;
    8:   $figfile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.dat/;
    9:   my $duefile = $texfile;
   10:   $duefile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.due/;
   11:   #do we have figures?
   12:   if (-e $figfile) {
   13:       my $temporary_file=IO::File->new($figfile) || die "Couldn't open file for reading: $!\n";
   14:       my @content_of_file = <$temporary_file>;
   15:       close $temporary_file;  
   16:       my $noteps;
   17:       foreach $not_eps (@content_of_file) {
   18: 	  if ($not_eps ne '') {
   19: 	      my $eps_f = $not_eps;
   20: 	      $eps_f =~ s/\.[^.]*$/\.eps/i;
   21: 	      $_ = $eps_f;
   22: 	      m/\/([^\/]+)$/;
   23: 	      $eps_f = '/home/httpd/prtspool/'.$1;
   24: 	      my $image = Image::Magick->new;
   25: 	      $not_eps =~ s/^\s+//;
   26: 	      $not_eps =~ s/\s+$//;
   27: 	      $status = $image->Read($not_eps);
   28: 	      if ($status) {print "  $status  ";}
   29: 	      $image->Set(page => '+100+200'); 
   30: 	      $status = $image->Write($eps_f);	    
   31: 	      if ($status) {print "  $status  ";}
   32:               #check is eps exist in prtspool
   33:               if(not -e $eps_f) {
   34: 		  for (my $i=0;$i<10000;$i++) {
   35: 		      if (-e $eps_f.'.'.$i) {
   36: 			  rename $eps_f.'.'.$i, $eps_f;
   37: 			  last;
   38: 		      }
   39: 		  }
   40: 	      }  
   41: 	  }
   42:       }
   43:       unlink $figfile;
   44:   }
   45:   #print "$texfile\n"; #name of the tex file for debugging only
   46:   if (-e $texfile) {
   47:       $texfile =~ m/^(.*)\/([^\/]+)$/; 
   48:       my $name_file = $2;
   49:       my $path_file = $1.'/';
   50:       chdir $path_file;
   51:       system("latex $name_file 1>/dev/null 2>/dev/null");
   52:       if ($selectionmade>=10) {system("latex $name_file 1>/dev/null 2>/dev/null");} #to create table of contents
   53:       my $idxname=$name_file;
   54:       $idxname=~s/\.tex$/\.idx/;
   55:       if ($selectionmade>=100) {
   56: 	  system("makeindex $idxname");
   57: 	  system("latex $name_file 1>/dev/null 2>/dev/null");
   58:       } #to create index
   59:       #Do we have a latex error in the log file?
   60:       my $logfilename = $texfile;
   61:       $logfilename =~ s/\.tex$/\.log/;
   62:       my $temporary_file=IO::File->new($logfilename) || die "Couldn't open file for reading: $!\n";
   63:       my @content_of_file = <$temporary_file>;
   64:       close $temporary_file; 
   65:       my $body_log_file = join(' ',@content_of_file);
   66:       $logfilename =~ s/\.log$/\.html/;
   67:       $temporary_file = IO::File->new('>'.$logfilename); 
   68:       print $temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_log_file.'</pre></body></html>'."\n";
   69:       if ($body_log_file=~m/!\s+Emergency stop/) {
   70: 	  #LaTeX failed to parse tex file 
   71: 	  print "<h2>LaTeX could not successfully parse your tex file.</h2>";
   72: 	  print "It probably has errors in it.<br />";
   73: 	  my $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART';
   74: 	  my $whereitends = rindex $body_log_file,'STAMPOFPASSEDRESOURCEEND';
   75: 	  if ($whereitbegins!=-1 and $whereitends!=-1) {
   76: 	      print "With very high probability this error occured in ".substr($body_log_file,$whereitbegins+26,$whereitends-$whereitbegins-26)."<br /><br />";
   77: 	  }
   78: 	  print "Here are the error messages in the LaTeX log file</br><br />";
   79: 	  my $sygnal = 0;
   80: 	  for (my $i=0;$i<=$#content_of_file;$i++) {
   81: 	      if ($content_of_file[$i]=~m/^Runaway argument?/ or $content_of_file[$i]=~m/^!/) {
   82: 		  $sygnal = 1;
   83: 	      } 
   84: 	      if ($content_of_file[$i]=~m/Here is how much of/) {
   85: 		  $sygnal = 0;
   86: 	      } 
   87: 	      if ($sygnal) {
   88: 		  print "$content_of_file[$i]<br />";
   89: 	      }  
   90: 	  } 
   91:       } elsif ($body_log_file=~m/<inserted text>/) {
   92: 	  my $whereitbegins = index $body_log_file,'<inserted text>';
   93: 	  print "You are running LaTeX in the <b>batch mode</b>.";
   94: 	  while ($whereitbegins != -1) {
   95: 	      my $tempobegin=$whereitbegins;
   96: 	      $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART',$whereitbegins;
   97: 	      my $whereitends = index $body_log_file,'STAMPOFPASSEDRESOURCEEND',$whereitbegins;
   98: 	      print "<br />It has found an error in".substr($body_log_file,$whereitbegins+26,$whereitends-$whereitbegins-26)." <br /> and corrected it.\n";
   99: 	      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";
  100: 	      $whereitbegins = index $body_log_file,'<inserted text>',$tempobegin+10;
  101: 	  }
  102: 	  $name_file =~ s/\.tex/\.dvi/;
  103: 	  my $new_name_file = $name_file;
  104: 	  $new_name_file =~ s/\.dvi/\.ps/;
  105: 	  my $comma = "dvips -Ppdf -G0 -o $new_name_file";
  106: 	  system("$comma $name_file 1>/dev/null 2>/dev/null");
  107: 	  if (-e $new_name_file) {
  108: 	      print "<h1>PDF output file (see link below)</h1>\n";
  109: 	      $new_name_file =~ m/^(.*)\./;
  110: 	      my $tempo_file = $1.'temporar.ps';
  111: 	      my $name_file = $1.'.pdf';
  112: 	      if ($laystyle eq 'album' and $numberofcolumns eq '2') {
  113: 		  $comma = "psnup -2 -s1.0 $new_name_file";
  114: 		  system("$comma $tempo_file 1>/dev/null 2>/dev/null"); 
  115: 		  system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
  116: 	      } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
  117: 		  $comma = 'pstops -pletter "2:0+1(0.48w,0)"';
  118: 		  system("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null");
  119: 		  system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
  120: 	      } else {
  121: 		  system("ps2pdf $new_name_file $name_file 1>/dev/null 2>/dev/null");
  122: 	      }	    
  123: 	      my $texlog = $texfile;
  124: 	      my $texaux = $texfile;
  125: 	      my $texdvi = $texfile;
  126: 	      my $texps = $texfile;
  127: 	      $texlog =~ s/\.tex/\.log/;
  128: 	      $texaux =~ s/\.tex/\.aux/;
  129: 	      $texdvi =~ s/\.tex/\.dvi/;
  130: 	      $texps =~ s/\.tex/\.ps/;
  131: 	      my @garb = ($texaux,$texdvi,$texps);
  132: #	  unlink @garb;
  133: 	      unlink $duefile;
  134: 	      print "<a href=\"/prtspool/$name_file\">Your PDF document</a>";
  135: 	      print "<br /><br />";
  136:               print "<b><big>The link to ";
  137:               $logfilename=~s/\/home\/httpd//;
  138: 	      print "<a href=\"$logfilename\">Your log file </a></big></b>";
  139: 	      print "\n";
  140:               #link tooriginal LaTeX file (included according Michael Hamlin desire)
  141: 	      my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open file for reading: $!\n";
  142: 	      my @tex_content_of_file = <$tex_temporary_file>;
  143: 	      close $tex_temporary_file; 
  144: 	      my $body_tex_file = join(' ',@tex_content_of_file);
  145: 	      $texfile =~ s/\.tex$/aaaaa\.html/;
  146: 	      $tex_temporary_file = IO::File->new('>'.$texfile); 
  147: 	      print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
  148: 	      print "<br /><br />";
  149: 	      print "<b><big>The link to ";
  150: 	      $texfile=~s/\/home\/httpd//;
  151: 	      print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
  152: 	      print "\n";
  153: 	  }
  154:       } else {
  155: 	  #LaTeX successfully parsed tex file 
  156: 	  $name_file =~ s/\.tex/\.dvi/;
  157: 	  my $new_name_file = $name_file;
  158: 	  $new_name_file =~ s/\.dvi/\.ps/;
  159: 	  my $comma = "dvips -Ppdf -G0 -o $new_name_file";
  160: 	  system("$comma $name_file 1>/dev/null 2>/dev/null");
  161: 	  if (-e $new_name_file) {
  162: 	      print "<h1>Successfully created PDF output file (see link below)</h1>\n";
  163: 	      $new_name_file =~ m/^(.*)\./;
  164: 	      my $tempo_file = $1.'temporar.ps';
  165: 	      my $name_file = $1.'.pdf';
  166: 	      if ($laystyle eq 'album' and $numberofcolumns eq '2') {
  167: 		  $comma = "psnup -2 -s1.0 $new_name_file";
  168: 		  system("$comma $tempo_file 1>/dev/null 2>/dev/null"); 
  169: 		  system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
  170: 	      } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
  171: 		  $comma = 'pstops -pletter "2:0+1(0.48w,0)"';
  172: 		  system("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null");
  173: 		  system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
  174: 	      } else {
  175: 		  system("ps2pdf $new_name_file $name_file 1>/dev/null 2>/dev/null");
  176: 	      }	    
  177: 	      my $texlog = $texfile;
  178: 	      my $texaux = $texfile;
  179: 	      my $texdvi = $texfile;
  180: 	      my $texps = $texfile;
  181: 	      $texlog =~ s/\.tex/\.log/;
  182: 	      $texaux =~ s/\.tex/\.aux/;
  183: 	      $texdvi =~ s/\.tex/\.dvi/;
  184: 	      $texps =~ s/\.tex/\.ps/;
  185: 	      my @garb = ($texlog,$texaux,$texdvi,$texps);
  186: #	  unlink @garb;
  187: 	      unlink $duefile;
  188: 	      print "<a href=\"/prtspool/$name_file\">Your PDF document</a>";
  189: 	      print "\n";
  190: 	  }
  191:       }
  192:   } else {
  193:       print "LaTeX file $texfile was not created successfully";
  194:   }
  195: 
  196: 
  197:   
  198: 
  199: 
  200: 
  201: 
  202: 

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