Annotation of loncom/interface/printout.pl, revision 1.48
1.1 sakharuk 1: #!/usr/bin/perl
1.37 albertel 2: # CGI-script to run LaTeX, dvips, ps2ps, ps2pdf etc.
3: #
1.48 ! albertel 4: # $Id: printout.pl,v 1.47 2004/02/13 21:17:50 sakharuk 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.44 sakharuk 30: use Time::Local;
1.39 sakharuk 31: use LONCAPA::loncgi();
1.38 sakharuk 32: use File::Path;
1.6 sakharuk 33: use IO::File;
1.7 sakharuk 34: use Image::Magick;
1.47 sakharuk 35: use Apache::lonhtmlcommon;
36:
1.39 sakharuk 37:
1.46 albertel 38: my %origENV=%ENV;
1.39 sakharuk 39: if (! &LONCAPA::loncgi::check_cookie_and_load_env()) {
40: print <<END;
41: Content-type: text/html
42:
43: <html>
44: <head><title>Bad Cookie</title></head>
45: <body>
1.40 sakharuk 46: Your cookie information is incorrect.
1.39 sakharuk 47: </body>
48: </html>
49: END
50: return;
51: }
52:
1.14 sakharuk 53: print "Content-type: text/html\n\n";
1.27 www 54: print "<body bgcolor=\"#FFFFFF\">\n";
1.39 sakharuk 55:
1.40 sakharuk 56: my $identifier = $ENV{'QUERY_STRING'};
57: my $texfile = $ENV{'cgi.'.$identifier.'.file'};
58: my $laystyle = $ENV{'cgi.'.$identifier.'.layout'};
59: my $numberofcolumns = $ENV{'cgi.'.$identifier.'.numcol'};
60: my $selectionmade = $ENV{'cgi.'.$identifier.'.selection'};
61: my $tableofcontents = $ENV{'cgi.'.$identifier.'tableofcontents'};
62: my $tableofindex = $ENV{'cgi.'.$identifier.'tableofindex'};
63: my $advans_role = $ENV{'cgi.'.$identifier.'role'};
1.43 sakharuk 64: my $back_ref = $ENV{'cgi.'.$identifier.'backref'};
65: my $number_of_files = $ENV{'cgi.'.$identifier.'numberoffiles'}+1;
1.44 sakharuk 66: my $student_names = $ENV{'cgi.'.$identifier.'studentnames'};
1.47 sakharuk 67: my $backref = $ENV{'cgi.'.$identifier.'backref'};
1.43 sakharuk 68:
1.44 sakharuk 69: my @names_pack=();
70: if ($student_names=~/_END_/) {
71: @names_pack=split(/_ENDPERSON_/,$student_names);
72: }
1.46 albertel 73: #got what we needed reset ENV in case it is to big for system
74: %ENV=%origENV;
1.39 sakharuk 75:
1.14 sakharuk 76: my $figfile = $texfile;
77: $figfile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.dat/;
78: my $duefile = $texfile;
79: $duefile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.due/;
80: #do we have figures?
81: if (-e $figfile) {
1.42 albertel 82: my %done_conversion;
1.14 sakharuk 83: my $temporary_file=IO::File->new($figfile) || die "Couldn't open file for reading: $!\n";
84: my @content_of_file = <$temporary_file>;
85: close $temporary_file;
86: my $noteps;
1.47 sakharuk 87: my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('','Pictures CreationPrint Status','Pictires Status window',$#content_of_file);
1.14 sakharuk 88: foreach $not_eps (@content_of_file) {
89: if ($not_eps ne '') {
1.44 sakharuk 90: my $status_statement='EPS picture for '.$not_eps;
1.47 sakharuk 91: &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,'last PDF file '.$not_eps);
1.41 sakharuk 92: $not_eps=~s|\/\.\/|\/|g;
1.14 sakharuk 93: my $eps_f = $not_eps;
94: $eps_f =~ s/\.[^.]*$/\.eps/i;
1.41 sakharuk 95: if ($eps_f=~/\/home\/([^\/]+)\/public_html\//) {
96: $eps_f=~s/\/home\/([^\/]+)\/public_html/$1/;
97: $eps_f = '/home/httpd/prtspool/'.$eps_f;
98: } else {
99: $eps_f=~m/\/home\/httpd\/html\/res\/(.+)/;
100: $eps_f = '/home/httpd/prtspool/'.$1;
101: }
1.38 sakharuk 102: my $path=$eps_f;
1.41 sakharuk 103: $path=~s/\/([^\/]+)\.eps$//;
1.38 sakharuk 104: File::Path::mkpath($path,0,0777);
1.14 sakharuk 105: my $image = Image::Magick->new;
106: $not_eps =~ s/^\s+//;
107: $not_eps =~ s/\s+$//;
1.42 albertel 108: if ( exists($done_conversion{$not_eps})) {
109: next;
110: }
111: $done_conversion{$not_eps}=1;
1.14 sakharuk 112: $status = $image->Read($not_eps);
113: if ($status) {print " $status ";}
114: $image->Set(page => '+100+200');
115: $status = $image->Write($eps_f);
116: if ($status) {print " $status ";}
1.20 sakharuk 117: #check is eps exist in prtspool
118: if(not -e $eps_f) {
119: for (my $i=0;$i<10000;$i++) {
120: if (-e $eps_f.'.'.$i) {
121: rename $eps_f.'.'.$i, $eps_f;
122: last;
123: }
124: }
125: }
1.14 sakharuk 126: }
127: }
1.47 sakharuk 128: &Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state);
1.14 sakharuk 129: }
1.43 sakharuk 130: #print "$texfile\n"; #name of the tex file for debugging only
131: my @texfile=($texfile);
132: if ($number_of_files>1) {
133: for (my $i=1;$i<=$number_of_files-1;$i++) {
134: my $new_texfile=$texfile;
135: $new_texfile=~s/\.tex/_add$i\.tex/;
136: push @texfile,$new_texfile;
137: }
138: }
1.44 sakharuk 139: my $ind=-1;
1.47 sakharuk 140: my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('','Print Status','Class Print Status',$number_of_files);
1.43 sakharuk 141: foreach $texfile (@texfile) {
1.47 sakharuk 142: my $final_statement="<a href=\"$backref\"><b>Back to where you started</b></a><br /><br /><b>Link to your PDF document:</b> ";
1.44 sakharuk 143: my $status_statement='PDF document for ';
144: if ($number_of_files>1) {
145: $ind++;
146: my @stud_info=split(/_END_/,$names_pack[$ind]);
147: my @tempo_array=split(/:/,$stud_info[0]);
1.48 ! albertel 148: my $name;
! 149: if ($tempo_array[3]) {
! 150: $name=$tempo_array[3];
! 151: } else {
! 152: $name=$tempo_array[0].'@'.$tempo_array[1];
! 153: }
! 154: $final_statement='Link to PDF document for <b>'.$name.'</b> ';
! 155: $status_statement.=$name;
1.44 sakharuk 156: if ($#stud_info>0) {
157: @tempo_array=split(/:/,$stud_info[-1]);
1.48 ! albertel 158: if ($tempo_array[3]) {
! 159: $name=$tempo_array[3];
! 160: } else {
! 161: $name=$tempo_array[0].'@'.$tempo_array[1];
! 162: }
! 163: $final_statement.='- <b>'.$name.':</b> ';
! 164: $status_statement.=' - '.$name;
1.44 sakharuk 165: }
166: }
1.47 sakharuk 167: &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,'last PDF file '.$status_statement);
1.29 sakharuk 168: if (-e $texfile) {
169: $texfile =~ m/^(.*)\/([^\/]+)$/;
170: my $name_file = $2;
171: my $path_file = $1.'/';
172: chdir $path_file;
173: system("latex $name_file 1>/dev/null 2>/dev/null");
1.40 sakharuk 174: if ($tableofcontents eq 'yes') {
175: system("latex $name_file 1>/dev/null 2>/dev/null");
176: } #to create table of contents
1.33 sakharuk 177: my $idxname=$name_file;
178: $idxname=~s/\.tex$/\.idx/;
1.40 sakharuk 179: if ($tableofindex eq 'yes') {
1.33 sakharuk 180: system("makeindex $idxname");
181: system("latex $name_file 1>/dev/null 2>/dev/null");
182: } #to create index
1.29 sakharuk 183: #Do we have a latex error in the log file?
184: my $logfilename = $texfile;
185: $logfilename =~ s/\.tex$/\.log/;
186: my $temporary_file=IO::File->new($logfilename) || die "Couldn't open file for reading: $!\n";
187: my @content_of_file = <$temporary_file>;
188: close $temporary_file;
1.30 sakharuk 189: my $body_log_file = join(' ',@content_of_file);
190: $logfilename =~ s/\.log$/\.html/;
191: $temporary_file = IO::File->new('>'.$logfilename);
192: print $temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_log_file.'</pre></body></html>'."\n";
1.29 sakharuk 193: if ($body_log_file=~m/!\s+Emergency stop/) {
194: #LaTeX failed to parse tex file
195: print "<h2>LaTeX could not successfully parse your tex file.</h2>";
196: print "It probably has errors in it.<br />";
197: my $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART';
198: my $whereitends = rindex $body_log_file,'STAMPOFPASSEDRESOURCEEND';
199: if ($whereitbegins!=-1 and $whereitends!=-1) {
1.44 sakharuk 200: print "With very high probability this error occured in ".substr($body_log_file,$whereitbegins+26,$whereitends-$whereitbegins-26)."<br /><br />";
1.29 sakharuk 201: }
202: print "Here are the error messages in the LaTeX log file</br><br />";
203: my $sygnal = 0;
204: for (my $i=0;$i<=$#content_of_file;$i++) {
205: if ($content_of_file[$i]=~m/^Runaway argument?/ or $content_of_file[$i]=~m/^!/) {
206: $sygnal = 1;
207: }
208: if ($content_of_file[$i]=~m/Here is how much of/) {
209: $sygnal = 0;
210: }
211: if ($sygnal) {
212: print "$content_of_file[$i]<br />";
213: }
1.36 sakharuk 214: }
215: if ($advans_role) {
1.35 sakharuk 216: print "<b><big>The link to ";
217: $logfilename=~s/\/home\/httpd//;
218: print "<a href=\"$logfilename\">Your log file </a></big></b>";
219: print "\n";
220: #link tooriginal LaTeX file (included according Michael Hamlin desire)
221: my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open file for reading: $!\n";
222: my @tex_content_of_file = <$tex_temporary_file>;
223: close $tex_temporary_file;
224: my $body_tex_file = join(' ',@tex_content_of_file);
225: $texfile =~ s/\.tex$/aaaaa\.html/;
226: $tex_temporary_file = IO::File->new('>'.$texfile);
227: print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
228: print "<br /><br />";
229: print "<b><big>The link to ";
230: $texfile=~s/\/home\/httpd//;
231: print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
232: print "\n";
1.36 sakharuk 233: }
1.35 sakharuk 234:
1.29 sakharuk 235: } elsif ($body_log_file=~m/<inserted text>/) {
236: my $whereitbegins = index $body_log_file,'<inserted text>';
1.31 sakharuk 237: print "You are running LaTeX in the <b>batch mode</b>.";
1.30 sakharuk 238: while ($whereitbegins != -1) {
239: my $tempobegin=$whereitbegins;
240: $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART',$whereitbegins;
241: my $whereitends = index $body_log_file,'STAMPOFPASSEDRESOURCEEND',$whereitbegins;
1.34 sakharuk 242: 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 243: 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";
244: $whereitbegins = index $body_log_file,'<inserted text>',$tempobegin+10;
245: }
1.29 sakharuk 246: $name_file =~ s/\.tex/\.dvi/;
247: my $new_name_file = $name_file;
248: $new_name_file =~ s/\.dvi/\.ps/;
249: my $comma = "dvips -Ppdf -G0 -o $new_name_file";
250: system("$comma $name_file 1>/dev/null 2>/dev/null");
251: if (-e $new_name_file) {
252: print "<h1>PDF output file (see link below)</h1>\n";
253: $new_name_file =~ m/^(.*)\./;
254: my $tempo_file = $1.'temporar.ps';
255: my $name_file = $1.'.pdf';
256: if ($laystyle eq 'album' and $numberofcolumns eq '2') {
257: $comma = "psnup -2 -s1.0 $new_name_file";
258: system("$comma $tempo_file 1>/dev/null 2>/dev/null");
259: system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
260: } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
261: $comma = 'pstops -pletter "2:0+1(0.48w,0)"';
262: system("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null");
263: system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
264: } else {
265: system("ps2pdf $new_name_file $name_file 1>/dev/null 2>/dev/null");
266: }
267: my $texlog = $texfile;
268: my $texaux = $texfile;
269: my $texdvi = $texfile;
270: my $texps = $texfile;
271: $texlog =~ s/\.tex/\.log/;
272: $texaux =~ s/\.tex/\.aux/;
273: $texdvi =~ s/\.tex/\.dvi/;
274: $texps =~ s/\.tex/\.ps/;
1.30 sakharuk 275: my @garb = ($texaux,$texdvi,$texps);
1.29 sakharuk 276: # unlink @garb;
277: unlink $duefile;
278: print "<a href=\"/prtspool/$name_file\">Your PDF document</a>";
1.36 sakharuk 279: if ($advans_role) {
280: print "<br /><br />";
281: print "<b><big>The link to ";
282: $logfilename=~s/\/home\/httpd//;
283: print "<a href=\"$logfilename\">Your log file </a></big></b>";
284: print "\n";
285: #link tooriginal LaTeX file (included according Michael Hamlin desire)
286: my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open file for reading: $!\n";
287: my @tex_content_of_file = <$tex_temporary_file>;
288: close $tex_temporary_file;
289: my $body_tex_file = join(' ',@tex_content_of_file);
290: $texfile =~ s/\.tex$/aaaaa\.html/;
291: $tex_temporary_file = IO::File->new('>'.$texfile);
292: print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
293: print "<br /><br />";
294: print "<b><big>The link to ";
295: $texfile=~s/\/home\/httpd//;
296: print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
297: print "\n";
298: }
1.29 sakharuk 299: }
300: } else {
301: #LaTeX successfully parsed tex file
302: $name_file =~ s/\.tex/\.dvi/;
303: my $new_name_file = $name_file;
304: $new_name_file =~ s/\.dvi/\.ps/;
305: my $comma = "dvips -Ppdf -G0 -o $new_name_file";
306: system("$comma $name_file 1>/dev/null 2>/dev/null");
307: if (-e $new_name_file) {
1.44 sakharuk 308: print "<br />$final_statement ";
1.29 sakharuk 309: $new_name_file =~ m/^(.*)\./;
310: my $tempo_file = $1.'temporar.ps';
311: my $name_file = $1.'.pdf';
312: if ($laystyle eq 'album' and $numberofcolumns eq '2') {
313: $comma = "psnup -2 -s1.0 $new_name_file";
314: system("$comma $tempo_file 1>/dev/null 2>/dev/null");
315: system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
316: } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
317: $comma = 'pstops -pletter "2:0+1(0.48w,0)"';
318: system("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null");
319: system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
320: } else {
321: system("ps2pdf $new_name_file $name_file 1>/dev/null 2>/dev/null");
322: }
323: my $texlog = $texfile;
324: my $texaux = $texfile;
325: my $texdvi = $texfile;
326: my $texps = $texfile;
327: $texlog =~ s/\.tex/\.log/;
328: $texaux =~ s/\.tex/\.aux/;
329: $texdvi =~ s/\.tex/\.dvi/;
330: $texps =~ s/\.tex/\.ps/;
331: my @garb = ($texlog,$texaux,$texdvi,$texps);
1.22 sakharuk 332: # unlink @garb;
1.29 sakharuk 333: unlink $duefile;
334: print "<a href=\"/prtspool/$name_file\">Your PDF document</a>";
335: print "\n";
336: }
1.14 sakharuk 337: }
1.29 sakharuk 338: } else {
339: print "LaTeX file $texfile was not created successfully";
1.14 sakharuk 340: }
1.43 sakharuk 341: }
1.45 sakharuk 342: print "<br />";
1.44 sakharuk 343: if ($number_of_files>1) {
1.45 sakharuk 344: my $zipfile=$texfile[0];
345: $zipfile=~s/\.tex/\.zip/;
346: my $statement="zip $zipfile";
1.44 sakharuk 347: foreach my $file (@texfile) {
1.45 sakharuk 348: $file=~s/\.tex/.\pdf/;
349: $statement.=' '.$file;
1.44 sakharuk 350: }
1.45 sakharuk 351: system("$statement");
352: $zipfile=~s/\/home\/httpd//;
353: print "<br /> <a href=\"$zipfile\">Your ZIP file is here</a>";
1.44 sakharuk 354: }
1.47 sakharuk 355: &Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state);
1.1 sakharuk 356:
357:
358:
359:
1.4 sakharuk 360:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>