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