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