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