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

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

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