File:  [LON-CAPA] / loncom / xml / lontexconvert.pm
Revision 1.68: download - view: text, annotated - select for diffs
Tue Aug 16 16:29:53 2005 UTC (18 years, 8 months ago) by albertel
Branches: MAIN
CVS tags: version_2_1_1, version_2_1_0, version_2_0_X, version_2_0_99_1, version_2_0_2, version_2_0_1, version_2_0_0, HEAD
- be case insensitive about the display value

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

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