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

1.1       sakharuk    1: #!/usr/bin/perl
1.37      albertel    2: # CGI-script to run LaTeX, dvips, ps2ps, ps2pdf etc.
                      3: #
1.121   ! albertel    4: # $Id: printout.pl,v 1.120 2007/01/19 20:22:33 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.76      albertel   30: use LONCAPA::loncgi;
1.38      sakharuk   31: use File::Path;
1.88      foxr       32: use File::Basename;
1.111     foxr       33: use File::Copy;
1.6       sakharuk   34: use IO::File;
1.7       sakharuk   35: use Image::Magick;
1.97      albertel   36: use Apache::lonhtmlcommon();
1.80      albertel   37: use Apache::lonnet;
1.97      albertel   38: use Apache::loncommon();
1.51      albertel   39: use Apache::lonlocal;
1.97      albertel   40: use Apache::lonmsg();
1.77      foxr       41: use LONCAPA::Enrollment;
1.47      sakharuk   42: 
1.59      albertel   43: use strict;
1.77      foxr       44: 
1.81      foxr       45: 
1.77      foxr       46: #   Determine if a user is operating as a student for this course/domain.
                     47: #Parameters:
1.92      albertel   48: #    none
1.77      foxr       49: #Implicit:
                     50: #    $env{request.role} contains the role under which this user operated this
                     51: #                       this request.
                     52: sub is_student {
1.92      albertel   53:     return ($env{'request.role'}=~/^st\./);
1.77      foxr       54: }
                     55: 
                     56: #
                     57: #   Debugging:  Dump the environment for debugging.
                     58: #
                     59: sub dumpenv  {
1.84      albertel   60:     print "<br />-------------------<br />";
1.77      foxr       61:     foreach my $key (sort (keys %env)) {
1.84      albertel   62: 	print "<br />$key -> $env{$key}";
1.77      foxr       63:     }
1.84      albertel   64:     print "<br />-------------------<br />";
1.77      foxr       65: }
                     66: 
                     67: #
                     68: #   This sub sends a message to the appropriate person if there was an error
                     69: #   rendering the latex  At present, there's only one case to consider:
                     70: #   a student printing inside a course results in messages to the course coordinator.
                     71: #Parmaeters:
                     72: #    identifier -  The unique identifier of this cgi request.
1.81      foxr       73: #    badresource-  Filepath to most likely failing 
1.77      foxr       74: #    logfile    -  The contents of the log file (included in the message).
                     75: #    texfile    -  reference to an array containing the LaTeX input file
                     76: #                  (included in the message).
                     77: #Implicit inputs:
                     78: #   From the environment:
                     79: #       cgi.$identifier.user     - User doing the printing.
                     80: #       cgi.$identifier.domain   - Domain the user is logged in on with printing.
                     81: #       cgi.$identifier.courseid - Id of the course (if this is a course).
                     82: #       cgi.$identifier.coursedom- Domain in which course was constituted.
                     83: #       cgi.$identifier.resources - List of resource URL's for which the print
                     84: #                                  was attempted.
                     85: # 
                     86: sub send_error_mail {
1.81      foxr       87:     my ($identifier, $badresource, $logfile, $texfile) = @_;
1.77      foxr       88:     my $user     = $env{"cgi.$identifier.user"};
                     89:     my $domain   = $env{"cgi.$identifier.domain"};
                     90:     my $courseid = $env{"cgi.$identifier.courseid"};
                     91:     my $coursedom= $env{"cgi.$identifier.coursedom"};
                     92:     my $resources= $env{"cgi.$identifier.resources"};
                     93: 
1.81      foxr       94:     #  resource file->URL
                     95:     #
                     96:     my $badurl = &Apache::lonnet::declutter($badresource);
                     97: 
                     98:     # &dumpenv();
1.77      foxr       99: 
                    100: 
                    101: 
                    102:     #  Only continue if there is a user, domain, courseid, course domain
                    103:     #  and resources:
                    104: 
                    105:     if(defined($user)       && defined($domain) && defined($courseid) &&
                    106:        defined($coursedom)  && defined($resources) ){
                    107: 	   
                    108: 	#  Only mail if the conditions are ripe for it:
                    109: 	#  The user is a student in the course:
                    110: 	#
                    111: 	
1.92      albertel  112: 	if (&is_student()) {
1.77      foxr      113: 	    # build the subject and message body:
1.84      albertel  114: 	    # print "sending message to course coordinators.<br />";
1.81      foxr      115: 
                    116: 	    # Todo: Convert badurl into a url from file path:
                    117: 
                    118: 	    my $subject  = "Error [$badurl] Print failed for $user".'@'.$domain;
1.77      foxr      119: 	    my $message .= "Print failed to render LaTeX for $user".'@'."$domain\n";
                    120: 	    $message    .= "  User was attempting to print: \n";
1.91      albertel  121: 	    foreach my $resource (split(/:/,$resources)) {
                    122: 		$message    .= "       $resource\n";
                    123: 	    }
1.77      foxr      124: 	    $message    .= "--------------------LaTeX logfile:------------ \n";
                    125: 	    $message    .= $logfile;
                    126: 	    $message    .= "-----------------LaTeX source file: ------------\n";
                    127: 	    
                    128: 	    foreach my $line (@$texfile) {
                    129: 		$message .= "$line\n";
                    130: 	    }
1.81      foxr      131: 	    my (undef, %receivers) = &Apache::lonfeedback::decide_receiver(undef, 0,
                    132: 									  1,1,1);
1.84      albertel  133: 	    # print "<br /> sending...section:  $env{'request.course.sec'}";
1.81      foxr      134: 	    foreach my $dest (keys %receivers) {
1.84      albertel  135: 		# print "<br /> dest is $dest";
1.77      foxr      136: 		my @destinfo = split(/:/,$dest);
                    137: 		my $user = $destinfo[0];
                    138: 		my $dom  = $destinfo[1];
                    139: 
                    140: 		&Apache::lonmsg::user_normal_msg($user, $dom,
                    141: 						 $subject, $message);
                    142: 		
                    143: 		# No point in looking at the return status as there's no good
                    144: 		# error action I can think of right now (log maybe?).
                    145: 	    }
                    146: 	}
                    147:     }
                    148: }
                    149: 
1.49      albertel  150: $|=1;
1.39      sakharuk  151: if (! &LONCAPA::loncgi::check_cookie_and_load_env()) {
                    152:     print <<END;
                    153: Content-type: text/html
                    154: 
                    155: <html>
                    156: <head><title>Bad Cookie</title></head>
                    157: <body>
1.40      sakharuk  158: Your cookie information is incorrect.
1.39      sakharuk  159: </body>
                    160: </html>
                    161: END
                    162:     return;
                    163: }
1.51      albertel  164:  &Apache::lonlocal::get_language_handle();
                    165:  &Apache::loncommon::content_type(undef,'text/html');
1.101     albertel  166: 
                    167:   print(&Apache::loncommon::start_page('Creating PDF'));
