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

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

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