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

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

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