Annotation of loncom/xml/lontexconvert.pm, revision 1.34
1.1 harris41 1: # The LearningOnline Network with CAPA
2: # TeX Conversion Module
3: #
1.34 ! www 4: # $Id: lontexconvert.pm,v 1.33 2004/03/04 15:46:43 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: # 05/29/00,05/30,10/11,10/20 Gerd Kortemeyer
1.3 www 40: # 5/4 Gerd Kortemeyer
1.1 harris41 41:
42: package Apache::lontexconvert;
43:
44: use strict;
1.33 albertel 45: use tth();
1.1 harris41 46: use vars qw($errorstring);
1.19 albertel 47: use Apache();
1.33 albertel 48: use Apache::lonmsg();
49: use Apache::lonxml();
50: use Apache::lonmenu();
51: use Apache::lonlocal;
1.1 harris41 52:
53: # ====================================================================== Header
54:
1.29 albertel 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:
1.1 harris41 74: sub header {
1.31 albertel 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";
1.1 harris41 83: }
84:
85: # ================================================================== Conversion
86:
1.19 albertel 87: $Apache::lontexconvert::messedup=0;
1.1 harris41 88: sub converted {
1.31 albertel 89: my $texstring=shift;
1.34 ! www 90: my $xmlstring='['.&mt('UNDISPLAYABLE').']';
1.31 albertel 91: if ($Apache::lontexconvert::messedup) {
1.33 albertel 92: return &mt('[TeX Unconverted Due To Previous Errors]');
1.31 albertel 93: }
94: eval(<<'ENDCONV');
95: {
96: local $SIG{SEGV}=sub { $Apache::lontexconvert::messedup=1; die; };
97: if ($ENV{'browser.mathml'}) {
98: $xmlstring=&tth::ttm($$texstring);
99: $xmlstring=~s/\<math\>/\<math xmlns=\"\&mathns\;\"\>/g;
100: $xmlstring=~s/\<br\>/\<br\/\>/g;
101: $xmlstring=~s/\<p\>/\<p\>\<\/p\>/g;
102: $errorstring.=&tth::ttmerror();
103: } else {
104: $xmlstring=&tth::tth($$texstring);
105: $errorstring.=&tth::ttherror();
106: $xmlstring=~s-</font(\s*)>-</font>-g;
107: }
108: $xmlstring=~s/^\s*//;
109: $xmlstring=~s/\s*$//;
1.12 www 110: }
111: ENDCONV
1.32 albertel 112: if ($@) {
1.33 albertel 113: $errorstring.=&mt("Evaluation Error ").$@;
1.32 albertel 114: $Apache::lontexconvert::messedup=1;
115: }
1.31 albertel 116: if ($Apache::lontexconvert::messedup || &tth::tthmessedup()) {
117: &Apache::lonnet::logthis("Trying to kill myself");
118: $Apache::lontexconvert::messedup=1;
119: my $request=Apache->request();
120: $request->child_terminate();
121: }
122: return $xmlstring;
1.1 harris41 123: }
124:
125: # ====================================================================== Footer
126:
127: sub footer {
128: my $xmlstring='';
129: if ($ENV{'request.state'} eq 'construct') {
130: $xmlstring.='<address>'.$errorstring.'</address>';
131: } else {
132: &Apache::lonmsg::author_res_msg($ENV{'request.filename'},$errorstring);
133: }
134: # -------------------------------------------------------------------- End Body
1.3 www 135: $xmlstring.=&Apache::lonxml::xmlend();
1.1 harris41 136: return $xmlstring;
1.6 www 137: }
138:
139: # ------------------------------------------------------------ Message display
140:
1.9 albertel 141: sub to_convert {
142: my ($string) = @_;
1.22 www 143: $string=~s/\<br\s*\/?\>/ /gs;
1.30 albertel 144: # $string=~s/\s/ /gs;
1.18 albertel 145: $string=&HTML::Entities::decode($string);
1.9 albertel 146: return &converted(\$string);
147: }
148:
1.20 www 149: sub smiley {
1.31 albertel 150: my $expression=shift;
151: if ($ENV{'browser.imagesuppress'} eq 'on') { return $expression; }
152: my %smileys=('\:\-\)' => 'smiley',
153: '8\-\)' => 'coolsmile',
154: '8\-(I|\|)' => 'coolindiff',
155: ':\-(I|\|)' => 'neutral',
156: '\:\-(o|O|\(\))' => 'shocked',
157: ':\-\(' => 'frowny',
158: '\;\-\)' => 'wink',
159: '\:\-P' => 'baeh',
160: '\:\-(\\\|\\/)' => 'hrrm',
161: '\:\-D' => 'bigsmile',
162: '\:\-C' => 'angry',
163: '\:(\'|\`)\-\(' => 'cry',
164: '\:\-(X|\#)' => 'lipsrsealed',
165: '\:\-S' => 'huh');
166: my $iconpath=$Apache::lonnet::perlvar{'lonIconsURL'};
167: foreach (keys %smileys) {
168: $expression=~s/$_/\<img src="$iconpath\/$smileys{$_}.gif" \/\>/gs;
169: }
170: return $expression;
1.20 www 171: }
172:
1.6 www 173: sub msgtexconverted {
174: my $message=shift;
1.30 albertel 175: my $email=shift;
1.17 albertel 176: $errorstring='';
1.29 albertel 177: &init_tth();
1.25 www 178: my $outmessage='';
179: my $tex=0;
180: foreach (split(/(?:\<\;|\<)\/*m\s*(?:\>\;|\>)/i,$message)) {
181: if ($tex) {
1.30 albertel 182: if ($email) {
183: $outmessage.='</pre><tt>'.&to_convert($_).'</tt><pre>'; $tex=0;
184: } else {
185: $outmessage.=&to_convert($_); $tex=0;
186: }
1.25 www 187: } else {
188: $outmessage.=&smiley($_); $tex=1;
189: }
190: }
1.24 albertel 191: if (wantarray) {
1.25 www 192: return ($outmessage,$errorstring);
1.24 albertel 193: } else {
1.25 www 194: return $outmessage.$errorstring;
1.24 albertel 195: }
1.1 harris41 196: }
197:
198: 1;
199: __END__
200:
201:
202:
203:
204:
205:
206:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>