Annotation of loncom/xml/lontexconvert.pm, revision 1.72
1.1 harris41 1: # The LearningOnline Network with CAPA
2: # TeX Conversion Module
3: #
1.72 ! albertel 4: # $Id: lontexconvert.pm,v 1.71 2006/03/27 20:40:42 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.33 albertel 45: use Apache::lonmsg();
46: use Apache::lonxml();
47: use Apache::lonmenu();
48: use Apache::lonlocal;
1.65 albertel 49: use Apache::lonnet;
1.1 harris41 50:
51: # ====================================================================== Header
52:
1.29 albertel 53: sub init_tth {
1.65 albertel 54: my $options=$env{'course.'.$env{'request.course.id'}.'.tthoptions'};
55: if ($env{'browser.mathml'}) {
1.29 albertel 56: &tth::ttminit();
1.65 albertel 57: if ($env{'browser.unicode'}) {
1.29 albertel 58: &tth::ttmoptions('-L -u1 '.$options);
59: } else {
60: &tth::ttmoptions('-L -u0 '.$options);
61: }
62: } else {
63: &tth::tthinit();
1.65 albertel 64: if ($env{'browser.unicode'}) {
1.29 albertel 65: &tth::tthoptions('-L -u1 '.$options);
66: } else {
67: &tth::tthoptions('-L -u0 '.$options);
68: }
69: }
70: }
71:
1.1 harris41 72: # ================================================================== Conversion
73:
1.19 albertel 74: $Apache::lontexconvert::messedup=0;
1.36 albertel 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 {
1.37 albertel 85: &Apache::lonnet::logthis("ALRM");
1.36 albertel 86: $xmlstring='['.&mt("TeX unconverted due to errors").']';
87: $Apache::lontexconvert::messedup=1;
88: die &mt("TeX unconverted due to errors");
89: };
1.63 albertel 90: &Apache::lonxml::start_alarm();
1.65 albertel 91: if ($env{'browser.mathml'}) {
1.36 albertel 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: }
1.43 www 102: $xmlstring=~s/^\s*\<br clear\=\"all\"/\<br/s;
1.36 albertel 103: $xmlstring=~s/^\s*//;
104: $xmlstring=~s/\s*$//;
1.63 albertel 105: &Apache::lonxml::end_alarm();
1.36 albertel 106: return ($xmlstring,$errorstring);
107: }
108:
1.59 albertel 109: sub tth_converted {
1.31 albertel 110: my $texstring=shift;
1.34 www 111: my $xmlstring='['.&mt('UNDISPLAYABLE').']';
1.31 albertel 112: if ($Apache::lontexconvert::messedup) {
1.35 albertel 113: return '['.&mt('TeX unconverted due to previous errors').']';
1.31 albertel 114: }
1.59 albertel 115: $$texstring ='\\documentstyle{article}'.$$texstring;
116:
1.31 albertel 117: eval(<<'ENDCONV');
1.36 albertel 118: ($xmlstring,$errorstring)=&convert_real($texstring)
1.12 www 119: ENDCONV
1.32 albertel 120: if ($@) {
1.35 albertel 121: $errorstring.=&mt("Evaluation Error: ").$@;
1.32 albertel 122: $Apache::lontexconvert::messedup=1;
123: }
1.37 albertel 124: if ($Apache::lontexconvert::messedup || &tth::tthmessedup() ||
125: $errorstring) {
1.31 albertel 126: &Apache::lonnet::logthis("Trying to kill myself");
127: $Apache::lontexconvert::messedup=1;
1.70 albertel 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: }
1.31 albertel 142: }
143: return $xmlstring;
1.1 harris41 144: }
145:
1.62 albertel 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:
1.59 albertel 163: sub jsMath_converted {
164: my $texstring=shift;
165: my $tag='span';
1.62 albertel 166: if (&displaystyle($texstring)) { $tag='div'; }
167: &clean_out_math_mode($texstring);
1.67 albertel 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".
1.71 albertel 184: '<script type="text/javascript" src="/adm/jsMath/jsMath.js"></script>'."\n";
1.67 albertel 185: }
186: sub jsMath_process {
187: return '' if !$jsMath_sent_header;
188: return '<script type="text/javascript">jsMath.Process()</script>';
189: }
1.62 albertel 190: }
191:
1.72 ! albertel 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:
1.62 albertel 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;
1.59 albertel 209: }
1.62 albertel 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;
1.59 albertel 215: }
216:
217: sub converted {
1.67 albertel 218: my ($string,$mode)=@_;
219: if ($mode eq '') { $mode=$env{'environment.texengine'}; }
1.68 albertel 220: if ($mode =~ /tth/i) {
1.67 albertel 221: return &tth_converted($string);
1.68 albertel 222: } elsif ($mode =~ /jsmath/i) {
1.67 albertel 223: return &jsMath_converted($string);
1.68 albertel 224: } elsif ($mode =~ /mimetex/i) {
1.67 albertel 225: return &mimetex_converted($string);
1.59 albertel 226: }
1.67 albertel 227: return &tth_converted($string);
1.59 albertel 228: }
229:
1.6 www 230: # ------------------------------------------------------------ Message display
231:
1.9 albertel 232: sub to_convert {
233: my ($string) = @_;
1.22 www 234: $string=~s/\<br\s*\/?\>/ /gs;
1.30 albertel 235: # $string=~s/\s/ /gs;
1.18 albertel 236: $string=&HTML::Entities::decode($string);
1.9 albertel 237: return &converted(\$string);
238: }
239:
1.20 www 240: sub smiley {
1.31 albertel 241: my $expression=shift;
1.65 albertel 242: if ($env{'browser.imagesuppress'} eq 'on') { return $expression; }
1.31 albertel 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;
1.20 www 262: }
263:
1.6 www 264: sub msgtexconverted {
1.39 raeburn 265: my ($message,$email) = @_;
1.17 albertel 266: $errorstring='';
1.29 albertel 267: &init_tth();
1.25 www 268: my $outmessage='';
269: my $tex=0;
270: foreach (split(/(?:\<\;|\<)\/*m\s*(?:\>\;|\>)/i,$message)) {
271: if ($tex) {
1.30 albertel 272: if ($email) {
273: $outmessage.='</pre><tt>'.&to_convert($_).'</tt><pre>'; $tex=0;
274: } else {
275: $outmessage.=&to_convert($_); $tex=0;
276: }
1.25 www 277: } else {
278: $outmessage.=&smiley($_); $tex=1;
279: }
280: }
1.64 albertel 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: }
1.24 albertel 293: if (wantarray) {
1.25 www 294: return ($outmessage,$errorstring);
1.24 albertel 295: } else {
1.25 www 296: return $outmessage.$errorstring;
1.24 albertel 297: }
1.1 harris41 298: }
299:
1.44 albertel 300: sub algebra {
1.49 albertel 301: use AlgParser;
302:
1.58 albertel 303: my ($string,$target,$style)=@_;
1.44 albertel 304: my $parser = new AlgParserWithImplicitExpand;
1.46 albertel 305: $string=&prepare_algebra($string);
1.44 albertel 306: my $ret = $parser->parse($string);
307: my $result='['.&mt('Algebra unconverted due to previous errors').']';
308: if ( ref($ret) ) {
1.50 albertel 309: #$parser->tostring();
1.44 albertel 310: $parser->normalize();
311: my $latex=$parser->tolatex();
1.49 albertel 312: $latex=&postprocess_algebra($latex);
1.58 albertel 313: if ($style eq 'display') {
314: $latex='$$'.$latex.'$$x';
315: } else {
1.61 albertel 316: # style is 'inline'
1.58 albertel 317: $latex='\\ensuremath{'.$latex.'}';
318: }
1.44 albertel 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:
1.46 albertel 329: sub prepare_algebra {
330: my ($string)=@_;
331:
1.50 albertel 332: # makes the decision about what is a minus sign easier supposedly
1.57 albertel 333: $string =~ s/(\<\>|\<\=|\>\=[\=\>\<] *)-/$1 zeroplace -/g;
1.48 albertel 334:
1.46 albertel 335: return $string;
336: }
337:
338: sub postprocess_algebra {
339: my ($string)=@_;
1.48 albertel 340:
1.50 albertel 341: # moodle had these and I don't know why, ignoring them for now
1.51 albertel 342: # $string =~s/\\fun/ /g;
1.47 albertel 343:
1.55 albertel 344: # remove the extra () in the denominator of a \frac
345: $string =~s/\\frac{(.+?)}{\\left\((.+?)\\right\)}/\\frac{$1}{$2}/gs;
1.51 albertel 346:
347: # sqrt(3,4) means the 4 root of 3
1.52 albertel 348: $string =~s/\\sqrt{([^,]+),([^\}]+)}/\\sqrt[$2]{$1}/gs;
349:
1.53 albertel 350: # log(3,4) means the log base 4 of 3
351: $string =~s/\\log\\left\((.+?),(.+?)\\right\)/\\log_{$2}\\left($1\\right)/gs;
352:
1.54 albertel 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:
1.55 albertel 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;
1.51 albertel 361:
1.56 albertel 362: #
363: $string =~s/\\lim\\left\((.+?),(.+?),(.+?)\\right\)/\\lim_{$2\\to $3}$1/gs;
1.46 albertel 364: return $string;
365: }
1.1 harris41 366: 1;
367: __END__
368:
369:
370:
371:
372:
373:
374:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>