1.39      sakharuk  168: 
1.40      sakharuk  169:   my $identifier = $ENV{'QUERY_STRING'};
1.76      albertel  170:   my $texfile = $env{'cgi.'.$identifier.'.file'};
                    171:   my $laystyle = $env{'cgi.'.$identifier.'.layout'};
                    172:   my $numberofcolumns = $env{'cgi.'.$identifier.'.numcol'};
                    173:   my $paper = $env{'cgi.'.$identifier.'.paper'};
                    174:   my $selectionmade = $env{'cgi.'.$identifier.'.selection'};
1.79      albertel  175:   my $tableofcontents = $env{'cgi.'.$identifier.'.tableofcontents'};
                    176:   my $tableofindex = $env{'cgi.'.$identifier.'.tableofindex'};
                    177:   my $advanced_role = $env{'cgi.'.$identifier.'.role'};
                    178:   my $number_of_files = $env{'cgi.'.$identifier.'.numberoffiles'}+1;
                    179:   my $student_names = $env{'cgi.'.$identifier.'.studentnames'};
                    180:   my $backref = &Apache::lonnet::unescape($env{'cgi.'.$identifier.'.backref'});
1.43      sakharuk  181: 
1.49      albertel  182:   
1.44      sakharuk  183:   my @names_pack=();
                    184:   if ($student_names=~/_END_/) {  
                    185:       @names_pack=split(/_ENDPERSON_/,$student_names);
                    186:   }
1.39      sakharuk  187: 
1.94      albertel  188: print "<a href=\"$backref\"><b>Return</b></a> to last resource.<br /><br />";
                    189: 
1.14      sakharuk  190:   my $figfile = $texfile;
1.121   ! albertel  191:   $figfile =~ s/^(.*_printout)_\d+_\d+_\d+\.tex/$1\.dat/;
1.14      sakharuk  192:   my $duefile = $texfile;
1.121   ! albertel  193:   $duefile =~ s/^(.*_printout)_\d+_\d+_\d+\.tex/$1\.due/;
1.14      sakharuk  194:   #do we have figures?
1.73      foxr      195:   # print "Figure file: $figfile\n";
1.14      sakharuk  196:   if (-e $figfile) {
1.73      foxr      197:       # print "$figfile exists\n";
1.42      albertel  198:       my %done_conversion;
1.69      matthew   199:       my $temporary_file=IO::File->new($figfile) || die "Couldn't open fig file $figfile for reading: $!\n";
1.14      sakharuk  200:       my @content_of_file = <$temporary_file>;
                    201:       close $temporary_file;  
                    202:       my $noteps;
1.49      albertel  203:       my %prog_state;
1.92      albertel  204:       if ($advanced_role) { %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('','Coverting Images to EPS','Picture Conversion Status',$#content_of_file,'inline','80');  }
1.94      albertel  205:       print('<br />');
1.59      albertel  206:       foreach my $not_eps (@content_of_file) {
1.49      albertel  207: 	  chomp($not_eps);
1.14      sakharuk  208: 	  if ($not_eps ne '') {
1.112     foxr      209: 	       # print "Converting $not_eps"; # Debugging.
1.44      sakharuk  210:               my $status_statement='EPS picture for '.$not_eps;
1.73      foxr      211: 	      # print "$status_statement\n";
1.41      sakharuk  212: 	      $not_eps=~s|\/\.\/|\/|g;
1.14      sakharuk  213: 	      my $eps_f = $not_eps;
1.85      foxr      214: 	      # $eps_f =~ s/\.[^.]*$/\.eps/i;
1.41      sakharuk  215: 	      if ($eps_f=~/\/home\/([^\/]+)\/public_html\//) {
                    216:                   $eps_f=~s/\/home\/([^\/]+)\/public_html/$1/;
                    217: 		  $eps_f = '/home/httpd/prtspool/'.$eps_f;
1.58      sakharuk  218: 	      } elsif ($eps_f=~/$Apache::lonnet::perlvar{'lonDocRoot'}\/res\//) {
                    219: 		  $eps_f=~m/$Apache::lonnet::perlvar{'lonDocRoot'}\/res\/(.+)/;
1.41      sakharuk  220: 		  $eps_f = '/home/httpd/prtspool/'.$1;
1.58      sakharuk  221: 	      } elsif ($eps_f=~/$Apache::lonnet::perlvar{'lonUsersDir'}\//) {
1.57      sakharuk  222: 		  $eps_f=~/$Apache::lonnet::perlvar{'lonUsersDir'}\/([^\/]+)\/\w\/\w\/\w\/(.+)/;
1.56      sakharuk  223: 		  $eps_f = '/home/httpd/prtspool/'.$1.'/'.$2;
1.41      sakharuk  224: 	      }
1.74      foxr      225: 	      $eps_f  =~ s/ /\_/g; # Spaces are problematic for system commands and LaTeX.
1.111     foxr      226: 	      # 
                    227: 	      # If the file is already an .eps or .ps file,
                    228: 	      # We really just need to copy it from where it was to prtspool
                    229: 	      # but with the spaces substituted to _'s.
                    230: 	      #
1.114     albertel  231: 	      my ($nsname,$path, $sext) = &fileparse($eps_f, qr/\.(ps|eps)/i);
1.111     foxr      232: 	      if ($sext =~/ps$/i) {
1.112     foxr      233: 		  # print "$not_eps is a postscript file. copy to $path\n";
1.114     albertel  234: 		  &File::Path::mkpath($path,0,0777);
                    235: 		  #print("Made path: $path");
                    236: 		  #$not_eps =~ s/^\s+//;
                    237: 		  #$not_eps =~ s/\s+$//;
                    238: 		  #$not_eps =~ s/ /\__/g;
                    239: 		  #print("Copying $not_eps to $eps_f\n");
1.112     foxr      240: 		  copy("$not_eps", "$eps_f"); 
                    241: 		  # print "Copy complete\n";
1.111     foxr      242: 	      } else {
                    243: 	      
                    244: 		  $eps_f .= '.eps';	# Just append the eps ext.
                    245: 		  my $path=$eps_f;
                    246: 		  $path =~ s/\/([^\/]+)\.eps$//;
                    247: 		  # print "Final file path: $path "; # Debugging
1.114     albertel  248: 		  &File::Path::mkpath($path,0,0777);
1.111     foxr      249: 		  $not_eps =~ s/^\s+//;
                    250: 		  $not_eps =~ s/\s+$//;
                    251: 		  $not_eps =~ s/ /\\ /g;
                    252: 		  if ( exists($done_conversion{$not_eps})) { next; }
                    253: 		  if ($advanced_role) {
                    254: 		      my $prettyname=$not_eps;
                    255: 		      $prettyname=~s|/home/([^/]+)/public_html|/priv/$1|;
                    256: 		      $prettyname=~s|$Apache::lonnet::perlvar{'lonDocRoot'}/|/|;
                    257: 		      &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,
                    258: 							    'Converting to EPS '.$prettyname);
                    259: 		  }
                    260: 		  $done_conversion{$not_eps}=1;
                    261: 		  # print "Converting $not_eps -> $eps_f"; # Debugging
                    262: 		  system("convert $not_eps $eps_f");
                    263: 		  # check is eps exist in prtspool
                    264: 		  if (not -e $eps_f) {
                    265: 		      # converting an animated gif creates either:
                    266: 		      # anim.gif.eps.0
                    267: 		      # or
                    268: 		      # anim.gif-0.eps
                    269: 		      for (my $i=0;$i<10000;$i++) {
                    270: 			  if (-e $eps_f.'.'.$i) {
                    271: 			      rename($eps_f.'.'.$i, $eps_f);
                    272: 			      last;
                    273: 			  }
                    274: 			  my $anim_eps = $eps_f;
                    275: 			  $anim_eps =~ s/(\.[^.]*)\.eps$/$1-$i\.eps/i;
                    276: 			  if (-e $anim_eps) {
                    277: 			      rename($anim_eps, $eps_f);
                    278: 			      last;
                    279: 			  }
1.20      sakharuk  280: 		      }
1.111     foxr      281: 		  }
                    282: 		  
                    283: 		  # imagemagick 6.2.0-6.2.7 fails to properly handle
                    284: 		  # convert anim.gif anim.gif.eps
                    285: 		  # it creates anim.eps instead. 
                    286: 		  if (not -e $eps_f) {
                    287: 		      my $eps_f2 = $eps_f;
                    288: 		      $eps_f2 =~ s/\.[^.]*\.eps$/\.eps/i;
                    289: 		      if(-e $eps_f2) {
                    290: 			  rename($eps_f2,$eps_f);
1.98      albertel  291: 		      }
                    292: 		  }
                    293: 	      }
                    294: 
1.14      sakharuk  295: 	  }
                    296:       }
1.92      albertel  297:       if ($advanced_role) { 
1.74      foxr      298: 	  &Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state); 
                    299:       }
1.52      albertel  300:       unlink($figfile);
1.14      sakharuk  301:   }
1.43      sakharuk  302:   #print "$texfile\n"; #name of the tex file for debugging only   
                    303:   my @texfile=($texfile);
                    304:   if ($number_of_files>1) {
1.49      albertel  305:       @texfile=();
                    306:       for (my $i=1;$i<=$number_of_files;$i++) {
1.43      sakharuk  307: 	  my $new_texfile=$texfile;
1.86      foxr      308: 	  $new_texfile=~s/\.tex//;
                    309: 	  $new_texfile = sprintf("%s_%03d.tex", $new_texfile,$i);
1.43      sakharuk  310: 	  push @texfile,$new_texfile;
                    311:       } 
                    312:   }
