File:  [LON-CAPA] / loncom / xml / lontexconvert.pm
Revision 1.61: download - view: text, annotated - select for diffs
Sun Feb 27 17:28:56 2005 UTC (19 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- comment and remove extra logthis

    1: # The LearningOnline Network with CAPA
    2: # TeX Conversion Module
    3: #
    4: # $Id: lontexconvert.pm,v 1.61 2005/02/27 17:28:56 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 tth_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:     $$texstring ='\\documentstyle{article}'.$$texstring;
  127: 
  128:     eval(<<'ENDCONV');
  129:     ($xmlstring,$errorstring)=&convert_real($texstring)
  130: ENDCONV
  131:     if ($@) {
  132: 	$errorstring.=&mt("Evaluation Error: ").$@;
  133: 	$Apache::lontexconvert::messedup=1;
  134:     }
  135:     if ($Apache::lontexconvert::messedup || &tth::tthmessedup() || 
  136: 	$errorstring) {
  137: 	&Apache::lonnet::logthis("Trying to kill myself");
  138: 	$Apache::lontexconvert::messedup=1;
  139: 	my $request=Apache->request();
  140: 	$request->child_terminate();
  141:     }
  142:     return $xmlstring;
  143: }
  144: 
  145: sub jsMath_converted {
  146:     my $texstring=shift;
  147:     my $tag='span';
  148:     if ($$texstring=~/[^\\]\$\$/) {
  149: 	#has a $$ in it, guessinng it's display mode
  150: 	$tag='div';
  151:     }
  152:     $$texstring=~s/(?!\\)\$//g;
  153:     $$texstring=~s/\\ensuremath//g;
  154:     return '<'.$tag.' class="math">'.$$texstring.'</'.$tag.'>';
  155: }
  156: 
  157: sub converted {
  158:     if ($ENV{'environment.texengine'} eq 'tth') {
  159: 	return &tth_converted;
  160:     } elsif ($ENV{'environment.texengine'} eq 'jsMath') {
  161: 	return &jsMath_converted;
  162:     }
  163:     return &tth_converted;
  164: }
  165: 
  166: # ====================================================================== Footer
  167: 
  168: sub footer {
  169:   my $xmlstring='';
  170:   if ($ENV{'request.state'} eq 'construct') {
  171:       $xmlstring.='<address>'.$errorstring.'</address>';
  172:   } else {
  173:       &Apache::lonmsg::author_res_msg($ENV{'request.filename'},$errorstring);
  174:   }
  175: # -------------------------------------------------------------------- End Body
  176:   $xmlstring.=&Apache::lonxml::xmlend();
  177:   return $xmlstring;
  178: }
  179: 
  180: # ------------------------------------------------------------ Message display
  181: 
  182: sub to_convert {
  183:     my ($string) = @_;
  184:     $string=~s/\<br\s*\/?\>/ /gs;
  185: #    $string=~s/\s/ /gs;
  186:     $string=&HTML::Entities::decode($string);
  187:     return &converted(\$string);
  188: }
  189: 
  190: sub smiley {
  191:     my $expression=shift;
  192:     if ($ENV{'browser.imagesuppress'} eq 'on') { return $expression; }
  193:     my %smileys=('\:\-\)' => 'smiley',
  194: 		 '8\-\)'  => 'coolsmile',
  195: 		 '8\-(I|\|)'   => 'coolindiff',
  196: 		 ':\-(I|\|)'   => 'neutral',
  197: 		 '\:\-(o|O|\(\))' => 'shocked',
  198: 		 ':\-\('  => 'frowny',
  199: 		 '\;\-\)' => 'wink',
  200: 		 '\:\-P'  => 'baeh',
  201: 		 '\:\-(\\\|\\/)' => 'hrrm',
  202: 		 '\:\-D'  => 'bigsmile',
  203: 		 '\:\-C'  => 'angry',
  204: 		 '\:(\'|\`)\-\(' => 'cry',
  205: 		 '\:\-(X|\#)' => 'lipsrsealed',
  206: 		 '\:\-S' => 'huh');
  207:     my $iconpath=$Apache::lonnet::perlvar{'lonIconsURL'};
  208:     foreach (keys %smileys) {
  209: 	$expression=~s/$_/\<img src="$iconpath\/$smileys{$_}.gif" \/\>/gs; 
  210:     }
  211:     return $expression;
  212: }
  213: 
  214: sub msgtexconverted {
  215:     my ($message,$email) = @_;
  216:     $errorstring='';
  217:     &init_tth();
  218:     my $outmessage='';
  219:     my $tex=0;
  220:     foreach (split(/(?:\&lt\;|\<)\/*m\s*(?:\&gt\;|\>)/i,$message)) {
  221: 	if ($tex) {
  222: 	    if ($email) {
  223: 		$outmessage.='</pre><tt>'.&to_convert($_).'</tt><pre>'; $tex=0;
  224: 	    } else {
  225: 		$outmessage.=&to_convert($_); $tex=0;
  226: 	    }
  227: 	} else {
  228:             $outmessage.=&smiley($_); $tex=1;
  229: 	}
  230:     }
  231:     if (wantarray) {
  232: 	return ($outmessage,$errorstring);
  233:     } else {
  234: 	return $outmessage.$errorstring;
  235:     }
  236: }
  237: 
  238: sub algebra {
  239:     use AlgParser;
  240: 
  241:     my ($string,$target,$style)=@_;
  242:     my $parser = new AlgParserWithImplicitExpand;
  243:     $string=&prepare_algebra($string);
  244:     my $ret = $parser->parse($string);
  245:     my $result='['.&mt('Algebra unconverted due to previous errors').']';
  246:     if ( ref($ret) ) {
  247: 	#$parser->tostring();
  248: 	$parser->normalize();
  249: 	my $latex=$parser->tolatex();
  250: 	$latex=&postprocess_algebra($latex);
  251: 	if ($style eq 'display') {
  252: 	    $latex='$$'.$latex.'$$x';
  253: 	} else {
  254: 	    # style is 'inline'
  255: 	    $latex='\\ensuremath{'.$latex.'}';
  256: 	}
  257: 	if ($target eq 'web' || $target eq 'analyze') {
  258: 	    $result = &converted(\$latex);
  259: 	} else {
  260: 	    $result = $latex;
  261: 	}
  262:     } else {
  263: 	&Apache::lonxml::error($parser->{'htmlerror'});
  264:     }
  265: }
  266: 
  267: sub prepare_algebra {
  268:     my ($string)=@_;
  269: 
  270:     # makes the decision about what is a minus sign easier supposedly
  271:     $string =~ s/(\<\>|\<\=|\>\=[\=\>\<] *)-/$1 zeroplace -/g;
  272: 
  273:     return $string;
  274: }
  275: 
  276: sub postprocess_algebra {
  277:     my ($string)=@_;
  278:     
  279:     # moodle had these and I don't know why, ignoring them for now
  280:     # $string =~s/\\fun/ /g;
  281: 
  282:     # sqrt(3,4) -> \sqrt\let{3,4\right}, which is annoying
  283:     $string =~s/\\left\{/\{/g;
  284:     $string =~s/\\right\}/\}/g;
  285: 
  286:     # remove the extra () in the denominator of a \frac
  287:     $string =~s/\\frac{(.+?)}{\\left\((.+?)\\right\)}/\\frac{$1}{$2}/gs;
  288:     
  289:     # sqrt(3,4) means the 4 root of 3
  290:     $string =~s/\\sqrt{([^,]+),([^\}]+)}/\\sqrt[$2]{$1}/gs;
  291: 
  292:     # log(3,4) means the log base 4 of 3
  293:     $string =~s/\\log\\left\((.+?),(.+?)\\right\)/\\log_{$2}\\left($1\\right)/gs;
  294: 
  295:     # log(3,4) means the log base 4 of 3
  296:     $string =~s/\\((?:sin|cos|tan|sec|csc|cot)(?:h)?)\\left\((.+?),(.+?)\\right\)/\\$1^{$3}\\left($2\\right)/gs;
  297: 
  298:     # int(3,a,b) integral from a to b of 3
  299:     $string =~s/\\int\\left\((.+?),(.+?),(.+?)\\right\)/\\int_{$2}^{$3}\\left($1\\right)/gs;
  300: 
  301:     # int( ... dx) -> ...
  302:     $string =~s/\\int\\left\((.+?)d[a-z]\\right\)/$1/gs;
  303: 
  304:     # 
  305:     $string =~s/\\lim\\left\((.+?),(.+?),(.+?)\\right\)/\\lim_{$2\\to $3}$1/gs;
  306:     return $string;
  307: }
  308: 1;
  309: __END__
  310: 
  311: 
  312: 
  313: 
  314: 
  315: 
  316: 

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