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

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

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