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

1.1       harris41    1: # The LearningOnline Network with CAPA
                      2: # TeX Conversion Module
                      3: #
1.70    ! albertel    4: # $Id: lontexconvert.pm,v 1.69 2006/01/27 23:55:15 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.19      albertel   45: use Apache();
1.33      albertel   46: use Apache::lonmsg();
                     47: use Apache::lonxml();
                     48: use Apache::lonmenu();
                     49: use Apache::lonlocal;
1.65      albertel   50: use Apache::lonnet;
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'};
                     56:     if ($env{'browser.mathml'}) {
1.29      albertel   57: 	&tth::ttminit();
1.65      albertel   58: 	if ($env{'browser.unicode'}) {
1.29      albertel   59: 	    &tth::ttmoptions('-L -u1 '.$options);
                     60: 	} else {
                     61: 	    &tth::ttmoptions('-L -u0 '.$options);
                     62: 	}
                     63:     } else {
                     64: 	&tth::tthinit();
1.65      albertel   65: 	if ($env{'browser.unicode'}) {
1.29      albertel   66: 	    &tth::tthoptions('-L -u1 '.$options);
                     67: 	} else {
                     68: 	    &tth::tthoptions('-L -u0 '.$options);
                     69: 	}
                     70:     }
                     71: }
                     72: 
1.1       harris41   73: # ================================================================== Conversion
                     74: 
1.19      albertel   75: $Apache::lontexconvert::messedup=0;
1.36      albertel   76: 
                     77: # we need this routine because &converted can get called from inside
                     78: # of the safespace (through &xmlparse('<m>stuff</m>') which doesn't
                     79: # allow the opcode for alarm, so we need to compile this before we get
                     80: # into the safe space since opcode checks only occur at compile time
                     81: sub convert_real {
                     82:     my ($texstring)=@_;
                     83:     my ($xmlstring,$errorstring);
                     84:     local $SIG{SEGV}=sub { $Apache::lontexconvert::messedup=1; die; };
                     85:     local $SIG{ALRM}=sub { 
1.37      albertel   86: 	&Apache::lonnet::logthis("ALRM");
1.36      albertel   87: 	$xmlstring='['.&mt("TeX unconverted due to errors").']';
                     88: 	$Apache::lontexconvert::messedup=1;
                     89: 	die &mt("TeX unconverted due to errors");
                     90:     };
1.63      albertel   91:     &Apache::lonxml::start_alarm();
1.65      albertel   92:     if ($env{'browser.mathml'}) {
1.36      albertel   93: 	$xmlstring=&tth::ttm($$texstring);
                     94: 	$xmlstring=~s/\<math\>/\<math xmlns=\"\&mathns\;\"\>/g;
                     95: 	$xmlstring=~s/\<br\>/\<br\/\>/g;
                     96: 	$xmlstring=~s/\<p\>/\<p\>\<\/p\>/g;
                     97: 	$errorstring.=&tth::ttmerror();
                     98:     } else {
                     99: 	$xmlstring=&tth::tth($$texstring);
                    100: 	$errorstring.=&tth::ttherror();
                    101: 	$xmlstring=~s-</font(\s*)>-</font>-g;
                    102:     }
1.43      www       103:     $xmlstring=~s/^\s*\<br clear\=\"all\"/\<br/s;
1.36      albertel  104:     $xmlstring=~s/^\s*//;
                    105:     $xmlstring=~s/\s*$//;
1.63      albertel  106:     &Apache::lonxml::end_alarm();
1.36      albertel  107:     return ($xmlstring,$errorstring);
                    108: }
                    109: 
