[system] / trunk / xmlrpc / daemon / MathTranslators.pm Repository:
ViewVC logotype

Annotation of /trunk/xmlrpc/daemon/MathTranslators.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9