1: # The LearningOnline Network with CAPA
2: # TeX Conversion Module
3: #
4: # $Id: lontexconvert.pm,v 1.72 2006/04/11 18:42:43 albertel Exp $
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: # Copyright for TtHfunc and TtMfunc by Ian Hutchinson.
29: # TtHfunc and TtMfunc (the "Code") may be compiled and linked into
30: # binary executable programs or libraries distributed by the
31: # Michigan State University (the "Licensee"), but any binaries so
32: # distributed are hereby licensed only for use in the context
33: # of a program or computational system for which the Licensee is the
34: # primary author or distributor, and which performs substantial
35: # additional tasks beyond the translation of (La)TeX into HTML.
36: # The C source of the Code may not be distributed by the Licensee
37: # to any other parties under any circumstances.
38: #
39:
40: package Apache::lontexconvert;
41:
42: use strict;
43: use tth();
44: use vars qw($errorstring);
45: use Apache::lonmsg();
46: use Apache::lonxml();
47: use Apache::lonmenu();
48: use Apache::lonlocal;
49: use Apache::lonnet;
50:
51: # ====================================================================== Header
52:
53: sub init_tth {
54: my $options=$env{'course.'.$env{'request.course.id'}.'.tthoptions'};
55: if ($env{'browser.mathml'}) {
56: &tth::ttminit();
57: if ($env{'browser.unicode'}) {
58: &tth::ttmoptions('-L -u1 '.$options);
59: } else {
60: &tth::ttmoptions('-L -u0 '.$options);
61: }
62: } else {
63: &tth::tthinit();
64: if ($env{'browser.unicode'}) {
65: &tth::tthoptions('-L -u1 '.$options);
66: } else {
67: &tth::tthoptions('-L -u0 '.$options);
68: }
69: }
70: }
71:
72: # ================================================================== Conversion
73:
74: $Apache::lontexconvert::messedup=0;
75:
76: # we need this routine because &converted can get called from inside
77: # of the safespace (through &xmlparse('<m>stuff</m>') which doesn't
78: # allow the opcode for alarm, so we need to compile this before we get
79: # into the safe space since opcode checks only occur at compile time
80: sub convert_real {
81: my ($texstring)=@_;
82: my ($xmlstring,$errorstring);
83: local $SIG{SEGV}=sub { $Apache::lontexconvert::messedup=1; die; };
84: local $SIG{ALRM}=sub {
85: &Apache::lonnet::logthis("ALRM");
86: $xmlstring='['.&mt("TeX unconverted due to errors").']';
87: $Apache::lontexconvert::messedup=1;
88: die &mt("TeX unconverted due to errors");
89: };
90: &Apache::lonxml::start_alarm();
91: if ($env{'browser.mathml'}) {
92: $xmlstring=&tth::ttm($$texstring);
93: $xmlstring=~s/\<math\>/\<math xmlns=\"\&mathns\;\"\>/g;
94: $xmlstring=~s/\<br\>/\<br\/\>/g;
95: $xmlstring=~s/\<p\>/\<p\>\<\/p\>/g;
96: $errorstring.=&tth::ttmerror();
97: } else {
98: $xmlstring=&tth::tth($$texstring);
99: $errorstring.=&tth::ttherror();
100: $xmlstring=~s-</font(\s*)>-</font>-g;
101: }
102: $xmlstring=~s/^\s*\<br clear\=\"all\"/\<br/s;
103: $xmlstring=~s/^\s*//;
104: $xmlstring=~s/\s*$//;
105: &Apache::lonxml::end_alarm();
106: return ($xmlstring,$errorstring);
107: }
108:
109: sub tth_converted {
110: my $texstring=shift;
111: my $xmlstring='['.&mt('UNDISPLAYABLE').']';
112: if ($Apache::lontexconvert::messedup) {
113: return '['.&mt('TeX unconverted due to previous errors').']';
114: }
115: $$texstring ='\\documentstyle{article}'.$$texstring;
116:
117: eval(<<'ENDCONV');
118: ($xmlstring,$errorstring)=&convert_real($texstring)
119: ENDCONV
120: if ($@) {
121: $errorstring.=&mt("Evaluation Error: ").$@;
122: $Apache::lontexconvert::messedup=1;
123: }
124: if ($Apache::lontexconvert::messedup || &tth::tthmessedup() ||
125: $errorstring) {
126: &Apache::lonnet::logthis("Trying to kill myself");
127: $Apache::lontexconvert::messedup=1;
128: if (ref($Apache::lonxml::request)) {
129: $Apache::lonxml::request->child_terminate();
130: } else {
131: my $request;
132: eval { $request=Apache->request; };
133: if (!$request) {
134: eval { $request=Apache2::RequestUtil->request; };
135: }
136: if ($request) {
137: $request->child_terminate();
138: } else {
139: &Apache::lonnet::logthis("Unable to find a request to do child_terminate on");
140: }
141: }
142: }
143: return $xmlstring;
144: }
145:
146: sub clean_out_math_mode {
147: my ($texstring)=@_;
148: $$texstring=~s/(?!\\)\$//g;
149: $$texstring=~s/\\[\)\(\]\[]//g;
150: $$texstring=~s/\\ensuremath//g;
151: return '';
152: }
153:
154: sub displaystyle {
155: my ($texstring)=@_;
156: #has a $$ or \[ or \displaystyle in it, guessinng it's display mode
157: if ($$texstring=~/[^\\]\$\$/ ||
158: $$texstring=~/\\\[/ ||
159: $$texstring=~/\\displaystyle/) { return 1; }
160: return 0;
161: }
162:
163: sub jsMath_converted {
164: my $texstring=shift;
165: my $tag='span';
166: if (&displaystyle($texstring)) { $tag='div'; }
167: &clean_out_math_mode($texstring);
168: return &jsMath_header().
169: '<'.$tag.' class="math">'.$$texstring.'</'.$tag.'>';
170: }
171:
172: {
173: my $jsMath_sent_header;
174: sub jsMath_reset {
175: $jsMath_sent_header=0;
176: }
177: sub jsMath_header {
178: return '' if $jsMath_sent_header;
179: $jsMath_sent_header=1;
180: return
181: '<script type="text/javascript">
182: function NoFontMessage () {}
183: </script>'."\n".
184: '<script type="text/javascript" src="/adm/jsMath/jsMath.js"></script>'."\n";
185: }
186: sub jsMath_process {
187: return '' if !$jsMath_sent_header;
188: return '<script type="text/javascript">jsMath.Process()</script>';
189: }
190: }
191:
192: sub init_math_support {
193: &Apache::lontexconvert::jsMath_reset();
194: if ($env{'environment.texengine'} eq 'jsMath' ||
195: $env{'form.texengine'} eq 'jsMath' ) {
196: return &Apache::lontexconvert::jsMath_header();
197: }
198: return;
199: }
200:
201: sub mimetex_converted {
202: my $texstring=shift;
203: my $displaystyle=&displaystyle($texstring);
204:
205: &clean_out_math_mode($texstring);
206:
207: if ($displaystyle) {
208: $$texstring='\\displaystyle \\Large '.$$texstring;
209: }
210: my $result='<img src="/cgi-bin/mimetex.cgi?'.&Apache::lonnet::escape($$texstring).'" />';
211: if ($displaystyle) {
212: $result='<center>'.$result.'</center>';
213: }
214: return $result;
215: }
216:
217: sub converted {
218: my ($string,$mode)=@_;
219: if ($mode eq '') { $mode=$env{'environment.texengine'}; }
220: if ($mode =~ /tth/i) {
221: return &tth_converted($string);
222: } elsif ($mode =~ /jsmath/i) {
223: return &jsMath_converted($string);
224: } elsif ($mode =~ /mimetex/i) {
225: return &mimetex_converted($string);
226: }
227: return &tth_converted($string);
228: }
229:
230: # ------------------------------------------------------------ Message display
231:
232: sub to_convert {
233: my ($string) = @_;
234: $string=~s/\<br\s*\/?\>/ /gs;
235: # $string=~s/\s/ /gs;
236: $string=&HTML::Entities::decode($string);
237: return &converted(\$string);
238: }
239:
240: sub smiley {
241: my $expression=shift;
242: if ($env{'browser.imagesuppress'} eq 'on') { return $expression; }
243: my %smileys=('\:\-\)' => 'smiley',
244: '8\-\)' => 'coolsmile',
245: '8\-(I|\|)' => 'coolindiff',
246: ':\-(I|\|)' => 'neutral',
247: '\:\-(o|O|\(\))' => 'shocked',
248: ':\-\(' => 'frowny',
249: '\;\-\)' => 'wink',
250: '\:\-P' => 'baeh',
251: '\:\-(\\\|\\/)' => 'hrrm',
252: '\:\-D' => 'bigsmile',
253: '\:\-C' => 'angry',
254: '\:(\'|\`)\-\(' => 'cry',
255: '\:\-(X|\#)' => 'lipsrsealed',
256: '\:\-S' => 'huh');
257: my $iconpath=$Apache::lonnet::perlvar{'lonIconsURL'};
258: foreach (keys %smileys) {
259: $expression=~s/$_/\<img src="$iconpath\/$smileys{$_}.gif" \/\>/gs;
260: }
261: return $expression;
262: }
263:
264: sub msgtexconverted {
265: my ($message,$email) = @_;
266: $errorstring='';
267: &init_tth();
268: my $outmessage='';
269: my $tex=0;
270: foreach (split(/(?:\<\;|\<)\/*m\s*(?:\>\;|\>)/i,$message)) {
271: if ($tex) {
272: if ($email) {
273: $outmessage.='</pre><tt>'.&to_convert($_).'</tt><pre>'; $tex=0;
274: } else {
275: $outmessage.=&to_convert($_); $tex=0;
276: }
277: } else {
278: $outmessage.=&smiley($_); $tex=1;
279: }
280: }
281: $message=$outmessage; $outmessage=''; $tex=0;
282: foreach (split(/(?:\<\;|\<)\/*algebra\s*(?:\>\;|\>)/i,$message)) {
283: if ($tex) {
284: if ($email) {
285: $outmessage.='</pre><tt>'.&algebra($_,'web').'</tt><pre>'; $tex=0;
286: } else {
287: $outmessage.=&algebra($_,'web'); $tex=0;
288: }
289: } else {
290: $outmessage.=$_; $tex=1;
291: }
292: }
293: if (wantarray) {
294: return ($outmessage,$errorstring);
295: } else {
296: return $outmessage.$errorstring;
297: }
298: }
299:
300: sub algebra {
301: use AlgParser;
302:
303: my ($string,$target,$style)=@_;
304: my $parser = new AlgParserWithImplicitExpand;
305: $string=&prepare_algebra($string);
306: my $ret = $parser->parse($string);
307: my $result='['.&mt('Algebra unconverted due to previous errors').']';
308: if ( ref($ret) ) {
309: #$parser->tostring();
310: $parser->normalize();
311: my $latex=$parser->tolatex();
312: $latex=&postprocess_algebra($latex);
313: if ($style eq 'display') {
314: $latex='$$'.$latex.'$$x';
315: } else {
316: # style is 'inline'
317: $latex='\\ensuremath{'.$latex.'}';
318: }
319: if ($target eq 'web' || $target eq 'analyze') {
320: $result = &converted(\$latex);
321: } else {
322: $result = $latex;
323: }
324: } else {
325: &Apache::lonxml::error($parser->{'htmlerror'});
326: }
327: }
328:
329: sub prepare_algebra {
330: my ($string)=@_;
331:
332: # makes the decision about what is a minus sign easier supposedly
333: $string =~ s/(\<\>|\<\=|\>\=[\=\>\<] *)-/$1 zeroplace -/g;
334:
335: return $string;
336: }
337:
338: sub postprocess_algebra {
339: my ($string)=@_;
340:
341: # moodle had these and I don't know why, ignoring them for now
342: # $string =~s/\\fun/ /g;
343:
344: # remove the extra () in the denominator of a \frac
345: $string =~s/\\frac{(.+?)}{\\left\((.+?)\\right\)}/\\frac{$1}{$2}/gs;
346:
347: # sqrt(3,4) means the 4 root of 3
348: $string =~s/\\sqrt{([^,]+),([^\}]+)}/\\sqrt[$2]{$1}/gs;
349:
350: # log(3,4) means the log base 4 of 3
351: $string =~s/\\log\\left\((.+?),(.+?)\\right\)/\\log_{$2}\\left($1\\right)/gs;
352:
353: # log(3,4) means the log base 4 of 3
354: $string =~s/\\((?:sin|cos|tan|sec|csc|cot)(?:h)?)\\left\((.+?),(.+?)\\right\)/\\$1^{$3}\\left($2\\right)/gs;
355:
356: # int(3,a,b) integral from a to b of 3
357: $string =~s/\\int\\left\((.+?),(.+?),(.+?)\\right\)/\\int_{$2}^{$3}\\left($1\\right)/gs;
358:
359: # int( ... dx) -> ...
360: $string =~s/\\int\\left\((.+?)d[a-z]\\right\)/$1/gs;
361:
362: #
363: $string =~s/\\lim\\left\((.+?),(.+?),(.+?)\\right\)/\\lim_{$2\\to $3}$1/gs;
364: return $string;
365: }
366: 1;
367: __END__
368:
369:
370:
371:
372:
373:
374:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>