File:  [LON-CAPA] / loncom / xml / lontexconvert.pm
Revision 1.71: download - view: text, annotated - select for diffs
Mon Mar 27 20:40:42 2006 UTC (18 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- adde type arg

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

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