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

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

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