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