File:  [LON-CAPA] / loncom / xml / lontexconvert.pm
Revision 1.35: download - view: text, annotated - select for diffs
Tue Mar 9 15:53:18 2004 UTC (20 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- BUG#2809, reports of tth going off into an infinite loop, should catch that and kill off the child cleanly.

    1: # The LearningOnline Network with CAPA
    2: # TeX Conversion Module
    3: #
    4: # $Id: lontexconvert.pm,v 1.35 2004/03/09 15:53:18 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: # 05/29/00,05/30,10/11,10/20 Gerd Kortemeyer
   40: # 5/4 Gerd Kortemeyer
   41: 
   42: package Apache::lontexconvert;
   43: 
   44: use strict;
   45: use tth();
   46: use vars qw($errorstring);
   47: use Apache();
   48: use Apache::lonmsg();
   49: use Apache::lonxml();
   50: use Apache::lonmenu();
   51: use Apache::lonlocal;
   52: 
   53: # ====================================================================== Header
   54: 
   55: sub init_tth {
   56:     my $options=$ENV{'course.'.$ENV{'request.course.id'}.'.tthoptions'};
   57:     if ($ENV{'browser.mathml'}) {
   58: 	&tth::ttminit();
   59: 	if ($ENV{'browser.unicode'}) {
   60: 	    &tth::ttmoptions('-L -u1 '.$options);
   61: 	} else {
   62: 	    &tth::ttmoptions('-L -u0 '.$options);
   63: 	}
   64:     } else {
   65: 	&tth::tthinit();
   66: 	if ($ENV{'browser.unicode'}) {
   67: 	    &tth::tthoptions('-L -u1 '.$options);
   68: 	} else {
   69: 	    &tth::tthoptions('-L -u0 '.$options);
   70: 	}
   71:     }
   72: }
   73: 
   74: sub header {
   75:     $errorstring='';
   76:     my $time=time;
   77:     &init_tth();
   78:     return &Apache::lonxml::xmlbegin().
   79: 	&Apache::lonxml::fontsettings().
   80: 	"\n<head>\n".
   81: 	&Apache::lonmenu::registerurl(undef,'tex').
   82: 	"\n</head>\n";
   83: }
   84: 
   85: # ================================================================== Conversion
   86: 
   87: $Apache::lontexconvert::messedup=0;
   88: sub converted {
   89:     my $texstring=shift;
   90:     my $xmlstring='['.&mt('UNDISPLAYABLE').']';
   91:     if ($Apache::lontexconvert::messedup) {
   92: 	return '['.&mt('TeX unconverted due to previous errors').']';
   93:     }
   94:     eval(<<'ENDCONV');
   95:     {
   96: 	local $SIG{SEGV}=sub { $Apache::lontexconvert::messedup=1; die; };
   97: 	local $SIG{ALRM}=sub { 
   98: 	    $xmlstring='['.&mt("TeX unconverted due to errors").']';
   99: 	    $Apache::lontexconvert::messedup=1;
  100: 	    die &mt("TeX unconverted due to errors"); };
  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*//;
  114: 	$xmlstring=~s/\s*$//;
  115: 	alarm(0);
  116:     }
  117: ENDCONV
  118:     if ($@) {
  119: 	$errorstring.=&mt("Evaluation Error: ").$@;
  120: 	$Apache::lontexconvert::messedup=1;
  121:     }
  122:     if ($Apache::lontexconvert::messedup || &tth::tthmessedup()) {
  123: 	&Apache::lonnet::logthis("Trying to kill myself");
  124: 	$Apache::lontexconvert::messedup=1;
  125: 	my $request=Apache->request();
  126: 	$request->child_terminate();
  127:     }
  128:     return $xmlstring;
  129: }
  130: 
  131: # ====================================================================== Footer
  132: 
  133: sub footer {
  134:   my $xmlstring='';
  135:   if ($ENV{'request.state'} eq 'construct') {
  136:       $xmlstring.='<address>'.$errorstring.'</address>';
  137:   } else {
  138:       &Apache::lonmsg::author_res_msg($ENV{'request.filename'},$errorstring);
  139:   }
  140: # -------------------------------------------------------------------- End Body
  141:   $xmlstring.=&Apache::lonxml::xmlend();
  142:   return $xmlstring;
  143: }
  144: 
  145: # ------------------------------------------------------------ Message display
  146: 
  147: sub to_convert {
  148:     my ($string) = @_;
  149:     $string=~s/\<br\s*\/?\>/ /gs;
  150: #    $string=~s/\s/ /gs;
  151:     $string=&HTML::Entities::decode($string);
  152:     return &converted(\$string);
  153: }
  154: 
  155: sub smiley {
  156:     my $expression=shift;
  157:     if ($ENV{'browser.imagesuppress'} eq 'on') { return $expression; }
  158:     my %smileys=('\:\-\)' => 'smiley',
  159: 		 '8\-\)'  => 'coolsmile',
  160: 		 '8\-(I|\|)'   => 'coolindiff',
  161: 		 ':\-(I|\|)'   => 'neutral',
  162: 		 '\:\-(o|O|\(\))' => 'shocked',
  163: 		 ':\-\('  => 'frowny',
  164: 		 '\;\-\)' => 'wink',
  165: 		 '\:\-P'  => 'baeh',
  166: 		 '\:\-(\\\|\\/)' => 'hrrm',
  167: 		 '\:\-D'  => 'bigsmile',
  168: 		 '\:\-C'  => 'angry',
  169: 		 '\:(\'|\`)\-\(' => 'cry',
  170: 		 '\:\-(X|\#)' => 'lipsrsealed',
  171: 		 '\:\-S' => 'huh');
  172:     my $iconpath=$Apache::lonnet::perlvar{'lonIconsURL'};
  173:     foreach (keys %smileys) {
  174: 	$expression=~s/$_/\<img src="$iconpath\/$smileys{$_}.gif" \/\>/gs; 
  175:     }
  176:     return $expression;
  177: }
  178: 
  179: sub msgtexconverted {
  180:     my $message=shift;
  181:     my $email=shift;
  182:     $errorstring='';
  183:     &init_tth();
  184:     my $outmessage='';
  185:     my $tex=0;
  186:     foreach (split(/(?:\&lt\;|\<)\/*m\s*(?:\&gt\;|\>)/i,$message)) {
  187: 	if ($tex) {
  188: 	    if ($email) {
  189: 		$outmessage.='</pre><tt>'.&to_convert($_).'</tt><pre>'; $tex=0;
  190: 	    } else {
  191: 		$outmessage.=&to_convert($_); $tex=0;
  192: 	    }
  193: 	} else {
  194:             $outmessage.=&smiley($_); $tex=1;
  195: 	}
  196:     }
  197:     if (wantarray) {
  198: 	return ($outmessage,$errorstring);
  199:     } else {
  200: 	return $outmessage.$errorstring;
  201:     }
  202: }
  203: 
  204: 1;
  205: __END__
  206: 
  207: 
  208: 
  209: 
  210: 
  211: 
  212: 

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