File:  [LON-CAPA] / loncom / xml / lontexconvert.pm
Revision 1.38: download - view: text, annotated - select for diffs
Thu Jul 22 23:18:00 2004 UTC (19 years, 10 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Allow posters to edit/delete their own discussion postings.  Controlled by
course parameter set via PARM. Previous versions of postings are preserved.
Deleted postings are hidden. Course Coordinators etc. will be able to see
all versions of postings (interface still needed). Editing currently works for
resource discussions, but not bulletin boards (work in progress).

    1: # The LearningOnline Network with CAPA
    2: # TeX Conversion Module
    3: #
    4: # $Id: lontexconvert.pm,v 1.38 2004/07/22 23:18:00 raeburn 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: 
   89: # we need this routine because &converted can get called from inside
   90: # of the safespace (through &xmlparse('<m>stuff</m>') which doesn't
   91: # allow the opcode for alarm, so we need to compile this before we get
   92: # into the safe space since opcode checks only occur at compile time
   93: sub convert_real {
   94:     my ($texstring)=@_;
   95:     my ($xmlstring,$errorstring);
   96:     local $SIG{SEGV}=sub { $Apache::lontexconvert::messedup=1; die; };
   97:     local $SIG{ALRM}=sub { 
   98: 	&Apache::lonnet::logthis("ALRM");
   99: 	$xmlstring='['.&mt("TeX unconverted due to errors").']';
  100: 	$Apache::lontexconvert::messedup=1;
  101: 	die &mt("TeX unconverted due to errors");
  102:     };
  103:     alarm($Apache::lonnet::perlvar{'lonScriptTimeout'});
  104:     if ($ENV{'browser.mathml'}) {
  105: 	$xmlstring=&tth::ttm($$texstring);
  106: 	$xmlstring=~s/\<math\>/\<math xmlns=\"\&mathns\;\"\>/g;
  107: 	$xmlstring=~s/\<br\>/\<br\/\>/g;
  108: 	$xmlstring=~s/\<p\>/\<p\>\<\/p\>/g;
  109: 	$errorstring.=&tth::ttmerror();
  110:     } else {
  111: 	$xmlstring=&tth::tth($$texstring);
  112: 	$errorstring.=&tth::ttherror();
  113: 	$xmlstring=~s-</font(\s*)>-</font>-g;
  114:     }
  115:     $xmlstring=~s/^\s*//;
  116:     $xmlstring=~s/\s*$//;
  117:     alarm(0);
  118:     return ($xmlstring,$errorstring);
  119: }
  120: 
  121: sub 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:     eval(<<'ENDCONV');
  128:     ($xmlstring,$errorstring)=&convert_real($texstring)
  129: ENDCONV
  130:     if ($@) {
  131: 	$errorstring.=&mt("Evaluation Error: ").$@;
  132: 	$Apache::lontexconvert::messedup=1;
  133:     }
  134:     if ($Apache::lontexconvert::messedup || &tth::tthmessedup() || 
  135: 	$errorstring) {
  136: 	&Apache::lonnet::logthis("Trying to kill myself");
  137: 	$Apache::lontexconvert::messedup=1;
  138: 	my $request=Apache->request();
  139: 	$request->child_terminate();
  140:     }
  141:     return $xmlstring;
  142: }
  143: 
  144: # ====================================================================== Footer
  145: 
  146: sub footer {
  147:   my $xmlstring='';
  148:   if ($ENV{'request.state'} eq 'construct') {
  149:       $xmlstring.='<address>'.$errorstring.'</address>';
  150:   } else {
  151:       &Apache::lonmsg::author_res_msg($ENV{'request.filename'},$errorstring);
  152:   }
  153: # -------------------------------------------------------------------- End Body
  154:   $xmlstring.=&Apache::lonxml::xmlend();
  155:   return $xmlstring;
  156: }
  157: 
  158: # ------------------------------------------------------------ Message display
  159: 
  160: sub to_convert {
  161:     my ($string) = @_;
  162:     $string=~s/\<br\s*\/?\>/ /gs;
  163: #    $string=~s/\s/ /gs;
  164:     $string=&HTML::Entities::decode($string);
  165:     return &converted(\$string);
  166: }
  167: 
  168: sub smiley {
  169:     my $expression=shift;
  170:     if ($ENV{'browser.imagesuppress'} eq 'on') { return $expression; }
  171:     my %smileys=('\:\-\)' => 'smiley',
  172: 		 '8\-\)'  => 'coolsmile',
  173: 		 '8\-(I|\|)'   => 'coolindiff',
  174: 		 ':\-(I|\|)'   => 'neutral',
  175: 		 '\:\-(o|O|\(\))' => 'shocked',
  176: 		 ':\-\('  => 'frowny',
  177: 		 '\;\-\)' => 'wink',
  178: 		 '\:\-P'  => 'baeh',
  179: 		 '\:\-(\\\|\\/)' => 'hrrm',
  180: 		 '\:\-D'  => 'bigsmile',
  181: 		 '\:\-C'  => 'angry',
  182: 		 '\:(\'|\`)\-\(' => 'cry',
  183: 		 '\:\-(X|\#)' => 'lipsrsealed',
  184: 		 '\:\-S' => 'huh');
  185:     my $iconpath=$Apache::lonnet::perlvar{'lonIconsURL'};
  186:     foreach (keys %smileys) {
  187: 	$expression=~s/$_/\<img src="$iconpath\/$smileys{$_}.gif" \/\>/gs; 
  188:     }
  189:     return $expression;
  190: }
  191: 
  192: sub msgtexconverted {
  193:     my ($message,$email,$priorversions) = @_;
  194:     $errorstring='';
  195:     &init_tth();
  196:     my $outmessage='';
  197:     my $tex=0;
  198:     if ($priorversions > 0) {
  199:         if ($message =~ /::::\d+::::(.+)$/) {
  200:             $message = $1;
  201:         }
  202:     }
  203:     foreach (split(/(?:\&lt\;|\<)\/*m\s*(?:\&gt\;|\>)/i,$message)) {
  204: 	if ($tex) {
  205: 	    if ($email) {
  206: 		$outmessage.='</pre><tt>'.&to_convert($_).'</tt><pre>'; $tex=0;
  207: 	    } else {
  208: 		$outmessage.=&to_convert($_); $tex=0;
  209: 	    }
  210: 	} else {
  211:             $outmessage.=&smiley($_); $tex=1;
  212: 	}
  213:     }
  214:     if (wantarray) {
  215: 	return ($outmessage,$errorstring);
  216:     } else {
  217: 	return $outmessage.$errorstring;
  218:     }
  219: }
  220: 
  221: 1;
  222: __END__
  223: 
  224: 
  225: 
  226: 
  227: 
  228: 
  229: 

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