Annotation of loncom/xml/lontexconvert.pm, revision 1.66

1.1       harris41    1: # The LearningOnline Network with CAPA
                      2: # TeX Conversion Module
                      3: #
1.66    ! albertel    4: # $Id: lontexconvert.pm,v 1.65 2005/04/07 06:56:27 albertel Exp $
1.4       www         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: #
1.1       harris41   39: 
                     40: package Apache::lontexconvert;
                     41: 
                     42: use strict;
1.33      albertel   43: use tth();
1.1       harris41   44: use vars qw($errorstring);
1.19      albertel   45: use Apache();
1.33      albertel   46: use Apache::lonmsg();
                     47: use Apache::lonxml();
                     48: use Apache::lonmenu();
                     49: use Apache::lonlocal;
1.65      albertel   50: use Apache::lonnet;
1.1       harris41   51: 
                     52: # ====================================================================== Header
                     53: 
1.29      albertel   54: sub init_tth {
1.65      albertel   55:     my $options=$env{'course.'.$env{'request.course.id'}.'.tthoptions'};
                     56:     if ($env{'browser.mathml'}) {
1.29      albertel   57: 	&tth::ttminit();
1.65      albertel   58: 	if ($env{'browser.unicode'}) {
1.29      albertel   59: 	    &tth::ttmoptions('-L -u1 '.$options);
                     60: 	} else {
                     61: 	    &tth::ttmoptions('-L -u0 '.$options);
                     62: 	}
                     63:     } else {
                     64: 	&tth::tthinit();
1.65      albertel   65: 	if ($env{'browser.unicode'}) {
1.29      albertel   66: 	    &tth::tthoptions('-L -u1 '.$options);
                     67: 	} else {
                     68: 	    &tth::tthoptions('-L -u0 '.$options);
                     69: 	}
                     70:     }
                     71: }
                     72: 
1.1       harris41   73: sub header {
1.31      albertel   74:     $errorstring='';
                     75:     my $time=time;
                     76:     &init_tth();
                     77:     return &Apache::lonxml::xmlbegin().
1.40      albertel   78: 	"\n<head>\n".
1.31      albertel   79: 	&Apache::lonxml::fontsettings().
                     80: 	&Apache::lonmenu::registerurl(undef,'tex').
                     81: 	"\n</head>\n";
1.1       harris41   82: }
                     83: 
                     84: # ================================================================== Conversion
                     85: 
1.19      albertel   86: $Apache::lontexconvert::messedup=0;
1.36      albertel   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 { 
1.37      albertel   97: 	&Apache::lonnet::logthis("ALRM");
1.36      albertel   98: 	$xmlstring='['.&mt("TeX unconverted due to errors").']';
                     99: 	$Apache::lontexconvert::messedup=1;
                    100: 	die &mt("TeX unconverted due to errors");
                    101:     };
1.63      albertel  102:     &Apache::lonxml::start_alarm();
1.65      albertel  103:     if ($env{'browser.mathml'}) {
1.36      albertel  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:     }
1.43      www       114:     $xmlstring=~s/^\s*\<br clear\=\"all\"/\<br/s;
1.36      albertel  115:     $xmlstring=~s/^\s*//;
                    116:     $xmlstring=~s/\s*$//;
1.63      albertel  117:     &Apache::lonxml::end_alarm();
1.36      albertel  118:     return ($xmlstring,$errorstring);
                    119: }
                    120: 
