File:  [LON-CAPA] / loncom / xml / lontexconvert.pm
Revision 1.117: download - view: text, annotated - select for diffs
Fri Jan 22 22:31:51 2016 UTC (8 years, 4 months ago) by damieng
Branches: MAIN
CVS tags: HEAD
removed extra br before table in tth output with display mode (to avoid a large blank space when there is a div or p before)

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

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