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

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

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