1.59      albertel  121: sub tth_converted {
1.31      albertel  122:     my $texstring=shift;
1.34      www       123:     my $xmlstring='['.&mt('UNDISPLAYABLE').']';
1.31      albertel  124:     if ($Apache::lontexconvert::messedup) {
1.35      albertel  125: 	return '['.&mt('TeX unconverted due to previous errors').']';
1.31      albertel  126:     }
1.59      albertel  127:     $$texstring ='\\documentstyle{article}'.$$texstring;
                    128: 
1.31      albertel  129:     eval(<<'ENDCONV');
1.36      albertel  130:     ($xmlstring,$errorstring)=&convert_real($texstring)
1.12      www       131: ENDCONV
1.32      albertel  132:     if ($@) {
1.35      albertel  133: 	$errorstring.=&mt("Evaluation Error: ").$@;
1.32      albertel  134: 	$Apache::lontexconvert::messedup=1;
                    135:     }
1.37      albertel  136:     if ($Apache::lontexconvert::messedup || &tth::tthmessedup() || 
                    137: 	$errorstring) {
1.31      albertel  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;
1.1       harris41  144: }
                    145: 
1.62      albertel  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: 
1.59      albertel  163: sub jsMath_converted {
                    164:     my $texstring=shift;
                    165:     my $tag='span';
1.62      albertel  166:     if (&displaystyle($texstring)) { $tag='div'; }
                    167:     &clean_out_math_mode($texstring);
                    168:     return '<'.$tag.' class="math">'.$$texstring.'</'.$tag.'>';
                    169: }
                    170: 
                    171: sub mimetex_converted {
                    172:     my $texstring=shift;
                    173:     my $displaystyle=&displaystyle($texstring);
                    174: 
                    175:     &clean_out_math_mode($texstring);
                    176: 
                    177:     if ($displaystyle) {
                    178: 	$$texstring='\\displaystyle \\Large '.$$texstring;
1.59      albertel  179:     }
1.62      albertel  180:     my $result='<img src="/cgi-bin/mimetex.cgi?'.&Apache::lonnet::escape($$texstring).'" />';
                    181:     if ($displaystyle) {
                    182: 	$result='<center>'.$result.'</center>';
                    183:     }
                    184:     return $result;
1.59      albertel  185: }
                    186: 
                    187: sub converted {
1.65      albertel  188:     if ($env{'environment.texengine'} eq 'tth') {
1.59      albertel  189: 	return &tth_converted;
1.65      albertel  190:     } elsif ($env{'environment.texengine'} eq 'jsMath') {
1.59      albertel  191: 	return &jsMath_converted;
1.65      albertel  192:     } elsif ($env{'environment.texengine'} eq 'mimetex') {
1.62      albertel  193: 	return &mimetex_converted;
1.59      albertel  194:     }
                    195:     return &tth_converted;
                    196: }
                    197: 
1.1       harris41  198: # ====================================================================== Footer
                    199: 
                    200: sub footer {
                    201:   my $xmlstring='';
1.65      albertel  202:   if ($env{'request.state'} eq 'construct') {
1.1       harris41  203:       $xmlstring.='<address>'.$errorstring.'</address>';
                    204:   } else {
1.65      albertel  205:       &Apache::lonmsg::author_res_msg($env{'request.filename'},$errorstring);
1.1       harris41  206:   }
                    207: # -------------------------------------------------------------------- End Body
1.3       www       208:   $xmlstring.=&Apache::lonxml::xmlend();
1.1       harris41  209:   return $xmlstring;
1.6       www       210: }
                    211: 
                    212: # ------------------------------------------------------------ Message display
                    213: 
1.9       albertel  214: sub to_convert {
                    215:     my ($string) = @_;
1.22      www       216:     $string=~s/\<br\s*\/?\>/ /gs;
1.30      albertel  217: #    $string=~s/\s/ /gs;
1.18      albertel  218:     $string=&HTML::Entities::decode($string);
1.9       albertel  219:     return &converted(\$string);
                    220: }
                    221: 
1.20      www       222: sub smiley {
1.31      albertel  223:     my $expression=shift;
1.65      albertel  224:     if ($env{'browser.imagesuppress'} eq 'on') { return $expression; }
1.31      albertel  225:     my %smileys=('\:\-\)' => 'smiley',
                    226: 		 '8\-\)'  => 'coolsmile',
                    227: 		 '8\-(I|\|)'   => 'coolindiff',
                    228: 		 ':\-(I|\|)'   => 'neutral',
                    229: 		 '\:\-(o|O|\(\))' => 'shocked',
                    230: 		 ':\-\('  => 'frowny',
                    231: 		 '\;\-\)' => 'wink',
                    232: 		 '\:\-P'  => 'baeh',
                    233: 		 '\:\-(\\\|\\/)' => 'hrrm',
                    234: 		 '\:\-D'  => 'bigsmile',
                    235: 		 '\:\-C'  => 'angry',
                    236: 		 '\:(\'|\`)\-\(' => 'cry',
                    237: 		 '\:\-(X|\#)' => 'lipsrsealed',
                    238: 		 '\:\-S' => 'huh');
                    239:     my $iconpath=$Apache::lonnet::perlvar{'lonIconsURL'};
                    240:     foreach (keys %smileys) {
                    241: 	$expression=~s/$_/\<img src="$iconpath\/$smileys{$_}.gif" \/\>/gs; 
                    242:     }
                    243:     return $expression;
1.20      www       244: }
                    245: 
1.6       www       246: sub msgtexconverted {
1.39      raeburn   247:     my ($message,$email) = @_;
1.17      albertel  248:     $errorstring='';
1.29      albertel  249:     &init_tth();
1.25      www       250:     my $outmessage='';
                    251:     my $tex=0;
                    252:     foreach (split(/(?:\&lt\;|\<)\/*m\s*(?:\&gt\;|\>)/i,$message)) {
                    253: 	if ($tex) {
1.30      albertel  254: 	    if ($email) {
                    255: 		$outmessage.='</pre><tt>'.&to_convert($_).'</tt><pre>'; $tex=0;
                    256: 	    } else {
                    257: 		$outmessage.=&to_convert($_); $tex=0;
                    258: 	    }
1.25      www       259: 	} else {
                    260:             $outmessage.=&smiley($_); $tex=1;
                    261: 	}
                    262:     }
1.64      albertel  263:     $message=$outmessage; $outmessage=''; $tex=0;
                    264:     foreach (split(/(?:\&lt\;|\<)\/*algebra\s*(?:\&gt\;|\>)/i,$message)) {
                    265: 	if ($tex) {
                    266: 	    if ($email) {
                    267: 		$outmessage.='</pre><tt>'.&algebra($_,'web').'</tt><pre>'; $tex=0;
                    268: 	    } else {
                    269: 		$outmessage.=&algebra($_,'web'); $tex=0;
                    270: 	    }
                    271: 	} else {
                    272:             $outmessage.=$_; $tex=1;
                    273: 	}
                    274:     }
1.24      albertel  275:     if (wantarray) {
1.25      www       276: 	return ($outmessage,$errorstring);
1.24      albertel  277:     } else {
1.25      www       278: 	return $outmessage.$errorstring;
1.24      albertel  279:     }
1.1       harris41  280: }
                    281: 
1.44      albertel  282: sub algebra {
1.49      albertel  283:     use AlgParser;
                    284: 
1.58      albertel  285:     my ($string,$target,$style)=@_;
1.44      albertel  286:     my $parser = new AlgParserWithImplicitExpand;
1.46      albertel  287:     $string=&prepare_algebra($string);
1.44      albertel  288:     my $ret = $parser->parse($string);
                    289:     my $result='['.&mt('Algebra unconverted due to previous errors').']';
                    290:     if ( ref($ret) ) {
1.50      albertel  291: 	#$parser->tostring();
1.44      albertel  292: 	$parser->normalize();
                    293: 	my $latex=$parser->tolatex();
1.49      albertel  294: 	$latex=&postprocess_algebra($latex);
1.58      albertel  295: 	if ($style eq 'display') {
                    296: 	    $latex='$$'.$latex.'$$x';
                    297: 	} else {
1.61      albertel  298: 	    # style is 'inline'
1.58      albertel  299: 	    $latex='\\ensuremath{'.$latex.'}';
                    300: 	}
1.44      albertel  301: 	if ($target eq 'web' || $target eq 'analyze') {
                    302: 	    $result = &converted(\$latex);
                    303: 	} else {
                    304: 	    $result = $latex;
                    305: 	}
                    306:     } else {
                    307: 	&Apache::lonxml::error($parser->{'htmlerror'});
                    308:     }
                    309: }
                    310: 
1.46      albertel  311: sub prepare_algebra {
                    312:     my ($string)=@_;
                    313: 
1.50      albertel  314:     # makes the decision about what is a minus sign easier supposedly
1.57      albertel  315:     $string =~ s/(\<\>|\<\=|\>\=[\=\>\<] *)-/$1 zeroplace -/g;
1.48      albertel  316: 
1.46      albertel  317:     return $string;
                    318: }
                    319: 
                    320: sub postprocess_algebra {
                    321:     my ($string)=@_;
1.48      albertel  322:     
1.50      albertel  323:     # moodle had these and I don't know why, ignoring them for now
1.51      albertel  324:     # $string =~s/\\fun/ /g;
1.47      albertel  325: 
1.55      albertel  326:     # remove the extra () in the denominator of a \frac
                    327:     $string =~s/\\frac{(.+?)}{\\left\((.+?)\\right\)}/\\frac{$1}{$2}/gs;
1.51      albertel  328:     
                    329:     # sqrt(3,4) means the 4 root of 3
1.52      albertel  330:     $string =~s/\\sqrt{([^,]+),([^\}]+)}/\\sqrt[$2]{$1}/gs;
                    331: 
1.53      albertel  332:     # log(3,4) means the log base 4 of 3
                    333:     $string =~s/\\log\\left\((.+?),(.+?)\\right\)/\\log_{$2}\\left($1\\right)/gs;
                    334: 
1.54      albertel  335:     # log(3,4) means the log base 4 of 3
                    336:     $string =~s/\\((?:sin|cos|tan|sec|csc|cot)(?:h)?)\\left\((.+?),(.+?)\\right\)/\\$1^{$3}\\left($2\\right)/gs;
                    337: 
1.55      albertel  338:     # int(3,a,b) integral from a to b of 3
                    339:     $string =~s/\\int\\left\((.+?),(.+?),(.+?)\\right\)/\\int_{$2}^{$3}\\left($1\\right)/gs;
                    340: 
                    341:     # int( ... dx) -> ...
                    342:     $string =~s/\\int\\left\((.+?)d[a-z]\\right\)/$1/gs;
1.51      albertel  343: 
1.56      albertel  344:     # 
                    345:     $string =~s/\\lim\\left\((.+?),(.+?),(.+?)\\right\)/\\lim_{$2\\to $3}$1/gs;
1.46      albertel  346:     return $string;
                    347: }
1.1       harris41  348: 1;
                    349: __END__
                    350: 
                    351: 
                    352: 
                    353: 
                    354: 
                    355: 
                    356: 

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