File:  [LON-CAPA] / loncom / xml / lontexconvert.pm
Revision 1.98: download - view: text, annotated - select for diffs
Wed Aug 12 20:43:25 2009 UTC (14 years, 9 months ago) by www
Branches: MAIN
CVS tags: version_2_9_X, version_2_9_1, version_2_9_0, version_2_8_99_1, version_2_8_99_0, HEAD, GCI_3, GCI_2
Raw mode for formula display

    1: # The LearningOnline Network with CAPA
    2: # TeX Conversion Module
    3: #
    4: # $Id: lontexconvert.pm,v 1.98 2009/08/12 20:43:25 www 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: use IO::Socket::SSL;
   53: 
   54: # ====================================================================== Header
   55: 
   56: sub init_tth {
   57:     my $options=$env{'course.'.$env{'request.course.id'}.'.tthoptions'};
   58:     if ($options =~ /\S/) {
   59: 	$options = ' '.$options;
   60:     } else {
   61: 	undef($options);
   62:     }
   63:     if ($env{'browser.mathml'}) {
   64: 	&tth::ttminit();
   65: 	if ($env{'browser.unicode'}) {
   66: 	    &tth::ttmoptions('-L -u1'.$options);
   67: 	} else {
   68: 	    &tth::ttmoptions('-L -u0'.$options);
   69: 	}
   70:     } else {
   71: 	&tth::tthinit();
   72: 	if ($env{'browser.unicode'}) {
   73: 	    &tth::tthoptions('-L -u1'.$options);
   74: 	} else {
   75: 	    &tth::tthoptions('-L -u0'.$options);
   76: 	}
   77:     }
   78: }
   79: 
   80: # ================================================================== Conversion
   81: 
   82: $Apache::lontexconvert::messedup=0;
   83: 
   84: 
   85: sub convert_real {
   86:     my ($texstring)=@_;
   87:     my ($xmlstring,$errorstring);
   88:     local $SIG{SEGV}=sub { $Apache::lontexconvert::messedup=1; die; };
   89:     local $SIG{ALRM}=sub { 
   90: 	&Apache::lonnet::logthis("ALRM");
   91: 	$xmlstring='['.&mt("TeX unconverted due to errors").']';
   92: 	$Apache::lontexconvert::messedup=1;
   93: 	die &mt("TeX unconverted due to errors");
   94:     };
   95:     &Apache::lonxml::start_alarm();
   96:     if ($env{'browser.mathml'}) {
   97: 	$xmlstring=&tth::ttm($$texstring);
   98: 	$xmlstring=~s/\<math\>/\<math xmlns=\"\&mathns\;\"\>/g;
   99: 	$xmlstring=~s/\<br\>/\<br\/\>/g;
  100: 	$xmlstring=~s/\<p\>/\<p\>\<\/p\>/g;
  101: 	$errorstring.=&tth::ttmerror();
  102:     } else {
  103: 	$xmlstring=&tth::tth($$texstring);
  104: 	$errorstring.=&tth::ttherror();
  105: 	$xmlstring=~s-</font(\s*)>-</font>-g;
  106:     }
  107:     $xmlstring=~s/^\s*\<br clear\=\"all\"/\<br/s;
  108:     $xmlstring=~s/^\s*//;
  109:     $xmlstring=~s/\s*$//;
  110:     #
  111:     # \rightleftharpoons is not converted by tth but maps
  112:     # reasonably well to &#8660;.  If we get many more of these,
  113:     # we're going to need to have a translation sub.
  114:     #
  115:     my $lrharpoon = pack("U", 0x21cc);
  116:     $xmlstring=~s/\\rightleftharpoons/$lrharpoon/g;
  117: 
  118:     &Apache::lonxml::end_alarm();
  119:     return ($xmlstring,$errorstring);
  120: }
  121: 
  122: sub tth_converted {
  123:     my $texstring=shift;
  124:     my $xmlstring='['.&mt('UNDISPLAYABLE').']';
  125:     if ($Apache::lontexconvert::messedup) {
  126: 	return '['.&mt('TeX unconverted due to previous errors').']';
  127:     }
  128:     $$texstring ='\\documentstyle{article}'.$$texstring;
  129: 
  130:     eval(<<'ENDCONV');
  131:     ($xmlstring,$errorstring)=&convert_real($texstring)
  132: ENDCONV
  133:     if ($@) {
  134: 	$errorstring.=&mt("Evaluation Error: ").$@;
  135: 	$Apache::lontexconvert::messedup=1;
  136:     }
  137:     if ($Apache::lontexconvert::messedup || &tth::tthmessedup() || 
  138: 	$errorstring) {
  139: 	&Apache::lonnet::logthis("Trying to kill myself");
  140: 	$Apache::lontexconvert::messedup=1;
  141: 	if (ref($Apache::lonxml::request)) {
  142: 	    $Apache::lonxml::request->child_terminate();
  143: 	} else {
  144: 	    my $request;
  145: 	    eval { $request=Apache->request; };
  146: 	    if (!$request) {
  147: 		eval { $request=Apache2::RequestUtil->request; };
  148: 	    }
  149: 	    if ($request) {
  150: 		$request->child_terminate();
  151: 	    } else {
  152: 		&Apache::lonnet::logthis("Unable to find a request to do child_terminate on");
  153: 	    }
  154: 	}
  155:     }
  156:     return $xmlstring;
  157: }
  158: 
  159: sub clean_out_math_mode {
  160:     my ($texstring)=@_;
  161:     $$texstring=~s/(?<!\\)\$//g;
  162:     $$texstring=~s/\\[\)\(\]\[]//g;
  163:     $$texstring=~s/\\ensuremath//g;
  164:     return '';
  165: }
  166: 
  167: sub displaystyle {
  168:     my ($texstring)=@_;
  169:     #has a $$ or \[ or \displaystyle in it, guessinng it's display mode
  170:     if ($$texstring=~/[^\\]\$\$/ ||
  171: 	$$texstring=~/\\\[/ ||
  172: 	$$texstring=~/\\displaystyle/) { return 1; }
  173:     return 0;
  174: }
  175: 
  176: sub jsMath_converted {
  177:     my $texstring=shift;
  178:     my $tag='span';
  179:     if (&displaystyle($texstring)) { $tag='div'; }
  180:     &clean_out_math_mode($texstring);
  181:     return &jsMath_header().
  182: 	'<'.$tag.' class="math">'.$$texstring.'</'.$tag.'>';
  183: }
  184: 
  185: {
  186:     my @jsMath_sent_header;
  187:     sub jsMath_reset {
  188: 	undef(@jsMath_sent_header);
  189:     }
  190:     sub jsMath_push {
  191: 	push(@jsMath_sent_header,0);
  192:     }
  193:     sub jsMath_header {
  194: 	if (!@jsMath_sent_header) {
  195: 	    &Apache::lonnet::logthis("mismatched calls of jsMath_header and jsMath_process");
  196: 	    return '';
  197: 	}
  198: 	return '' if $jsMath_sent_header[-1];
  199: 	$jsMath_sent_header[-1]=1;
  200: 	return
  201:             '<script type="text/javascript">
  202:                      function NoFontMessage () {}
  203:                      jsMath = {Parser: {prototype: {environments: {\'eqnarray*\' :[\'Array\',null,null,\'rcl\',[5/18,5/18],3,\'D\']}}}};
  204:                    </script>'."\n".
  205: 	    '<script type="text/javascript" src="/adm/jsMath/jsMath.js"></script>'."\n";
  206:     }
  207:     sub jsMath_process {
  208: 	my $state = pop(@jsMath_sent_header);
  209: 	return '' if !$state;
  210: 	return "\n".
  211: 	    '<script type="text/javascript">jsMath.Process()</script>'."\n";
  212:     }
  213:     sub jsMath_state {
  214: 	my ($level) = @_;
  215: 	return $jsMath_sent_header[$level];
  216:     }
  217: }
  218: 
  219: sub tex_engine {
  220:     if (exists($env{'form.texengine'})) {
  221: 	if ($env{'form.texengine'} ne '') {
  222:             return $env{'form.texengine'};
  223:         }
  224:     }    
  225:     if ($env{'request.course.id'}
  226: 	&& exists($env{'course.'.$env{'request.course.id'}.'.texengine'})) {
  227: 	return $env{'course.'.$env{'request.course.id'}.'.texengine'};
  228:     }
  229:     if (exists($env{'environment.texengine'})) {
  230: 	return $env{'environment.texengine'};
  231:     }
  232:     return 'tth';
  233: }
  234: 
  235: sub init_math_support {
  236:     my ($inherit_jsmath) = @_;
  237:     &init_tth();
  238:     &Apache::lontexconvert::jsMath_push();
  239:     if (lc(&tex_engine()) eq 'jsmath' ||
  240: 	($inherit_jsmath && &jsMath_state(-2))) {
  241: 	return &Apache::lontexconvert::jsMath_header();
  242:     }
  243:     return;
  244: }
  245: 
  246: sub mimetex_valign {
  247:     my ($esc_texstring)=@_;
  248:     my $valign = 0;
  249:     my $lonhost = $Apache::lonnet::perlvar{'lonHostID'};
  250:     my $hostname = &Apache::lonnet::hostname($lonhost);
  251:     my $protocol = $Apache::lonnet::protocol{$lonhost};
  252:     my $path = '/cgi-bin/mimetex.cgi?'.$esc_texstring;
  253:     my $socket;
  254:     &Apache::lonxml::start_alarm();
  255:     if ($protocol eq 'https') {
  256:         $socket = IO::Socket::SSL->new(PeerAddr => $hostname,
  257:                                        PeerPort => 'https(443)',
  258:                                        Proto    => 'tcp');
  259:     } else {
  260:         $socket = IO::Socket::INET->new(PeerAddr => $hostname,
  261:                                         PeerPort => 'http(80)',
  262:                                         Proto    => 'tcp');
  263:     }
  264:     if ($socket) {
  265:         my $headreq = "HEAD $path HTTP/1.0\r\n\r\n";
  266:         print $socket $headreq;
  267:         while (<$socket>) {
  268:             if (/Vertical\-Align\:\s*?([\-\d]+)/) {
  269:                 $valign = $1;
  270:             }
  271:         }
  272:         if ($protocol eq 'https') {
  273:             $socket->close(SSL_no_shutdown => 1,
  274:                            SSL_ctx_free => 1);
  275:         } else {
  276:             $socket->close();
  277:         }
  278:     }
  279:     &Apache::lonxml::end_alarm();
  280:     return $valign;
  281: }
  282: 
  283: sub mimetex_converted {
  284:     my $texstring=shift;
  285: 
  286: # Alt-Argument for screen readers
  287:     my $alt_string=$$texstring;
  288:     $alt_string=~s/\"/\'\'/g;
  289: 
  290: # Is this displaystyle?
  291: 
  292:     my $displaystyle=&displaystyle($texstring);
  293: 
  294: # Remove math environment delimiters
  295: 
  296:     &clean_out_math_mode($texstring);
  297: 
  298:     if ($displaystyle) {
  299: 	$$texstring='\\displaystyle \\Large '.$$texstring;
  300:     }
  301:     my $esc_texstring = &uri_escape($$texstring);
  302:     my $valign = &mimetex_valign($esc_texstring);
  303:     my $result='<img src="/cgi-bin/mimetex.cgi?'.$esc_texstring.'" style="vertical-align:'.$valign.'px" alt="'.$alt_string.'" />';
  304:     if ($displaystyle) {
  305: 	$result='<center>'.$result.'</center>';
  306:     }
  307:     return $result;
  308: }
  309: 
  310: sub converted {
  311:     my ($string,$mode)=@_;
  312:     if ($mode eq '') { $mode = &tex_engine(); }
  313:     if ($mode =~ /tth/i) {
  314: 	return &tth_converted($string);
  315:     } elsif ($mode =~ /jsmath/i) {
  316: 	return &jsMath_converted($string);
  317:     } elsif ($mode =~ /mimetex/i) {
  318: 	return &mimetex_converted($string);
  319:     } elsif ($mode =~ /raw/i) {
  320:         return $$string;
  321:     }
  322:     return &tth_converted($string);
  323: }
  324: 
  325: # ------------------------------------------------------------ Message display
  326: 
  327: sub to_convert {
  328:     my ($string) = @_;
  329:     $string=~s/\<br\s*\/?\>/ /gs;
  330: #    $string=~s/\s/ /gs;
  331:     $string=&HTML::Entities::decode($string);
  332:     return &converted(\$string);
  333: }
  334: 
  335: sub smiley {
  336:     my $expression=shift;
  337:     if ($env{'browser.imagesuppress'} eq 'on') { return $expression; }
  338:     my %smileys=('\:\-\)' => 'smiley',
  339: 		 '8\-\)'  => 'coolsmile',
  340: 		 '8\-(I|\|)'   => 'coolindiff',
  341: 		 ':\-(I|\|)'   => 'neutral',
  342: 		 '\:\-(o|O|\(\))' => 'shocked',
  343: 		 ':\-\('  => 'frowny',
  344: 		 '\;\-\)' => 'wink',
  345: 		 '\:\-P'  => 'baeh',
  346: 		 '\:\-(\\\|\\/)' => 'hrrm',
  347: 		 '\:\-D'  => 'bigsmile',
  348: 		 '\:\-C'  => 'angry',
  349: 		 '\:(\'|\`)\-\(' => 'cry',
  350: 		 '\:\-(X|\#)' => 'lipsrsealed',
  351: 		 '\:\-S' => 'huh');
  352:     my $iconpath=$Apache::lonnet::perlvar{'lonIconsURL'};
  353:     foreach my $smiley (keys(%smileys)) {
  354: 	$expression=~s/$smiley/\<img src="$iconpath\/$smileys{$smiley}.gif" \/\>/gs; 
  355:     }
  356:     return $expression;
  357: }
  358: 
  359: sub msgtexconverted {
  360:     my ($message,$email) = @_;
  361:     $errorstring='';
  362:     &init_tth();
  363:     my $outmessage='';
  364:     my $tex=0;
  365:     foreach my $fragment (split(/(?:\&lt\;|\<)\/*m\s*(?:\&gt\;|\>)/i,$message)) {
  366: 	if ($tex) {
  367: 	    if ($email) {
  368: 		$outmessage.='</pre><tt>'.&to_convert($fragment).'</tt><pre>';
  369: 		$tex=0;
  370: 	    } else {
  371: 		$outmessage.=&to_convert($fragment);
  372: 		$tex=0;
  373: 	    }
  374: 	} else {
  375:             $outmessage.=&smiley($fragment);
  376: 	    $tex=1;
  377: 	}
  378:     }
  379:     $message=$outmessage; $outmessage=''; $tex=0;
  380:     foreach my $fragment (split(/(?:\&lt\;|\<)\/*algebra\s*(?:\&gt\;|\>)/i,
  381: 				$message)) {
  382: 	if ($tex) {
  383: 	    if ($email) {
  384: 		$outmessage.='</pre><tt>'.&algebra($fragment,'web').'</tt><pre>';
  385: 		$tex=0;
  386: 	    } else {
  387: 		$outmessage.=&algebra($fragment,'web');
  388: 		$tex=0;
  389: 	    }
  390: 	} else {
  391:             $outmessage.=$fragment;
  392: 	    $tex=1;
  393: 	}
  394:     }
  395:     if (wantarray) {
  396: 	return ($outmessage,$errorstring);
  397:     } else {
  398: 	return $outmessage.$errorstring;
  399:     }
  400: }
  401: 
  402: sub algebra {
  403:     use AlgParser;
  404: 
  405:     my ($string,$target,$style,$parstack,$safeeval)=@_;
  406:     my $parser = new AlgParserWithImplicitExpand;
  407:     $string=&prepare_algebra($string);
  408:     my $ret = $parser->parse($string);
  409:     my $result='['.&mt('Algebra unconverted due to previous errors').']';
  410:     if ( ref($ret) ) {
  411: 	#$parser->tostring();
  412: 	$parser->normalize();
  413: 	my $latex=$parser->tolatex();
  414: 	$latex=&postprocess_algebra($latex);
  415: 	if ($style eq 'display') {
  416: 	    $latex='$$'.$latex.'$$x';
  417: 	} else {
  418: 	    # style is 'inline'
  419: 	    $latex='\\ensuremath{'.$latex.'}';
  420: 	}
  421: 	if ($target eq 'web' || $target eq 'analyze') {
  422:             my $display=&Apache::lonxml::get_param('display',$parstack,$safeeval);
  423:             $result = &converted(\$latex,$display);
  424: #	    $result = &converted(\$latex);
  425: 	} else {
  426: 	    $result = $latex;
  427: 	}
  428:     } else {
  429: 	&Apache::lonxml::error($parser->{'htmlerror'});
  430:     }
  431: }
  432: 
  433: sub prepare_algebra {
  434:     my ($string)=@_;
  435: 
  436:     # makes the decision about what is a minus sign easier supposedly
  437:     $string =~ s/(\<\>|\<\=|\>\=[\=\>\<] *)-/$1 zeroplace -/g;
  438: 
  439:     return $string;
  440: }
  441: 
  442: sub postprocess_algebra {
  443:     my ($string)=@_;
  444:     
  445:     # moodle had these and I don't know why, ignoring them for now
  446:     # $string =~s/\\fun/ /g;
  447: 
  448:     # sqrt(3,4) means the 4 root of 3
  449:     $string =~s/\\sqrt{([^,]+),([^\}]+)}/\\sqrt[$2]{$1}/gs;
  450: 
  451:     # log(3,4) means the log base 4 of 3
  452:     $string =~s/\\log\\left\((.+?),(.+?)\\right\)/\\log_{$2}\\left($1\\right)/gs;
  453: 
  454:     # log(3,4) means the log base 4 of 3
  455:     $string =~s/\\((?:sin|cos|tan|sec|csc|cot)(?:h)?)\\left\((.+?),(.+?)\\right\)/\\$1^{$3}\\left($2\\right)/gs;
  456: 
  457:     # int(3,a,b) integral from a to b of 3
  458:     $string =~s/\\int\\left\((.+?),(.+?),(.+?)\\right\)/\\int_{$2}^{$3}\\left($1\\right)/gs;
  459: 
  460:     # int( ... dx) -> ...
  461:     $string =~s/\\int\\left\((.+?)d[a-z]\\right\)/$1/gs;
  462: 
  463:     # 
  464:     $string =~s/\\lim\\left\((.+?),(.+?),(.+?)\\right\)/\\lim_{$2\\to $3}$1/gs;
  465:     return $string;
  466: }
  467: 1;
  468: __END__
  469: 
  470: 
  471: =pod
  472: 
  473: =head1 NAME
  474: 
  475: Apache::lontexconvert;
  476: 
  477: =head1 SYNOPSIS
  478: 
  479: Access to tth/ttm
  480: 
  481: This is part of the LearningOnline Network with CAPA project
  482: described at http://www.lon-capa.org.
  483: 
  484: 
  485: =head1 SUBROUTINES
  486: 
  487: =over
  488: 
  489: =item init_tth()
  490: 
  491: Header
  492: 
  493: =item convert_real()
  494: 
  495:  we need this routine because &converted can get called from inside
  496:  of the safespace (through &xmlparse('<m>stuff</m>') which doesn't
  497:  allow the opcode for alarm, so we need to compile this before we get
  498:  into the safe space since opcode checks only occur at compile time
  499: 
  500: =item tth_converted()
  501: 
  502: 
  503: =item clean_out_math_mode()
  504: 
  505: 
  506: =item displaystyle()
  507: 
  508: 
  509: =item jsMath_converted()
  510: 
  511: 
  512: =item tex_engine()
  513: 
  514: 
  515: =item init_math_support()
  516: 
  517: 
  518: =item mimetex_converted()
  519: 
  520: 
  521: =item converted()
  522: 
  523: 
  524: =item to_convert()
  525: 
  526: message display
  527: 
  528: =item smiley()
  529: 
  530: ???
  531: 
  532: =item msgtexconverted()
  533: 
  534: =item algebra()
  535: 
  536: =item prepare_algebra()
  537: 
  538: =item postprocess_algebra()
  539: 
  540: =back
  541: 
  542: =cut
  543: 
  544: 
  545: 

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