1.49      albertel  313: 
1.44      sakharuk  314: my $ind=-1;
1.49      albertel  315: my %prog_state;
1.92      albertel  316: if ($advanced_role) { %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('','Print Status','Class Print Status',$number_of_files,'inline','80'); }
1.61      albertel  317: print "<br />";
1.88      foxr      318: my $num_files = @texfile;
1.43      sakharuk  319: foreach $texfile (@texfile) {
1.49      albertel  320:   my $status_statement='';
                    321:   my $link_text='download PDF';
                    322:   $ind++;
                    323:   my @stud_info=split(/_END_/,$names_pack[$ind]);
                    324:   my @tempo_array=split(/:/,$stud_info[0]);
                    325:   my $name;
1.88      foxr      326:   my $name_range='';
1.49      albertel  327:   if ($tempo_array[3]) {
                    328:       $name=$tempo_array[3];
1.89      foxr      329:       ($name_range) = split(/,/,$name, 2);
1.49      albertel  330:   } else {
                    331:       $name=$tempo_array[0].'@'.$tempo_array[1];
1.88      foxr      332:       $name_range = $tempo_array[0];
                    333:   }
                    334:   if (($name ne "") && ($name ne '@') ) { # Could be printing codes...
                    335:       $link_text='<b>'.$name.'</b>';
                    336:       $status_statement.=$name;
1.49      albertel  337:   }
                    338:   if ($#stud_info>0) {
                    339:       @tempo_array=split(/:/,$stud_info[-1]);
1.48      albertel  340:       if ($tempo_array[3]) {
                    341: 	  $name=$tempo_array[3];
1.89      foxr      342: 	  my ($lastname) = split(/,/, $name,2);
                    343: 	  $name_range .= "-".$lastname;
1.48      albertel  344:       } else {
                    345: 	  $name=$tempo_array[0].'@'.$tempo_array[1];
1.88      foxr      346: 	  $name_range .= '-'.$tempo_array[0];
                    347:       }
                    348:       if (($name ne "") && ($name ne '@')) {
                    349: 	  $link_text.=' - <b>'.$name.'</b>';
                    350: 	  $status_statement.=' -  '.$name;
                    351:   
1.48      albertel  352:       }
1.88      foxr      353:   }
                    354:   if(($num_files > 1) && ($link_text eq 'download PDF')) { # Printing codes
                    355:       $link_text = '<b>'.basename($texfile,'.tex').'.pdf</b>';
                    356:       $status_statement .= basename($texfile);
                    357:   }
                    358:   $name_range =~ s/'//g;	# O'Neil -> ONeil e.g.
                    359:   print "<br/>";
1.92      albertel  360:   if ($advanced_role) { &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,'Creating PDF for '.$status_statement); }
1.88      foxr      361:   #  This little piece of dirt puts username ranges into the original tex
                    362:   #  Tex filename from which they'll propagate into the other filenames as well.
                    363:   #
1.29      sakharuk  364:   if (-e $texfile) {
1.88      foxr      365:       if (($name_range ne '') && ($num_files > 1)) {
                    366: 	  my $newtexfile = $texfile;
                    367: 	  $newtexfile    =~ s/\.tex/$name_range\.tex/;
                    368: 	  rename($texfile, $newtexfile);
                    369: 	  $texfile       = $newtexfile;
                    370:       }
1.29      sakharuk  371:       $texfile =~ m/^(.*)\/([^\/]+)$/; 
                    372:       my $name_file = $2;
                    373:       my $path_file = $1.'/';
                    374:       chdir $path_file;
1.88      foxr      375:       my $dvi_file= $name_file; $dvi_file =~ s/\.tex/$name_range\.dvi/;
1.59      albertel  376:       &busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null",
                    377: 			 "for $status_statement now LaTeXing file",
                    378: 			 \%prog_state,$dvi_file);
1.40      sakharuk  379:       if ($tableofcontents eq 'yes') {
1.63      sakharuk  380:       &busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null",
1.95      foxr      381: 			 "for $status_statement First LaTeX of file for table of contents",
                    382: 			 \%prog_state,$dvi_file);
                    383:       &busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null",
                    384: 			 "for $status_statement Second LaTeX of file for table of contents",
1.63      sakharuk  385: 			 \%prog_state,$dvi_file);
1.40      sakharuk  386:       } #to create table of contents
1.33      sakharuk  387:       my $idxname=$name_file;
                    388:       $idxname=~s/\.tex$/\.idx/;
1.40      sakharuk  389:       if ($tableofindex eq 'yes') {
1.63      sakharuk  390: 	  &busy_wait_command("makeindex $idxname",
                    391: 			     "making index file",
                    392: 			     \%prog_state,$idxname);
                    393: 	  &busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null",
                    394: 			     "for $status_statement now LaTeXing file for index section",
                    395: 			     \%prog_state,$dvi_file);
