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