| 1 | #!/usr/local/bin/perl |
|
|
| 2 | |
|
|
| 3 | ################################################################################ |
1 | ################################################################################ |
| 4 | # WeBWorK |
2 | # WeBWorK mod_perl (c) 2000-2002 WeBWorK Project |
| 5 | # |
3 | # $Id$ |
| 6 | # Copyright (c) 1995-2002 University of Rochester |
|
|
| 7 | # All rights reserved |
|
|
| 8 | # |
|
|
| 9 | ################################################################################ |
4 | ################################################################################ |
| 10 | |
5 | |
| 11 | use lib '.'; use webworkInit; # WeBWorKInitLine |
6 | package WeBWorK::PG::ImageGenerator; |
|
|
7 | |
|
|
8 | =head1 NAME |
|
|
9 | |
|
|
10 | WeBWorK::PG::ImageGenerator - create an object for holding bits of math |
|
|
11 | for LaTeX, and then to process them later. |
|
|
12 | |
|
|
13 | =head1 SYNPOSIS |
|
|
14 | |
|
|
15 | my $imgen = new ImageGenerator; #create a image generator |
|
|
16 | |
|
|
17 | $imgen->initialize(\%envir); # provide basic data in preparation of image collection |
|
|
18 | |
|
|
19 | $imgen->add(string); # add a new LaTeX string to be processed. |
|
|
20 | # Should be in math mode with \( or \[ |
|
|
21 | # It returns the html tag |
|
|
22 | |
|
|
23 | $imgen->getCount(); # Returns the number of images which have been added |
|
|
24 | $imgen->tmpurl(); # Returns the beginning of the html path |
|
|
25 | |
|
|
26 | $imgen->render(); # Generates the images. By default, we reuse old |
|
|
27 | # images when reasonable, unless passed a flag |
|
|
28 | # refresh=>'yes' (or 1) |
|
|
29 | |
|
|
30 | =cut |
| 12 | |
31 | |
| 13 | use strict; |
32 | use strict; |
|
|
33 | use warnings; |
| 14 | |
34 | |
| 15 | =head1 NAME |
35 | =head1 METHODS |
| 16 | |
36 | |
| 17 | ImageGenerator.pm |
37 | =over |
| 18 | |
38 | |
| 19 | =head1 SYNPOSIS |
39 | =item new |
| 20 | |
40 | |
| 21 | Create an object for holding bits of math for LaTeX, and then to process |
41 | Creates the ImageGenerator object. |
| 22 | them later. |
|
|
| 23 | |
42 | |
| 24 | my $imgen = new ImageGenerator; #create a image generator |
43 | =back |
| 25 | $imgen->initialize(\%envir); # provide basic data in preparation of image collection |
|
|
| 26 | $imgen->add(string); # add a new LaTeX string to be processed. |
|
|
| 27 | # Should be in math mode with \( or \[ |
|
|
| 28 | # It returns the html tag |
|
|
| 29 | |
|
|
| 30 | $imgen->getCount(); # Returns the number of images which have been added |
|
|
| 31 | $imgen->tmpurl(); # Returns the beginning of the html path |
|
|
| 32 | $imgen->render(); # Generates the images. By default, we reuse old |
|
|
| 33 | # images when reasonable, unless passed a flag |
|
|
| 34 | # refresh=>'yes' (or 1) |
|
|
| 35 | |
|
|
| 36 | =head1 DESCRIPTION |
|
|
| 37 | |
|
|
| 38 | This module defines an object which holds bits of LaTeX math for |
|
|
| 39 | generating images. |
|
|
| 40 | |
44 | |
| 41 | =cut |
45 | =cut |
| 42 | |
46 | |
| 43 | |
|
|
| 44 | package ImageGenerator; |
|
|
| 45 | |
|
|
| 46 | |
|
|
| 47 | =head2 new |
|
|
| 48 | Creates the ImageGenerator object. |
|
|
| 49 | |
|
|
| 50 | =cut |
|
|
| 51 | |
|
|
| 52 | |
|
|
| 53 | sub new { |
47 | sub new { |
| 54 | my $class = shift; |
48 | my $class = shift; |
| 55 | my $self = { |
49 | my $self = { |
| 56 | latexlines => [], |
50 | latexlines => [], |
| 57 | count => 0, |
51 | count => 0, |
| 58 | tmppath => "", |
52 | tmppath => "", |
| 59 | tmpURLstart=>"", |
53 | tmpURLstart=>"", |
| 60 | filenamestart=> "" |
54 | filenamestart=> "" |
| 61 | }; |
55 | }; |
| 62 | |
56 | |
| 63 | bless $self, $class; |
57 | bless $self, $class; |
| 64 | } |
58 | } |
| 65 | |
|
|
| 66 | |
59 | |
| 67 | sub initialize { |
60 | sub initialize { |
| 68 | my $self = shift; |
61 | my $self = shift; |
| 69 | my $envir = shift; # pointer to problem envirment hash |
62 | my $envir = shift; # pointer to problem envirment hash |
|
|
63 | |
| 70 | my $problemnum = $envir->{'probNum'}; |
64 | my $problemnum = $envir->{'probNum'}; |
| 71 | my $studname = $envir->{'studentLogin'}; |
65 | my $studname = $envir->{'studentLogin'}; |
| 72 | my $psvn = $envir->{'psvn'}; |
66 | my $psvn = $envir->{'psvn'}; |
| 73 | my $setname = $envir->{'setNumber'}; |
67 | my $setname = $envir->{'setNumber'}; |
| 74 | my $tmpURLstart = $envir->{'tempURL'}; |
68 | my $tmpURLstart = $envir->{'tempURL'}; |
|
|
69 | |
| 75 | my $path=main::surePathToTmpFile(main::convertPath("png/$setname/$psvn/foo")); |
70 | my $path=main::surePathToTmpFile(main::convertPath("png/$setname/$psvn/foo")); |
| 76 | $path =~ s/foo$//; # remove final foo |
71 | $path =~ s/foo$//; # remove final foo |
|
|
72 | |
| 77 | $self->{sourceFile} = $envir->{templateDirectory} . "/" . |
73 | $self->{sourceFile} = $envir->{templateDirectory} . "/" . $envir->{fileName}; |
| 78 | $envir->{fileName}; |
|
|
| 79 | $self->{tmpURLstart} = $tmpURLstart."png/$setname/$psvn"; |
74 | $self->{tmpURLstart} = $tmpURLstart."png/$setname/$psvn"; |
| 80 | $self->{tmppath}=$path; |
75 | $self->{tmppath}=$path; |
| 81 | $self->{filenamestart}="$studname-prob${problemnum}image"; |
76 | $self->{filenamestart}="$studname-prob${problemnum}image"; |
| 82 | } |
77 | } |
| 83 | |
78 | |
| 84 | # Add another string to list to be LaTeX'ed |
79 | # Add another string to list to be LaTeX'ed |
| 85 | # return the tag |
80 | # return the tag |
| 86 | sub add { |
81 | sub add { |
| … | |
… | |
| 101 | $tag =~ s|^\\\[ *||; |
96 | $tag =~ s|^\\\[ *||; |
| 102 | $tag =~ s|\\\]$||; |
97 | $tag =~ s|\\\]$||; |
| 103 | $newstr = '\(\displaystyle{'.$tag.'}\)'; |
98 | $newstr = '\(\displaystyle{'.$tag.'}\)'; |
| 104 | $tag = qq!<div align="center"><img src="$tempURL" align="middle" alt="$tag"></div>!; |
99 | $tag = qq!<div align="center"><img src="$tempURL" align="middle" alt="$tag"></div>!; |
| 105 | } |
100 | } |
|
|
101 | |
| 106 | push @{$self->{latexlines}}, $newstr; |
102 | push @{$self->{latexlines}}, $newstr; |
| 107 | |
|
|
| 108 | return $tag; |
103 | return $tag; |
| 109 | } |
104 | } |
| 110 | |
105 | |
| 111 | sub getCount { |
106 | sub getCount { |
| 112 | my $self = shift; |
107 | my $self = shift; |
| … | |
… | |
| 119 | } |
114 | } |
| 120 | |
115 | |
| 121 | sub render { |
116 | sub render { |
| 122 | my $self = shift; |
117 | my $self = shift; |
| 123 | my %opts = @_; |
118 | my %opts = @_; |
| 124 | |
119 | |
| 125 | if($self->{count}==0) {return ;} # Don't run latex if there are no images |
120 | # Don't run latex if there are no images |
| 126 | |
121 | if($self->{count}==0) { |
|
|
122 | return; |
|
|
123 | } |
|
|
124 | |
| 127 | my $refreshMe = 0; |
125 | my $refreshMe = 0; |
| 128 | if (defined($opts{refresh}) and |
|
|
| 129 | (($opts{refresh} eq "yes") or ($opts{refresh} == 1))) { |
126 | if (defined($opts{refresh}) and (($opts{refresh} eq "yes") or ($opts{refresh} == 1))) { |
| 130 | $refreshMe = 1; |
127 | $refreshMe = 1; |
| 131 | } |
128 | } |
| 132 | |
129 | |
| 133 | # $refreshMe = 1; # Uncomment for testing |
130 | #$refreshMe = 1; # Uncomment for testing |
| 134 | my $latexfilenamebase = $self->{tmppath}.$self->{filenamestart}; |
131 | my $latexfilenamebase = $self->{tmppath} . $self->{filenamestart}; |
| 135 | |
132 | |
| 136 | my $sourcePath = $self->{sourceFile}; |
133 | my $sourcePath = $self->{sourceFile}; |
| 137 | my $tempFile = "${latexfilenamebase}".$self->{count}.".png"; # last image |
134 | my $tempFile = "${latexfilenamebase}" . $self->{count} . ".png"; # last image |
| 138 | |
135 | |
| 139 | if ($refreshMe or not -e $tempFile |
136 | if ($refreshMe or not -e $tempFile or (stat $sourcePath)[9] > (stat $tempFile)[9]) { |
| 140 | or (stat $sourcePath)[9] > (stat $tempFile)[9]) { |
|
|
| 141 | # image file doesn't exist, or source file is newer then image file |
137 | # image file doesn't exist, or source file is newer then image file |
| 142 | # or we just want new images produced |
138 | # or we just want new images produced |
| 143 | |
139 | |
| 144 | # my $old_cdir = `pwd`; # cd for running latex |
140 | #my $old_cdir = `pwd`; # cd for running latex |
| 145 | # chomp($old_cdir); |
141 | #chomp($old_cdir); |
| 146 | chdir($self->{tmppath}) || |
142 | chdir($self->{tmppath}) |
| 147 | warn "Could not move into temporary directory $self->{tmppath}"; |
143 | || warn "Could not move into temporary directory $self->{tmppath}"; |
| 148 | |
144 | |
| 149 | if (-e "$latexfilenamebase.tex") { |
145 | if (-e "$latexfilenamebase.tex") { |
| 150 | unlink("$latexfilenamebase.tex") || |
146 | unlink("$latexfilenamebase.tex") || |
| 151 | warn "Could not delete old LaTeX file"; |
147 | warn "Could not delete old LaTeX file"; |
| 152 | } |
148 | } |
| … | |
… | |
| 195 | unlink("$self->{filenamestart}.dvi","$self->{filenamestart}.log", |
191 | unlink("$self->{filenamestart}.dvi","$self->{filenamestart}.log", |
| 196 | "$self->{filenamestart}.tex", |
192 | "$self->{filenamestart}.tex", |
| 197 | "$self->{filenamestart}.aux" |
193 | "$self->{filenamestart}.aux" |
| 198 | ); |
194 | ); |
| 199 | } |
195 | } |
| 200 | # chdir($old_cdir); |
196 | #chdir($old_cdir); |
| 201 | } |
197 | } |
| 202 | } |
198 | } |
| 203 | |
199 | |
| 204 | 1; |
200 | 1; |