1: # The LearningOnline Network with CAPA
2: # TeX Conversion Module
3: #
4: # $Id: lontexconvert.pm,v 1.87 2008/02/06 10:16:00 foxr 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::lonxml();
46: use Apache::lonlocal;
47: use Apache::lonnet;
48: use lib '/home/httpd/lib/perl/';
49: use LONCAPA;
50:
51:
52: # ====================================================================== Header
53:
54: sub init_tth {
55: my $options=$env{'course.'.$env{'request.course.id'}.'.tthoptions'};
56: if ($options =~ /\S/) {
57: $options = ' '.$options;
58: } else {
59: undef($options);
60: }
61: if ($env{'browser.mathml'}) {
62: &tth::ttminit();
63: if ($env{'browser.unicode'}) {
64: &tth::ttmoptions('-L -u1'.$options);
65: } else {
66: &tth::ttmoptions('-L -u0'.$options);
67: }
68: } else {
69: &tth::tthinit();
70: if ($env{'browser.unicode'}) {
71: &tth::tthoptions('-L -u1'.$options);
72: } else {
73: &tth::tthoptions('-L -u0'.$options);
74: }
75: }
76: }
77:
78: # ================================================================== Conversion
79:
80: $Apache::lontexconvert::messedup=0;
81:
82: # we need this routine because &converted can get called from inside
83: # of the safespace (through &xmlparse('<m>stuff</m>') which doesn't
84: # allow the opcode for alarm, so we need to compile this before we get
85: # into the safe space since opcode checks only occur at compile time
86: sub convert_real {
87: my ($texstring)=@_;
88: my ($xmlstring,$errorstring);
89: local $SIG{SEGV}=sub { $Apache::lontexconvert::messedup=1; die; };
90: local $SIG{ALRM}=sub {
91: &Apache::lonnet::logthis("ALRM");
92: $xmlstring='['.&mt("TeX unconverted due to errors").']';
93: $Apache::lontexconvert::messedup=1;
94: die &mt("TeX unconverted due to errors");
95: };
96: &Apache::lonxml::start_alarm();
97: if ($env{'browser.mathml'}) {
98: $xmlstring=&tth::ttm($$texstring);
99: $xmlstring=~s/\<math\>/\<math xmlns=\"\&mathns\;\"\>/g;
100: $xmlstring=~s/\<br\>/\<br\/\>/g;
101: $xmlstring=~s/\<p\>/\<p\>\<\/p\>/g;
102: $errorstring.=&tth::ttmerror();
103: } else {
104: $xmlstring=&tth::tth($$texstring);
105: $errorstring.=&tth::ttherror();
106: $xmlstring=~s-</font(\s*)>-</font>-g;
107: }
108: $xmlstring=~s/^\s*\<br clear\=\"all\"/\<br/s;
109: $xmlstring=~s/^\s*//;
110: $xmlstring=~s/\s*$//;
111: #
112: # \rightleftharpoons is not converted by tth but maps
113: # reasonably well to ⇔. If we get many more of these,
114: # we're going to need to have a translation sub.
115: #
116: my $lrharpoon = pack("U", 0x21cc);
117: $xmlstring=~s/\\rightleftharpoons/$lrharpoon/g;
118:
119: &Apache::lonxml::end_alarm();
120: return ($xmlstring,$errorstring);
121: }
122:
123: sub tth_converted {
124: my $texstring=shift;
125: my $xmlstring='['.&mt('UNDISPLAYABLE').']';
126: if ($Apache::lontexconvert::messedup) {
127: return '['.&mt('TeX unconverted due to previous errors').']';
128: }
129: $$texstring ='\\documentstyle{article}'.$$texstring;
130:
131: eval(<<'ENDCONV');
132: ($xmlstring,$errorstring)=&convert_real($texstring)
133: ENDCONV
134: if ($@) {
135: $errorstring.=&mt("Evaluation Error: ").$@;
136: $Apache::lontexconvert::messedup=1;
137: }
138: if ($Apache::lontexconvert::messedup || &tth::tthmessedup() ||
139: $errorstring) {
140: &Apache::lonnet::logthis("Trying to kill myself");
141: $Apache::lontexconvert::messedup=1;
142: if (ref($Apache::lonxml::request)) {
143: $Apache::lonxml::request->child_terminate();
144: } else {
145: my $request;
146: eval { $request=Apache->request; };
147: if (!$request) {
148: eval { $request=Apache2::RequestUtil->request; };
149: }
150: if ($request) {
151: $request->child_terminate();
152: } else {
153: &Apache::lonnet::logthis("Unable to find a request to do child_terminate on");
154: }
155: }
156: }
157: return $xmlstring;
158: }
159:
160: sub clean_out_math_mode {
161: my ($texstring)=@_;
162: $$texstring=~s/(?<!\\)\$//g;
163: $$texstring=~s/\\[\)\(\]\[]//g;
164: $$texstring=~s/\\ensuremath//g;
165: return '';
166: }
167:
168: sub displaystyle {
169: my ($texstring)=@_;
170: #has a $$ or \[ or \displaystyle in it, guessinng it's display mode
171: if ($$texstring=~/[^\\]\$\$/ ||
172: $$texstring=~/\\\[/ ||
173: $$texstring=~/\\displaystyle/) { return 1; }
174: return 0;
175: }
176:
177: sub jsMath_converted {
178: my $texstring=shift;
179: my $tag='span';
180: if (&displaystyle($texstring)) { $tag='div'; }
181: &clean_out_math_mode($texstring);
182: return &jsMath_header().
183: '<'.$tag.' class="math">'.$$texstring.'</'.$tag.'>';
184: }
185:
186: {
187: my @jsMath_sent_header;
188: sub jsMath_reset {
189: undef(@jsMath_sent_header);
190: }
191: sub jsMath_push {
192: push(@jsMath_sent_header,0);
193: }
194: sub jsMath_header {
195: if (!@jsMath_sent_header) {
196: &Apache::lonnet::logthis("mismatched calls of jsMath_header and jsMath_process");
197: return '';
198: }
199: return '' if $jsMath_sent_header[-1];
200: $jsMath_sent_header[-1]=1;
201: return
202: '<script type="text/javascript">
203: function NoFontMessage () {}
204: jsMath = {Parser: {prototype: {environments: {\'eqnarray*\' :[\'Array\',null,null,\'rcl\',[5/18,5/18],3,\'D\']}}}};
205: </script>'."\n".
206: '<script type="text/javascript" src="/adm/jsMath/jsMath.js"></script>'."\n";
207: }
208: sub jsMath_process {
209: my $state = pop(@jsMath_sent_header);
210: return '' if !$state;
211: return "\n".
212: '<script type="text/javascript">jsMath.Process()</script>'."\n";
213: }
214: sub jsMath_state {
215: my ($level) = @_;
216: return $jsMath_sent_header[$level];
217: }
218: }
219:
220: sub tex_engine {
221: if (exists($env{'form.texengine'})) {
222: return $env{'form.texengine'};
223: }
224: if ($env{'request.course.id'}
225: && exists($env{'course.'.$env{'request.course.id'}.'.texengine'})) {
226: return $env{'course.'.$env{'request.course.id'}.'.texengine'};
227: }
228: if (exists($env{'environment.texengine'})) {
229: return $env{'environment.texengine'};
230: }
231: return 'tth';
232: }
233:
234: sub init_math_support {
235: my ($inherit_jsmath) = @_;
236: &init_tth();
237: &Apache::lontexconvert::jsMath_push();
238: if (lc(&tex_engine()) eq 'jsmath' ||
239: ($inherit_jsmath && &jsMath_state(-2))) {
240: return &Apache::lontexconvert::jsMath_header();
241: }
242: return;
243: }
244:
245: sub mimetex_converted {
246: my $texstring=shift;
247: my $displaystyle=&displaystyle($texstring);
248:
249: &clean_out_math_mode($texstring);
250:
251: if ($displaystyle) {
252: $$texstring='\\displaystyle \\Large '.$$texstring;
253: }
254: my $result='<img src="/cgi-bin/mimetex.cgi?'.&escape($$texstring).'" />';
255: if ($displaystyle) {
256: $result='<center>'.$result.'</center>';
257: }
258: return $result;
259: }
260:
261: sub converted {
262: my ($string,$mode)=@_;
263: if ($mode eq '') { $mode = &tex_engine(); }
264: if ($mode =~ /tth/i) {
265: return &tth_converted($string);
266: } elsif ($mode =~ /jsmath/i) {
267: return &jsMath_converted($string);
268: } elsif ($mode =~ /mimetex/i) {
269: return &mimetex_converted($string);
270: }
271: return &tth_converted($string);
272: }
273:
274: # ------------------------------------------------------------ Message display
275:
276: sub to_convert {
277: my ($string) = @_;
278: $string=~s/\<br\s*\/?\>/ /gs;
279: # $string=~s/\s/ /gs;
280: $string=&HTML::Entities::decode($string);
281: return &converted(\$string);
282: }
283:
284: sub smiley {
285: my $expression=shift;
286: if ($env{'browser.imagesuppress'} eq 'on') { return $expression; }
287: my %smileys=('\:\-\)' => 'smiley',
288: '8\-\)' => 'coolsmile',
289: '8\-(I|\|)' => 'coolindiff',
290: ':\-(I|\|)' => 'neutral',
291: '\:\-(o|O|\(\))' => 'shocked',
292: ':\-\(' => 'frowny',
293: '\;\-\)' => 'wink',
294: '\:\-P' => 'baeh',
295: '\:\-(\\\|\\/)' => 'hrrm',
296: '\:\-D' => 'bigsmile',
297: '\:\-C' => 'angry',
298: '\:(\'|\`)\-\(' => 'cry',
299: '\:\-(X|\#)' => 'lipsrsealed',
300: '\:\-S' => 'huh');
301: my $iconpath=$Apache::lonnet::perlvar{'lonIconsURL'};
302: foreach my $smiley (keys(%smileys)) {
303: $expression=~s/$smiley/\<img src="$iconpath\/$smileys{$smiley}.gif" \/\>/gs;
304: }
305: return $expression;
306: }
307:
308: sub msgtexconverted {
309: my ($message,$email) = @_;
310: $errorstring='';
311: &init_tth();
312: my $outmessage='';
313: my $tex=0;
314: foreach my $fragment (split(/(?:\<\;|\<)\/*m\s*(?:\>\;|\>)/i,$message)) {
315: if ($tex) {
316: if ($email) {
317: $outmessage.='</pre><tt>'.&to_convert($fragment).'</tt><pre>';
318: $tex=0;
319: } else {
320: $outmessage.=&to_convert($fragment);
321: $tex=0;
322: }
323: } else {
324: $outmessage.=&smiley($fragment);
325: $tex=1;
326: }
327: }
328: $message=$outmessage; $outmessage=''; $tex=0;
329: foreach my $fragment (split(/(?:\<\;|\<)\/*algebra\s*(?:\>\;|\>)/i,
330: $message)) {
331: if ($tex) {
332: if ($email) {
333: $outmessage.='</pre><tt>'.&algebra($fragment,'web').'</tt><pre>';
334: $tex=0;
335: } else {
336: $outmessage.=&algebra($fragment,'web');
337: $tex=0;
338: }
339: } else {
340: $outmessage.=$fragment;
341: $tex=1;
342: }
343: }
344: if (wantarray) {
345: return ($outmessage,$errorstring);
346: } else {
347: return $outmessage.$errorstring;
348: }
349: }
350:
351: sub algebra {
352: use AlgParser;
353:
354: my ($string,$target,$style)=@_;
355: my $parser = new AlgParserWithImplicitExpand;
356: $string=&prepare_algebra($string);
357: my $ret = $parser->parse($string);
358: my $result='['.&mt('Algebra unconverted due to previous errors').']';
359: if ( ref($ret) ) {
360: #$parser->tostring();
361: $parser->normalize();
362: my $latex=$parser->tolatex();
363: $latex=&postprocess_algebra($latex);
364: if ($style eq 'display') {
365: $latex='$$'.$latex.'$$x';
366: } else {
367: # style is 'inline'
368: $latex='\\ensuremath{'.$latex.'}';
369: }
370: if ($target eq 'web' || $target eq 'analyze') {
371: $result = &converted(\$latex);
372: } else {
373: $result = $latex;
374: }
375: } else {
376: &Apache::lonxml::error($parser->{'htmlerror'});
377: }
378: }
379:
380: sub prepare_algebra {
381: my ($string)=@_;
382:
383: # makes the decision about what is a minus sign easier supposedly
384: $string =~ s/(\<\>|\<\=|\>\=[\=\>\<] *)-/$1 zeroplace -/g;
385:
386: return $string;
387: }
388:
389: sub postprocess_algebra {
390: my ($string)=@_;
391:
392: # moodle had these and I don't know why, ignoring them for now
393: # $string =~s/\\fun/ /g;
394:
395: # sqrt(3,4) means the 4 root of 3
396: $string =~s/\\sqrt{([^,]+),([^\}]+)}/\\sqrt[$2]{$1}/gs;
397:
398: # log(3,4) means the log base 4 of 3
399: $string =~s/\\log\\left\((.+?),(.+?)\\right\)/\\log_{$2}\\left($1\\right)/gs;
400:
401: # log(3,4) means the log base 4 of 3
402: $string =~s/\\((?:sin|cos|tan|sec|csc|cot)(?:h)?)\\left\((.+?),(.+?)\\right\)/\\$1^{$3}\\left($2\\right)/gs;
403:
404: # int(3,a,b) integral from a to b of 3
405: $string =~s/\\int\\left\((.+?),(.+?),(.+?)\\right\)/\\int_{$2}^{$3}\\left($1\\right)/gs;
406:
407: # int( ... dx) -> ...
408: $string =~s/\\int\\left\((.+?)d[a-z]\\right\)/$1/gs;
409:
410: #
411: $string =~s/\\lim\\left\((.+?),(.+?),(.+?)\\right\)/\\lim_{$2\\to $3}$1/gs;
412: return $string;
413: }
414: 1;
415: __END__
416:
417:
418:
419:
420:
421:
422:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>