Annotation of loncom/interface/printout.pl, revision 1.41
1.1 sakharuk 1: #!/usr/bin/perl
1.37 albertel 2: # CGI-script to run LaTeX, dvips, ps2ps, ps2pdf etc.
3: #
1.41 ! sakharuk 4: # $Id: printout.pl,v 1.40 2003/10/20 18:34:20 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';
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.39 sakharuk 60:
1.14 sakharuk 61: my $figfile = $texfile;
62: $figfile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.dat/;
63: my $duefile = $texfile;
64: $duefile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.due/;
65: #do we have figures?
66: if (-e $figfile) {
67: my $temporary_file=IO::File->new($figfile) || die "Couldn't open file for reading: $!\n";
68: my @content_of_file = <$temporary_file>;
69: close $temporary_file;
70: my $noteps;
71: foreach $not_eps (@content_of_file) {
72: if ($not_eps ne '') {
1.41 ! sakharuk 73: $not_eps=~s|\/\.\/|\/|g;
1.14 sakharuk 74: my $eps_f = $not_eps;
75: $eps_f =~ s/\.[^.]*$/\.eps/i;
1.41 ! sakharuk 76: if ($eps_f=~/\/home\/([^\/]+)\/public_html\//) {
! 77: $eps_f=~s/\/home\/([^\/]+)\/public_html/$1/;
! 78: $eps_f = '/home/httpd/prtspool/'.$eps_f;
! 79: } else {
! 80: $eps_f=~m/\/home\/httpd\/html\/res\/(.+)/;
! 81: $eps_f = '/home/httpd/prtspool/'.$1;
! 82: }
1.38 sakharuk 83: my $path=$eps_f;
1.41 ! sakharuk 84: $path=~s/\/([^\/]+)\.eps$//;
1.38 sakharuk 85: File::Path::mkpath($path,0,0777);
1.14 sakharuk 86: my $image = Image::Magick->new;
87: $not_eps =~ s/^\s+//;
88: $not_eps =~ s/\s+$//;
89: $status = $image->Read($not_eps);
90: if ($status) {print " $status ";}
91: $image->Set(page => '+100+200');
92: $status = $image->Write($eps_f);
93: if ($status) {print " $status ";}
1.20 sakharuk 94: #check is eps exist in prtspool
95: if(not -e $eps_f) {
96: for (my $i=0;$i<10000;$i++) {
97: if (-e $eps_f.'.'.$i) {
98: rename $eps_f.'.'.$i, $eps_f;
99: last;
100: }
101: }
102: }
1.14 sakharuk 103: }
104: }
105: }
106: #print "$texfile\n"; #name of the tex file for debugging only
1.29 sakharuk 107: if (-e $texfile) {
108: $texfile =~ m/^(.*)\/([^\/]+)$/;
109: my $name_file = $2;
110: my $path_file = $1.'/';
111: chdir $path_file;
112: system("latex $name_file 1>/dev/null 2>/dev/null");
1.40 sakharuk 113: if ($tableofcontents eq 'yes') {
114: system("latex $name_file 1>/dev/null 2>/dev/null");
115: } #to create table of contents
1.33 sakharuk 116: my $idxname=$name_file;
117: $idxname=~s/\.tex$/\.idx/;
1.40 sakharuk 118: if ($tableofindex eq 'yes') {
1.33 sakharuk 119: system("makeindex $idxname");
120: system("latex $name_file 1>/dev/null 2>/dev/null");
121: } #to create index
1.29 sakharuk 122: #Do we have a latex error in the log file?
123: my $logfilename = $texfile;
124: $logfilename =~ s/\.tex$/\.log/;
125: my $temporary_file=IO::File->new($logfilename) || die "Couldn't open file for reading: $!\n";
126: my @content_of_file = <$temporary_file>;
127: close $temporary_file;
1.30 sakharuk 128: my $body_log_file = join(' ',@content_of_file);
129: $logfilename =~ s/\.log$/\.html/;
130: $temporary_file = IO::File->new('>'.$logfilename);
131: print $temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_log_file.'</pre></body></html>'."\n";
1.29 sakharuk 132: if ($body_log_file=~m/!\s+Emergency stop/) {
133: #LaTeX failed to parse tex file
134: print "<h2>LaTeX could not successfully parse your tex file.</h2>";
135: print "It probably has errors in it.<br />";
136: my $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART';
137: my $whereitends = rindex $body_log_file,'STAMPOFPASSEDRESOURCEEND';
138: if ($whereitbegins!=-1 and $whereitends!=-1) {
139: print "With very high probability this error occured in ".substr($body_log_file,$whereitbegins+26,$whereitends-$whereitbegins-26)."<br /><br />";
140: }
141: print "Here are the error messages in the LaTeX log file</br><br />";
142: my $sygnal = 0;
143: for (my $i=0;$i<=$#content_of_file;$i++) {
144: if ($content_of_file[$i]=~m/^Runaway argument?/ or $content_of_file[$i]=~m/^!/) {
145: $sygnal = 1;
146: }
147: if ($content_of_file[$i]=~m/Here is how much of/) {
148: $sygnal = 0;
149: }
150: if ($sygnal) {
151: print "$content_of_file[$i]<br />";
152: }
1.36 sakharuk 153: }
154: if ($advans_role) {
1.35 sakharuk 155: print "<b><big>The link to ";
156: $logfilename=~s/\/home\/httpd//;
157: print "<a href=\"$logfilename\">Your log file </a></big></b>";
158: print "\n";
159: #link tooriginal LaTeX file (included according Michael Hamlin desire)
160: my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open file for reading: $!\n";
161: my @tex_content_of_file = <$tex_temporary_file>;
162: close $tex_temporary_file;
163: my $body_tex_file = join(' ',@tex_content_of_file);
164: $texfile =~ s/\.tex$/aaaaa\.html/;
165: $tex_temporary_file = IO::File->new('>'.$texfile);
166: print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
167: print "<br /><br />";
168: print "<b><big>The link to ";
169: $texfile=~s/\/home\/httpd//;
170: print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
171: print "\n";
1.36 sakharuk 172: }
1.35 sakharuk 173:
1.29 sakharuk 174: } elsif ($body_log_file=~m/<inserted text>/) {
175: my $whereitbegins = index $body_log_file,'<inserted text>';
1.31 sakharuk 176: print "You are running LaTeX in the <b>batch mode</b>.";
1.30 sakharuk 177: while ($whereitbegins != -1) {
178: my $tempobegin=$whereitbegins;
179: $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART',$whereitbegins;
180: my $whereitends = index $body_log_file,'STAMPOFPASSEDRESOURCEEND',$whereitbegins;
1.34 sakharuk 181: 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 182: 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";
183: $whereitbegins = index $body_log_file,'<inserted text>',$tempobegin+10;
184: }
1.29 sakharuk 185: $name_file =~ s/\.tex/\.dvi/;
186: my $new_name_file = $name_file;
187: $new_name_file =~ s/\.dvi/\.ps/;
188: my $comma = "dvips -Ppdf -G0 -o $new_name_file";
189: system("$comma $name_file 1>/dev/null 2>/dev/null");
190: if (-e $new_name_file) {
191: print "<h1>PDF output file (see link below)</h1>\n";
192: $new_name_file =~ m/^(.*)\./;
193: my $tempo_file = $1.'temporar.ps';
194: my $name_file = $1.'.pdf';
195: if ($laystyle eq 'album' and $numberofcolumns eq '2') {
196: $comma = "psnup -2 -s1.0 $new_name_file";
197: system("$comma $tempo_file 1>/dev/null 2>/dev/null");
198: system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
199: } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
200: $comma = 'pstops -pletter "2:0+1(0.48w,0)"';
201: system("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null");
202: system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
203: } else {
204: system("ps2pdf $new_name_file $name_file 1>/dev/null 2>/dev/null");
205: }
206: my $texlog = $texfile;
207: my $texaux = $texfile;
208: my $texdvi = $texfile;
209: my $texps = $texfile;
210: $texlog =~ s/\.tex/\.log/;
211: $texaux =~ s/\.tex/\.aux/;
212: $texdvi =~ s/\.tex/\.dvi/;
213: $texps =~ s/\.tex/\.ps/;
1.30 sakharuk 214: my @garb = ($texaux,$texdvi,$texps);
1.29 sakharuk 215: # unlink @garb;
216: unlink $duefile;
217: print "<a href=\"/prtspool/$name_file\">Your PDF document</a>";
1.36 sakharuk 218: if ($advans_role) {
219: print "<br /><br />";
220: print "<b><big>The link to ";
221: $logfilename=~s/\/home\/httpd//;
222: print "<a href=\"$logfilename\">Your log file </a></big></b>";
223: print "\n";
224: #link tooriginal LaTeX file (included according Michael Hamlin desire)
225: my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open file for reading: $!\n";
226: my @tex_content_of_file = <$tex_temporary_file>;
227: close $tex_temporary_file;
228: my $body_tex_file = join(' ',@tex_content_of_file);
229: $texfile =~ s/\.tex$/aaaaa\.html/;
230: $tex_temporary_file = IO::File->new('>'.$texfile);
231: print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
232: print "<br /><br />";
233: print "<b><big>The link to ";
234: $texfile=~s/\/home\/httpd//;
235: print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
236: print "\n";
237: }
1.29 sakharuk 238: }
239: } else {
240: #LaTeX successfully parsed tex file
241: $name_file =~ s/\.tex/\.dvi/;
242: my $new_name_file = $name_file;
243: $new_name_file =~ s/\.dvi/\.ps/;
244: my $comma = "dvips -Ppdf -G0 -o $new_name_file";
245: system("$comma $name_file 1>/dev/null 2>/dev/null");
246: if (-e $new_name_file) {
247: print "<h1>Successfully created PDF output file (see link below)</h1>\n";
248: $new_name_file =~ m/^(.*)\./;
249: my $tempo_file = $1.'temporar.ps';
250: my $name_file = $1.'.pdf';
251: if ($laystyle eq 'album' and $numberofcolumns eq '2') {
252: $comma = "psnup -2 -s1.0 $new_name_file";
253: system("$comma $tempo_file 1>/dev/null 2>/dev/null");
254: system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
255: } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
256: $comma = 'pstops -pletter "2:0+1(0.48w,0)"';
257: system("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null");
258: system("ps2pdf $tempo_file $name_file 1>/dev/null 2>/dev/null");
259: } else {
260: system("ps2pdf $new_name_file $name_file 1>/dev/null 2>/dev/null");
261: }
262: my $texlog = $texfile;
263: my $texaux = $texfile;
264: my $texdvi = $texfile;
265: my $texps = $texfile;
266: $texlog =~ s/\.tex/\.log/;
267: $texaux =~ s/\.tex/\.aux/;
268: $texdvi =~ s/\.tex/\.dvi/;
269: $texps =~ s/\.tex/\.ps/;
270: my @garb = ($texlog,$texaux,$texdvi,$texps);
1.22 sakharuk 271: # unlink @garb;
1.29 sakharuk 272: unlink $duefile;
273: print "<a href=\"/prtspool/$name_file\">Your PDF document</a>";
274: print "\n";
275: }
1.14 sakharuk 276: }
1.29 sakharuk 277: } else {
278: print "LaTeX file $texfile was not created successfully";
1.14 sakharuk 279: }
1.17 sakharuk 280:
281:
282:
1.1 sakharuk 283:
284:
285:
286:
1.4 sakharuk 287:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>