1.33      sakharuk  396:       } #to create index
1.29      sakharuk  397:       #Do we have a latex error in the log file?
1.67      sakharuk  398:       my $logfilename = $texfile; $logfilename =~ s/\.tex$/\.log/;
1.69      matthew   399:       my $temporary_file=IO::File->new($logfilename) || die "Couldn't open log file $logfilename for reading: $!\n";
1.29      sakharuk  400:       my @content_of_file = <$temporary_file>;
                    401:       close $temporary_file; 
1.30      sakharuk  402:       my $body_log_file = join(' ',@content_of_file);
                    403:       $logfilename =~ s/\.log$/\.html/;
                    404:       $temporary_file = IO::File->new('>'.$logfilename); 
                    405:       print $temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_log_file.'</pre></body></html>'."\n";
1.29      sakharuk  406:       if ($body_log_file=~m/!\s+Emergency stop/) {
                    407: 	  my $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART';
                    408: 	  my $whereitends = rindex $body_log_file,'STAMPOFPASSEDRESOURCEEND';
1.81      foxr      409: 	  my $badresource;
1.83      foxr      410: 	  my $badtext;
1.29      sakharuk  411: 	  if ($whereitbegins!=-1 and $whereitends!=-1) {
1.83      foxr      412: 	      $badtext = substr($body_log_file,$whereitbegins+26, $whereitends-$whereitbegins-26);
1.81      foxr      413: 	      $whereitbegins  = rindex $badtext,'located in';
                    414: 	      if ($whereitbegins != -1) {
                    415: 		  
                    416: 		  $badresource = substr($badtext, $whereitbegins+27, 
                    417: 					length($badtext) - $whereitbegins - 48);
1.84      albertel  418: 		  # print "<br />failing resourcename: $badresource<br />";
1.81      foxr      419: 	      }
1.29      sakharuk  420: 	  }
1.83      foxr      421: 	  
1.75      albertel  422:           if ($advanced_role) {  
1.83      foxr      423: 	      #LaTeX failed to parse tex file 
                    424: 	      print "<h2>LaTeX could not successfully parse your tex file.</h2>";
                    425: 	      print "It probably has errors in it.<br />";
                    426: 	      print "With very high probability this error occured in ".$badtext."<br /><br />";
1.84      albertel  427: 	      print "Here are the error messages in the LaTeX log file<br /><pre>";
1.90      foxr      428: 
1.83      foxr      429: 	      my $sygnal = 0;
                    430: 	      for (my $i=0;$i<=$#content_of_file;$i++) {
                    431: 		  if ($content_of_file[$i]=~m/^Runaway argument?/ or $content_of_file[$i]=~m/^!/) {
                    432: 		      $sygnal = 1;
                    433: 		  } 
                    434: 		  if ($content_of_file[$i]=~m/Here is how much of/) {
                    435: 		      $sygnal = 0;
                    436: 		  } 
                    437: 		  if ($sygnal) {
                    438: 		      print "$content_of_file[$i]";
                    439: 		  }  
                    440: 	      }
                    441: 	      print "</pre>\n";
1.84      albertel  442: 	      # print "<br /> Advanced role <br />";
1.35      sakharuk  443:               print "<b><big>The link to ";
                    444:               $logfilename=~s/\/home\/httpd//;
                    445: 	      print "<a href=\"$logfilename\">Your log file </a></big></b>";
                    446: 	      print "\n";
                    447:               #link tooriginal LaTeX file (included according Michael Hamlin desire)
1.69      matthew   448: 	      my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open tex file $texfile for reading: $!\n";
1.35      sakharuk  449: 	      my @tex_content_of_file = <$tex_temporary_file>;
                    450: 	      close $tex_temporary_file; 
                    451: 	      my $body_tex_file = join(' ',@tex_content_of_file);
                    452: 	      $texfile =~ s/\.tex$/aaaaa\.html/;
                    453: 	      $tex_temporary_file = IO::File->new('>'.$texfile); 
                    454: 	      print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
                    455: 	      print "<br /><br />";
                    456: 	      print "<b><big>The link to ";
                    457: 	      $texfile=~s/\/home\/httpd//;
                    458: 	      print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
                    459: 	      print "\n";
1.90      foxr      460: 	      my $help_text = &Apache::loncommon::help_open_topic("Print_Resource", "Help on printing");
                    461: 	      print ("$help_text");
                    462: 
1.77      foxr      463: 	  } else {		# Student role...
                    464: 	      #  at this point:
                    465: 	      #    $body_log_file - contains the log file.
                    466:               #    $name_file     - is the name of the LaTeX file.
                    467:               #    $identifier    - is the unique LaTeX identifier.l
                    468: 
1.84      albertel  469: 	      print "<br />There are errors in $badtext";
                    470: 	      print "<br />These errors prevent this resource from printing correctly";
1.77      foxr      471: 	      my $tex_handle = IO::File->new($name_file);
                    472: 	      my @tex_contents = <$tex_handle>;
1.81      foxr      473: 	      &send_error_mail($identifier, $badresource, $body_log_file, \@tex_contents);
1.90      foxr      474: 	      print "<br />A message has been sent to the instructor describing this failure<br />";
                    475: 	      my $help_text = &Apache::loncommon::help_open_topic("Print_Resource", "Help on printing");
                    476: 	      print  ("$help_text");
                    477: 
1.36      sakharuk  478: 	  }
1.35      sakharuk  479: 
1.29      sakharuk  480:       } elsif ($body_log_file=~m/<inserted text>/) {
                    481: 	  my $whereitbegins = index $body_log_file,'<inserted text>';
1.59      albertel  482: 	  print "You are running LaTeX in <b>batch mode</b>.";
1.30      sakharuk  483: 	  while ($whereitbegins != -1) {
                    484: 	      my $tempobegin=$whereitbegins;
                    485: 	      $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART',$whereitbegins;
                    486: 	      my $whereitends = index $body_log_file,'STAMPOFPASSEDRESOURCEEND',$whereitbegins;
1.34      sakharuk  487: 	      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  488: 	      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";
                    489: 	      $whereitbegins = index $body_log_file,'<inserted text>',$tempobegin+10;
                    490: 	  }
1.29      sakharuk  491: 	  $name_file =~ s/\.tex/\.dvi/;
                    492: 	  my $new_name_file = $name_file;
                    493: 	  $new_name_file =~ s/\.dvi/\.ps/;
1.67      sakharuk  494: 	  my $papera=$paper;
1.62      sakharuk  495: 	  if ($papera eq 'letter') {$papera='';}
                    496: 	  if ($papera ne '') {$papera='-t'.$papera;}
                    497: 	  my $comma = "dvips $papera -Ppdf -G0 -o $new_name_file";
1.59      albertel  498: 	  &busy_wait_command("$comma $name_file 1>/dev/null 2>/dev/null",
                    499: 			     "for $status_statement now Converting to PS",
                    500: 			     \%prog_state,$new_name_file);
1.29      sakharuk  501: 	  if (-e $new_name_file) {
1.102     foxr      502: 	      my $latex_file = $name_file;
                    503: 	      $latex_file    =~ s/\.dvi/\.tex/;
                    504: 	      &repaginate($new_name_file, $latex_file, $numberofcolumns);
                    505: 	      #
                    506: 	      #  Now have to re-latex, re dvips again to 
                    507: 	      #  get the repaginated postscript.
                    508: 	      #
                    509: 	      &busy_wait_command("latex $latex_file 1>/dev/null 2>/dev/null",
                    510: 				 "for $status_statement first latex to repaginate",
                    511: 				 \%prog_state, $name_file);
1.105     foxr      512: 	      if ($tableofcontents eq 'yes') {
                    513: 		  &busy_wait_command("latex $latex_file 1>/dev/null 2>/dev/null",
                    514: 				     "for $status_statement second latex to repaginate",
                    515: 				     \%prog_state, $name_file);
                    516: 		  &busy_wait_command("latex $latex_file 1>/dev/null 2>/dev/null",
                    517: 				     "for $status_statement third latex to repaginate",
                    518: 				     \%prog_state, $name_file);
                    519: 	      }
                    520: 	      if ($tableofindex eq 'yes') {
                    521: 		  my $idxname = $latex_file;
                    522: 		  $idxname =~ s/\.tex$/\.idx/;
                    523: 		  &busy_wait_command("makindex $idxname",
                    524: 				     "Re-creating index file",
                    525: 				     \%prog_state, $idxname);
                    526: 		  &busy_wait_command("latex $latex_file 1>/dev/null 2>/dev/null",
                    527: 				     "for $status_statement now Recreting index (latex)",
                    528: 				     \%prog_state, $dvi_file);
                    529: 
                    530: 	      }
1.118     albertel  531: 	      &busy_wait_command("$comma $name_file 1>/dev/null 2>/dev/null",
1.102     foxr      532: 				 "for $status_statement dvips to repaginate",
                    533: 				 \%prog_state, $new_name_file);
1.115     foxr      534: 	      #
                    535: 	      #  One last little hinky kinky thing.
                    536: 	      #  It's just possible that some fonts could not be maded
                    537: 	      #  at the resolution of the pdf print driver.
                    538: 	      #  In that case a file called missfont.log will have been
                    539: 	      #  created that will contain the commands that were attempted
                    540: 	      # to create the missing fonts.  If we basically
                    541: 	      # take all the 8000 strings in that file, and
                    542: 	      # replace them with 600 (the ljfour resolution)
                    543: 	      # run the commands in that file and redvips,
                    544: 	      # we'll be able to print the missing glyphs at 600dpi.
                    545: 	      #
                    546: 	      # Supposedly it is possible to tune TeX/Metafont to do this
                    547: 	      # right but I failed to get that to work when following the
                    548: 	      # docs at the tug site, hence this rather kludgey fix.
                    549: 	      #
                    550: 	      #  We make the (I think) reasonable assumption that
                    551: 	      #  missing glyphs won't change the pagination and I think
                    552: 	      #  this is true because TeX/dvips will leave a space
                    553: 	      #  instead of these glyphs based on the font metrics
                    554: 	      #  (fancy way to say there will be a blank the size of the missing
                    555: 	      #  glyphs).
                    556: 	      #
                    557: 	      my $print_directory = dirname($name_file);
                    558: 	      my $missfonts_file  = $print_directory."/missfont.log";
1.116     albertel  559: 	      #print("<br /> Missing fonts file is: $missfonts_file");
1.115     foxr      560: 	      if (-e $missfonts_file) {
1.116     albertel  561: 		  #print("<br />Missing fonts file exists\n");
1.115     foxr      562: 		  &create_missing_fonts($missfonts_file);
1.118     albertel  563: 		  &busy_wait_command("$comma $name_file 1>/dev/null 2>/dev/null",
1.115     foxr      564: 				     "for $status_statement dvips generated missing fonts",
                    565: 				     \%prog_state, $new_name_file);
                    566: 	      }
                    567: 
                    568: 	      #
1.102     foxr      569: 	      print "\n<h1>PDF output file (see link below)</h1>\n";
1.29      sakharuk  570: 	      $new_name_file =~ m/^(.*)\./;
1.59      albertel  571: 	      my $ps_file = my $tempo_file = $1.'temporar.ps';
                    572: 	      my $pdf_file = $1.'.pdf';
1.29      sakharuk  573: 	      if ($laystyle eq 'album' and $numberofcolumns eq '2') {
1.82      albertel  574: 		  my $papera=$paper;
                    575:                   if ($papera eq 'letter') {$papera='';}
                    576: 		  if ($papera ne '') {$papera='-p'.$papera;}
                    577: 		  $comma = "psnup $papera -2 -s1.0 $new_name_file";
1.59      albertel  578: 		  &busy_wait_command("$comma $tempo_file 1>/dev/null 2>/dev/null",
                    579: 				     "for $status_statement now Modifying PS layout",
                    580: 				     \%prog_state,$tempo_file); 
1.29      sakharuk  581: 	      } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
1.67      sakharuk  582: 		  my $papera=$paper;
                    583:                   if ($papera eq 'letter') {$papera='';}
1.62      sakharuk  584: 		  if ($papera ne '') {$papera='-p'.$papera;}
                    585: 		  $comma = 'pstops '.$papera.' "2:0+1(0.48w,0)"';
1.63      sakharuk  586: 		  &busy_wait_command("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null",
                    587: 				     "for $status_statement now Modifying PS layout",
                    588: 				     \%prog_state,$tempo_file); 
                    589: 
1.29      sakharuk  590: 	      } else {
1.59      albertel  591: 		  $ps_file=$new_name_file;
1.29      sakharuk  592: 	      }	    
1.59      albertel  593: 	      &busy_wait_command("ps2pdf $ps_file $pdf_file 1>/dev/null 2>/dev/null",
                    594: 				 "for $status_statement now Converting PS to PDF",
                    595: 				 \%prog_state, $pdf_file);
                    596: 
1.29      sakharuk  597: 	      my $texlog = $texfile;
                    598: 	      my $texaux = $texfile;
                    599: 	      my $texdvi = $texfile;
                    600: 	      my $texps = $texfile;
                    601: 	      $texlog =~ s/\.tex/\.log/;
                    602: 	      $texaux =~ s/\.tex/\.aux/;
                    603: 	      $texdvi =~ s/\.tex/\.dvi/;
                    604: 	      $texps =~ s/\.tex/\.ps/;
1.30      sakharuk  605: 	      my @garb = ($texaux,$texdvi,$texps);
1.29      sakharuk  606: #	  unlink @garb;
1.120     albertel  607: 	      unlink($duefile);
1.59      albertel  608: 	      print "<a href=\"/prtspool/$pdf_file\">Your PDF document</a>";
1.120     albertel  609: 	      unlink($missfonts_file);
1.119     foxr      610: 
1.75      albertel  611: 	  }
                    612: 	  if ($advanced_role) {  
                    613: 	      print "<br /><br />";
                    614: 	      print "<b><big>The link to ";
                    615: 	      $logfilename=~s/\/home\/httpd//;
                    616: 	      print "<a href=\"$logfilename\">Your log file </a></big></b>";
                    617: 	      print "\n";
                    618: 	      #link tooriginal LaTeX file (included according Michael Hamlin desire)
                    619: 	      my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open tex file $texfile for reading: $!\n";
                    620: 	      my @tex_content_of_file = <$tex_temporary_file>;
                    621: 	      close $tex_temporary_file; 
                    622: 	      my $body_tex_file = join(' ',@tex_content_of_file);
                    623: 	      $texfile =~ s/\.tex$/aaaaa\.html/;
                    624: 	      $tex_temporary_file = IO::File->new('>'.$texfile); 
                    625: 	      print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
                    626: 	      print "<br /><br />";
                    627: 	      print "<b><big>The link to ";
                    628: 	      $texfile=~s/\/home\/httpd//;
                    629: 	      print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
                    630: 	      print "\n";
1.29      sakharuk  631: 	  }
1.119     foxr      632: 
1.29      sakharuk  633:       } else {
                    634: 	  #LaTeX successfully parsed tex file 
                    635: 	  $name_file =~ s/\.tex/\.dvi/;
                    636: 	  my $new_name_file = $name_file;
                    637: 	  $new_name_file =~ s/\.dvi/\.ps/;
1.67      sakharuk  638: 	  my $papera=$paper;
1.62      sakharuk  639: 	  if ($papera eq 'letter') {$papera='';}
                    640: 	  if ($papera ne '') {$papera='-t'.$papera;}
                    641: 	  my $comma = "dvips $papera -Ppdf -G0 -o $new_name_file";
1.59      albertel  642: 	  &busy_wait_command("$comma $name_file 1>/dev/null 2>/dev/null",
                    643: 			     "for $status_statement now Converting to PS",
                    644: 			     \%prog_state,$new_name_file);
1.115     foxr      645: 	  #
                    646: 	  #  One last little hinky kinky thing.
                    647: 	  #  It's just possible that some fonts could not be maded
                    648: 	  #  at the resolution of the pdf print driver.
                    649: 	  #  In that case a file called missfont.log will have been
                    650: 	  #  created that will contain the commands that were attempted
                    651: 	  # to create the missing fonts.  If we basically
                    652: 	  # take all the 8000 strings in that file, and
                    653: 	  # replace them with 600 (the ljfour resolution)
                    654: 	  # run the commands in that file and redvips,
                    655: 	  # we'll be able to print the missing glyphs at 600dpi.
                    656: 	  #
                    657: 	  # Supposedly it is possible to tune TeX/Metafont to do this
                    658: 	  # right but I failed to get that to work when following the
                    659: 	  # docs at the tug site, hence this rather kludgey fix.
                    660: 	  #
                    661: 	  #  We make the (I think) reasonable assumption that
                    662: 	  #  missing glyphs won't change the pagination and I think
                    663: 	  #  this is true because TeX/dvips will leave a space
                    664: 	  #  instead of these glyphs based on the font metrics
                    665: 	  #  (fancy way to say there will be a blank the size of the missing
                    666: 	  #  glyphs).
                    667: 	  #
                    668: 	  my $print_directory = dirname($name_file);
                    669: 	  my $missfonts_file  = $print_directory."/missfont.log";
1.116     albertel  670: 	  #print("<br /> Missing fonts file is: $missfonts_file");
1.115     foxr      671: 	  if (-e $missfonts_file) {
1.116     albertel  672: 	      #print("<br />Missing fonts file exists\n");
1.115     foxr      673: 	      &create_missing_fonts($missfonts_file);
1.118     albertel  674: 	      &busy_wait_command("$comma $name_file 1>/dev/null 2>/dev/null",
1.115     foxr      675: 				 "for $status_statement dvips generated missing fonts",
                    676: 				 \%prog_state, $new_name_file);
                    677: 	  }
1.29      sakharuk  678: 	  if (-e $new_name_file) {
1.102     foxr      679: 	      my $latex_file = $name_file;
                    680: 	      $latex_file =~ s/\.dvi/\.tex/;
                    681: 	      &repaginate($new_name_file, $latex_file,  $numberofcolumns);
                    682: 	      &busy_wait_command("latex $latex_file 1>/dev/null 2>/dev/null",
                    683: 				 "for $status_statement first latex to repaginate",
                    684: 				 \%prog_state, $name_file);
1.105     foxr      685: 	      if ($tableofcontents eq 'yes') {
                    686: 		  &busy_wait_command("latex $latex_file 1>/dev/null 2>/dev/null",
                    687: 				     "for $status_statement second latex to repaginate",
                    688: 				     \%prog_state, $name_file);
                    689: 		  &busy_wait_command("latex $latex_file 1>/dev/null 2>/dev/null",
                    690: 				     "for $status_statement third latex to repaginate",
                    691: 				     \%prog_state, $name_file);
                    692: 	      }
                    693: 	      if ($tableofindex eq 'yes') {
                    694: 		  my $idxname = $latex_file;
                    695: 		  $idxname    =~ s/\.tex$/\.idx/;
                    696: 		  &busy_wait_command("makeindex $idxname",
                    697: 				     "Re-creating index file",
                    698: 				     \%prog_state, $idxname);
                    699: 		  &busy_wait_command("latex $latex_file 1>/dev/null 2>/dev/null",
                    700: 				     "for $status_statement now Recreting index (latex)",
                    701: 				     \%prog_state, $dvi_file);
                    702: 	      }
1.102     foxr      703: 	      &busy_wait_command("$comma $name_file 1>/dev/null 2>/dev/null",
                    704: 				 "for $status_statement dvips to repaginate",
                    705: 				 \%prog_state, $new_name_file);
1.115     foxr      706: 
1.61      albertel  707: 	      print "<br />";
1.29      sakharuk  708: 	      $new_name_file =~ m/^(.*)\./;
1.59      albertel  709: 	      my $ps_file = my $tempo_file = $1.'temporar.ps';
                    710: 	      my $pdf_file = $1.'.pdf';
1.82      albertel  711: 	      $papera=~s/t/p/;
1.29      sakharuk  712: 	      if ($laystyle eq 'album' and $numberofcolumns eq '2') {
1.82      albertel  713: 		  $comma = "psnup $papera -2 -s1.0 $new_name_file";
1.59      albertel  714: 		  &busy_wait_command("$comma $tempo_file 1>/dev/null 2>/dev/null",
                    715: 				     "for $status_statement now Modifying PS layout",
                    716: 				     \%prog_state,$tempo_file);
1.29      sakharuk  717: 	      } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
1.65      sakharuk  718: 		  $comma = 'pstops '.$papera.' "2:0+1(0.48w,0)"';
1.59      albertel  719: 		  &busy_wait_command("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null",
                    720: 				     "for $status_statement now Modifying PS layout",
                    721: 				     \%prog_state,$tempo_file); 
1.29      sakharuk  722: 	      } else {
1.59      albertel  723: 		  $ps_file=$new_name_file;
                    724: 	      }
1.67      sakharuk  725: 	      my $addtoPSfile={'legal'=>'<< /PageSize [612 1008] >> setpagedevice',
                    726:                                'tabloid'=>'<< /PageSize [792 1224] >> setpagedevice',
                    727:                                'executive'=>,'<< /PageSize [540 720] >> setpagedevice',
                    728:                                'a2'=>'<< /PageSize [1195.02 1690.09] >> setpagedevice',
                    729:                                'a3'=>'<< /PageSize [842 1195.02] >> setpagedevice',
                    730:                                'a4'=>'<< /PageSize [595.2 842] >> setpagedevice',
                    731:                                'a5'=>'<< /PageSize [421.1 595.2] >> setpagedevice',
                    732:                                'a6'=>'<< /PageSize [298.75 421.1] >> setpagedevice',
                    733: 			   };
                    734: 	      if ($paper ne 'letter') {
1.69      matthew   735: 		  open(FFH,'<',$ps_file) || die "Couldn't open ps file $ps_file for reading: $!\n";
1.63      sakharuk  736: 		  my $new_ps_file='new'.$ps_file;
1.69      matthew   737: 		  open(FFHS,'>',$new_ps_file) || die "Couldn't open new ps file $new_ps_file for reading: $!\n";
1.67      sakharuk  738: 		  print FFHS $addtoPSfile->{$paper}."\n";
1.63      sakharuk  739: 		  while (<FFH>) {
                    740: 		      print FFHS $_;
                    741: 		  }
1.62      sakharuk  742: 		  close(FFH);
1.63      sakharuk  743: 		  close(FFHS);
                    744: 		  $ps_file=$new_ps_file;	  
1.62      sakharuk  745: 	      }
1.59      albertel  746: 	      &busy_wait_command("ps2pdf $ps_file $pdf_file 1>/dev/null 2>/dev/null",
                    747: 				 "for $status_statement now Converting PS to PDF",
                    748: 				 \%prog_state,$pdf_file);
                    749: 	    
1.29      sakharuk  750: 	      my $texlog = $texfile;
                    751: 	      my $texaux = $texfile;
                    752: 	      my $texdvi = $texfile;
                    753: 	      my $texps = $texfile;
                    754: 	      $texlog =~ s/\.tex/\.log/;
                    755: 	      $texaux =~ s/\.tex/\.aux/;
                    756: 	      $texdvi =~ s/\.tex/\.dvi/;
                    757: 	      $texps =~ s/\.tex/\.ps/;
                    758: 	      my @garb = ($texlog,$texaux,$texdvi,$texps);
1.22      sakharuk  759: #	  unlink @garb;
1.120     albertel  760: 	      unlink($duefile);
1.68      sakharuk  761: 	      print "<a href=\"/prtspool/$pdf_file\">$link_text - click here to download pdf</a>";
1.29      sakharuk  762: 	      print "\n";
                    763: 	  }
