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

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

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