File:  [LON-CAPA] / loncom / xml / lontexconvert.pm
Revision 1.108: download - view: text, annotated - select for diffs
Mon Jun 6 16:08:45 2011 UTC (12 years, 11 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_10_X, version_2_10_1, loncapaMITrelate_1, HEAD
- Bug 5925.
- Simplify internal HEAD request for /cgi-bin/mimetex.cgi by using loopback.

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

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