1.120     albertel  764: 	  unlink($missfonts_file);
1.119     foxr      765: 
1.63      sakharuk  766:       }  
1.29      sakharuk  767:   } else {
                    768:       print "LaTeX file $texfile was not created successfully";
1.14      sakharuk  769:   }
1.43      sakharuk  770: }
1.45      sakharuk  771: print "<br />";
1.44      sakharuk  772: if ($number_of_files>1) {
1.45      sakharuk  773:     my $zipfile=$texfile[0];
                    774:     $zipfile=~s/\.tex/\.zip/;
                    775:     my $statement="zip $zipfile";
1.44      sakharuk  776:     foreach my $file (@texfile) {
1.45      sakharuk  777: 	$file=~s/\.tex/.\pdf/;
                    778: 	$statement.=' '.$file; 
1.44      sakharuk  779:     }
1.49      albertel  780:     print("<pre>Zip Output:\n");
                    781:     system($statement);
                    782:     print("</pre>");
1.45      sakharuk  783:     $zipfile=~s/\/home\/httpd//;
1.49      albertel  784:     print "<br /> A <a href=\"$zipfile\">ZIP file</a> of all the PDFs.";
1.44      sakharuk  785: }
1.92      albertel  786: if ($advanced_role) { &Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state); }
1.97      albertel  787: print(&Apache::loncommon::end_page());
1.59      albertel  788: my $done;
1.102     foxr      789: 
1.59      albertel  790: sub REAPER {
                    791:     $done=1;
                    792: }
                    793: 
                    794: sub busy_wait_command {
                    795:     my ($command,$message,$progress_win,$output_file)=@_;
                    796:     
                    797:     $SIG{CHLD} = \&REAPER;
                    798:     $done=0;
                    799:     my $pid=open(CMD,"$command |");
1.92      albertel  800:     if ($advanced_role) {
1.60      albertel  801: 	&Apache::lonhtmlcommon::Update_PrgWin('',$progress_win,$message);
                    802:     }
1.59      albertel  803:     while(!$done) {
                    804: 	sleep 1;
                    805: 	my $extra_msg;
                    806: 	if ($output_file) {
                    807: 	    my $size=(stat($output_file))[7];
                    808: 	    $extra_msg=", $size bytes generated";
                    809: 	}
1.92      albertel  810: 	if ($advanced_role) {
1.60      albertel  811: 	    &Apache::lonhtmlcommon::Update_PrgWin('',$progress_win,
                    812: 						  $message.$extra_msg);
                    813: 	}
1.59      albertel  814:     }
                    815:     $SIG{CHLD}='IGNORE';
                    816:     close(CMD);
                    817: }
