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