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

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

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