1.1       sakharuk  818: 
1.102     foxr      819: #  Repagninate
1.99      foxr      820: #  What we need to do:
                    821: #   - Count the number of pages in each student.
1.102     foxr      822: #   - Rewrite the latex file replacing the \specials that
                    823: #     mark the end of student with an appropriate number of newlines.
                    824: #   parameters:
                    825: #     psfile     - Postscript filename
                    826: #     latexfile  - LaTeX filename
                    827: #     columns    - number of columns.
                    828: sub repaginate {
1.100     foxr      829: 
                    830:     # We will try to do this in 2 passes through the postscript since
                    831:     # the postscript is potentially large, to do 2 passes, the first pass
                    832:     # must be able to calculate the total number of document pages so that
                    833:     # at the beginning of the second pass we already know how to replace
                    834:     #  %%Pages:
                    835: 
                    836:     #  Figure out
                    837:     #    1. Number of pages in the document
                    838:     #    2. Maximum number of pages in a student
                    839:     #    3. Number of pages in each student.
1.99      foxr      840: 
1.102     foxr      841:     my ($postscript_filename, $latex_filename, $num_columns) = @_;
1.99      foxr      842:     open(PSFILE, "<$postscript_filename");
1.100     foxr      843:     my $line;
                    844:     my $total_pages;		# Total pages in document.
                    845:     my $seen_pages        = 0;	# There are several %%Pages only the first is useful
                    846:     my @pages_in_student;	# For each student his/her initial page count.
                    847:     my $max_pages = 0;		# Pages in 'longest' student.
1.102     foxr      848:     my $page_number = 0;
1.104     foxr      849:     &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state, 
1.110     albertel  850: 					  &mt("Counting pages for student: [_1]",1));
1.104     foxr      851: 
1.100     foxr      852:     while ($line = <PSFILE>) {
                    853: 	
                    854: 	# Check for total pages (%%Pages:)
                    855: 
1.101     albertel  856: 	if (($line =~ /^%%Pages:/) && (!$seen_pages)) {
1.100     foxr      857: 	    my @pageinfo = split(/ /,$line);
                    858: 	    $total_pages = $pageinfo[1];
                    859: 	    $seen_pages  = 1;
                    860: 	}
                    861: 	#  Check for %%Page: n m  $page_number will be the
                    862: 	#  biggest of these until we see an endofstudent.
                    863: 	#  Note that minipages generate spurious %Page: 1 1's so
                    864: 	#  we only are looking for the largest n (n is page number at the
                    865: 	#  bottom of the page, m the page number within the document.
                    866: 	#
1.102     foxr      867: 
1.101     albertel  868: 	if ($line =~ /^%%Page:/) {
1.120     albertel  869: 	    my @pageinfo = split(/\s+/, $line);
1.100     foxr      870: 	    if ($page_number < $pageinfo[1]) {
                    871: 		$page_number = $pageinfo[1];
1.110     albertel  872: 	    } elsif ($pageinfo[2] ne 1) {
                    873: 		#  current page count reset, and it's not because of a 
                    874: 		#    minipage 
                    875: 		# - save the page_number, reset and, if necessary
                    876: 		#    update max_pages.
                    877: 		push(@pages_in_student, $page_number);
                    878: 		&Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state, 
                    879: 						      &mt("Counting pages for student: [_1]", scalar(@pages_in_student)));
                    880: 		if ($page_number > $max_pages) {
                    881: 		    $max_pages = $page_number;
                    882: 		}
                    883: 		$page_number = $pageinfo[1];
1.100     foxr      884: 	    }
                    885: 	}
                    886: 
                    887: 	
                    888:     }
