File:  [LON-CAPA] / loncom / xml / lontexconvert.pm
Revision 1.30: download - view: text, annotated - select for diffs
Wed Jan 28 20:48:35 2004 UTC (20 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- Fixes BUG#2499, emails with large <m> where getting shoved onto one line because
   - we use <pre>
   - we strip out extra returns before processing

- I tired these changes out with a variety of messages both with and without <m> sections, and post discussion, looks good to me. let me know if you see anything else that looks ugly

    1: # The LearningOnline Network with CAPA
    2: # TeX Conversion Module
    3: #
    4: # $Id: lontexconvert.pm,v 1.30 2004/01/28 20:48:35 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: 
   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:          &Apache::lonxml::fontsettings().
   79:          "\n<head>\n".
   80:          &Apache::lonmenu::registerurl(undef,'tex').
   81: 	 "\n</head>\n";
   82: }
   83: 
   84: # ================================================================== Conversion
   85: 
   86: $Apache::lontexconvert::messedup=0;
   87: sub converted {
   88:   my $texstring=shift;
   89:   my $xmlstring='[UNDISPLAYABLE]';
   90:   if ($Apache::lontexconvert::messedup) {
   91:       return '[TeX Unconverted Due To Previous Errors]';
   92:   }
   93:   eval(<<'ENDCONV');
   94:   {
   95:     local $SIG{SEGV}=sub { $Apache::lontexconvert::messedup=1; die; };
   96:     if ($ENV{'browser.mathml'}) {
   97:       $xmlstring=&tth::ttm($$texstring);
   98:       $xmlstring=~s/\<math\>/\<math xmlns=\"\&mathns\;\"\>/g;
   99:       $xmlstring=~s/\<br\>/\<br\/\>/g;
  100:       $xmlstring=~s/\<p\>/\<p\>\<\/p\>/g;
  101:       $errorstring.=&tth::ttmerror();
  102:     } else {
  103:       $xmlstring=&tth::tth($$texstring);
  104:       $errorstring.=&tth::ttherror();
  105:       $xmlstring=~s-</font(\s*)>-</font>-g;
  106:     }
  107:     $xmlstring=~s/^\s*//;
  108:     $xmlstring=~s/\s*$//;
  109:   }
  110: ENDCONV
  111:   if ($Apache::lontexconvert::messedup || &tth::tthmessedup()) {
  112:       &Apache::lonnet::logthis("Trying to kill myself");
  113:       $Apache::lontexconvert::messedup=1;
  114:       my $request=Apache->request();
  115:       $request->child_terminate();
  116:   }
  117:   return $xmlstring;
  118: }
  119: 
  120: # ====================================================================== Footer
  121: 
  122: sub footer {
  123:   my $xmlstring='';
  124:   if ($ENV{'request.state'} eq 'construct') {
  125:       $xmlstring.='<address>'.$errorstring.'</address>';
  126:   } else {
  127:       &Apache::lonmsg::author_res_msg($ENV{'request.filename'},$errorstring);
  128:   }
  129: # -------------------------------------------------------------------- End Body
  130:   $xmlstring.=&Apache::lonxml::xmlend();
  131:   return $xmlstring;
  132: }
  133: 
  134: # ------------------------------------------------------------ Message display
  135: 
  136: sub to_convert {
  137:     my ($string) = @_;
  138:     $string=~s/\<br\s*\/?\>/ /gs;
  139: #    $string=~s/\s/ /gs;
  140:     $string=&HTML::Entities::decode($string);
  141:     return &converted(\$string);
  142: }
  143: 
  144: sub smiley {
  145:    my $expression=shift;
  146:    if ($ENV{'browser.imagesuppress'} eq 'on') { return $expression; }
  147:    my %smileys=('\:\-\)' => 'smiley',
  148:                 '8\-\)'  => 'coolsmile',
  149:                 '8\-(I|\|)'   => 'coolindiff',
  150:                 ':\-(I|\|)'   => 'neutral',
  151:                 '\:\-(o|O|\(\))' => 'shocked',
  152:                 ':\-\('  => 'frowny',
  153:                 '\;\-\)' => 'wink',
  154:                 '\:\-P'  => 'baeh',
  155:                 '\:\-(\\\|\\/)' => 'hrrm',
  156:                 '\:\-D'  => 'bigsmile',
  157:                 '\:\-C'  => 'angry',
  158:                 '\:(\'|\`)\-\(' => 'cry',
  159:                 '\:\-(X|\#)' => 'lipsrsealed',
  160:                 '\:\-S' => 'huh');
  161:    my $iconpath=$Apache::lonnet::perlvar{'lonIconsURL'};
  162:    foreach (keys %smileys) {
  163:       $expression=~s/$_/\<img src="$iconpath\/$smileys{$_}.gif" \/\>/gs; 
  164:    }
  165:    return $expression;
  166: }
  167: 
  168: sub msgtexconverted {
  169:     my $message=shift;
  170:     my $email=shift;
  171:     $errorstring='';
  172:     &init_tth();
  173:     my $outmessage='';
  174:     my $tex=0;
  175:     foreach (split(/(?:\&lt\;|\<)\/*m\s*(?:\&gt\;|\>)/i,$message)) {
  176: 	if ($tex) {
  177: 	    if ($email) {
  178: 		$outmessage.='</pre><tt>'.&to_convert($_).'</tt><pre>'; $tex=0;
  179: 	    } else {
  180: 		$outmessage.=&to_convert($_); $tex=0;
  181: 	    }
  182: 	} else {
  183:             $outmessage.=&smiley($_); $tex=1;
  184: 	}
  185:     }
  186:     if (wantarray) {
  187: 	return ($outmessage,$errorstring);
  188:     } else {
  189: 	return $outmessage.$errorstring;
  190:     }
  191: }
  192: 
  193: 1;
  194: __END__
  195: 
  196: 
  197: 
  198: 
  199: 
  200: 
  201: 

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