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