1.110     albertel  889:     # file ended so one more student
                    890:     push(@pages_in_student, $page_number);
                    891:     &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state, 
                    892: 					  &mt("Counting pages for student: [_1]",scalar(@pages_in_student)));
                    893:     if ($page_number > $max_pages) {
                    894: 	$max_pages = $page_number;
                    895:     }
                    896:     $page_number = 0;
                    897:     
1.99      foxr      898:     close(PSFILE);
1.102     foxr      899: 
                    900:     #  If 2 columns, max_pages must go to an even number of columns:
1.103     foxr      901: 
1.102     foxr      902:    
                    903:     if ($num_columns == 2) {
                    904: 	if ($max_pages % 2) {
                    905: 	    $max_pages++;
                    906: 	}
                    907:     }
1.100     foxr      908:     
1.102     foxr      909:     #  Now rewrite the LaTex file, substituting our \special
                    910:     #  with an appropriate number of \newpage directives.
                    911: 
                    912:     my $outfilename = $latex_filename."temp";
                    913: 
                    914:     open(LATEXIN, "<$latex_filename");
                    915:     open(LATEXOUT, ">$outfilename");
                    916: 
                    917: 
1.110     albertel  918:     my $student_number    = 0;	# Index of student we're working on.
1.104     foxr      919:     &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state, 
                    920: 					  "Repaginating student ".$student_number+1);
