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

1.1       harris41    1: # The LearningOnline Network with CAPA
                      2: # TeX Conversion Module
                      3: #
1.112.2.1! raeburn     4: # $Id: lontexconvert.pm,v 1.112 2012/03/11 14:46:46 foxr 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;
1.1       harris41   52: 
1.110     foxr       53: 
                     54: #
                     55: # Table of substitutions to unicode characters.
                     56: #
                     57: my %unicode_translations = (
                     58:     '\rightleftharpoons'  => 0x21cc,
1.111     foxr       59: 
                     60: # Brackets - unicode is commented out with pure 8-bit ascii ugliness while we need it.
                     61: 
                     62: #    ''             => 0x23a1,
                     63: #    ''             => 0x23a2,
                     64: #    ''             => 0x23a3,   # when unicode catches up with browsers
                     65: #    ''             => 0x23a4,   # use these instead of the cheesey brackets below
                     66: #    ''             => 0x23a5,
                     67: #    ''             => 0x23a6   
                     68:     ''              => 0x5b,
                     69:     ''              => 0x5b,    # '['
                     70:     ''              => 0x5b,
                     71:     ''              => 0x5d,    # ']'
                     72:     ''              => 0x5d,
                     73:     ''              => 0x5d,
                     74: 
                     75: #  Parens..again the unicode is commented out with the 8-bit ascii ugliness
                     76: #  turned on until browsers catch up with the unicode world.
                     77: 
                     78: #    ''              => 0x239b,
                     79: #    ''              => 0x239c,
                     80: #    ''              => 0x239d,
                     81: #    ''              => 0x239e,
                     82: #    ''              => 0x239f,
                     83: #    ''              => 0x23a0
                     84: 
                     85:     ''              => 0x28,
                     86:     ''              => 0x28,      # '('
                     87:     ''              => 0x28,
                     88: 
                     89:     ''              => 0x29,
                     90:     ''              => 0x29,      # '('
                     91:     ''              => 0x29
                     92: 
                     93: 
1.110     foxr       94: );
                     95: 
                     96: ##
                     97: # Utility to convert elements of a string to unicode:
                     98: #
                     99: # @param input - Input string
                    100: # @param pattern - Pattern to convert
                    101: # @param unicode - Unicode to substitute for pattern.
                    102: #
                    103: # @return string - resulting string.
                    104: # 
                    105: sub unicode_subst {
                    106:     my ($input, $pattern, $unicode) = @_;
                    107:     
                    108:     my $char = pack('U', $unicode);
                    109: 
                    110:     $input =~ s/$pattern/$char/g;
                    111: 
                    112:     return $input;
                    113: }
                    114: 
1.1       harris41  115: # ====================================================================== Header
                    116: 
1.29      albertel  117: sub init_tth {
1.65      albertel  118:     my $options=$env{'course.'.$env{'request.course.id'}.'.tthoptions'};
1.74      albertel  119:     if ($options =~ /\S/) {
                    120: 	$options = ' '.$options;
                    121:     } else {
                    122: 	undef($options);
                    123:     }
1.65      albertel  124:     if ($env{'browser.mathml'}) {
1.29      albertel  125: 	&tth::ttminit();
1.65      albertel  126: 	if ($env{'browser.unicode'}) {
1.74      albertel  127: 	    &tth::ttmoptions('-L -u1'.$options);
1.29      albertel  128: 	} else {
1.74      albertel  129: 	    &tth::ttmoptions('-L -u0'.$options);
1.29      albertel  130: 	}
                    131:     } else {
                    132: 	&tth::tthinit();
1.65      albertel  133: 	if ($env{'browser.unicode'}) {
1.74      albertel  134: 	    &tth::tthoptions('-L -u1'.$options);
1.29      albertel  135: 	} else {
1.74      albertel  136: 	    &tth::tthoptions('-L -u0'.$options);
1.29      albertel  137: 	}
                    138:     }
                    139: }
                    140: 
1.1       harris41  141: # ================================================================== Conversion
                    142: 
