Annotation of loncom/interface/printout.pl, revision 1.43
1.1 sakharuk 1: #!/usr/bin/perl
1.37 albertel 2: # CGI-script to run LaTeX, dvips, ps2ps, ps2pdf etc.
3: #
1.43 ! sakharuk 4: # $Id: printout.pl,v 1.42 2004/02/03 00:02:15 albertel Exp $
1.37 albertel 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28:
1.39 sakharuk 29: use lib '/home/httpd/lib/perl';
30: use LONCAPA::loncgi();
1.38 sakharuk 31: use File::Path;
1.6 sakharuk 32: use IO::File;
1.7 sakharuk 33: use Image::Magick;
1.39 sakharuk 34:
35: if (! &LONCAPA::loncgi::check_cookie_and_load_env()) {
36: print <<END;
37: Content-type: text/html
38:
39: <html>
40: <head><title>Bad Cookie</title></head>
41: <body>
1.40 sakharuk 42: Your cookie information is incorrect.
1.39 sakharuk 43: </body>
44: </html>
45: END
46: return;
47: }
48:
1.14 sakharuk 49: print "Content-type: text/html\n\n";
1.27 www 50: print "<body bgcolor=\"#FFFFFF\">\n";
1.39 sakharuk 51:
1.40 sakharuk 52: my $identifier = $ENV{'QUERY_STRING'};
53: my $texfile = $ENV{'cgi.'.$identifier.'.file'};
54: my $laystyle = $ENV{'cgi.'.$identifier.'.layout'};
55: my $numberofcolumns = $ENV{'cgi.'.$identifier.'.numcol'};
56: my $selectionmade = $ENV{'cgi.'.$identifier.'.selection'};
57: my $tableofcontents = $ENV{'cgi.'.$identifier.'tableofcontents'};
58: my $tableofindex = $ENV{'cgi.'.$identifier.'tableofindex'};
59: my $advans_role = $ENV{'cgi.'.$identifier.'role'};
1.43 ! sakharuk 60: my $back_ref = $ENV{'cgi.'.$identifier.'backref'};
! 61: my $number_of_files = $ENV{'cgi.'.$identifier.'numberoffiles'}+1;
! 62:
1.39 sakharuk 63:
1.14 sakharuk 64: my $figfile = $texfile;
65: $figfile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.dat/;
66: my $duefile = $texfile;
67: $duefile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.due/;
68: #do we have figures?
69: if (-e $figfile) {
1.42 albertel 70: my %done_conversion;
1.14 sakharuk 71: my $temporary_file=IO::File->new($figfile) || die "Couldn't open file for reading: $!\n";
72: my @content_of_file = <$temporary_file>;
73: close $temporary_file;
74: my $noteps;
75: foreach $not_eps (@content_of_file) {
76: if ($not_eps ne '') {
1.41 sakharuk 77: $not_eps=~s|\/\.\/|\/|g;
1.14 sakharuk 78: my $eps_f = $not_eps;
79: $eps_f =~ s/\.[^.]*$/\.eps/i;
1.41 sakharuk 80: if ($eps_f=~/\/home\/([^\/]+)\/public_html\//) {
81: $eps_f=~s/\/home\/([^\/]+)\/public_html/$1/;
82: $eps_f = '/home/httpd/prtspool/'.$eps_f;
83: } else {
84: $eps_f=~m/\/home\/httpd\/html\/res\/(.+)/;
85: $eps_f = '/home/httpd/prtspool/'.$1;
86: }
1.38 sakharuk 87: my $path=$eps_f;
1.41 sakharuk 88: $path=~s/\/([^\/]+)\.eps$//;
1.38 sakharuk 89: File::Path::mkpath($path,0,0777);
1.14 sakharuk 90: my $image = Image::Magick->new;
91: $not_eps =~ s/^\s+//;
92: $not_eps =~ s/\s+$//;
1.42 albertel 93: if ( exists($done_conversion{$not_eps})) {
94: next;
95: }
96: $done_conversion{$not_eps}=1;
1.14 sakharuk 97: $status = $image->Read($not_eps);
98: if ($status) {print " $status ";}
99: $image->Set(page => '+100+200');
100: $status = $image->Write($eps_f);
101: if ($status) {print " $status ";}
1.20 sakharuk 102: #check is eps exist in prtspool
103: if(not -e $eps_f) {
104: for (my $i=0;$i<10000;$i++) {
105: if (-e $eps_f.'.'.$i) {
106: rename $eps_f.'.'.$i, $eps_f;
107: last;
108: }
109: }
110: }
1.14 sakharuk 111: }
112: }
113: }
1.43 ! sakharuk 114: #print "$texfile\n"; #name of the tex file for debugging only
! 115: my @texfile=($texfile);
! 116: if ($number_of_files>1) {
! 117: for (my $i=1;$i<=$number_of_files-1;$i++) {
! 118: my $new_texfile=$texfile;
! 119: $new_texfile=~s/\.tex/_add$i\.tex/;
! 120: push @texfile,$new_texfile;
! 121: }
! 122: }
! 123: foreach $texfile (@texfile) {
1.29 sakharuk 124: if (-e $texfile) {
125: $texfile =~ m/^(.*)\/([^\/]+)$/;
126: my $name_file = $2;
127: my $path_file = $1.'/';
128: chdir $path_file;
129: system("latex $name_file 1>/dev/null 2>/dev/null");
1.40 sakharuk 130: if ($tableofcontents eq 'yes') {
131: system("latex $name_file 1>/dev/null 2>/dev/null");
132: } #to create table of contents
1.33 sakharuk 133: my $idxname=$name_file;
134: $idxname=~s/\.tex$/\.idx/;
1.40 sakharuk 135: if ($tableofindex eq 'yes') {
1.33 sakharuk 136: system("makeindex $idxname");
137: system("latex $name_file 1>/dev/null 2>/dev/null");
138: } #to create index
1.29 sakharuk 139: #Do we have a latex error in the log file?
140: my $logfilename = $texfile;
141: $logfilename =~ s/\.tex$/\.log/;
142: my $temporary_file=IO::File->new($logfilename) || die "Couldn't open file for reading: $!\n";
143: my @content_of_file = <$temporary_file>;
144: close $temporary_file;
1.30 sakharuk 145: my $body_log_file = join(' ',@content_of_file);
146: $logfilename =~ s/\.log$/\.html/;
147: $temporary_file = IO::File->new('>'.$logfilename);
148: print $temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_log_file.'</pre></body></html>'."\n";
1.29 sakharuk 149: if ($body_log_file=~m/!\s+Emergency stop/) {
150: #LaTeX failed to parse tex file
151: print "<h2>LaTeX could not successfully parse your tex file.</h2>";
152: print "It probably has errors in it.<br />";
153: my $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART';
154: my $whereitends = rindex $body_log_file,'STAMPOFPASSEDRESOURCEEND';
155: if ($whereitbegins!=-1 and $whereitends!=-1) {
156: print "With very high probability this error occured in ".substr($body_log_file,$whereitbegins+26,$whereitends-$whereitbegins-26)."<br /><br />";
157: }
158: print "Here are the error messages in the LaTeX log file</br><br />";
159: my $sygnal = 0;
160: for (my $i=0;$i<=$#content_of_file;$i++) {
161: if ($content_of_file[$i]=~m/^Runaway argument?/ or $content_of_file[$i]=~m/^!/) {
162: $sygnal = 1;
163: }
164: if ($content_of_file[$i]=~m/Here is how much of/) {
165: $sygnal = 0;
166: }
167: if ($sygnal) {
168: print "$content_of_file[$i]<br />";
169: }
1.36 sakharuk 170: }
171: if ($advans_role) {
1.35 sakharuk 172: print "<b><big>The link to ";
173: $logfilename=~s/\/home\/httpd//;
174: print "<a href=\"$logfilename\">Your log file </a></big></b>";
175: print "\n";
176: #link tooriginal LaTeX file (included according Michael Hamlin desire)
177: my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open file for reading: $!\n";
178: my @tex_content_of_file = <$tex_temporary_file>;
179: close $tex_temporary_file;
180: my $body_tex_file = join(' ',@tex_content_of_file);
181: $texfile =~ s/\.tex$/aaaaa\.html/;
182: $tex_temporary_file = IO::File->new('>'.$texfile);
183: print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
184: print "<br /><br />";
185: print "<b><big>The link to ";
186: $texfile=~s/\/home\/httpd//;
187: print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
188: print "\n";
1.36 sakharuk 189: }
1.35 sakharuk 190:
1.29 sakharuk 191: } elsif ($body_log_file=~m/<inserted text>/) {
192: my $whereitbegins = index $body_log_file,'<inserted text>';
1.31 sakharuk 193: print "You are running LaTeX in the <b>batch mode</b>.";
1.30 sakharuk 194: while ($whereitbegins != -1) {
195: my $tempobegin=$whereitbegins;
196: $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART',$whereitbegins;
197: my $whereitends = index $body_log_file,'STAMPOFPASSEDRESOURCEEND',$whereitbegins;
1.34 sakharuk 198: 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 199: 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";
200: $whereitbegins = index $body_log_file,'<inserted text>',$tempobegin+10;
201: }
1.29 sakharuk 202: $name_file =~ s/\.tex/\.dvi/;
203: my $new_name_file = $name_file;
204: $new_name_file =~ s/\.dvi/\.ps/;
205: my $comma = "dvips -Ppdf -G0 -o $new_name_file";
206: system("$comma $name_file 1>/dev/null 2>/dev/null");
207: if (-e $new_name_file) {
208: print "<h1>PDF output file (see link below)</h1>\n";
209: $new_name_file =~ m/^(.*)\./;
210: my $tempo_file = $1.'temporar.ps';
211: my $name_file = $1.'.pdf';
212: if ($laystyle eq 'album' and $numberofcolumns eq '2') {
213: $comma = "psnup -2 -s1.0 $new_name_file";
214: system("$comma $tempo_file 1>/dev/null 2>/dev/null");
215: system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
216: } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
217: $comma = 'pstops -pletter "2:0+1(0.48w,0)"';
218: system("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null");
219: system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
220: } else {
221: system("ps2pdf $new_name_file $name_file 1>/dev/null 2>/dev/null");
222: }
223: my $texlog = $texfile;
224: my $texaux = $texfile;
225: my $texdvi = $texfile;
226: my $texps = $texfile;
227: $texlog =~ s/\.tex/\.log/;
228: $texaux =~ s/\.tex/\.aux/;
229: $texdvi =~ s/\.tex/\.dvi/;
230: $texps =~ s/\.tex/\.ps/;
1.30 sakharuk 231: my @garb = ($texaux,$texdvi,$texps);
1.29 sakharuk 232: # unlink @garb;
233: unlink $duefile;
234: print "<a href=\"/prtspool/$name_file\">Your PDF document</a>";
1.36 sakharuk 235: if ($advans_role) {
236: print "<br /><br />";
237: print "<b><big>The link to ";
238: $logfilename=~s/\/home\/httpd//;
239: print "<a href=\"$logfilename\">Your log file </a></big></b>";
240: print "\n";
241: #link tooriginal LaTeX file (included according Michael Hamlin desire)
242: my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open file for reading: $!\n";
243: my @tex_content_of_file = <$tex_temporary_file>;
244: close $tex_temporary_file;
245: my $body_tex_file = join(' ',@tex_content_of_file);
246: $texfile =~ s/\.tex$/aaaaa\.html/;
247: $tex_temporary_file = IO::File->new('>'.$texfile);
248: print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
249: print "<br /><br />";
250: print "<b><big>The link to ";
251: $texfile=~s/\/home\/httpd//;
252: print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
253: print "\n";
254: }
1.29 sakharuk 255: }
256: } else {
257: #LaTeX successfully parsed tex file
258: $name_file =~ s/\.tex/\.dvi/;
259: my $new_name_file = $name_file;
260: $new_name_file =~ s/\.dvi/\.ps/;
261: my $comma = "dvips -Ppdf -G0 -o $new_name_file";
262: system("$comma $name_file 1>/dev/null 2>/dev/null");
263: if (-e $new_name_file) {
264: print "<h1>Successfully created PDF output file (see link below)</h1>\n";
265: $new_name_file =~ m/^(.*)\./;
266: my $tempo_file = $1.'temporar.ps';
267: my $name_file = $1.'.pdf';
268: if ($laystyle eq 'album' and $numberofcolumns eq '2') {
269: $comma = "psnup -2 -s1.0 $new_name_file";
270: system("$comma $tempo_file 1>/dev/null 2>/dev/null");
271: system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
272: } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
273: $comma = 'pstops -pletter "2:0+1(0.48w,0)"';
274: system("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null");
275: system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
276: } else {
277: system("ps2pdf $new_name_file $name_file 1>/dev/null 2>/dev/null");
278: }
279: my $texlog = $texfile;
280: my $texaux = $texfile;
281: my $texdvi = $texfile;
282: my $texps = $texfile;
283: $texlog =~ s/\.tex/\.log/;
284: $texaux =~ s/\.tex/\.aux/;
285: $texdvi =~ s/\.tex/\.dvi/;
286: $texps =~ s/\.tex/\.ps/;
287: my @garb = ($texlog,$texaux,$texdvi,$texps);
1.22 sakharuk 288: # unlink @garb;
1.29 sakharuk 289: unlink $duefile;
290: print "<a href=\"/prtspool/$name_file\">Your PDF document</a>";
291: print "\n";
292: }
1.14 sakharuk 293: }
1.29 sakharuk 294: } else {
295: print "LaTeX file $texfile was not created successfully";
1.14 sakharuk 296: }
1.43 ! sakharuk 297: }
1.17 sakharuk 298:
299:
300:
1.1 sakharuk 301:
302:
303:
304:
1.4 sakharuk 305:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>