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