1.19      albertel  143: $Apache::lontexconvert::messedup=0;
1.36      albertel  144: 
1.91      jms       145: 
1.36      albertel  146: sub convert_real {
                    147:     my ($texstring)=@_;
                    148:     my ($xmlstring,$errorstring);
                    149:     local $SIG{SEGV}=sub { $Apache::lontexconvert::messedup=1; die; };
                    150:     local $SIG{ALRM}=sub { 
1.37      albertel  151: 	&Apache::lonnet::logthis("ALRM");
1.36      albertel  152: 	$xmlstring='['.&mt("TeX unconverted due to errors").']';
                    153: 	$Apache::lontexconvert::messedup=1;
                    154: 	die &mt("TeX unconverted due to errors");
                    155:     };
1.63      albertel  156:     &Apache::lonxml::start_alarm();
1.65      albertel  157:     if ($env{'browser.mathml'}) {
1.36      albertel  158: 	$xmlstring=&tth::ttm($$texstring);
                    159: 	$xmlstring=~s/\<math\>/\<math xmlns=\"\&mathns\;\"\>/g;
                    160: 	$xmlstring=~s/\<br\>/\<br\/\>/g;
                    161: 	$xmlstring=~s/\<p\>/\<p\>\<\/p\>/g;
                    162: 	$errorstring.=&tth::ttmerror();
                    163:     } else {
                    164: 	$xmlstring=&tth::tth($$texstring);
                    165: 	$errorstring.=&tth::ttherror();
                    166: 	$xmlstring=~s-</font(\s*)>-</font>-g;
                    167:     }
1.43      www       168:     $xmlstring=~s/^\s*\<br clear\=\"all\"/\<br/s;
1.36      albertel  169:     $xmlstring=~s/^\s*//;
                    170:     $xmlstring=~s/\s*$//;
1.110     foxr      171:     &Apache::lonxml::end_alarm();
                    172: 
1.85      foxr      173:     #
1.110     foxr      174:     #  Several strings produced by tth require
                    175:     # transliteration -> unicode equivalents to render reliably
                    176:     # in browsers. %unicode_translations is a table of
                    177:     # string->substitution which we now apply:
                    178: 
                    179:     foreach my $pattern (keys(%unicode_translations)) {
                    180: 	my $unicode = $unicode_translations{$pattern};
                    181: 	$xmlstring = &unicode_subst($xmlstring, $pattern, $unicode);
                    182:     }
                    183: 
1.85      foxr      184: 
1.36      albertel  185:     return ($xmlstring,$errorstring);
                    186: }
                    187: 
1.59      albertel  188: sub tth_converted {
1.31      albertel  189:     my $texstring=shift;
1.34      www       190:     my $xmlstring='['.&mt('UNDISPLAYABLE').']';
1.31      albertel  191:     if ($Apache::lontexconvert::messedup) {
1.35      albertel  192: 	return '['.&mt('TeX unconverted due to previous errors').']';
1.31      albertel  193:     }
1.59      albertel  194:     $$texstring ='\\documentstyle{article}'.$$texstring;
                    195: 
1.31      albertel  196:     eval(<<'ENDCONV');
1.36      albertel  197:     ($xmlstring,$errorstring)=&convert_real($texstring)
1.12      www       198: ENDCONV
1.32      albertel  199:     if ($@) {
1.35      albertel  200: 	$errorstring.=&mt("Evaluation Error: ").$@;
1.32      albertel  201: 	$Apache::lontexconvert::messedup=1;
                    202:     }
1.37      albertel  203:     if ($Apache::lontexconvert::messedup || &tth::tthmessedup() || 
                    204: 	$errorstring) {
1.31      albertel  205: 	&Apache::lonnet::logthis("Trying to kill myself");
                    206: 	$Apache::lontexconvert::messedup=1;
1.70      albertel  207: 	if (ref($Apache::lonxml::request)) {
                    208: 	    $Apache::lonxml::request->child_terminate();
                    209: 	} else {
                    210: 	    my $request;
                    211: 	    eval { $request=Apache->request; };
                    212: 	    if (!$request) {
                    213: 		eval { $request=Apache2::RequestUtil->request; };
                    214: 	    }
                    215: 	    if ($request) {
                    216: 		$request->child_terminate();
                    217: 	    } else {
                    218: 		&Apache::lonnet::logthis("Unable to find a request to do child_terminate on");
                    219: 	    }
                    220: 	}
1.31      albertel  221:     }
                    222:     return $xmlstring;
1.1       harris41  223: }
                    224: 
