Annotation of loncom/xml/lontexconvert.pm, revision 1.89

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>