1.102     foxr      921: 
                    922:     while (my $line = <LATEXIN>) {
                    923: 	if ($line eq "\\special{ps:ENDOFSTUDENTSTAMP}\n") {
                    924: 	    # only end of student stamp if next line is ENDOFSTUDENTSTAMP:
                    925: 
                    926: 
                    927: 	    # End of student replace with 0 or more newpages.
                    928: 	    
                    929: 	    my $addlines = $max_pages - $pages_in_student[$student_number];
                    930: 	    while($addlines)  {
1.108     albertel  931: 		print LATEXOUT '\clearpage \strut \clearpage';
1.105     foxr      932: 
1.102     foxr      933: 		$addlines--;
1.100     foxr      934: 	    }
1.102     foxr      935: 	    
1.100     foxr      936: 	    $student_number++;
1.104     foxr      937: 	    &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state, 
                    938: 						  "Repaginating student ".$student_number+1);
1.102     foxr      939: 	    
                    940: 	} else {
                    941: 	    print LATEXOUT $line;
1.100     foxr      942: 	}
                    943:     }
                    944: 
1.102     foxr      945:     close(LATEXIN);
                    946:     close(LATEXOUT);
1.108     albertel  947:     rename($outfilename, $latex_filename);
1.100     foxr      948: 
1.99      foxr      949: }
1.4       sakharuk  950: 
1.115     foxr      951: #
                    952: #   Create missing fonts given a latex missfonts.log file.
                    953: #   This file will have lines like:
                    954: #
                    955: #   mktexpk --mfmode ljfour --bdpi 8000 --mag 1+0/8000 --dpi 8000 tcrm0500
                    956: #
                    957: #  We want to execute those lines with the 8000's changed to 600's
                    958: #  in order to match the resolution of the ljfour printer.
                    959: #  Of course if some wiseguy has changed the default printer from ljfour
                    960: #  in the dvips's config.ps file that will break so we'll also
                    961: #  ensure that --mfmode is ljfour.
                    962: #
                    963: sub create_missing_fonts {
                    964:     my ($fontfile, $state) = @_;
                    965: 
                    966:     # Open and read in the font file..we'll read it into the array
                    967:     #  font_commands.
                    968:     #
1.118     albertel  969:     open(my $font_handle, $fontfile);
1.115     foxr      970:     my @font_commands = <$font_handle>;
1.118     albertel  971: 
                    972:     # make the list contain each command only once
                    973:     my %uniq;
                    974:     @font_commands = map { $uniq{$_}++ == 0 ? $_ : () } @font_commands;
1.115     foxr      975: 
                    976:     #  Now process each command replacing the appropriate 8000's with
                    977:     #  600's ensuring that font names with 8000's in them are not corrupted.
                    978:     #  and if the --mfmode is not ljfour we turn it into ljfour.
                    979:     #   Then we execute the command.
                    980:     #
1.118     albertel  981:     
1.115     foxr      982:     foreach my $command (@font_commands) {
1.116     albertel  983: 	#print("<br />Raw command: $command");
1.115     foxr      984: 	$command =~ s/ 8000/ 600/g;    # dpi directives.
                    985: 	$command =~ s/\/8000/\/600/g;  # mag directives.
1.116     albertel  986: 	#print("<br />After dpi replacements: $command");
1.115     foxr      987: 
                    988: 	my @cmdarray = split(/ /,$command);
                    989: 	for (my $i =0; $i < scalar(@cmdarray); $i++) {
                    990: 	    if ($cmdarray[$i] eq '--mfmode') {
                    991: 		$cmdarray[$i+1] = "ljfour";
                    992: 	    }
                    993: 	}
1.116     albertel  994: 	#print("<br /> before reassembly : (@cmdarray)");
1.115     foxr      995: 	$command = join(" ", (@cmdarray));
                    996: 
1.116     albertel  997: 	#print("<br />Creating fonts via command: $command");
1.118     albertel  998: 	&busy_wait_command("$command 1>/dev/null 2>/dev/null",
1.115     foxr      999: 			   "Creating missing font",
                   1000: 			   $state, $command);
                   1001: 			   
                   1002:     }
                   1003: 
                   1004: }

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