Annotation of loncom/xml/lontexconvert.pm, revision 1.91
1.1 harris41 1: # The LearningOnline Network with CAPA
2: # TeX Conversion Module
3: #
1.91 ! jms 4: # $Id: lontexconvert.pm,v 1.90 2008/11/18 10:02:56 hauer 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:
1.91 ! jms 82:
1.36 albertel 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.85 foxr 108: #
109: # \rightleftharpoons is not converted by tth but maps
110: # reasonably well to ⇔. If we get many more of these,
111: # we're going to need to have a translation sub.
112: #
1.87 foxr 113: my $lrharpoon = pack("U", 0x21cc);
114: $xmlstring=~s/\\rightleftharpoons/$lrharpoon/g;
1.85 foxr 115:
1.63 albertel 116: &Apache::lonxml::end_alarm();
1.36 albertel 117: return ($xmlstring,$errorstring);
118: }
119:
1.59 albertel 120: sub tth_converted {
1.31 albertel 121: my $texstring=shift;
1.34 www 122: my $xmlstring='['.&mt('UNDISPLAYABLE').']';
1.31 albertel 123: if ($Apache::lontexconvert::messedup) {
1.35 albertel 124: return '['.&mt('TeX unconverted due to previous errors').']';
1.31 albertel 125: }
1.59 albertel 126: $$texstring ='\\documentstyle{article}'.$$texstring;
127:
1.31 albertel 128: eval(<<'ENDCONV');
1.36 albertel 129: ($xmlstring,$errorstring)=&convert_real($texstring)
1.12 www 130: ENDCONV
1.32 albertel 131: if ($@) {
1.35 albertel 132: $errorstring.=&mt("Evaluation Error: ").$@;
1.32 albertel 133: $Apache::lontexconvert::messedup=1;
134: }
1.37 albertel 135: if ($Apache::lontexconvert::messedup || &tth::tthmessedup() ||
136: $errorstring) {
1.31 albertel 137: &Apache::lonnet::logthis("Trying to kill myself");
138: $Apache::lontexconvert::messedup=1;
1.70 albertel 139: if (ref($Apache::lonxml::request)) {
140: $Apache::lonxml::request->child_terminate();
141: } else {
142: my $request;
143: eval { $request=Apache->request; };
144: if (!$request) {
145: eval { $request=Apache2::RequestUtil->request; };
146: }
147: if ($request) {
148: $request->child_terminate();
149: } else {
150: &Apache::lonnet::logthis("Unable to find a request to do child_terminate on");
151: }
152: }
1.31 albertel 153: }
154: return $xmlstring;
1.1 harris41 155: }
156:
1.62 albertel 157: sub clean_out_math_mode {
158: my ($texstring)=@_;
1.82 albertel 159: $$texstring=~s/(?<!\\)\$//g;
1.62 albertel 160: $$texstring=~s/\\[\)\(\]\[]//g;
161: $$texstring=~s/\\ensuremath//g;
162: return '';
163: }
164:
165: sub displaystyle {
166: my ($texstring)=@_;
167: #has a $$ or \[ or \displaystyle in it, guessinng it's display mode
168: if ($$texstring=~/[^\\]\$\$/ ||
169: $$texstring=~/\\\[/ ||
170: $$texstring=~/\\displaystyle/) { return 1; }
171: return 0;
172: }
173:
1.59 albertel 174: sub jsMath_converted {
175: my $texstring=shift;
176: my $tag='span';
1.62 albertel 177: if (&displaystyle($texstring)) { $tag='div'; }
178: &clean_out_math_mode($texstring);
1.67 albertel 179: return &jsMath_header().
180: '<'.$tag.' class="math">'.$$texstring.'</'.$tag.'>';
181: }
182:
183: {
1.76 albertel 184: my @jsMath_sent_header;
1.67 albertel 185: sub jsMath_reset {
1.76 albertel 186: undef(@jsMath_sent_header);
187: }
188: sub jsMath_push {
189: push(@jsMath_sent_header,0);
1.67 albertel 190: }
191: sub jsMath_header {
1.80 albertel 192: if (!@jsMath_sent_header) {
193: &Apache::lonnet::logthis("mismatched calls of jsMath_header and jsMath_process");
194: return '';
195: }
1.76 albertel 196: return '' if $jsMath_sent_header[-1];
197: $jsMath_sent_header[-1]=1;
1.67 albertel 198: return
199: '<script type="text/javascript">
200: function NoFontMessage () {}
1.78 albertel 201: jsMath = {Parser: {prototype: {environments: {\'eqnarray*\' :[\'Array\',null,null,\'rcl\',[5/18,5/18],3,\'D\']}}}};
1.67 albertel 202: </script>'."\n".
1.71 albertel 203: '<script type="text/javascript" src="/adm/jsMath/jsMath.js"></script>'."\n";
1.67 albertel 204: }
205: sub jsMath_process {
1.76 albertel 206: my $state = pop(@jsMath_sent_header);
207: return '' if !$state;
1.81 albertel 208: return "\n".
209: '<script type="text/javascript">jsMath.Process()</script>'."\n";
1.67 albertel 210: }
1.84 albertel 211: sub jsMath_state {
212: my ($level) = @_;
213: return $jsMath_sent_header[$level];
214: }
1.62 albertel 215: }
216:
1.83 albertel 217: sub tex_engine {
218: if (exists($env{'form.texengine'})) {
1.88 raeburn 219: if ($env{'form.texengine'} ne '') {
220: return $env{'form.texengine'};
221: }
1.83 albertel 222: }
223: if ($env{'request.course.id'}
224: && exists($env{'course.'.$env{'request.course.id'}.'.texengine'})) {
225: return $env{'course.'.$env{'request.course.id'}.'.texengine'};
226: }
227: if (exists($env{'environment.texengine'})) {
228: return $env{'environment.texengine'};
229: }
230: return 'tth';
231: }
232:
1.72 albertel 233: sub init_math_support {
1.84 albertel 234: my ($inherit_jsmath) = @_;
1.73 albertel 235: &init_tth();
1.76 albertel 236: &Apache::lontexconvert::jsMath_push();
1.84 albertel 237: if (lc(&tex_engine()) eq 'jsmath' ||
238: ($inherit_jsmath && &jsMath_state(-2))) {
1.72 albertel 239: return &Apache::lontexconvert::jsMath_header();
240: }
241: return;
242: }
243:
1.62 albertel 244: sub mimetex_converted {
245: my $texstring=shift;
246: my $displaystyle=&displaystyle($texstring);
247:
248: &clean_out_math_mode($texstring);
249:
250: if ($displaystyle) {
251: $$texstring='\\displaystyle \\Large '.$$texstring;
1.59 albertel 252: }
1.90 hauer 253: my $result='<img src="/cgi-bin/mimetex.cgi?'.&escape($$texstring).'" alt="$'.$$texstring.'$" />';
1.62 albertel 254: if ($displaystyle) {
255: $result='<center>'.$result.'</center>';
256: }
257: return $result;
1.59 albertel 258: }
259:
260: sub converted {
1.67 albertel 261: my ($string,$mode)=@_;
1.83 albertel 262: if ($mode eq '') { $mode = &tex_engine(); }
1.68 albertel 263: if ($mode =~ /tth/i) {
1.67 albertel 264: return &tth_converted($string);
1.68 albertel 265: } elsif ($mode =~ /jsmath/i) {
1.67 albertel 266: return &jsMath_converted($string);
1.68 albertel 267: } elsif ($mode =~ /mimetex/i) {
1.67 albertel 268: return &mimetex_converted($string);
1.59 albertel 269: }
1.67 albertel 270: return &tth_converted($string);
1.59 albertel 271: }
272:
1.6 www 273: # ------------------------------------------------------------ Message display
274:
1.9 albertel 275: sub to_convert {
276: my ($string) = @_;
1.22 www 277: $string=~s/\<br\s*\/?\>/ /gs;
1.30 albertel 278: # $string=~s/\s/ /gs;
1.18 albertel 279: $string=&HTML::Entities::decode($string);
1.9 albertel 280: return &converted(\$string);
281: }
282:
1.20 www 283: sub smiley {
1.31 albertel 284: my $expression=shift;
1.65 albertel 285: if ($env{'browser.imagesuppress'} eq 'on') { return $expression; }
1.31 albertel 286: my %smileys=('\:\-\)' => 'smiley',
287: '8\-\)' => 'coolsmile',
288: '8\-(I|\|)' => 'coolindiff',
289: ':\-(I|\|)' => 'neutral',
290: '\:\-(o|O|\(\))' => 'shocked',
291: ':\-\(' => 'frowny',
292: '\;\-\)' => 'wink',
293: '\:\-P' => 'baeh',
294: '\:\-(\\\|\\/)' => 'hrrm',
295: '\:\-D' => 'bigsmile',
296: '\:\-C' => 'angry',
297: '\:(\'|\`)\-\(' => 'cry',
298: '\:\-(X|\#)' => 'lipsrsealed',
299: '\:\-S' => 'huh');
300: my $iconpath=$Apache::lonnet::perlvar{'lonIconsURL'};
1.79 albertel 301: foreach my $smiley (keys(%smileys)) {
302: $expression=~s/$smiley/\<img src="$iconpath\/$smileys{$smiley}.gif" \/\>/gs;
1.31 albertel 303: }
304: return $expression;
1.20 www 305: }
306:
1.6 www 307: sub msgtexconverted {
1.39 raeburn 308: my ($message,$email) = @_;
1.17 albertel 309: $errorstring='';
1.29 albertel 310: &init_tth();
1.25 www 311: my $outmessage='';
312: my $tex=0;
1.79 albertel 313: foreach my $fragment (split(/(?:\<\;|\<)\/*m\s*(?:\>\;|\>)/i,$message)) {
1.25 www 314: if ($tex) {
1.30 albertel 315: if ($email) {
1.79 albertel 316: $outmessage.='</pre><tt>'.&to_convert($fragment).'</tt><pre>';
317: $tex=0;
1.30 albertel 318: } else {
1.79 albertel 319: $outmessage.=&to_convert($fragment);
320: $tex=0;
1.30 albertel 321: }
1.25 www 322: } else {
1.79 albertel 323: $outmessage.=&smiley($fragment);
324: $tex=1;
1.25 www 325: }
326: }
1.64 albertel 327: $message=$outmessage; $outmessage=''; $tex=0;
1.79 albertel 328: foreach my $fragment (split(/(?:\<\;|\<)\/*algebra\s*(?:\>\;|\>)/i,
329: $message)) {
1.64 albertel 330: if ($tex) {
331: if ($email) {
1.79 albertel 332: $outmessage.='</pre><tt>'.&algebra($fragment,'web').'</tt><pre>';
333: $tex=0;
1.64 albertel 334: } else {
1.79 albertel 335: $outmessage.=&algebra($fragment,'web');
336: $tex=0;
1.64 albertel 337: }
338: } else {
1.79 albertel 339: $outmessage.=$fragment;
340: $tex=1;
1.64 albertel 341: }
342: }
1.24 albertel 343: if (wantarray) {
1.25 www 344: return ($outmessage,$errorstring);
1.24 albertel 345: } else {
1.25 www 346: return $outmessage.$errorstring;
1.24 albertel 347: }
1.1 harris41 348: }
349:
1.44 albertel 350: sub algebra {
1.49 albertel 351: use AlgParser;
352:
1.89 droeschl 353: my ($string,$target,$style,$parstack,$safeeval)=@_;
1.44 albertel 354: my $parser = new AlgParserWithImplicitExpand;
1.46 albertel 355: $string=&prepare_algebra($string);
1.44 albertel 356: my $ret = $parser->parse($string);
357: my $result='['.&mt('Algebra unconverted due to previous errors').']';
358: if ( ref($ret) ) {
1.50 albertel 359: #$parser->tostring();
1.44 albertel 360: $parser->normalize();
361: my $latex=$parser->tolatex();
1.49 albertel 362: $latex=&postprocess_algebra($latex);
1.58 albertel 363: if ($style eq 'display') {
364: $latex='$$'.$latex.'$$x';
365: } else {
1.61 albertel 366: # style is 'inline'
1.58 albertel 367: $latex='\\ensuremath{'.$latex.'}';
368: }
1.44 albertel 369: if ($target eq 'web' || $target eq 'analyze') {
1.89 droeschl 370: my $display=&Apache::lonxml::get_param('display',$parstack,$safeeval);
371: $result = &converted(\$latex,$display);
372: # $result = &converted(\$latex);
1.44 albertel 373: } else {
374: $result = $latex;
375: }
376: } else {
377: &Apache::lonxml::error($parser->{'htmlerror'});
378: }
379: }
380:
1.46 albertel 381: sub prepare_algebra {
382: my ($string)=@_;
383:
1.50 albertel 384: # makes the decision about what is a minus sign easier supposedly
1.57 albertel 385: $string =~ s/(\<\>|\<\=|\>\=[\=\>\<] *)-/$1 zeroplace -/g;
1.48 albertel 386:
1.46 albertel 387: return $string;
388: }
389:
390: sub postprocess_algebra {
391: my ($string)=@_;
1.48 albertel 392:
1.50 albertel 393: # moodle had these and I don't know why, ignoring them for now
1.51 albertel 394: # $string =~s/\\fun/ /g;
1.47 albertel 395:
1.51 albertel 396: # sqrt(3,4) means the 4 root of 3
1.52 albertel 397: $string =~s/\\sqrt{([^,]+),([^\}]+)}/\\sqrt[$2]{$1}/gs;
398:
1.53 albertel 399: # log(3,4) means the log base 4 of 3
400: $string =~s/\\log\\left\((.+?),(.+?)\\right\)/\\log_{$2}\\left($1\\right)/gs;
401:
1.54 albertel 402: # log(3,4) means the log base 4 of 3
403: $string =~s/\\((?:sin|cos|tan|sec|csc|cot)(?:h)?)\\left\((.+?),(.+?)\\right\)/\\$1^{$3}\\left($2\\right)/gs;
404:
1.55 albertel 405: # int(3,a,b) integral from a to b of 3
406: $string =~s/\\int\\left\((.+?),(.+?),(.+?)\\right\)/\\int_{$2}^{$3}\\left($1\\right)/gs;
407:
408: # int( ... dx) -> ...
409: $string =~s/\\int\\left\((.+?)d[a-z]\\right\)/$1/gs;
1.51 albertel 410:
1.56 albertel 411: #
412: $string =~s/\\lim\\left\((.+?),(.+?),(.+?)\\right\)/\\lim_{$2\\to $3}$1/gs;
1.46 albertel 413: return $string;
414: }
1.1 harris41 415: 1;
416: __END__
417:
418:
1.91 ! jms 419: =pod
! 420:
! 421: =head1 NAME
! 422:
! 423: Apache::lontexconvert;
! 424:
! 425: =head1 SYNOPSIS
! 426:
! 427: Access to tth/ttm
! 428:
! 429: This is part of the LearningOnline Network with CAPA project
! 430: described at http://www.lon-capa.org.
! 431:
! 432:
! 433: =head1 SUBROUTINES
! 434:
! 435: =over
! 436:
! 437: =item init_tth()
! 438:
! 439: Header
! 440:
! 441: =item convert_real()
! 442:
! 443: we need this routine because &converted can get called from inside
! 444: of the safespace (through &xmlparse('<m>stuff</m>') which doesn't
! 445: allow the opcode for alarm, so we need to compile this before we get
! 446: into the safe space since opcode checks only occur at compile time
! 447:
! 448: =item tth_converted()
! 449:
! 450:
! 451: =item clean_out_math_mode()
! 452:
! 453:
! 454: =item displaystyle()
! 455:
! 456:
! 457: =item jsMath_converted()
! 458:
! 459:
! 460: =item tex_engine()
! 461:
! 462:
! 463: =item init_math_support()
! 464:
! 465:
! 466: =item mimetex_converted()
! 467:
! 468:
! 469: =item converted()
! 470:
! 471:
! 472: =item to_convert()
! 473:
! 474: message display
! 475:
! 476: =item smiley()
! 477:
! 478: ???
! 479:
! 480: =item msgtexconverted()
! 481:
! 482: =item algebra()
! 483:
! 484: =item prepare_algebra()
! 485:
! 486: =item postprocess_algebra()
1.1 harris41 487:
1.91 ! jms 488: =back
1.1 harris41 489:
1.91 ! jms 490: =cut
1.1 harris41 491:
492:
493:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>