1.59      albertel  110: sub tth_converted {
1.31      albertel  111:     my $texstring=shift;
1.34      www       112:     my $xmlstring='['.&mt('UNDISPLAYABLE').']';
1.31      albertel  113:     if ($Apache::lontexconvert::messedup) {
1.35      albertel  114: 	return '['.&mt('TeX unconverted due to previous errors').']';
1.31      albertel  115:     }
1.59      albertel  116:     $$texstring ='\\documentstyle{article}'.$$texstring;
                    117: 
1.31      albertel  118:     eval(<<'ENDCONV');
1.36      albertel  119:     ($xmlstring,$errorstring)=&convert_real($texstring)
1.12      www       120: ENDCONV
1.32      albertel  121:     if ($@) {
1.35      albertel  122: 	$errorstring.=&mt("Evaluation Error: ").$@;
1.32      albertel  123: 	$Apache::lontexconvert::messedup=1;
                    124:     }
1.37      albertel  125:     if ($Apache::lontexconvert::messedup || &tth::tthmessedup() || 
                    126: 	$errorstring) {
1.31      albertel  127: 	&Apache::lonnet::logthis("Trying to kill myself");
                    128: 	$Apache::lontexconvert::messedup=1;
1.70    ! albertel  129: 	if (ref($Apache::lonxml::request)) {
        !           130: 	    $Apache::lonxml::request->child_terminate();
        !           131: 	} else {
        !           132: 	    my $request;
        !           133: 	    eval { $request=Apache->request; };
        !           134: 	    if (!$request) {
        !           135: 		eval { $request=Apache2::RequestUtil->request; };
        !           136: 	    }
        !           137: 	    if ($request) {
        !           138: 		$request->child_terminate();
        !           139: 	    } else {
        !           140: 		&Apache::lonnet::logthis("Unable to find a request to do child_terminate on");
        !           141: 	    }
        !           142: 	}
1.31      albertel  143:     }
                    144:     return $xmlstring;
1.1       harris41  145: }
                    146: 
