File:  [LON-CAPA] / loncom / xml / lontexconvert.pm
Revision 1.47: download - view: text, annotated - select for diffs
Thu Feb 24 05:33:55 2005 UTC (19 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- take care of some nic operators

    1: # The LearningOnline Network with CAPA
    2: # TeX Conversion Module
    3: #
    4: # $Id: lontexconvert.pm,v 1.47 2005/02/24 05:33:55 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: 
   51: # ====================================================================== Header
   52: 
   53: sub init_tth {
   54:     my $options=$ENV{'course.'.$ENV{'request.course.id'}.'.tthoptions'};
   55:     if ($ENV{'browser.mathml'}) {
   56: 	&tth::ttminit();
   57: 	if ($ENV{'browser.unicode'}) {
   58: 	    &tth::ttmoptions('-L -u1 '.$options);
   59: 	} else {
   60: 	    &tth::ttmoptions('-L -u0 '.$options);
   61: 	}
   62:     } else {
   63: 	&tth::tthinit();
   64: 	if ($ENV{'browser.unicode'}) {
   65: 	    &tth::tthoptions('-L -u1 '.$options);
   66: 	} else {
   67: 	    &tth::tthoptions('-L -u0 '.$options);
   68: 	}
   69:     }
   70: }
   71: 
   72: sub header {
   73:     $errorstring='';
   74:     my $time=time;
   75:     &init_tth();
   76:     return &Apache::lonxml::xmlbegin().
   77: 	"\n<head>\n".
   78: 	&Apache::lonxml::fontsettings().
   79: 	&Apache::lonmenu::registerurl(undef,'tex').
   80: 	"\n</head>\n";
   81: }
   82: 
   83: # ================================================================== Conversion
   84: 
   85: $Apache::lontexconvert::messedup=0;
   86: 
   87: # we need this routine because &converted can get called from inside
   88: # of the safespace (through &xmlparse('<m>stuff</m>') which doesn't
   89: # allow the opcode for alarm, so we need to compile this before we get
   90: # into the safe space since opcode checks only occur at compile time
   91: sub convert_real {
   92:     my ($texstring)=@_;
   93:     my ($xmlstring,$errorstring);
   94:     local $SIG{SEGV}=sub { $Apache::lontexconvert::messedup=1; die; };
   95:     local $SIG{ALRM}=sub { 
   96: 	&Apache::lonnet::logthis("ALRM");
   97: 	$xmlstring='['.&mt("TeX unconverted due to errors").']';
   98: 	$Apache::lontexconvert::messedup=1;
   99: 	die &mt("TeX unconverted due to errors");
  100:     };
  101:     alarm($Apache::lonnet::perlvar{'lonScriptTimeout'});
  102:     if ($ENV{'browser.mathml'}) {
  103: 	$xmlstring=&tth::ttm($$texstring);
  104: 	$xmlstring=~s/\<math\>/\<math xmlns=\"\&mathns\;\"\>/g;
  105: 	$xmlstring=~s/\<br\>/\<br\/\>/g;
  106: 	$xmlstring=~s/\<p\>/\<p\>\<\/p\>/g;
  107: 	$errorstring.=&tth::ttmerror();
  108:     } else {
  109: 	$xmlstring=&tth::tth($$texstring);
  110: 	$errorstring.=&tth::ttherror();
  111: 	$xmlstring=~s-</font(\s*)>-</font>-g;
  112:     }
  113:     $xmlstring=~s/^\s*\<br clear\=\"all\"/\<br/s;
  114:     $xmlstring=~s/^\s*//;
  115:     $xmlstring=~s/\s*$//;
  116:     alarm(0);
  117:     return ($xmlstring,$errorstring);
  118: }
  119: 
  120: sub converted {
  121:     my $texstring=shift;
  122:     my $xmlstring='['.&mt('UNDISPLAYABLE').']';
  123:     if ($Apache::lontexconvert::messedup) {
  124: 	return '['.&mt('TeX unconverted due to previous errors').']';
  125:     }
  126:     eval(<<'ENDCONV');
  127:     ($xmlstring,$errorstring)=&convert_real($texstring)
  128: ENDCONV
  129:     if ($@) {
  130: 	$errorstring.=&mt("Evaluation Error: ").$@;
  131: 	$Apache::lontexconvert::messedup=1;
  132:     }
  133:     if ($Apache::lontexconvert::messedup || &tth::tthmessedup() || 
  134: 	$errorstring) {
  135: 	&Apache::lonnet::logthis("Trying to kill myself");
  136: 	$Apache::lontexconvert::messedup=1;
  137: 	my $request=Apache->request();
  138: 	$request->child_terminate();
  139:     }
  140:     return $xmlstring;
  141: }
  142: 
  143: # ====================================================================== Footer
  144: 
  145: sub footer {
  146:   my $xmlstring='';
  147:   if ($ENV{'request.state'} eq 'construct') {
  148:       $xmlstring.='<address>'.$errorstring.'</address>';
  149:   } else {
  150:       &Apache::lonmsg::author_res_msg($ENV{'request.filename'},$errorstring);
  151:   }
  152: # -------------------------------------------------------------------- End Body
  153:   $xmlstring.=&Apache::lonxml::xmlend();
  154:   return $xmlstring;
  155: }
  156: 
  157: # ------------------------------------------------------------ Message display
  158: 
  159: sub to_convert {
  160:     my ($string) = @_;
  161:     $string=~s/\<br\s*\/?\>/ /gs;
  162: #    $string=~s/\s/ /gs;
  163:     $string=&HTML::Entities::decode($string);
  164:     return &converted(\$string);
  165: }
  166: 
  167: sub smiley {
  168:     my $expression=shift;
  169:     if ($ENV{'browser.imagesuppress'} eq 'on') { return $expression; }
  170:     my %smileys=('\:\-\)' => 'smiley',
  171: 		 '8\-\)'  => 'coolsmile',
  172: 		 '8\-(I|\|)'   => 'coolindiff',
  173: 		 ':\-(I|\|)'   => 'neutral',
  174: 		 '\:\-(o|O|\(\))' => 'shocked',
  175: 		 ':\-\('  => 'frowny',
  176: 		 '\;\-\)' => 'wink',
  177: 		 '\:\-P'  => 'baeh',
  178: 		 '\:\-(\\\|\\/)' => 'hrrm',
  179: 		 '\:\-D'  => 'bigsmile',
  180: 		 '\:\-C'  => 'angry',
  181: 		 '\:(\'|\`)\-\(' => 'cry',
  182: 		 '\:\-(X|\#)' => 'lipsrsealed',
  183: 		 '\:\-S' => 'huh');
  184:     my $iconpath=$Apache::lonnet::perlvar{'lonIconsURL'};
  185:     foreach (keys %smileys) {
  186: 	$expression=~s/$_/\<img src="$iconpath\/$smileys{$_}.gif" \/\>/gs; 
  187:     }
  188:     return $expression;
  189: }
  190: 
  191: sub msgtexconverted {
  192:     my ($message,$email) = @_;
  193:     $errorstring='';
  194:     &init_tth();
  195:     my $outmessage='';
  196:     my $tex=0;
  197:     foreach (split(/(?:\&lt\;|\<)\/*m\s*(?:\&gt\;|\>)/i,$message)) {
  198: 	if ($tex) {
  199: 	    if ($email) {
  200: 		$outmessage.='</pre><tt>'.&to_convert($_).'</tt><pre>'; $tex=0;
  201: 	    } else {
  202: 		$outmessage.=&to_convert($_); $tex=0;
  203: 	    }
  204: 	} else {
  205:             $outmessage.=&smiley($_); $tex=1;
  206: 	}
  207:     }
  208:     if (wantarray) {
  209: 	return ($outmessage,$errorstring);
  210:     } else {
  211: 	return $outmessage.$errorstring;
  212:     }
  213: }
  214: 
  215: sub algebra {
  216:     my ($string,$target)=@_;
  217:     my $parser = new AlgParserWithImplicitExpand;
  218:     $string=&prepare_algebra($string);
  219:     my $ret = $parser->parse($string);
  220:     my $result='['.&mt('Algebra unconverted due to previous errors').']';
  221:     if ( ref($ret) ) {
  222: 	$parser->tostring();
  223: 	$parser->normalize();
  224: 	my $latex=$parser->tolatex();
  225: 	$latex=&postprocess_algebra($string);
  226: 	$latex='$'.$latex.'$';
  227: 	if ($target eq 'web' || $target eq 'analyze') {
  228: 	    $result = &converted(\$latex);
  229: 	} else {
  230: 	    $result = $latex;
  231: 	}
  232:     } else {
  233: 	&Apache::lonxml::error($parser->{'htmlerror'});
  234:     }
  235: }
  236: 
  237: sub prepare_algebra {
  238:     my ($string)=@_;
  239: 
  240:     # change some multi character operators into single character operators
  241:     $string =~ s/<>/\#/g;
  242:     $string =~ s/<\=/\%/g;
  243:     $string =~ s/>\=/\!/g;
  244:     
  245:     #protect the confusable greek symbols
  246:     $string =~ s/delta/zdelta/g;
  247:     $string =~ s/beta/bita/g;
  248:     $string =~ s/theta/thita/g;
  249:     $string =~ s/zeta/zita/g;
  250:     $string =~ s/eta/xeta/g;
  251:     $string =~ s/epsilon/zepslon/g;
  252:     $string =~ s/upsilon/zupslon/g;
  253:   
  254:     return $string;
  255: }
  256: 
  257: sub postprocess_algebra {
  258:     my ($string)=@_;
  259: 
  260:     # exapnd out some operators
  261:     $string =~ s/\#/\\not= /g;
  262:     $string =~ s/\%/\\leq /g;
  263:     $string =~ s/\!/\\geq /g;
  264: 
  265:     # replace some special symbols with the LaTeX equivalents
  266:     $string =~ s/infty/\\infty/g;
  267:     $string =~ s/infinity/\\infty/g;
  268:     $string =~ s/alpha/\\alpha/g;  
  269:     $string =~ s/gamma/\\gamma/g; 
  270:     $string =~ s/iota/\\iota/g;
  271:     $string =~ s/kappa/\\kappa/g;
  272:     $string =~ s/lambda/\\lambda/g;
  273:     $string =~ s/mu/\\mu/g;
  274:     $string =~ s/nu/\\nu/g;
  275:     $string =~ s/xi/\\xi/g;
  276:     $string =~ s/rho/\\rho/g;
  277:     $string =~ s/sigma/\\sigma/g;
  278:     $string =~ s/tau/\\tau/g;
  279:     $string =~ s/phi/\\phi/g;
  280:     $string =~ s/chi/\\chi/g;
  281:     $string =~ s/psi/\\psi/g;
  282:     $string =~ s/omega/\\omega/g;
  283:     $string =~ s/zdelta/\\delta/g;
  284:     $string =~ s/bita/\\beta/g;
  285:     $string =~ s/thita/\\theta/g;
  286:     $string =~ s/zita/\\zeta/g;
  287:     $string =~ s/xeta/\\eta/g;
  288:     $string =~ s/zepslon/\\epsilon/g;
  289:     $string =~ s/zupslon/\\upsilon/g;
  290: 
  291:     return $string;
  292: }
  293: 1;
  294: __END__
  295: 
  296: 
  297: 
  298: 
  299: 
  300: 
  301: 

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