[system] / trunk / webwork2 / lib / WebworkWebservice / MathTranslators.pm Repository:
ViewVC logotype

Annotation of /trunk/webwork2/lib/WebworkWebservice/MathTranslators.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3065 - (view) (download) (as text)

1 : gage 3065 #!/usr/local/bin/perl -w
2 :    
3 :     # Copyright (C) 2002 Michael Gage
4 :    
5 :     ###############################################################################
6 :     # Web service which translates TeX to pdf or to HTML
7 :     ###############################################################################
8 :    
9 :     #use lib '/home/gage/webwork/pg/lib';
10 :     #use lib '/home/gage/webwork/webwork-modperl/lib';
11 :    
12 :     package WebworkWebserice::MathTranslators;
13 :     use WebworkWebservice;
14 :     use base qw(WebworkWebservice);
15 :    
16 :     use strict;
17 :     use sigtrap;
18 :     use Carp;
19 :     use Safe;
20 :     use Apache;
21 :     use WeBWorK::PG::Translator;
22 :     use WeBWorK::PG::IO;
23 :     use Benchmark;
24 :     use MIME::Base64 qw( encode_base64 decode_base64);
25 :    
26 :    
27 :    
28 :     our $WW_DIRECTORY = $WebworkWebservice::WW_DIRECTORY;
29 :     our $PG_DIRECTORY = $WebworkWebservice::PG_DIRECTORY;
30 :     our $COURSENAME = $WebworkWebservice::COURSENAME;
31 :     our $HOST_NAME = $WebworkWebservice::HOST_NAME;
32 :    
33 :     our $ce = $WebworkWebservice::SeedCE;
34 :    
35 :    
36 :    
37 :    
38 :     my $debugOn = 1;
39 :     my $PASSWORD = $WebworkWebservice::PASSWORD;
40 :    
41 :    
42 :     my $TEMPDIRECTORY = $ce->{webworkDirs}->{htdocs_temp};
43 :     my $TEMP_BASE_URL = $ce->{webworkURLs}->{htdocs_temp};
44 :     my $externalLatexPath = $ce->{externalPrograms}->{latex};
45 :     # my $externalDvipsPath = $Global::externalDvipsPath;
46 :     my $externalpdflatexPath = $ce->{externalPrograms}->{pdflatex};;
47 :     # my $externalGsPath = $Global::externalGsPath;
48 :    
49 :    
50 :     # variables formerly set in Global
51 :     $Global::tmp_directory_permission = 0775;
52 :     $Global::numericalGroupID='100'; # group ID for wwdev
53 :    
54 :     my $tmp_directory_permission = $Global::tmp_directory_permission;
55 :     my $numericalGroupID = $Global::numericalGroupID; # group ID for webadmin
56 :    
57 :     sub tex2pdf {
58 :    
59 :     my $rh = shift;
60 :     local($|) = 1;
61 :     my $out = {};
62 :     unless ($rh->{pw} eq $PASSWORD ) {
63 :     $out->{error} = 404;
64 :     return($out);
65 :     }
66 :     #obtain the path to the file
67 :     my $filePath = $rh->{fileName};
68 :     my @pathElements = split("/", $filePath);
69 :    
70 :     # grab the last element as the fileName
71 :     #remove the extension from the file name
72 :     my $fileName = pop @pathElements;
73 :     $fileName =~ s/\.pg$//;
74 :    
75 :     #Create the full url and the full directory path -- If pathElements is empty this can give an extra //? Should I worry? maybe
76 :     my $url = $TEMP_BASE_URL. join("/",@pathElements);
77 :     $url .= '/' unless $url =~ m|/$|; # only add / if it is needed.
78 :     $url .= "$fileName.pdf";
79 :     my $texFileBasePath = $TEMPDIRECTORY.join("/",@pathElements);
80 :     $texFileBasePath .= '/' unless $texFileBasePath =~ m|/$|;
81 :     $texFileBasePath .= "$fileName";
82 :    
83 :     # create the intermediate directories if they don't exists
84 :     # create a dummy .pdf file
85 :     surePathToTmpFile2($texFileBasePath.'.pdf');
86 :     # my $filePermission = '0775';
87 :     # chmod("$filePermission","$texFileBasePath.pdf")
88 :     # or die "Can't change file permissions on $texFileBasePath.pdf to $filePermission";
89 :    
90 :     # Decode and cleanup the tex string
91 :     my $texString = decode_base64( $rh->{'texString'});
92 :     #Make sure the line endings are correct
93 :     $texString=~s/\r\n/\n/g;
94 :     $texString=~s/\r/\n/g;
95 :    
96 :     # Make sure that TeX is run in batchmode so that the tex program doesn't hang on errors
97 :     $texString = "\\batchmode\n".$texString; # force errors to log.
98 :    
99 :     # Remove any old files
100 :     unlink("$texFileBasePath.tex","$texFileBasePath.dvi","$texFileBasePath.log","$texFileBasePath.aux",
101 :     "$texFileBasePath.ps","$texFileBasePath.pdf");
102 :     # Create the texfile of the problem.
103 :     local(*TEX);
104 :     open(TEX,"> $texFileBasePath.tex") or die "Can't open $texFileBasePath.tex to store tex code";
105 :     local($/)=undef;
106 :     print TEX $texString;
107 :     close(TEX);
108 :    
109 :    
110 :     # my $dviCommandLine = "$externalLatexPath $texFileBasePath.tex";# >/dev/null 2>/dev/null";
111 :     # my $psCommandLine = "$externalDvipsPath -o $texFileBasePath.ps $texFileBasePath.dvi >/dev/null";# 2>/dev/null";
112 :     # my $pdfCommandLine = "$externalGsPath -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=$texFileBasePath.pdf -c save pop -f $texFileBasePath.ps";
113 :     # print "dviCommandLine: $dviCommandLine\n";
114 :     # print "psCommandLine: $psCommandLine\n";
115 :     # print "pdfCommandLine: $pdfCommandLine\n";
116 :     #print "execute pdflatex", `$externalpdflatexPath $texFileBasePath.tex`, "\n";
117 :     #print "done $externalpdflatexPath $texFileBasePath.tex\n";
118 :     # Change to the working directory and create the pdf files.
119 :     my $wd = $TEMPDIRECTORY.join("/",@pathElements); # working directory
120 :     # print "---cd $wd && $externalpdflatexPath $fileName.tex\n";
121 :    
122 :     system "cd $wd && $externalpdflatexPath $fileName.tex >>$fileName.log";
123 :     chmod 0777, "$texFileBasePath.pdf";
124 :     unless ($debugOn) {
125 :     unlink("$texFileBasePath.tex","$texFileBasePath.log","$texFileBasePath.aux",
126 :     );
127 :     }
128 :     return({pdfURL => $url});
129 :    
130 :    
131 :    
132 :    
133 :     }
134 :    
135 :     sub surePathToTmpFile2 { # constructs intermediate directories if needed beginning at ${Global::htmlDirectory}tmp/
136 :     # the input path must be either the full path, or the path relative to this tmp sub directory
137 :     my $path = shift;
138 :     my $delim = getDirDelim();
139 :     my $tmpDirectory = $TEMPDIRECTORY;
140 :     # if the path starts with $tmpDirectory (which is permitted but optional) remove this initial segment
141 :     print "Original path $path\n";
142 :     $path =~ s|^$tmpDirectory|| if $path =~ m|^$tmpDirectory|;
143 :     $path = convertPath($path);
144 :     print "Creating path to $path using $delim\n";
145 :     # find the nodes on the given path
146 :     my @nodes = split("$delim",$path);
147 :     # create new path
148 :     $path = convertPath("$tmpDirectory");
149 :     print "Creating path: $path\n ";
150 :     while (@nodes>1 ) {
151 :    
152 :     $path = convertPath($path . shift (@nodes) ."/");
153 :     print "Creating path: $path\n ";
154 :     unless (-e $path) {
155 :     # system("mkdir $path");
156 :     createDirectory($path,$tmp_directory_permission, $numericalGroupID) ||
157 :     die "Failed to create directory $path";
158 :    
159 :     }
160 :    
161 :     }
162 :     $path = convertPath($path . shift(@nodes));
163 :     print "Creating path: $path\n ";
164 :     # system(qq!echo "" > $path! );
165 :    
166 :     $path;
167 :    
168 :     }
169 :    
170 :    
171 :     sub pretty_print_rh {
172 :     my $rh = shift;
173 :     my $out = "";
174 :     my $type = ref($rh);
175 :     if ( ref($rh) =~/HASH/ ) {
176 :     foreach my $key (sort keys %{$rh}) {
177 :     $out .= " $key => " . pretty_print_rh( $rh->{$key} ) . "\n";
178 :     }
179 :     } elsif ( ref($rh) =~ /SCALAR/ ) {
180 :     $out = "scalar reference ". ${$rh};
181 :     } elsif ( ref($rh) =~/Base64/ ) {
182 :     $out .= "base64 reference " .$$rh;
183 :     } else {
184 :     $out = $rh;
185 :     }
186 :     if (defined($type) ) {
187 :     $out .= "type = $type \n";
188 :     }
189 :     return $out;
190 :     }
191 :    
192 :    
193 :     1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9