1.62      albertel  147: sub clean_out_math_mode {
                    148:     my ($texstring)=@_;
                    149:     $$texstring=~s/(?!\\)\$//g;
                    150:     $$texstring=~s/\\[\)\(\]\[]//g;
                    151:     $$texstring=~s/\\ensuremath//g;
                    152:     return '';
                    153: }
                    154: 
                    155: sub displaystyle {
                    156:     my ($texstring)=@_;
                    157:     #has a $$ or \[ or \displaystyle in it, guessinng it's display mode
                    158:     if ($$texstring=~/[^\\]\$\$/ ||
                    159: 	$$texstring=~/\\\[/ ||
                    160: 	$$texstring=~/\\displaystyle/) { return 1; }
                    161:     return 0;
                    162: }
                    163: 
1.59      albertel  164: sub jsMath_converted {
                    165:     my $texstring=shift;
                    166:     my $tag='span';
1.62      albertel  167:     if (&displaystyle($texstring)) { $tag='div'; }
                    168:     &clean_out_math_mode($texstring);
1.67      albertel  169:     return &jsMath_header().
                    170: 	'<'.$tag.' class="math">'.$$texstring.'</'.$tag.'>';
                    171: }
                    172: 
                    173: {
                    174:     my $jsMath_sent_header;
                    175:     sub jsMath_reset {
                    176: 	$jsMath_sent_header=0;
                    177:     }
                    178:     sub jsMath_header {
                    179: 	return '' if $jsMath_sent_header;
                    180: 	$jsMath_sent_header=1;
                    181: 	return
                    182:             '<script type="text/javascript">
                    183:                      function NoFontMessage () {}
                    184:                    </script>'."\n".
                    185: 	    '<script src="/adm/jsMath/jsMath.js"></script>'."\n";
                    186:     }
                    187:     sub jsMath_process {
                    188: 	return '' if !$jsMath_sent_header;
                    189: 	return '<script type="text/javascript">jsMath.Process()</script>';
                    190:     }
1.62      albertel  191: }
                    192: 
                    193: sub mimetex_converted {
                    194:     my $texstring=shift;
                    195:     my $displaystyle=&displaystyle($texstring);
                    196: 
                    197:     &clean_out_math_mode($texstring);
                    198: 
                    199:     if ($displaystyle) {
                    200: 	$$texstring='\\displaystyle \\Large '.$$texstring;
1.59      albertel  201:     }
1.62      albertel  202:     my $result='<img src="/cgi-bin/mimetex.cgi?'.&Apache::lonnet::escape($$texstring).'" />';
                    203:     if ($displaystyle) {
                    204: 	$result='<center>'.$result.'</center>';
                    205:     }
                    206:     return $result;
1.59      albertel  207: }
                    208: 
                    209: sub converted {
1.67      albertel  210:     my ($string,$mode)=@_;
                    211:     if ($mode eq '') { $mode=$env{'environment.texengine'}; }
1.68      albertel  212:     if ($mode =~ /tth/i) {
1.67      albertel  213: 	return &tth_converted($string);
1.68      albertel  214:     } elsif ($mode =~ /jsmath/i) {
1.67      albertel  215: 	return &jsMath_converted($string);
1.68      albertel  216:     } elsif ($mode =~ /mimetex/i) {
1.67      albertel  217: 	return &mimetex_converted($string);
1.59      albertel  218:     }
1.67      albertel  219:     return &tth_converted($string);
1.59      albertel  220: }
                    221: 
1.6       www       222: # ------------------------------------------------------------ Message display
                    223: 
1.9       albertel  224: sub to_convert {
                    225:     my ($string) = @_;
1.22      www       226:     $string=~s/\<br\s*\/?\>/ /gs;
1.30      albertel  227: #    $string=~s/\s/ /gs;
1.18      albertel  228:     $string=&HTML::Entities::decode($string);
1.9       albertel  229:     return &converted(\$string);
                    230: }
                    231: 
1.20      www       232: sub smiley {
1.31      albertel  233:     my $expression=shift;
1.65      albertel  234:     if ($env{'browser.imagesuppress'} eq 'on') { return $expression; }
1.31      albertel  235:     my %smileys=('\:\-\)' => 'smiley',
                    236: 		 '8\-\)'  => 'coolsmile',
                    237: 		 '8\-(I|\|)'   => 'coolindiff',
                    238: 		 ':\-(I|\|)'   => 'neutral',
                    239: 		 '\:\-(o|O|\(\))' => 'shocked',
                    240: 		 ':\-\('  => 'frowny',
                    241: 		 '\;\-\)' => 'wink',
                    242: 		 '\:\-P'  => 'baeh',
                    243: 		 '\:\-(\\\|\\/)' => 'hrrm',
                    244: 		 '\:\-D'  => 'bigsmile',
                    245: 		 '\:\-C'  => 'angry',
                    246: 		 '\:(\'|\`)\-\(' => 'cry',
                    247: 		 '\:\-(X|\#)' => 'lipsrsealed',
                    248: 		 '\:\-S' => 'huh');
                    249:     my $iconpath=$Apache::lonnet::perlvar{'lonIconsURL'};
                    250:     foreach (keys %smileys) {
                    251: 	$expression=~s/$_/\<img src="$iconpath\/$smileys{$_}.gif" \/\>/gs; 
                    252:     }
                    253:     return $expression;
1.20      www       254: }
                    255: 
1.6       www       256: sub msgtexconverted {
1.39      raeburn   257:     my ($message,$email) = @_;
1.17      albertel  258:     $errorstring='';
1.29      albertel  259:     &init_tth();
1.25      www       260:     my $outmessage='';
                    261:     my $tex=0;
                    262:     foreach (split(/(?:\&lt\;|\<)\/*m\s*(?:\&gt\;|\>)/i,$message)) {
                    263: 	if ($tex) {
1.30      albertel  264: 	    if ($email) {
                    265: 		$outmessage.='</pre><tt>'.&to_convert($_).'</tt><pre>'; $tex=0;
                    266: 	    } else {
                    267: 		$outmessage.=&to_convert($_); $tex=0;
                    268: 	    }
1.25      www       269: 	} else {
                    270:             $outmessage.=&smiley($_); $tex=1;
                    271: 	}
                    272:     }
1.64      albertel  273:     $message=$outmessage; $outmessage=''; $tex=0;
                    274:     foreach (split(/(?:\&lt\;|\<)\/*algebra\s*(?:\&gt\;|\>)/i,$message)) {
                    275: 	if ($tex) {
                    276: 	    if ($email) {
                    277: 		$outmessage.='</pre><tt>'.&algebra($_,'web').'</tt><pre>'; $tex=0;
                    278: 	    } else {
                    279: 		$outmessage.=&algebra($_,'web'); $tex=0;
                    280: 	    }
                    281: 	} else {
                    282:             $outmessage.=$_; $tex=1;
                    283: 	}
                    284:     }
1.24      albertel  285:     if (wantarray) {
1.25      www       286: 	return ($outmessage,$errorstring);
1.24      albertel  287:     } else {
1.25      www       288: 	return $outmessage.$errorstring;
1.24      albertel  289:     }
1.1       harris41  290: }
                    291: 
1.44      albertel  292: sub algebra {
1.49      albertel  293:     use AlgParser;
                    294: 
1.58      albertel  295:     my ($string,$target,$style)=@_;
1.44      albertel  296:     my $parser = new AlgParserWithImplicitExpand;
1.46      albertel  297:     $string=&prepare_algebra($string);
1.44      albertel  298:     my $ret = $parser->parse($string);
                    299:     my $result='['.&mt('Algebra unconverted due to previous errors').']';
                    300:     if ( ref($ret) ) {
1.50      albertel  301: 	#$parser->tostring();
1.44      albertel  302: 	$parser->normalize();
                    303: 	my $latex=$parser->tolatex();
1.49      albertel  304: 	$latex=&postprocess_algebra($latex);
1.58      albertel  305: 	if ($style eq 'display') {
                    306: 	    $latex='$$'.$latex.'$$x';
                    307: 	} else {
1.61      albertel  308: 	    # style is 'inline'
1.58      albertel  309: 	    $latex='\\ensuremath{'.$latex.'}';
                    310: 	}
1.44      albertel  311: 	if ($target eq 'web' || $target eq 'analyze') {
                    312: 	    $result = &converted(\$latex);
                    313: 	} else {
                    314: 	    $result = $latex;
                    315: 	}
                    316:     } else {
                    317: 	&Apache::lonxml::error($parser->{'htmlerror'});
                    318:     }
                    319: }
                    320: 
1.46      albertel  321: sub prepare_algebra {
                    322:     my ($string)=@_;
                    323: 
1.50      albertel  324:     # makes the decision about what is a minus sign easier supposedly
1.57      albertel  325:     $string =~ s/(\<\>|\<\=|\>\=[\=\>\<] *)-/$1 zeroplace -/g;
1.48      albertel  326: 
1.46      albertel  327:     return $string;
                    328: }
                    329: 
                    330: sub postprocess_algebra {
                    331:     my ($string)=@_;
1.48      albertel  332:     
1.50      albertel  333:     # moodle had these and I don't know why, ignoring them for now
1.51      albertel  334:     # $string =~s/\\fun/ /g;
1.47      albertel  335: 
1.55      albertel  336:     # remove the extra () in the denominator of a \frac
                    337:     $string =~s/\\frac{(.+?)}{\\left\((.+?)\\right\)}/\\frac{$1}{$2}/gs;
1.51      albertel  338:     
                    339:     # sqrt(3,4) means the 4 root of 3
1.52      albertel  340:     $string =~s/\\sqrt{([^,]+),([^\}]+)}/\\sqrt[$2]{$1}/gs;
                    341: 
1.53      albertel  342:     # log(3,4) means the log base 4 of 3
                    343:     $string =~s/\\log\\left\((.+?),(.+?)\\right\)/\\log_{$2}\\left($1\\right)/gs;
                    344: 
1.54      albertel  345:     # log(3,4) means the log base 4 of 3
                    346:     $string =~s/\\((?:sin|cos|tan|sec|csc|cot)(?:h)?)\\left\((.+?),(.+?)\\right\)/\\$1^{$3}\\left($2\\right)/gs;
                    347: 
1.55      albertel  348:     # int(3,a,b) integral from a to b of 3
                    349:     $string =~s/\\int\\left\((.+?),(.+?),(.+?)\\right\)/\\int_{$2}^{$3}\\left($1\\right)/gs;
                    350: 
                    351:     # int( ... dx) -> ...
                    352:     $string =~s/\\int\\left\((.+?)d[a-z]\\right\)/$1/gs;
1.51      albertel  353: 
1.56      albertel  354:     # 
                    355:     $string =~s/\\lim\\left\((.+?),(.+?),(.+?)\\right\)/\\lim_{$2\\to $3}$1/gs;
1.46      albertel  356:     return $string;
                    357: }
1.1       harris41  358: 1;
                    359: __END__
                    360: 
                    361: 
                    362: 
                    363: 
                    364: 
                    365: 
                    366: 

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