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

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

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