File:
[LON-CAPA] /
loncom /
interface /
printout.pl
Revision
1.49:
download - view:
text,
annotated -
select for diffs
Tue Feb 17 00:26:20 2004 UTC (20 years, 4 months ago) by
albertel
Branches:
MAIN
CVS tags:
HEAD
- generally cleaning the printout screens up
- lonprintout.pm
- printout files have shorter unique names
- printout files when doing multiple students are all sequentially numbered
- cgi ids use loncommon's get_cgi_id
- printout.pl
- only request.role.adv users get popup status windows
- turn on autoflush
- only update the conversion popup if actually converting
- use a pretty version of the image name when updating the progress window
- use updated counting system for multiple output files
- Updated the English on the various status outputs to read better (probably need more help still)
- <pre> the zip output
- yeah I probably should have made some of these seperate commits
1: #!/usr/bin/perl
2: # CGI-script to run LaTeX, dvips, ps2ps, ps2pdf etc.
3: #
4: # $Id: printout.pl,v 1.49 2004/02/17 00:26:20 albertel Exp $
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:
29: use lib '/home/httpd/lib/perl';
30: use Time::Local;
31: use LONCAPA::loncgi();
32: use File::Path;
33: use IO::File;
34: use Image::Magick;
35: use Apache::lonhtmlcommon;
36:
37: $|=1;
38: my %origENV=%ENV;
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>
46: Your cookie information is incorrect.
47: </body>
48: </html>
49: END
50: return;
51: }
52:
53: print "Content-type: text/html\n\n";
54: print "<body bgcolor=\"#FFFFFF\">\n";
55:
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'};
64: my $back_ref = $ENV{'cgi.'.$identifier.'backref'};
65: my $number_of_files = $ENV{'cgi.'.$identifier.'numberoffiles'}+1;
66: my $student_names = $ENV{'cgi.'.$identifier.'studentnames'};
67: my $backref = $ENV{'cgi.'.$identifier.'backref'};
68:
69: my $adv = $ENV{'request.role.adv'};
70:
71: my @names_pack=();
72: if ($student_names=~/_END_/) {
73: @names_pack=split(/_ENDPERSON_/,$student_names);
74: }
75: #got what we needed reset ENV in case it is to big for system
76: %ENV=%origENV;
77:
78: my $figfile = $texfile;
79: $figfile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.dat/;
80: my $duefile = $texfile;
81: $duefile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.due/;
82: #do we have figures?
83: if (-e $figfile) {
84: my %done_conversion;
85: my $temporary_file=IO::File->new($figfile) || die "Couldn't open file for reading: $!\n";
86: my @content_of_file = <$temporary_file>;
87: close $temporary_file;
88: my $noteps;
89: my %prog_state;
90: if ($adv) { %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('','Coverting Images to EPS','Picture Conversion Status',$#content_of_file); }
91: foreach $not_eps (@content_of_file) {
92: chomp($not_eps);
93: if ($not_eps ne '') {
94: my $status_statement='EPS picture for '.$not_eps;
95: $not_eps=~s|\/\.\/|\/|g;
96: my $eps_f = $not_eps;
97: $eps_f =~ s/\.[^.]*$/\.eps/i;
98: if ($eps_f=~/\/home\/([^\/]+)\/public_html\//) {
99: $eps_f=~s/\/home\/([^\/]+)\/public_html/$1/;
100: $eps_f = '/home/httpd/prtspool/'.$eps_f;
101: } else {
102: $eps_f=~m/\/home\/httpd\/html\/res\/(.+)/;
103: $eps_f = '/home/httpd/prtspool/'.$1;
104: }
105: my $path=$eps_f;
106: $path=~s/\/([^\/]+)\.eps$//;
107: File::Path::mkpath($path,0,0777);
108: my $image = Image::Magick->new;
109: $not_eps =~ s/^\s+//;
110: $not_eps =~ s/\s+$//;
111: if ( exists($done_conversion{$not_eps})) {
112: next;
113: }
114: if ($adv) {
115: my $prettyname=$not_eps;
116: $prettyname=~s|/home/([^/]+)/public_html|/priv/$1|;
117: $prettyname=~s|/home/httpd/html/|/|;
118: &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,'Converting to EPS '.$prettyname); }
119: $done_conversion{$not_eps}=1;
120: $status = $image->Read($not_eps);
121: if ($status) {print " $status ";}
122: $image->Set(page => '+100+200');
123: $status = $image->Write($eps_f);
124: if ($status) {print " $status ";}
125: #check is eps exist in prtspool
126: if(not -e $eps_f) {
127: for (my $i=0;$i<10000;$i++) {
128: if (-e $eps_f.'.'.$i) {
129: rename $eps_f.'.'.$i, $eps_f;
130: last;
131: }
132: }
133: }
134: }
135: }
136: if ($adv) { &Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state); }
137: }
138: #print "$texfile\n"; #name of the tex file for debugging only
139: my @texfile=($texfile);
140: if ($number_of_files>1) {
141: @texfile=();
142: for (my $i=1;$i<=$number_of_files;$i++) {
143: my $new_texfile=$texfile;
144: $new_texfile=~s/\.tex/_$i\.tex/;
145: push @texfile,$new_texfile;
146: }
147: }
148:
149: my $ind=-1;
150: my %prog_state;
151: if ($adv) { %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('','Print Status','Class Print Status',$number_of_files); }
152: my $final_statement="<a href=\"$backref\"><b>Return</b</a> to last resource.<br /><br />Generated PDF File for:<br />";
153: foreach $texfile (@texfile) {
154: my $status_statement='';
155: my $link_text='download PDF';
156: $ind++;
157: my @stud_info=split(/_END_/,$names_pack[$ind]);
158: my @tempo_array=split(/:/,$stud_info[0]);
159: my $name;
160: if ($tempo_array[3]) {
161: $name=$tempo_array[3];
162: } else {
163: $name=$tempo_array[0].'@'.$tempo_array[1];
164: }
165: $link_text='<b>'.$name.'</b> ';
166: $status_statement.=$name;
167: if ($#stud_info>0) {
168: @tempo_array=split(/:/,$stud_info[-1]);
169: if ($tempo_array[3]) {
170: $name=$tempo_array[3];
171: } else {
172: $name=$tempo_array[0].'@'.$tempo_array[1];
173: }
174: $link_text.='- <b>'.$name.':</b> ';
175: $status_statement.=' - '.$name;
176: }
177: if ($adv) { &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,'Creating PDF for '.$status_statement); }
178: if (-e $texfile) {
179: $texfile =~ m/^(.*)\/([^\/]+)$/;
180: my $name_file = $2;
181: my $path_file = $1.'/';
182: chdir $path_file;
183: system("latex $name_file 1>/dev/null 2>/dev/null");
184: if ($tableofcontents eq 'yes') {
185: system("latex $name_file 1>/dev/null 2>/dev/null");
186: } #to create table of contents
187: my $idxname=$name_file;
188: $idxname=~s/\.tex$/\.idx/;
189: if ($tableofindex eq 'yes') {
190: system("makeindex $idxname");
191: system("latex $name_file 1>/dev/null 2>/dev/null");
192: } #to create index
193: #Do we have a latex error in the log file?
194: my $logfilename = $texfile;
195: $logfilename =~ s/\.tex$/\.log/;
196: my $temporary_file=IO::File->new($logfilename) || die "Couldn't open file for reading: $!\n";
197: my @content_of_file = <$temporary_file>;
198: close $temporary_file;
199: my $body_log_file = join(' ',@content_of_file);
200: $logfilename =~ s/\.log$/\.html/;
201: $temporary_file = IO::File->new('>'.$logfilename);
202: print $temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_log_file.'</pre></body></html>'."\n";
203: if ($body_log_file=~m/!\s+Emergency stop/) {
204: #LaTeX failed to parse tex file
205: print "<h2>LaTeX could not successfully parse your tex file.</h2>";
206: print "It probably has errors in it.<br />";
207: my $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART';
208: my $whereitends = rindex $body_log_file,'STAMPOFPASSEDRESOURCEEND';
209: if ($whereitbegins!=-1 and $whereitends!=-1) {
210: print "With very high probability this error occured in ".substr($body_log_file,$whereitbegins+26,$whereitends-$whereitbegins-26)."<br /><br />";
211: }
212: print "Here are the error messages in the LaTeX log file</br><br />";
213: my $sygnal = 0;
214: for (my $i=0;$i<=$#content_of_file;$i++) {
215: if ($content_of_file[$i]=~m/^Runaway argument?/ or $content_of_file[$i]=~m/^!/) {
216: $sygnal = 1;
217: }
218: if ($content_of_file[$i]=~m/Here is how much of/) {
219: $sygnal = 0;
220: }
221: if ($sygnal) {
222: print "$content_of_file[$i]<br />";
223: }
224: }
225: if ($advans_role) {
226: print "<b><big>The link to ";
227: $logfilename=~s/\/home\/httpd//;
228: print "<a href=\"$logfilename\">Your log file </a></big></b>";
229: print "\n";
230: #link tooriginal LaTeX file (included according Michael Hamlin desire)
231: my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open file for reading: $!\n";
232: my @tex_content_of_file = <$tex_temporary_file>;
233: close $tex_temporary_file;
234: my $body_tex_file = join(' ',@tex_content_of_file);
235: $texfile =~ s/\.tex$/aaaaa\.html/;
236: $tex_temporary_file = IO::File->new('>'.$texfile);
237: print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
238: print "<br /><br />";
239: print "<b><big>The link to ";
240: $texfile=~s/\/home\/httpd//;
241: print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
242: print "\n";
243: }
244:
245: } elsif ($body_log_file=~m/<inserted text>/) {
246: my $whereitbegins = index $body_log_file,'<inserted text>';
247: print "You are running LaTeX in the <b>batch mode</b>.";
248: while ($whereitbegins != -1) {
249: my $tempobegin=$whereitbegins;
250: $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART',$whereitbegins;
251: my $whereitends = index $body_log_file,'STAMPOFPASSEDRESOURCEEND',$whereitbegins;
252: print "<br />It has found an error in".substr($body_log_file,$whereitbegins+26,$whereitends-$whereitbegins-26)." <br /> and corrected it.\n";
253: 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";
254: $whereitbegins = index $body_log_file,'<inserted text>',$tempobegin+10;
255: }
256: $name_file =~ s/\.tex/\.dvi/;
257: my $new_name_file = $name_file;
258: $new_name_file =~ s/\.dvi/\.ps/;
259: my $comma = "dvips -Ppdf -G0 -o $new_name_file";
260: system("$comma $name_file 1>/dev/null 2>/dev/null");
261: if (-e $new_name_file) {
262: print "<h1>PDF output file (see link below)</h1>\n";
263: $new_name_file =~ m/^(.*)\./;
264: my $tempo_file = $1.'temporar.ps';
265: my $name_file = $1.'.pdf';
266: if ($laystyle eq 'album' and $numberofcolumns eq '2') {
267: $comma = "psnup -2 -s1.0 $new_name_file";
268: system("$comma $tempo_file 1>/dev/null 2>/dev/null");
269: system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
270: } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
271: $comma = 'pstops -pletter "2:0+1(0.48w,0)"';
272: system("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null");
273: system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
274: } else {
275: system("ps2pdf $new_name_file $name_file 1>/dev/null 2>/dev/null");
276: }
277: my $texlog = $texfile;
278: my $texaux = $texfile;
279: my $texdvi = $texfile;
280: my $texps = $texfile;
281: $texlog =~ s/\.tex/\.log/;
282: $texaux =~ s/\.tex/\.aux/;
283: $texdvi =~ s/\.tex/\.dvi/;
284: $texps =~ s/\.tex/\.ps/;
285: my @garb = ($texaux,$texdvi,$texps);
286: # unlink @garb;
287: unlink $duefile;
288: print "<a href=\"/prtspool/$name_file\">Your PDF document</a>";
289: if ($advans_role) {
290: print "<br /><br />";
291: print "<b><big>The link to ";
292: $logfilename=~s/\/home\/httpd//;
293: print "<a href=\"$logfilename\">Your log file </a></big></b>";
294: print "\n";
295: #link tooriginal LaTeX file (included according Michael Hamlin desire)
296: my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open file for reading: $!\n";
297: my @tex_content_of_file = <$tex_temporary_file>;
298: close $tex_temporary_file;
299: my $body_tex_file = join(' ',@tex_content_of_file);
300: $texfile =~ s/\.tex$/aaaaa\.html/;
301: $tex_temporary_file = IO::File->new('>'.$texfile);
302: print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
303: print "<br /><br />";
304: print "<b><big>The link to ";
305: $texfile=~s/\/home\/httpd//;
306: print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
307: print "\n";
308: }
309: }
310: } else {
311: #LaTeX successfully parsed tex file
312: $name_file =~ s/\.tex/\.dvi/;
313: my $new_name_file = $name_file;
314: $new_name_file =~ s/\.dvi/\.ps/;
315: my $comma = "dvips -Ppdf -G0 -o $new_name_file";
316: system("$comma $name_file 1>/dev/null 2>/dev/null");
317: if (-e $new_name_file) {
318: print "<br />$final_statement ";
319: $final_statement='';
320: $new_name_file =~ m/^(.*)\./;
321: my $tempo_file = $1.'temporar.ps';
322: my $name_file = $1.'.pdf';
323: if ($laystyle eq 'album' and $numberofcolumns eq '2') {
324: $comma = "psnup -2 -s1.0 $new_name_file";
325: system("$comma $tempo_file 1>/dev/null 2>/dev/null");
326: system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
327: } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
328: $comma = 'pstops -pletter "2:0+1(0.48w,0)"';
329: system("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null");
330: system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
331: } else {
332: system("ps2pdf $new_name_file $name_file 1>/dev/null 2>/dev/null");
333: }
334: my $texlog = $texfile;
335: my $texaux = $texfile;
336: my $texdvi = $texfile;
337: my $texps = $texfile;
338: $texlog =~ s/\.tex/\.log/;
339: $texaux =~ s/\.tex/\.aux/;
340: $texdvi =~ s/\.tex/\.dvi/;
341: $texps =~ s/\.tex/\.ps/;
342: my @garb = ($texlog,$texaux,$texdvi,$texps);
343: # unlink @garb;
344: unlink $duefile;
345: print "<a href=\"/prtspool/$name_file\">$link_text</a>";
346: print "\n";
347: }
348: }
349: } else {
350: print "LaTeX file $texfile was not created successfully";
351: }
352: }
353: print "<br />";
354: if ($number_of_files>1) {
355: my $zipfile=$texfile[0];
356: $zipfile=~s/\.tex/\.zip/;
357: my $statement="zip $zipfile";
358: foreach my $file (@texfile) {
359: $file=~s/\.tex/.\pdf/;
360: $statement.=' '.$file;
361: }
362: print("<pre>Zip Output:\n");
363: system($statement);
364: print("</pre>");
365: $zipfile=~s/\/home\/httpd//;
366: print "<br /> A <a href=\"$zipfile\">ZIP file</a> of all the PDFs.";
367: }
368: if ($adv) { &Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state); }
369:
370:
371:
372:
373:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>