1.62      albertel  225: sub clean_out_math_mode {
                    226:     my ($texstring)=@_;
1.82      albertel  227:     $$texstring=~s/(?<!\\)\$//g;
1.62      albertel  228:     $$texstring=~s/\\[\)\(\]\[]//g;
                    229:     $$texstring=~s/\\ensuremath//g;
                    230:     return '';
                    231: }
                    232: 
                    233: sub displaystyle {
                    234:     my ($texstring)=@_;
1.106     bisitz    235:     #has a $$ or \[ or \displaystyle or eqnarray in it, guessinng it's display mode
1.62      albertel  236:     if ($$texstring=~/[^\\]\$\$/ ||
1.106     bisitz    237:         $$texstring=~/\\\[/ ||
                    238:         $$texstring=~/\\displaystyle/ ||
                    239:         $$texstring=~/eqnarray/
                    240:        ) { return 1; }
1.62      albertel  241:     return 0;
                    242: }
                    243: 
1.59      albertel  244: sub jsMath_converted {
                    245:     my $texstring=shift;
                    246:     my $tag='span';
1.62      albertel  247:     if (&displaystyle($texstring)) { $tag='div'; }
                    248:     &clean_out_math_mode($texstring);
1.67      albertel  249:     return &jsMath_header().
                    250: 	'<'.$tag.' class="math">'.$$texstring.'</'.$tag.'>';
                    251: }
                    252: 
                    253: {
1.76      albertel  254:     my @jsMath_sent_header;
1.67      albertel  255:     sub jsMath_reset {
1.76      albertel  256: 	undef(@jsMath_sent_header);
                    257:     }
                    258:     sub jsMath_push {
                    259: 	push(@jsMath_sent_header,0);
1.67      albertel  260:     }
                    261:     sub jsMath_header {
1.80      albertel  262: 	if (!@jsMath_sent_header) {
                    263: 	    &Apache::lonnet::logthis("mismatched calls of jsMath_header and jsMath_process");
                    264: 	    return '';
                    265: 	}
1.76      albertel  266: 	return '' if $jsMath_sent_header[-1];
                    267: 	$jsMath_sent_header[-1]=1;
1.67      albertel  268: 	return
                    269:             '<script type="text/javascript">
                    270:                      function NoFontMessage () {}
1.78      albertel  271:                      jsMath = {Parser: {prototype: {environments: {\'eqnarray*\' :[\'Array\',null,null,\'rcl\',[5/18,5/18],3,\'D\']}}}};
1.67      albertel  272:                    </script>'."\n".
1.71      albertel  273: 	    '<script type="text/javascript" src="/adm/jsMath/jsMath.js"></script>'."\n";
1.67      albertel  274:     }
                    275:     sub jsMath_process {
1.76      albertel  276: 	my $state = pop(@jsMath_sent_header);
                    277: 	return '' if !$state;
1.81      albertel  278: 	return "\n".
                    279: 	    '<script type="text/javascript">jsMath.Process()</script>'."\n";
1.67      albertel  280:     }
1.84      albertel  281:     sub jsMath_state {
                    282: 	my ($level) = @_;
                    283: 	return $jsMath_sent_header[$level];
                    284:     }
1.62      albertel  285: }
                    286: 
1.83      albertel  287: sub tex_engine {
                    288:     if (exists($env{'form.texengine'})) {
1.88      raeburn   289: 	if ($env{'form.texengine'} ne '') {
                    290:             return $env{'form.texengine'};
                    291:         }
1.83      albertel  292:     }    
                    293:     if ($env{'request.course.id'}
                    294: 	&& exists($env{'course.'.$env{'request.course.id'}.'.texengine'})) {
                    295: 	return $env{'course.'.$env{'request.course.id'}.'.texengine'};
                    296:     }
                    297:     if (exists($env{'environment.texengine'})) {
                    298: 	return $env{'environment.texengine'};
                    299:     }
                    300:     return 'tth';
                    301: }
                    302: 
1.72      albertel  303: sub init_math_support {
1.84      albertel  304:     my ($inherit_jsmath) = @_;
1.73      albertel  305:     &init_tth();
1.76      albertel  306:     &Apache::lontexconvert::jsMath_push();
1.84      albertel  307:     if (lc(&tex_engine()) eq 'jsmath' ||
                    308: 	($inherit_jsmath && &jsMath_state(-2))) {
1.72      albertel  309: 	return &Apache::lontexconvert::jsMath_header();
                    310:     }
                    311:     return;
                    312: }
                    313: 
1.92      hauer     314: sub mimetex_valign {
1.94      raeburn   315:     my ($esc_texstring)=@_;
                    316:     my $valign = 0;
                    317:     my $path = '/cgi-bin/mimetex.cgi?'.$esc_texstring;
                    318:     my $socket;
                    319:     &Apache::lonxml::start_alarm();
1.108     raeburn   320:     $socket = IO::Socket::INET->new(PeerAddr => 'localhost',
1.107     raeburn   321:                                     PeerPort => 'http(80)',
                    322:                                     Proto    => 'tcp');
1.94      raeburn   323:     if ($socket) {
1.96      raeburn   324:         my $headreq = "HEAD $path HTTP/1.0\r\n\r\n";
1.94      raeburn   325:         print $socket $headreq;
                    326:         while (<$socket>) {
                    327:             if (/Vertical\-Align\:\s*?([\-\d]+)/) {
                    328:                 $valign = $1;
                    329:             }
                    330:         }
1.105     raeburn   331:         $socket->close();
1.92      hauer     332:     }
1.94      raeburn   333:     &Apache::lonxml::end_alarm();
                    334:     return $valign;
1.92      hauer     335: }
                    336: 
1.62      albertel  337: sub mimetex_converted {
                    338:     my $texstring=shift;
1.97      www       339: 
                    340: # Alt-Argument for screen readers
                    341:     my $alt_string=$$texstring;
                    342:     $alt_string=~s/\"/\'\'/g;
                    343: 
                    344: # Is this displaystyle?
                    345: 
1.62      albertel  346:     my $displaystyle=&displaystyle($texstring);
                    347: 
1.97      www       348: # Remove math environment delimiters
                    349: 
1.62      albertel  350:     &clean_out_math_mode($texstring);
                    351: 
                    352:     if ($displaystyle) {
                    353: 	$$texstring='\\displaystyle \\Large '.$$texstring;
1.59      albertel  354:     }
1.94      raeburn   355:     my $esc_texstring = &uri_escape($$texstring);
                    356:     my $valign = &mimetex_valign($esc_texstring);
1.97      www       357:     my $result='<img src="/cgi-bin/mimetex.cgi?'.$esc_texstring.'" style="vertical-align:'.$valign.'px" alt="'.$alt_string.'" />';
1.62      albertel  358:     if ($displaystyle) {
1.106     bisitz    359: 	$result='<div style="text-align:center">'.$result.'</div>';
1.62      albertel  360:     }
                    361:     return $result;
1.59      albertel  362: }
                    363: 
                    364: sub converted {
1.67      albertel  365:     my ($string,$mode)=@_;
1.83      albertel  366:     if ($mode eq '') { $mode = &tex_engine(); }
1.68      albertel  367:     if ($mode =~ /tth/i) {
1.67      albertel  368: 	return &tth_converted($string);
1.68      albertel  369:     } elsif ($mode =~ /jsmath/i) {
1.67      albertel  370: 	return &jsMath_converted($string);
1.68      albertel  371:     } elsif ($mode =~ /mimetex/i) {
1.67      albertel  372: 	return &mimetex_converted($string);
1.98      www       373:     } elsif ($mode =~ /raw/i) {
                    374:         return $$string;
1.59      albertel  375:     }
1.67      albertel  376:     return &tth_converted($string);
1.59      albertel  377: }
                    378: 
1.6       www       379: # ------------------------------------------------------------ Message display
                    380: 
1.9       albertel  381: sub to_convert {
                    382:     my ($string) = @_;
1.102     faziophi  383:     &init_tth();
1.22      www       384:     $string=~s/\<br\s*\/?\>/ /gs;
1.30      albertel  385: #    $string=~s/\s/ /gs;
1.18      albertel  386:     $string=&HTML::Entities::decode($string);
1.9       albertel  387:     return &converted(\$string);
                    388: }
                    389: 
1.20      www       390: sub smiley {
1.31      albertel  391:     my $expression=shift;
1.99      faziophi  392:     my %smileys=(
                    393:     	 '\:\-*\)' => 'face-smile.png',
1.100     faziophi  394: 		 '8\-\)'  => 'face-cool.png',
                    395: 		 '8\-(I|\|)'   => 'face-glasses.png',
1.101     faziophi  396: 		 '\:\-(I|\|)'   => 'face-plain.png',
1.99      faziophi  397: 		 '\:\-(o|O|\(\))' => 'face-surprise.png',
1.101     faziophi  398: 		 ':\-\('  => 'face-sad.png',
                    399: 		 '\;\-\)' => 'face-wink.png',
                    400: 		 '\:\-(P|p)'  => 'face-raspberry.png',
1.100     faziophi  401: 		 '\:\-(\\\|\\/)' => 'face-uncertain.png',
1.101     faziophi  402: 		 '\:\-D'  => 'face-smile-big.png',
                    403: 		 '\:\-(C|\@)'  => 'face-angry.png',
1.99      faziophi  404: 		 '\:(\'|\`)\-*\(' => 'face-crying.png',
1.101     faziophi  405: 		 '\:\-(X|x|\#)' => 'face-quiet.png',
                    406: 		 '\:\-(s|S)' => 'face-uncertain.png',
                    407: 		 '\:\-\$' => 'face-embarrassed.png',
                    408: 		 '\:\-\*' => 'face-kiss.png',
1.99      faziophi  409: 		 '\+O\(' => 'face-sick.png',
                    410: 		 '(\&lt\;3|\(heart\))' => 'heart.png',
                    411: 		 '\(rose\)' => 'rose.png',
                    412: 		 '\(pizza\)' => 'food-pizza.png',
                    413: 		 '\(cake\)' => 'food-cake.png',
                    414: 		 '\(ninja\)' => 'face-ninja.png',
                    415: 		 '\(pirate\)' => 'face-pirate.png',
                    416: 		 '\((agree|yes)\)' => 'opinion-agree.png',
                    417: 		 '\((disagree|nay)\)' => 'opinion-disagree.png',
1.101     faziophi  418: 		 '(o|O)\-\)' => 'face-angel.png',
1.99      faziophi  419: 		 );
1.31      albertel  420:     my $iconpath=$Apache::lonnet::perlvar{'lonIconsURL'};
1.79      albertel  421:     foreach my $smiley (keys(%smileys)) {
1.99      faziophi  422: 	$expression=~s/$smiley/\<img src="$iconpath\/$smileys{$smiley}" \/\>/gs; 
1.31      albertel  423:     }
                    424:     return $expression;
1.20      www       425: }
                    426: 
1.6       www       427: sub msgtexconverted {
1.39      raeburn   428:     my ($message,$email) = @_;
1.17      albertel  429:     $errorstring='';
1.25      www       430:     my $outmessage='';
                    431:     my $tex=0;
1.79      albertel  432:     foreach my $fragment (split(/(?:\&lt\;|\<)\/*m\s*(?:\&gt\;|\>)/i,$message)) {
1.25      www       433: 	if ($tex) {
1.30      albertel  434: 	    if ($email) {
1.79      albertel  435: 		$outmessage.='</pre><tt>'.&to_convert($fragment).'</tt><pre>';
                    436: 		$tex=0;
1.30      albertel  437: 	    } else {
1.79      albertel  438: 		$outmessage.=&to_convert($fragment);
                    439: 		$tex=0;
1.30      albertel  440: 	    }
1.25      www       441: 	} else {
1.79      albertel  442:             $outmessage.=&smiley($fragment);
                    443: 	    $tex=1;
1.25      www       444: 	}
                    445:     }
1.64      albertel  446:     $message=$outmessage; $outmessage=''; $tex=0;
1.79      albertel  447:     foreach my $fragment (split(/(?:\&lt\;|\<)\/*algebra\s*(?:\&gt\;|\>)/i,
                    448: 				$message)) {
1.64      albertel  449: 	if ($tex) {
1.103     faziophi  450:         my $algebra = &algebra($fragment, 'web', undef, undef, undef, undef, 'tth');
1.64      albertel  451: 	    if ($email) {
1.103     faziophi  452: 		$outmessage.='</pre><tt>'.$algebra.'</tt><pre>';
1.79      albertel  453: 		$tex=0;
1.64      albertel  454: 	    } else {
1.103     faziophi  455: 		$outmessage.=$algebra;
1.79      albertel  456: 		$tex=0;
1.64      albertel  457: 	    }
                    458: 	} else {
1.103     faziophi  459:         $outmessage.=$fragment;
1.79      albertel  460: 	    $tex=1;
1.64      albertel  461: 	}
                    462:     }
1.24      albertel  463:     if (wantarray) {
1.25      www       464: 	return ($outmessage,$errorstring);
1.24      albertel  465:     } else {
1.25      www       466: 	return $outmessage.$errorstring;
1.24      albertel  467:     }
1.1       harris41  468: }
                    469: 
1.44      albertel  470: sub algebra {
1.49      albertel  471:     use AlgParser;
1.103     faziophi  472:     my ($string,$target,$style,$parstack,$safeeval,$tth)=@_;
1.44      albertel  473:     my $parser = new AlgParserWithImplicitExpand;
1.103     faziophi  474:     if ($tth eq 'tth') {&init_tth();}
1.46      albertel  475:     $string=&prepare_algebra($string);
1.44      albertel  476:     my $ret = $parser->parse($string);
                    477:     my $result='['.&mt('Algebra unconverted due to previous errors').']';
                    478:     if ( ref($ret) ) {
1.50      albertel  479: 	#$parser->tostring();
1.44      albertel  480: 	$parser->normalize();
                    481: 	my $latex=$parser->tolatex();
1.49      albertel  482: 	$latex=&postprocess_algebra($latex);
1.58      albertel  483: 	if ($style eq 'display') {
                    484: 	    $latex='$$'.$latex.'$$x';
                    485: 	} else {
1.61      albertel  486: 	    # style is 'inline'
1.58      albertel  487: 	    $latex='\\ensuremath{'.$latex.'}';
                    488: 	}
1.44      albertel  489: 	if ($target eq 'web' || $target eq 'analyze') {
1.89      droeschl  490:             my $display=&Apache::lonxml::get_param('display',$parstack,$safeeval);
                    491:             $result = &converted(\$latex,$display);
                    492: #	    $result = &converted(\$latex);
1.44      albertel  493: 	} else {
                    494: 	    $result = $latex;
                    495: 	}
                    496:     } else {
                    497: 	&Apache::lonxml::error($parser->{'htmlerror'});
                    498:     }
                    499: }
                    500: 
1.46      albertel  501: sub prepare_algebra {
                    502:     my ($string)=@_;
                    503: 
1.50      albertel  504:     # makes the decision about what is a minus sign easier supposedly
1.57      albertel  505:     $string =~ s/(\<\>|\<\=|\>\=[\=\>\<] *)-/$1 zeroplace -/g;
1.48      albertel  506: 
1.46      albertel  507:     return $string;
                    508: }
                    509: 
                    510: sub postprocess_algebra {
                    511:     my ($string)=@_;
1.48      albertel  512:     
1.50      albertel  513:     # moodle had these and I don't know why, ignoring them for now
1.51      albertel  514:     # $string =~s/\\fun/ /g;
1.47      albertel  515: 
1.51      albertel  516:     # sqrt(3,4) means the 4 root of 3
1.52      albertel  517:     $string =~s/\\sqrt{([^,]+),([^\}]+)}/\\sqrt[$2]{$1}/gs;
                    518: 
1.53      albertel  519:     # log(3,4) means the log base 4 of 3
                    520:     $string =~s/\\log\\left\((.+?),(.+?)\\right\)/\\log_{$2}\\left($1\\right)/gs;
                    521: 
1.54      albertel  522:     # log(3,4) means the log base 4 of 3
                    523:     $string =~s/\\((?:sin|cos|tan|sec|csc|cot)(?:h)?)\\left\((.+?),(.+?)\\right\)/\\$1^{$3}\\left($2\\right)/gs;
                    524: 
1.55      albertel  525:     # int(3,a,b) integral from a to b of 3
                    526:     $string =~s/\\int\\left\((.+?),(.+?),(.+?)\\right\)/\\int_{$2}^{$3}\\left($1\\right)/gs;
                    527: 
                    528:     # int( ... dx) -> ...
                    529:     $string =~s/\\int\\left\((.+?)d[a-z]\\right\)/$1/gs;
1.51      albertel  530: 
1.56      albertel  531:     # 
                    532:     $string =~s/\\lim\\left\((.+?),(.+?),(.+?)\\right\)/\\lim_{$2\\to $3}$1/gs;
1.46      albertel  533:     return $string;
                    534: }
1.1       harris41  535: 1;
                    536: __END__
                    537: 
                    538: 
1.91      jms       539: =pod
                    540: 
                    541: =head1 NAME
                    542: 
                    543: Apache::lontexconvert;
                    544: 
                    545: =head1 SYNOPSIS
                    546: 
                    547: Access to tth/ttm
                    548: 
                    549: This is part of the LearningOnline Network with CAPA project
                    550: described at http://www.lon-capa.org.
                    551: 
                    552: 
                    553: =head1 SUBROUTINES
                    554: 
                    555: =over
                    556: 
                    557: =item init_tth()
                    558: 
                    559: Header
                    560: 
                    561: =item convert_real()
                    562: 
                    563:  we need this routine because &converted can get called from inside
                    564:  of the safespace (through &xmlparse('<m>stuff</m>') which doesn't
                    565:  allow the opcode for alarm, so we need to compile this before we get
                    566:  into the safe space since opcode checks only occur at compile time
                    567: 
                    568: =item tth_converted()
                    569: 
                    570: 
                    571: =item clean_out_math_mode()
                    572: 
                    573: 
                    574: =item displaystyle()
                    575: 
                    576: 
                    577: =item jsMath_converted()
                    578: 
                    579: =item tex_engine()
                    580: 
                    581: 
                    582: =item init_math_support()
                    583: 
1.105     raeburn   584: =item mimetex_valign()
                    585: 
                    586:  Makes a HEAD call to /cgi-bin/mimetex.cgi via IO:: to retrieve the 
                    587:  vertical alignment, before the subsequent call to mimetex_converted()
                    588:  which generates the <img> tag and the corresponding image.
                    589: 
                    590:  Input: 1.  $esc_texstring (escaped TeX to be rendered by mimetex).
                    591:  Output: 1. $valign - number of pixels: positive or negative integer 
                    592:             which will be included in <img> tag for mimetex image to
                    593:             support vertical alignment of image within a line of text.
                    594: 
                    595:  If a server is running SSL, and Apache rewrite rules are in place 
                    596:  to rewrite requests for http to https, modification will most likely 
                    597:  be needed for pass through for HEAD requests for /cgi-bin/mimetex.cgi. 
                    598: 
                    599:  Example rewrite rules which rewrite all http traffic to https, 
                    600:  except HEAD requests for /cgi-bin/mimetex.cgi are:
                    601: 
                    602:  <IfModule mod_rewrite.c>
                    603:      RewriteEngine On
                    604:      RewriteLogLevel 0
                    605: 
                    606:      RewriteCond %{HTTPS} off
                    607:      RewriteCond %{HTTP:Host} (.*)
                    608:      RewriteCond %{REQUEST_METHOD} !HEAD 
                    609:      RewriteRule ^/(.*) https://%1/$1 [R=301,L]
                    610: 
                    611:      RewriteCond %{HTTPS} off
                    612:      RewriteCond %{HTTP:Host} (.*)
                    613:      RewriteCond %{REQUEST_METHOD} HEAD
                    614:      RewriteCond %{REQUEST_URI} !^/cgi-bin/mimetex.cgi
                    615:      RewriteRule ^/(.*) https://%1/$1 [R=301,L]
                    616:  </IfModule>
1.91      jms       617: 
                    618: =item mimetex_converted()
                    619: 
                    620: 
                    621: =item converted()
                    622: 
                    623: 
                    624: =item to_convert()
                    625: 
                    626: message display
                    627: 
                    628: =item smiley()
                    629: 
                    630: ???
                    631: 
                    632: =item msgtexconverted()
                    633: 
                    634: =item algebra()
                    635: 
                    636: =item prepare_algebra()
                    637: 
                    638: =item postprocess_algebra()
1.1       harris41  639: 
1.91      jms       640: =back
1.1       harris41  641: 
1.91      jms       642: =cut
1.1       harris41  643: 
                    644: 
                    645: 

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