File:  [LON-CAPA] / loncom / xml / lontexconvert.pm
Revision 1.73: download - view: text, annotated - select for diffs
Wed May 17 22:08:08 2006 UTC (18 years ago) by albertel
Branches: MAIN
CVS tags: HEAD
- calling init_tth from start_page (effectively)
- reducing dependencies from lontextconvert.pm

    1: # The LearningOnline Network with CAPA
    2: # TeX Conversion Module
    3: #
    4: # $Id: lontexconvert.pm,v 1.73 2006/05/17 22:08:08 albertel 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: 
   49: # ====================================================================== Header
   50: 
   51: sub init_tth {
   52:     my $options=$env{'course.'.$env{'request.course.id'}.'.tthoptions'};
   53:     if ($env{'browser.mathml'}) {
   54: 	&tth::ttminit();
   55: 	if ($env{'browser.unicode'}) {
   56: 	    &tth::ttmoptions('-L -u1 '.$options);
   57: 	} else {
   58: 	    &tth::ttmoptions('-L -u0 '.$options);
   59: 	}
   60:     } else {
   61: 	&tth::tthinit();
   62: 	if ($env{'browser.unicode'}) {
   63: 	    &tth::tthoptions('-L -u1 '.$options);
   64: 	} else {
   65: 	    &tth::tthoptions('-L -u0 '.$options);
   66: 	}
   67:     }
   68: }
   69: 
   70: # ================================================================== Conversion
   71: 
   72: $Apache::lontexconvert::messedup=0;
   73: 
   74: # we need this routine because &converted can get called from inside
   75: # of the safespace (through &xmlparse('<m>stuff</m>') which doesn't
   76: # allow the opcode for alarm, so we need to compile this before we get
   77: # into the safe space since opcode checks only occur at compile time
   78: sub convert_real {
   79:     my ($texstring)=@_;
   80:     my ($xmlstring,$errorstring);
   81:     local $SIG{SEGV}=sub { $Apache::lontexconvert::messedup=1; die; };
   82:     local $SIG{ALRM}=sub { 
   83: 	&Apache::lonnet::logthis("ALRM");
   84: 	$xmlstring='['.&mt("TeX unconverted due to errors").']';
   85: 	$Apache::lontexconvert::messedup=1;
   86: 	die &mt("TeX unconverted due to errors");
   87:     };
   88:     &Apache::lonxml::start_alarm();
   89:     if ($env{'browser.mathml'}) {
   90: 	$xmlstring=&tth::ttm($$texstring);
   91: 	$xmlstring=~s/\<math\>/\<math xmlns=\"\&mathns\;\"\>/g;
   92: 	$xmlstring=~s/\<br\>/\<br\/\>/g;
   93: 	$xmlstring=~s/\<p\>/\<p\>\<\/p\>/g;
   94: 	$errorstring.=&tth::ttmerror();
   95:     } else {
   96: 	$xmlstring=&tth::tth($$texstring);
   97: 	$errorstring.=&tth::ttherror();
   98: 	$xmlstring=~s-</font(\s*)>-</font>-g;
   99:     }
  100:     $xmlstring=~s/^\s*\<br clear\=\"all\"/\<br/s;
  101:     $xmlstring=~s/^\s*//;
  102:     $xmlstring=~s/\s*$//;
  103:     &Apache::lonxml::end_alarm();
  104:     return ($xmlstring,$errorstring);
  105: }
  106: 
  107: sub tth_converted {
  108:     my $texstring=shift;
  109:     my $xmlstring='['.&mt('UNDISPLAYABLE').']';
  110:     if ($Apache::lontexconvert::messedup) {
  111: 	return '['.&mt('TeX unconverted due to previous errors').']';
  112:     }
  113:     $$texstring ='\\documentstyle{article}'.$$texstring;
  114: 
  115:     eval(<<'ENDCONV');
  116:     ($xmlstring,$errorstring)=&convert_real($texstring)
  117: ENDCONV
  118:     if ($@) {
  119: 	$errorstring.=&mt("Evaluation Error: ").$@;
  120: 	$Apache::lontexconvert::messedup=1;
  121:     }
  122:     if ($Apache::lontexconvert::messedup || &tth::tthmessedup() || 
  123: 	$errorstring) {
  124: 	&Apache::lonnet::logthis("Trying to kill myself");
  125: 	$Apache::lontexconvert::messedup=1;
  126: 	if (ref($Apache::lonxml::request)) {
  127: 	    $Apache::lonxml::request->child_terminate();
  128: 	} else {
  129: 	    my $request;
  130: 	    eval { $request=Apache->request; };
  131: 	    if (!$request) {
  132: 		eval { $request=Apache2::RequestUtil->request; };
  133: 	    }
  134: 	    if ($request) {
  135: 		$request->child_terminate();
  136: 	    } else {
  137: 		&Apache::lonnet::logthis("Unable to find a request to do child_terminate on");
  138: 	    }
  139: 	}
  140:     }
  141:     return $xmlstring;
  142: }
  143: 
  144: sub clean_out_math_mode {
  145:     my ($texstring)=@_;
  146:     $$texstring=~s/(?!\\)\$//g;
  147:     $$texstring=~s/\\[\)\(\]\[]//g;
  148:     $$texstring=~s/\\ensuremath//g;
  149:     return '';
  150: }
  151: 
  152: sub displaystyle {
  153:     my ($texstring)=@_;
  154:     #has a $$ or \[ or \displaystyle in it, guessinng it's display mode
  155:     if ($$texstring=~/[^\\]\$\$/ ||
  156: 	$$texstring=~/\\\[/ ||
  157: 	$$texstring=~/\\displaystyle/) { return 1; }
  158:     return 0;
  159: }
  160: 
  161: sub jsMath_converted {
  162:     my $texstring=shift;
  163:     my $tag='span';
  164:     if (&displaystyle($texstring)) { $tag='div'; }
  165:     &clean_out_math_mode($texstring);
  166:     return &jsMath_header().
  167: 	'<'.$tag.' class="math">'.$$texstring.'</'.$tag.'>';
  168: }
  169: 
  170: {
  171:     my $jsMath_sent_header;
  172:     sub jsMath_reset {
  173: 	$jsMath_sent_header=0;
  174:     }
  175:     sub jsMath_header {
  176: 	return '' if $jsMath_sent_header;
  177: 	$jsMath_sent_header=1;
  178: 	return
  179:             '<script type="text/javascript">
  180:                      function NoFontMessage () {}
  181:                    </script>'."\n".
  182: 	    '<script type="text/javascript" src="/adm/jsMath/jsMath.js"></script>'."\n";
  183:     }
  184:     sub jsMath_process {
  185: 	return '' if !$jsMath_sent_header;
  186: 	return '<script type="text/javascript">jsMath.Process()</script>';
  187:     }
  188: }
  189: 
  190: sub init_math_support {
  191:     &init_tth();
  192:     &Apache::lontexconvert::jsMath_reset();
  193:     if ($env{'environment.texengine'} eq 'jsMath' ||
  194: 	$env{'form.texengine'}        eq 'jsMath' ) {
  195: 	return &Apache::lontexconvert::jsMath_header();
  196:     }
  197:     return;
  198: }
  199: 
  200: sub mimetex_converted {
  201:     my $texstring=shift;
  202:     my $displaystyle=&displaystyle($texstring);
  203: 
  204:     &clean_out_math_mode($texstring);
  205: 
  206:     if ($displaystyle) {
  207: 	$$texstring='\\displaystyle \\Large '.$$texstring;
  208:     }
  209:     my $result='<img src="/cgi-bin/mimetex.cgi?'.&Apache::lonnet::escape($$texstring).'" />';
  210:     if ($displaystyle) {
  211: 	$result='<center>'.$result.'</center>';
  212:     }
  213:     return $result;
  214: }
  215: 
  216: sub converted {
  217:     my ($string,$mode)=@_;
  218:     if ($mode eq '') { $mode=$env{'environment.texengine'}; }
  219:     if ($mode =~ /tth/i) {
  220: 	return &tth_converted($string);
  221:     } elsif ($mode =~ /jsmath/i) {
  222: 	return &jsMath_converted($string);
  223:     } elsif ($mode =~ /mimetex/i) {
  224: 	return &mimetex_converted($string);
  225:     }
  226:     return &tth_converted($string);
  227: }
  228: 
  229: # ------------------------------------------------------------ Message display
  230: 
  231: sub to_convert {
  232:     my ($string) = @_;
  233:     $string=~s/\<br\s*\/?\>/ /gs;
  234: #    $string=~s/\s/ /gs;
  235:     $string=&HTML::Entities::decode($string);
  236:     return &converted(\$string);
  237: }
  238: 
  239: sub smiley {
  240:     my $expression=shift;
  241:     if ($env{'browser.imagesuppress'} eq 'on') { return $expression; }
  242:     my %smileys=('\:\-\)' => 'smiley',
  243: 		 '8\-\)'  => 'coolsmile',
  244: 		 '8\-(I|\|)'   => 'coolindiff',
  245: 		 ':\-(I|\|)'   => 'neutral',
  246: 		 '\:\-(o|O|\(\))' => 'shocked',
  247: 		 ':\-\('  => 'frowny',
  248: 		 '\;\-\)' => 'wink',
  249: 		 '\:\-P'  => 'baeh',
  250: 		 '\:\-(\\\|\\/)' => 'hrrm',
  251: 		 '\:\-D'  => 'bigsmile',
  252: 		 '\:\-C'  => 'angry',
  253: 		 '\:(\'|\`)\-\(' => 'cry',
  254: 		 '\:\-(X|\#)' => 'lipsrsealed',
  255: 		 '\:\-S' => 'huh');
  256:     my $iconpath=$Apache::lonnet::perlvar{'lonIconsURL'};
  257:     foreach (keys %smileys) {
  258: 	$expression=~s/$_/\<img src="$iconpath\/$smileys{$_}.gif" \/\>/gs; 
  259:     }
  260:     return $expression;
  261: }
  262: 
  263: sub msgtexconverted {
  264:     my ($message,$email) = @_;
  265:     $errorstring='';
  266:     &init_tth();
  267:     my $outmessage='';
  268:     my $tex=0;
  269:     foreach (split(/(?:\&lt\;|\<)\/*m\s*(?:\&gt\;|\>)/i,$message)) {
  270: 	if ($tex) {
  271: 	    if ($email) {
  272: 		$outmessage.='</pre><tt>'.&to_convert($_).'</tt><pre>'; $tex=0;
  273: 	    } else {
  274: 		$outmessage.=&to_convert($_); $tex=0;
  275: 	    }
  276: 	} else {
  277:             $outmessage.=&smiley($_); $tex=1;
  278: 	}
  279:     }
  280:     $message=$outmessage; $outmessage=''; $tex=0;
  281:     foreach (split(/(?:\&lt\;|\<)\/*algebra\s*(?:\&gt\;|\>)/i,$message)) {
  282: 	if ($tex) {
  283: 	    if ($email) {
  284: 		$outmessage.='</pre><tt>'.&algebra($_,'web').'</tt><pre>'; $tex=0;
  285: 	    } else {
  286: 		$outmessage.=&algebra($_,'web'); $tex=0;
  287: 	    }
  288: 	} else {
  289:             $outmessage.=$_; $tex=1;
  290: 	}
  291:     }
  292:     if (wantarray) {
  293: 	return ($outmessage,$errorstring);
  294:     } else {
  295: 	return $outmessage.$errorstring;
  296:     }
  297: }
  298: 
  299: sub algebra {
  300:     use AlgParser;
  301: 
  302:     my ($string,$target,$style)=@_;
  303:     my $parser = new AlgParserWithImplicitExpand;
  304:     $string=&prepare_algebra($string);
  305:     my $ret = $parser->parse($string);
  306:     my $result='['.&mt('Algebra unconverted due to previous errors').']';
  307:     if ( ref($ret) ) {
  308: 	#$parser->tostring();
  309: 	$parser->normalize();
  310: 	my $latex=$parser->tolatex();
  311: 	$latex=&postprocess_algebra($latex);
  312: 	if ($style eq 'display') {
  313: 	    $latex='$$'.$latex.'$$x';
  314: 	} else {
  315: 	    # style is 'inline'
  316: 	    $latex='\\ensuremath{'.$latex.'}';
  317: 	}
  318: 	if ($target eq 'web' || $target eq 'analyze') {
  319: 	    $result = &converted(\$latex);
  320: 	} else {
  321: 	    $result = $latex;
  322: 	}
  323:     } else {
  324: 	&Apache::lonxml::error($parser->{'htmlerror'});
  325:     }
  326: }
  327: 
  328: sub prepare_algebra {
  329:     my ($string)=@_;
  330: 
  331:     # makes the decision about what is a minus sign easier supposedly
  332:     $string =~ s/(\<\>|\<\=|\>\=[\=\>\<] *)-/$1 zeroplace -/g;
  333: 
  334:     return $string;
  335: }
  336: 
  337: sub postprocess_algebra {
  338:     my ($string)=@_;
  339:     
  340:     # moodle had these and I don't know why, ignoring them for now
  341:     # $string =~s/\\fun/ /g;
  342: 
  343:     # remove the extra () in the denominator of a \frac
  344:     $string =~s/\\frac{(.+?)}{\\left\((.+?)\\right\)}/\\frac{$1}{$2}/gs;
  345:     
  346:     # sqrt(3,4) means the 4 root of 3
  347:     $string =~s/\\sqrt{([^,]+),([^\}]+)}/\\sqrt[$2]{$1}/gs;
  348: 
  349:     # log(3,4) means the log base 4 of 3
  350:     $string =~s/\\log\\left\((.+?),(.+?)\\right\)/\\log_{$2}\\left($1\\right)/gs;
  351: 
  352:     # log(3,4) means the log base 4 of 3
  353:     $string =~s/\\((?:sin|cos|tan|sec|csc|cot)(?:h)?)\\left\((.+?),(.+?)\\right\)/\\$1^{$3}\\left($2\\right)/gs;
  354: 
  355:     # int(3,a,b) integral from a to b of 3
  356:     $string =~s/\\int\\left\((.+?),(.+?),(.+?)\\right\)/\\int_{$2}^{$3}\\left($1\\right)/gs;
  357: 
  358:     # int( ... dx) -> ...
  359:     $string =~s/\\int\\left\((.+?)d[a-z]\\right\)/$1/gs;
  360: 
  361:     # 
  362:     $string =~s/\\lim\\left\((.+?),(.+?),(.+?)\\right\)/\\lim_{$2\\to $3}$1/gs;
  363:     return $string;
  364: }
  365: 1;
  366: __END__
  367: 
  368: 
  369: 
  370: 
  371: 
  372: 
  373: 

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