Annotation of loncom/xml/lontexconvert.pm, revision 1.99
1.1 harris41 1: # The LearningOnline Network with CAPA
2: # TeX Conversion Module
3: #
1.99 ! faziophi 4: # $Id: lontexconvert.pm,v 1.98 2009/08/12 20:43:25 www 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;
1.94 raeburn 50: use URI::Escape;
51: use IO::Socket::INET;
52: use IO::Socket::SSL;
1.1 harris41 53:
54: # ====================================================================== Header
55:
1.29 albertel 56: sub init_tth {
1.65 albertel 57: my $options=$env{'course.'.$env{'request.course.id'}.'.tthoptions'};
1.74 albertel 58: if ($options =~ /\S/) {
59: $options = ' '.$options;
60: } else {
61: undef($options);
62: }
1.65 albertel 63: if ($env{'browser.mathml'}) {
1.29 albertel 64: &tth::ttminit();
1.65 albertel 65: if ($env{'browser.unicode'}) {
1.74 albertel 66: &tth::ttmoptions('-L -u1'.$options);
1.29 albertel 67: } else {
1.74 albertel 68: &tth::ttmoptions('-L -u0'.$options);
1.29 albertel 69: }
70: } else {
71: &tth::tthinit();
1.65 albertel 72: if ($env{'browser.unicode'}) {
1.74 albertel 73: &tth::tthoptions('-L -u1'.$options);
1.29 albertel 74: } else {
1.74 albertel 75: &tth::tthoptions('-L -u0'.$options);
1.29 albertel 76: }
77: }
78: }
79:
1.1 harris41 80: # ================================================================== Conversion
81:
1.19 albertel 82: $Apache::lontexconvert::messedup=0;
1.36 albertel 83:
1.91 jms 84:
1.36 albertel 85: sub convert_real {
86: my ($texstring)=@_;
87: my ($xmlstring,$errorstring);
88: local $SIG{SEGV}=sub { $Apache::lontexconvert::messedup=1; die; };
89: local $SIG{ALRM}=sub {
1.37 albertel 90: &Apache::lonnet::logthis("ALRM");
1.36 albertel 91: $xmlstring='['.&mt("TeX unconverted due to errors").']';
92: $Apache::lontexconvert::messedup=1;
93: die &mt("TeX unconverted due to errors");
94: };
1.63 albertel 95: &Apache::lonxml::start_alarm();
1.65 albertel 96: if ($env{'browser.mathml'}) {
1.36 albertel 97: $xmlstring=&tth::ttm($$texstring);
98: $xmlstring=~s/\<math\>/\<math xmlns=\"\&mathns\;\"\>/g;
99: $xmlstring=~s/\<br\>/\<br\/\>/g;
100: $xmlstring=~s/\<p\>/\<p\>\<\/p\>/g;
101: $errorstring.=&tth::ttmerror();
102: } else {
103: $xmlstring=&tth::tth($$texstring);
104: $errorstring.=&tth::ttherror();
105: $xmlstring=~s-</font(\s*)>-</font>-g;
106: }
1.43 www 107: $xmlstring=~s/^\s*\<br clear\=\"all\"/\<br/s;
1.36 albertel 108: $xmlstring=~s/^\s*//;
109: $xmlstring=~s/\s*$//;
1.85 foxr 110: #
111: # \rightleftharpoons is not converted by tth but maps
112: # reasonably well to ⇔. If we get many more of these,
113: # we're going to need to have a translation sub.
114: #
1.87 foxr 115: my $lrharpoon = pack("U", 0x21cc);
116: $xmlstring=~s/\\rightleftharpoons/$lrharpoon/g;
1.85 foxr 117:
1.63 albertel 118: &Apache::lonxml::end_alarm();
1.36 albertel 119: return ($xmlstring,$errorstring);
120: }
121:
1.59 albertel 122: sub tth_converted {
1.31 albertel 123: my $texstring=shift;
1.34 www 124: my $xmlstring='['.&mt('UNDISPLAYABLE').']';
1.31 albertel 125: if ($Apache::lontexconvert::messedup) {
1.35 albertel 126: return '['.&mt('TeX unconverted due to previous errors').']';
1.31 albertel 127: }
1.59 albertel 128: $$texstring ='\\documentstyle{article}'.$$texstring;
129:
1.31 albertel 130: eval(<<'ENDCONV');
1.36 albertel 131: ($xmlstring,$errorstring)=&convert_real($texstring)
1.12 www 132: ENDCONV
1.32 albertel 133: if ($@) {
1.35 albertel 134: $errorstring.=&mt("Evaluation Error: ").$@;
1.32 albertel 135: $Apache::lontexconvert::messedup=1;
136: }
1.37 albertel 137: if ($Apache::lontexconvert::messedup || &tth::tthmessedup() ||
138: $errorstring) {
1.31 albertel 139: &Apache::lonnet::logthis("Trying to kill myself");
140: $Apache::lontexconvert::messedup=1;
1.70 albertel 141: if (ref($Apache::lonxml::request)) {
142: $Apache::lonxml::request->child_terminate();
143: } else {
144: my $request;
145: eval { $request=Apache->request; };
146: if (!$request) {
147: eval { $request=Apache2::RequestUtil->request; };
148: }
149: if ($request) {
150: $request->child_terminate();
151: } else {
152: &Apache::lonnet::logthis("Unable to find a request to do child_terminate on");
153: }
154: }
1.31 albertel 155: }
156: return $xmlstring;
1.1 harris41 157: }
158:
1.62 albertel 159: sub clean_out_math_mode {
160: my ($texstring)=@_;
1.82 albertel 161: $$texstring=~s/(?<!\\)\$//g;
1.62 albertel 162: $$texstring=~s/\\[\)\(\]\[]//g;
163: $$texstring=~s/\\ensuremath//g;
164: return '';
165: }
166:
167: sub displaystyle {
168: my ($texstring)=@_;
169: #has a $$ or \[ or \displaystyle in it, guessinng it's display mode
170: if ($$texstring=~/[^\\]\$\$/ ||
171: $$texstring=~/\\\[/ ||
172: $$texstring=~/\\displaystyle/) { return 1; }
173: return 0;
174: }
175:
1.59 albertel 176: sub jsMath_converted {
177: my $texstring=shift;
178: my $tag='span';
1.62 albertel 179: if (&displaystyle($texstring)) { $tag='div'; }
180: &clean_out_math_mode($texstring);
1.67 albertel 181: return &jsMath_header().
182: '<'.$tag.' class="math">'.$$texstring.'</'.$tag.'>';
183: }
184:
185: {
1.76 albertel 186: my @jsMath_sent_header;
1.67 albertel 187: sub jsMath_reset {
1.76 albertel 188: undef(@jsMath_sent_header);
189: }
190: sub jsMath_push {
191: push(@jsMath_sent_header,0);
1.67 albertel 192: }
193: sub jsMath_header {
1.80 albertel 194: if (!@jsMath_sent_header) {
195: &Apache::lonnet::logthis("mismatched calls of jsMath_header and jsMath_process");
196: return '';
197: }
1.76 albertel 198: return '' if $jsMath_sent_header[-1];
199: $jsMath_sent_header[-1]=1;
1.67 albertel 200: return
201: '<script type="text/javascript">
202: function NoFontMessage () {}
1.78 albertel 203: jsMath = {Parser: {prototype: {environments: {\'eqnarray*\' :[\'Array\',null,null,\'rcl\',[5/18,5/18],3,\'D\']}}}};
1.67 albertel 204: </script>'."\n".
1.71 albertel 205: '<script type="text/javascript" src="/adm/jsMath/jsMath.js"></script>'."\n";
1.67 albertel 206: }
207: sub jsMath_process {
1.76 albertel 208: my $state = pop(@jsMath_sent_header);
209: return '' if !$state;
1.81 albertel 210: return "\n".
211: '<script type="text/javascript">jsMath.Process()</script>'."\n";
1.67 albertel 212: }
1.84 albertel 213: sub jsMath_state {
214: my ($level) = @_;
215: return $jsMath_sent_header[$level];
216: }
1.62 albertel 217: }
218:
1.83 albertel 219: sub tex_engine {
220: if (exists($env{'form.texengine'})) {
1.88 raeburn 221: if ($env{'form.texengine'} ne '') {
222: return $env{'form.texengine'};
223: }
1.83 albertel 224: }
225: if ($env{'request.course.id'}
226: && exists($env{'course.'.$env{'request.course.id'}.'.texengine'})) {
227: return $env{'course.'.$env{'request.course.id'}.'.texengine'};
228: }
229: if (exists($env{'environment.texengine'})) {
230: return $env{'environment.texengine'};
231: }
232: return 'tth';
233: }
234:
1.72 albertel 235: sub init_math_support {
1.84 albertel 236: my ($inherit_jsmath) = @_;
1.73 albertel 237: &init_tth();
1.76 albertel 238: &Apache::lontexconvert::jsMath_push();
1.84 albertel 239: if (lc(&tex_engine()) eq 'jsmath' ||
240: ($inherit_jsmath && &jsMath_state(-2))) {
1.72 albertel 241: return &Apache::lontexconvert::jsMath_header();
242: }
243: return;
244: }
245:
1.92 hauer 246: sub mimetex_valign {
1.94 raeburn 247: my ($esc_texstring)=@_;
248: my $valign = 0;
249: my $lonhost = $Apache::lonnet::perlvar{'lonHostID'};
250: my $hostname = &Apache::lonnet::hostname($lonhost);
251: my $protocol = $Apache::lonnet::protocol{$lonhost};
252: my $path = '/cgi-bin/mimetex.cgi?'.$esc_texstring;
253: my $socket;
254: &Apache::lonxml::start_alarm();
255: if ($protocol eq 'https') {
256: $socket = IO::Socket::SSL->new(PeerAddr => $hostname,
257: PeerPort => 'https(443)',
258: Proto => 'tcp');
259: } else {
260: $socket = IO::Socket::INET->new(PeerAddr => $hostname,
261: PeerPort => 'http(80)',
262: Proto => 'tcp');
263: }
264: if ($socket) {
1.96 raeburn 265: my $headreq = "HEAD $path HTTP/1.0\r\n\r\n";
1.94 raeburn 266: print $socket $headreq;
267: while (<$socket>) {
268: if (/Vertical\-Align\:\s*?([\-\d]+)/) {
269: $valign = $1;
270: }
271: }
272: if ($protocol eq 'https') {
273: $socket->close(SSL_no_shutdown => 1,
274: SSL_ctx_free => 1);
275: } else {
276: $socket->close();
277: }
1.92 hauer 278: }
1.94 raeburn 279: &Apache::lonxml::end_alarm();
280: return $valign;
1.92 hauer 281: }
282:
1.62 albertel 283: sub mimetex_converted {
284: my $texstring=shift;
1.97 www 285:
286: # Alt-Argument for screen readers
287: my $alt_string=$$texstring;
288: $alt_string=~s/\"/\'\'/g;
289:
290: # Is this displaystyle?
291:
1.62 albertel 292: my $displaystyle=&displaystyle($texstring);
293:
1.97 www 294: # Remove math environment delimiters
295:
1.62 albertel 296: &clean_out_math_mode($texstring);
297:
298: if ($displaystyle) {
299: $$texstring='\\displaystyle \\Large '.$$texstring;
1.59 albertel 300: }
1.94 raeburn 301: my $esc_texstring = &uri_escape($$texstring);
302: my $valign = &mimetex_valign($esc_texstring);
1.97 www 303: my $result='<img src="/cgi-bin/mimetex.cgi?'.$esc_texstring.'" style="vertical-align:'.$valign.'px" alt="'.$alt_string.'" />';
1.62 albertel 304: if ($displaystyle) {
305: $result='<center>'.$result.'</center>';
306: }
307: return $result;
1.59 albertel 308: }
309:
310: sub converted {
1.67 albertel 311: my ($string,$mode)=@_;
1.83 albertel 312: if ($mode eq '') { $mode = &tex_engine(); }
1.68 albertel 313: if ($mode =~ /tth/i) {
1.67 albertel 314: return &tth_converted($string);
1.68 albertel 315: } elsif ($mode =~ /jsmath/i) {
1.67 albertel 316: return &jsMath_converted($string);
1.68 albertel 317: } elsif ($mode =~ /mimetex/i) {
1.67 albertel 318: return &mimetex_converted($string);
1.98 www 319: } elsif ($mode =~ /raw/i) {
320: return $$string;
1.59 albertel 321: }
1.67 albertel 322: return &tth_converted($string);
1.59 albertel 323: }
324:
1.6 www 325: # ------------------------------------------------------------ Message display
326:
1.9 albertel 327: sub to_convert {
328: my ($string) = @_;
1.22 www 329: $string=~s/\<br\s*\/?\>/ /gs;
1.30 albertel 330: # $string=~s/\s/ /gs;
1.18 albertel 331: $string=&HTML::Entities::decode($string);
1.9 albertel 332: return &converted(\$string);
333: }
334:
1.20 www 335: sub smiley {
1.31 albertel 336: my $expression=shift;
1.65 albertel 337: if ($env{'browser.imagesuppress'} eq 'on') { return $expression; }
1.99 ! faziophi 338: my %smileys=(
! 339: '\:\-*\)' => 'face-smile.png',
! 340: '8\-*\)' => 'face-cool.png',
! 341: '8\-*(I|\|)' => 'face-glasses.png',
! 342: ':\-*(I|\|)' => 'face-plain.png',
! 343: '\:\-(o|O|\(\))' => 'face-surprise.png',
! 344: ':\-*\(' => 'face-sad.png',
! 345: '\;\-*\)' => 'face-wink.png',
! 346: '\:\-*(P|p)' => 'face-raspberry.png',
! 347: '\:\-*(\\\|\\/)' => 'face-uncertain.png',
! 348: '\:\-*D' => 'face-smile-big.png',
! 349: '\:\-*(C|\@)' => 'face-angry.png',
! 350: '\:(\'|\`)\-*\(' => 'face-crying.png',
! 351: '\:\-*(X|x|\#)' => 'face-quiet.png',
! 352: '\:\-*(s|S)' => 'face-uncertain.png',
! 353: '\:\-*\$' => 'face-embarrassed.png',
! 354: '\:\-*\*' => 'face-kiss.png',
! 355: '\+O\(' => 'face-sick.png',
! 356: '(\<\;3|\(heart\))' => 'heart.png',
! 357: '\(rose\)' => 'rose.png',
! 358: '\(pizza\)' => 'food-pizza.png',
! 359: '\(cake\)' => 'food-cake.png',
! 360: '\(ninja\)' => 'face-ninja.png',
! 361: '\(pirate\)' => 'face-pirate.png',
! 362: '\((agree|yes)\)' => 'opinion-agree.png',
! 363: '\((disagree|nay)\)' => 'opinion-disagree.png',
! 364: '(o|O)\-*\)' => 'face-angel.png',
! 365: );
1.31 albertel 366: my $iconpath=$Apache::lonnet::perlvar{'lonIconsURL'};
1.79 albertel 367: foreach my $smiley (keys(%smileys)) {
1.99 ! faziophi 368: $expression=~s/$smiley/\<img src="$iconpath\/$smileys{$smiley}" \/\>/gs;
1.31 albertel 369: }
370: return $expression;
1.20 www 371: }
372:
1.6 www 373: sub msgtexconverted {
1.39 raeburn 374: my ($message,$email) = @_;
1.17 albertel 375: $errorstring='';
1.29 albertel 376: &init_tth();
1.25 www 377: my $outmessage='';
378: my $tex=0;
1.79 albertel 379: foreach my $fragment (split(/(?:\<\;|\<)\/*m\s*(?:\>\;|\>)/i,$message)) {
1.25 www 380: if ($tex) {
1.30 albertel 381: if ($email) {
1.79 albertel 382: $outmessage.='</pre><tt>'.&to_convert($fragment).'</tt><pre>';
383: $tex=0;
1.30 albertel 384: } else {
1.79 albertel 385: $outmessage.=&to_convert($fragment);
386: $tex=0;
1.30 albertel 387: }
1.25 www 388: } else {
1.79 albertel 389: $outmessage.=&smiley($fragment);
390: $tex=1;
1.25 www 391: }
392: }
1.64 albertel 393: $message=$outmessage; $outmessage=''; $tex=0;
1.79 albertel 394: foreach my $fragment (split(/(?:\<\;|\<)\/*algebra\s*(?:\>\;|\>)/i,
395: $message)) {
1.64 albertel 396: if ($tex) {
397: if ($email) {
1.79 albertel 398: $outmessage.='</pre><tt>'.&algebra($fragment,'web').'</tt><pre>';
399: $tex=0;
1.64 albertel 400: } else {
1.79 albertel 401: $outmessage.=&algebra($fragment,'web');
402: $tex=0;
1.64 albertel 403: }
404: } else {
1.79 albertel 405: $outmessage.=$fragment;
406: $tex=1;
1.64 albertel 407: }
408: }
1.24 albertel 409: if (wantarray) {
1.25 www 410: return ($outmessage,$errorstring);
1.24 albertel 411: } else {
1.25 www 412: return $outmessage.$errorstring;
1.24 albertel 413: }
1.1 harris41 414: }
415:
1.44 albertel 416: sub algebra {
1.49 albertel 417: use AlgParser;
418:
1.89 droeschl 419: my ($string,$target,$style,$parstack,$safeeval)=@_;
1.44 albertel 420: my $parser = new AlgParserWithImplicitExpand;
1.46 albertel 421: $string=&prepare_algebra($string);
1.44 albertel 422: my $ret = $parser->parse($string);
423: my $result='['.&mt('Algebra unconverted due to previous errors').']';
424: if ( ref($ret) ) {
1.50 albertel 425: #$parser->tostring();
1.44 albertel 426: $parser->normalize();
427: my $latex=$parser->tolatex();
1.49 albertel 428: $latex=&postprocess_algebra($latex);
1.58 albertel 429: if ($style eq 'display') {
430: $latex='$$'.$latex.'$$x';
431: } else {
1.61 albertel 432: # style is 'inline'
1.58 albertel 433: $latex='\\ensuremath{'.$latex.'}';
434: }
1.44 albertel 435: if ($target eq 'web' || $target eq 'analyze') {
1.89 droeschl 436: my $display=&Apache::lonxml::get_param('display',$parstack,$safeeval);
437: $result = &converted(\$latex,$display);
438: # $result = &converted(\$latex);
1.44 albertel 439: } else {
440: $result = $latex;
441: }
442: } else {
443: &Apache::lonxml::error($parser->{'htmlerror'});
444: }
445: }
446:
1.46 albertel 447: sub prepare_algebra {
448: my ($string)=@_;
449:
1.50 albertel 450: # makes the decision about what is a minus sign easier supposedly
1.57 albertel 451: $string =~ s/(\<\>|\<\=|\>\=[\=\>\<] *)-/$1 zeroplace -/g;
1.48 albertel 452:
1.46 albertel 453: return $string;
454: }
455:
456: sub postprocess_algebra {
457: my ($string)=@_;
1.48 albertel 458:
1.50 albertel 459: # moodle had these and I don't know why, ignoring them for now
1.51 albertel 460: # $string =~s/\\fun/ /g;
1.47 albertel 461:
1.51 albertel 462: # sqrt(3,4) means the 4 root of 3
1.52 albertel 463: $string =~s/\\sqrt{([^,]+),([^\}]+)}/\\sqrt[$2]{$1}/gs;
464:
1.53 albertel 465: # log(3,4) means the log base 4 of 3
466: $string =~s/\\log\\left\((.+?),(.+?)\\right\)/\\log_{$2}\\left($1\\right)/gs;
467:
1.54 albertel 468: # log(3,4) means the log base 4 of 3
469: $string =~s/\\((?:sin|cos|tan|sec|csc|cot)(?:h)?)\\left\((.+?),(.+?)\\right\)/\\$1^{$3}\\left($2\\right)/gs;
470:
1.55 albertel 471: # int(3,a,b) integral from a to b of 3
472: $string =~s/\\int\\left\((.+?),(.+?),(.+?)\\right\)/\\int_{$2}^{$3}\\left($1\\right)/gs;
473:
474: # int( ... dx) -> ...
475: $string =~s/\\int\\left\((.+?)d[a-z]\\right\)/$1/gs;
1.51 albertel 476:
1.56 albertel 477: #
478: $string =~s/\\lim\\left\((.+?),(.+?),(.+?)\\right\)/\\lim_{$2\\to $3}$1/gs;
1.46 albertel 479: return $string;
480: }
1.1 harris41 481: 1;
482: __END__
483:
484:
1.91 jms 485: =pod
486:
487: =head1 NAME
488:
489: Apache::lontexconvert;
490:
491: =head1 SYNOPSIS
492:
493: Access to tth/ttm
494:
495: This is part of the LearningOnline Network with CAPA project
496: described at http://www.lon-capa.org.
497:
498:
499: =head1 SUBROUTINES
500:
501: =over
502:
503: =item init_tth()
504:
505: Header
506:
507: =item convert_real()
508:
509: we need this routine because &converted can get called from inside
510: of the safespace (through &xmlparse('<m>stuff</m>') which doesn't
511: allow the opcode for alarm, so we need to compile this before we get
512: into the safe space since opcode checks only occur at compile time
513:
514: =item tth_converted()
515:
516:
517: =item clean_out_math_mode()
518:
519:
520: =item displaystyle()
521:
522:
523: =item jsMath_converted()
524:
525:
526: =item tex_engine()
527:
528:
529: =item init_math_support()
530:
531:
532: =item mimetex_converted()
533:
534:
535: =item converted()
536:
537:
538: =item to_convert()
539:
540: message display
541:
542: =item smiley()
543:
544: ???
545:
546: =item msgtexconverted()
547:
548: =item algebra()
549:
550: =item prepare_algebra()
551:
552: =item postprocess_algebra()
1.1 harris41 553:
1.91 jms 554: =back
1.1 harris41 555:
1.91 jms 556: =cut
1.1 harris41 557:
558:
559:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>