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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9