version 1.24, 2003/06/23 21:28:59
|
version 1.93, 2008/12/04 19:53:53
|
Line 36
|
Line 36
|
# The C source of the Code may not be distributed by the Licensee |
# The C source of the Code may not be distributed by the Licensee |
# to any other parties under any circumstances. |
# to any other parties under any circumstances. |
# |
# |
# 05/29/00,05/30,10/11,10/20 Gerd Kortemeyer |
|
# 5/4 Gerd Kortemeyer |
|
|
|
package Apache::lontexconvert; |
package Apache::lontexconvert; |
|
|
use strict; |
use strict; |
use tth; |
use tth(); |
use vars qw($errorstring); |
use vars qw($errorstring); |
use Apache(); |
#use Apache::lonxml(); |
use Apache::lonmsg; |
use Apache::lonlocal; |
use Apache::lonxml; |
use Apache::lonnet; |
use Apache::lonmenu; |
use lib '/home/httpd/lib/perl/'; |
|
use LONCAPA; |
|
use LWP::UserAgent; |
|
|
|
|
# ====================================================================== Header |
# ====================================================================== Header |
|
|
sub header { |
sub init_tth { |
$errorstring=''; |
my $options=$env{'course.'.$env{'request.course.id'}.'.tthoptions'}; |
my $time=time; |
if ($options =~ /\S/) { |
if ($ENV{'browser.mathml'}) { |
$options = ' '.$options; |
&tth::ttminit(); |
} else { |
if ($ENV{'browser.unicode'}) { |
undef($options); |
&tth::ttmoptions('-L -u1'); |
} |
} else { |
if ($env{'browser.mathml'}) { |
&tth::ttmoptions('-L -u0'); |
&tth::ttminit(); |
} |
if ($env{'browser.unicode'}) { |
} else { |
&tth::ttmoptions('-L -u1'.$options); |
&tth::tthinit(); |
} else { |
if ($ENV{'browser.unicode'}) { |
&tth::ttmoptions('-L -u0'.$options); |
&tth::tthoptions('-L -u1'); |
} |
} else { |
} else { |
&tth::tthoptions('-L -u0'); |
&tth::tthinit(); |
} |
if ($env{'browser.unicode'}) { |
} |
&tth::tthoptions('-L -u1'.$options); |
return &Apache::lonxml::xmlbegin(). |
} else { |
&Apache::lonxml::fontsettings(). |
&tth::tthoptions('-L -u0'.$options); |
"\n<head>\n". |
} |
&Apache::lonmenu::registerurl(undef,'tex'). |
} |
"\n</head>\n"; |
|
} |
} |
|
|
# ================================================================== Conversion |
# ================================================================== Conversion |
|
|
$Apache::lontexconvert::messedup=0; |
$Apache::lontexconvert::messedup=0; |
sub converted { |
|
my $texstring=shift; |
|
my $xmlstring='[UNDISPLAYABLE]'; |
sub convert_real { |
if ($Apache::lontexconvert::messedup) { |
my ($texstring)=@_; |
return '[TeX Uncoverted Due To Previous Errors]'; |
my ($xmlstring,$errorstring); |
} |
|
eval(<<'ENDCONV'); |
|
{ |
|
local $SIG{SEGV}=sub { $Apache::lontexconvert::messedup=1; die; }; |
local $SIG{SEGV}=sub { $Apache::lontexconvert::messedup=1; die; }; |
if ($ENV{'browser.mathml'}) { |
local $SIG{ALRM}=sub { |
$xmlstring=&tth::ttm($$texstring); |
&Apache::lonnet::logthis("ALRM"); |
$xmlstring=~s/\<math\>/\<math xmlns=\"\&mathns\;\"\>/g; |
$xmlstring='['.&mt("TeX unconverted due to errors").']'; |
$xmlstring=~s/\<br\>/\<br\/\>/g; |
$Apache::lontexconvert::messedup=1; |
$xmlstring=~s/\<p\>/\<p\>\<\/p\>/g; |
die &mt("TeX unconverted due to errors"); |
$errorstring.=&tth::ttmerror(); |
}; |
|
&Apache::lonxml::start_alarm(); |
|
if ($env{'browser.mathml'}) { |
|
$xmlstring=&tth::ttm($$texstring); |
|
$xmlstring=~s/\<math\>/\<math xmlns=\"\&mathns\;\"\>/g; |
|
$xmlstring=~s/\<br\>/\<br\/\>/g; |
|
$xmlstring=~s/\<p\>/\<p\>\<\/p\>/g; |
|
$errorstring.=&tth::ttmerror(); |
} else { |
} else { |
$xmlstring=&tth::tth($$texstring); |
$xmlstring=&tth::tth($$texstring); |
$errorstring.=&tth::ttherror(); |
$errorstring.=&tth::ttherror(); |
|
$xmlstring=~s-</font(\s*)>-</font>-g; |
} |
} |
|
$xmlstring=~s/^\s*\<br clear\=\"all\"/\<br/s; |
$xmlstring=~s/^\s*//; |
$xmlstring=~s/^\s*//; |
$xmlstring=~s/\s*$//; |
$xmlstring=~s/\s*$//; |
} |
# |
|
# \rightleftharpoons is not converted by tth but maps |
|
# reasonably well to ⇔. If we get many more of these, |
|
# we're going to need to have a translation sub. |
|
# |
|
my $lrharpoon = pack("U", 0x21cc); |
|
$xmlstring=~s/\\rightleftharpoons/$lrharpoon/g; |
|
|
|
&Apache::lonxml::end_alarm(); |
|
return ($xmlstring,$errorstring); |
|
} |
|
|
|
sub tth_converted { |
|
my $texstring=shift; |
|
my $xmlstring='['.&mt('UNDISPLAYABLE').']'; |
|
if ($Apache::lontexconvert::messedup) { |
|
return '['.&mt('TeX unconverted due to previous errors').']'; |
|
} |
|
$$texstring ='\\documentstyle{article}'.$$texstring; |
|
|
|
eval(<<'ENDCONV'); |
|
($xmlstring,$errorstring)=&convert_real($texstring) |
ENDCONV |
ENDCONV |
if ($Apache::lontexconvert::messedup || &tth::tthmessedup()) { |
if ($@) { |
&Apache::lonnet::logthis("Trying to kill myself"); |
$errorstring.=&mt("Evaluation Error: ").$@; |
$Apache::lontexconvert::messedup=1; |
$Apache::lontexconvert::messedup=1; |
my $request=Apache->request(); |
} |
$request->child_terminate(); |
if ($Apache::lontexconvert::messedup || &tth::tthmessedup() || |
} |
$errorstring) { |
return $xmlstring; |
&Apache::lonnet::logthis("Trying to kill myself"); |
} |
$Apache::lontexconvert::messedup=1; |
|
if (ref($Apache::lonxml::request)) { |
# ====================================================================== Footer |
$Apache::lonxml::request->child_terminate(); |
|
} else { |
sub footer { |
my $request; |
my $xmlstring=''; |
eval { $request=Apache->request; }; |
if ($ENV{'request.state'} eq 'construct') { |
if (!$request) { |
$xmlstring.='<address>'.$errorstring.'</address>'; |
eval { $request=Apache2::RequestUtil->request; }; |
} else { |
} |
&Apache::lonmsg::author_res_msg($ENV{'request.filename'},$errorstring); |
if ($request) { |
} |
$request->child_terminate(); |
# -------------------------------------------------------------------- End Body |
} else { |
$xmlstring.=&Apache::lonxml::xmlend(); |
&Apache::lonnet::logthis("Unable to find a request to do child_terminate on"); |
return $xmlstring; |
} |
|
} |
|
} |
|
return $xmlstring; |
|
} |
|
|
|
sub clean_out_math_mode { |
|
my ($texstring)=@_; |
|
$$texstring=~s/(?<!\\)\$//g; |
|
$$texstring=~s/\\[\)\(\]\[]//g; |
|
$$texstring=~s/\\ensuremath//g; |
|
return ''; |
|
} |
|
|
|
sub displaystyle { |
|
my ($texstring)=@_; |
|
#has a $$ or \[ or \displaystyle in it, guessinng it's display mode |
|
if ($$texstring=~/[^\\]\$\$/ || |
|
$$texstring=~/\\\[/ || |
|
$$texstring=~/\\displaystyle/) { return 1; } |
|
return 0; |
|
} |
|
|
|
sub jsMath_converted { |
|
my $texstring=shift; |
|
my $tag='span'; |
|
if (&displaystyle($texstring)) { $tag='div'; } |
|
&clean_out_math_mode($texstring); |
|
return &jsMath_header(). |
|
'<'.$tag.' class="math">'.$$texstring.'</'.$tag.'>'; |
|
} |
|
|
|
{ |
|
my @jsMath_sent_header; |
|
sub jsMath_reset { |
|
undef(@jsMath_sent_header); |
|
} |
|
sub jsMath_push { |
|
push(@jsMath_sent_header,0); |
|
} |
|
sub jsMath_header { |
|
if (!@jsMath_sent_header) { |
|
&Apache::lonnet::logthis("mismatched calls of jsMath_header and jsMath_process"); |
|
return ''; |
|
} |
|
return '' if $jsMath_sent_header[-1]; |
|
$jsMath_sent_header[-1]=1; |
|
return |
|
'<script type="text/javascript"> |
|
function NoFontMessage () {} |
|
jsMath = {Parser: {prototype: {environments: {\'eqnarray*\' :[\'Array\',null,null,\'rcl\',[5/18,5/18],3,\'D\']}}}}; |
|
</script>'."\n". |
|
'<script type="text/javascript" src="/adm/jsMath/jsMath.js"></script>'."\n"; |
|
} |
|
sub jsMath_process { |
|
my $state = pop(@jsMath_sent_header); |
|
return '' if !$state; |
|
return "\n". |
|
'<script type="text/javascript">jsMath.Process()</script>'."\n"; |
|
} |
|
sub jsMath_state { |
|
my ($level) = @_; |
|
return $jsMath_sent_header[$level]; |
|
} |
|
} |
|
|
|
sub tex_engine { |
|
if (exists($env{'form.texengine'})) { |
|
if ($env{'form.texengine'} ne '') { |
|
return $env{'form.texengine'}; |
|
} |
|
} |
|
if ($env{'request.course.id'} |
|
&& exists($env{'course.'.$env{'request.course.id'}.'.texengine'})) { |
|
return $env{'course.'.$env{'request.course.id'}.'.texengine'}; |
|
} |
|
if (exists($env{'environment.texengine'})) { |
|
return $env{'environment.texengine'}; |
|
} |
|
return 'tth'; |
|
} |
|
|
|
sub init_math_support { |
|
my ($inherit_jsmath) = @_; |
|
&init_tth(); |
|
&Apache::lontexconvert::jsMath_push(); |
|
if (lc(&tex_engine()) eq 'jsmath' || |
|
($inherit_jsmath && &jsMath_state(-2))) { |
|
return &Apache::lontexconvert::jsMath_header(); |
|
} |
|
return; |
|
} |
|
|
|
sub mimetex_valign { |
|
my ($texstring)=@_; |
|
my $ua = LWP::UserAgent->new; #from the perldoc of LWP::UserAgent |
|
$ua->timeout(10); |
|
$ua->env_proxy; |
|
#header without imagedata saved to response: |
|
my $response = $ua->head('http://localhost.localdomain/cgi-bin/mimetex.cgi?'.$texstring); |
|
if ($response->is_success) { |
|
#get the valign-value: |
|
return($response->headers->{'vertical-align'});} |
|
else { |
|
return(0); #if (error) than continue without valign |
|
} |
|
} |
|
|
|
sub mimetex_converted { |
|
my $texstring=shift; |
|
my $displaystyle=&displaystyle($texstring); |
|
|
|
&clean_out_math_mode($texstring); |
|
|
|
if ($displaystyle) { |
|
$$texstring='\\displaystyle \\Large '.$$texstring; |
|
} |
|
|
|
my $result='<img src="/cgi-bin/mimetex.cgi?'.&escape($$texstring).'" style="vertical-align:'.&mimetex_valign($$texstring).'px" alt="$'.$$texstring.'$" />'; |
|
if ($displaystyle) { |
|
$result='<center>'.$result.'</center>'; |
|
} |
|
return $result; |
|
} |
|
|
|
sub converted { |
|
my ($string,$mode)=@_; |
|
if ($mode eq '') { $mode = &tex_engine(); } |
|
if ($mode =~ /tth/i) { |
|
return &tth_converted($string); |
|
} elsif ($mode =~ /jsmath/i) { |
|
return &jsMath_converted($string); |
|
} elsif ($mode =~ /mimetex/i) { |
|
return &mimetex_converted($string); |
|
} |
|
return &tth_converted($string); |
} |
} |
|
|
# ------------------------------------------------------------ Message display |
# ------------------------------------------------------------ Message display |
Line 130 sub footer {
|
Line 292 sub footer {
|
sub to_convert { |
sub to_convert { |
my ($string) = @_; |
my ($string) = @_; |
$string=~s/\<br\s*\/?\>/ /gs; |
$string=~s/\<br\s*\/?\>/ /gs; |
$string=~s/\s/ /gs; |
# $string=~s/\s/ /gs; |
$string=&HTML::Entities::decode($string); |
$string=&HTML::Entities::decode($string); |
return &converted(\$string); |
return &converted(\$string); |
} |
} |
|
|
sub smiley { |
sub smiley { |
my $expression=shift; |
my $expression=shift; |
if ($ENV{'browser.imagesuppress'} eq 'on') { return $expression; } |
if ($env{'browser.imagesuppress'} eq 'on') { return $expression; } |
my %smileys=('\:\-*\)' => 'smiley', |
my %smileys=('\:\-\)' => 'smiley', |
'8\-*\)' => 'coolsmile', |
'8\-\)' => 'coolsmile', |
'8\-*I' => 'coolindiff', |
'8\-(I|\|)' => 'coolindiff', |
':\-*\(' => 'frowny', |
':\-(I|\|)' => 'neutral', |
'\:\-*(o|O|\(\))' => 'shocked', |
'\:\-(o|O|\(\))' => 'shocked', |
'\;\-*\)' => 'wink', |
':\-\(' => 'frowny', |
'\:\-*P' => 'baeh', |
'\;\-\)' => 'wink', |
'\:\-(\\\|\\/)' => 'hrrm', |
'\:\-P' => 'baeh', |
'\:\-*D' => 'bigsmile', |
'\:\-(\\\|\\/)' => 'hrrm', |
'\:\-*C' => 'angry', |
'\:\-D' => 'bigsmile', |
'\:(\'|\`)\-*\(' => 'cry', |
'\:\-C' => 'angry', |
'\:\-*(X|\#)' => 'lipsrsealed', |
'\:(\'|\`)\-\(' => 'cry', |
'\:\-*S' => 'huh'); |
'\:\-(X|\#)' => 'lipsrsealed', |
my $iconpath=$Apache::lonnet::perlvar{'lonIconsURL'}; |
'\:\-S' => 'huh'); |
foreach (keys %smileys) { |
my $iconpath=$Apache::lonnet::perlvar{'lonIconsURL'}; |
$expression=~s/$_/\<img src="$iconpath\/$smileys{$_}.gif" \/\>/gs; |
foreach my $smiley (keys(%smileys)) { |
} |
$expression=~s/$smiley/\<img src="$iconpath\/$smileys{$smiley}.gif" \/\>/gs; |
return $expression; |
} |
|
return $expression; |
} |
} |
|
|
sub msgtexconverted { |
sub msgtexconverted { |
my $message=shift; |
my ($message,$email) = @_; |
|
|
$errorstring=''; |
$errorstring=''; |
if ($ENV{'browser.mathml'}) { |
&init_tth(); |
&tth::ttminit(); |
my $outmessage=''; |
if ($ENV{'browser.unicode'}) { |
my $tex=0; |
&tth::ttmoptions('-L -u1'); |
foreach my $fragment (split(/(?:\<\;|\<)\/*m\s*(?:\>\;|\>)/i,$message)) { |
|
if ($tex) { |
|
if ($email) { |
|
$outmessage.='</pre><tt>'.&to_convert($fragment).'</tt><pre>'; |
|
$tex=0; |
|
} else { |
|
$outmessage.=&to_convert($fragment); |
|
$tex=0; |
|
} |
} else { |
} else { |
&tth::ttmoptions('-L -u0'); |
$outmessage.=&smiley($fragment); |
|
$tex=1; |
} |
} |
} else { |
} |
&tth::tthinit(); |
$message=$outmessage; $outmessage=''; $tex=0; |
if ($ENV{'browser.unicode'}) { |
foreach my $fragment (split(/(?:\<\;|\<)\/*algebra\s*(?:\>\;|\>)/i, |
&tth::tthoptions('-L -u1'); |
$message)) { |
|
if ($tex) { |
|
if ($email) { |
|
$outmessage.='</pre><tt>'.&algebra($fragment,'web').'</tt><pre>'; |
|
$tex=0; |
|
} else { |
|
$outmessage.=&algebra($fragment,'web'); |
|
$tex=0; |
|
} |
} else { |
} else { |
&tth::tthoptions('-L -u0'); |
$outmessage.=$fragment; |
|
$tex=1; |
} |
} |
} |
} |
$message=~s/(\$\$.+?\$\$)/&to_convert($1)/gse; |
|
$message=~s/(\$.+?\$)/&to_convert($1)/gse; |
|
$message=~s/(\\\(.+?\\\))/&to_convert($1)/gse; |
|
$message=~s/(\\\[.+?\\\])/&to_convert($1)/gse; |
|
if (wantarray) { |
if (wantarray) { |
return (&smiley($message),$errorstring); |
return ($outmessage,$errorstring); |
|
} else { |
|
return $outmessage.$errorstring; |
|
} |
|
} |
|
|
|
sub algebra { |
|
use AlgParser; |
|
|
|
my ($string,$target,$style,$parstack,$safeeval)=@_; |
|
my $parser = new AlgParserWithImplicitExpand; |
|
$string=&prepare_algebra($string); |
|
my $ret = $parser->parse($string); |
|
my $result='['.&mt('Algebra unconverted due to previous errors').']'; |
|
if ( ref($ret) ) { |
|
#$parser->tostring(); |
|
$parser->normalize(); |
|
my $latex=$parser->tolatex(); |
|
$latex=&postprocess_algebra($latex); |
|
if ($style eq 'display') { |
|
$latex='$$'.$latex.'$$x'; |
|
} else { |
|
# style is 'inline' |
|
$latex='\\ensuremath{'.$latex.'}'; |
|
} |
|
if ($target eq 'web' || $target eq 'analyze') { |
|
my $display=&Apache::lonxml::get_param('display',$parstack,$safeeval); |
|
$result = &converted(\$latex,$display); |
|
# $result = &converted(\$latex); |
|
} else { |
|
$result = $latex; |
|
} |
} else { |
} else { |
return &smiley($message).$errorstring; |
&Apache::lonxml::error($parser->{'htmlerror'}); |
} |
} |
} |
} |
|
|
|
sub prepare_algebra { |
|
my ($string)=@_; |
|
|
|
# makes the decision about what is a minus sign easier supposedly |
|
$string =~ s/(\<\>|\<\=|\>\=[\=\>\<] *)-/$1 zeroplace -/g; |
|
|
|
return $string; |
|
} |
|
|
|
sub postprocess_algebra { |
|
my ($string)=@_; |
|
|
|
# moodle had these and I don't know why, ignoring them for now |
|
# $string =~s/\\fun/ /g; |
|
|
|
# sqrt(3,4) means the 4 root of 3 |
|
$string =~s/\\sqrt{([^,]+),([^\}]+)}/\\sqrt[$2]{$1}/gs; |
|
|
|
# log(3,4) means the log base 4 of 3 |
|
$string =~s/\\log\\left\((.+?),(.+?)\\right\)/\\log_{$2}\\left($1\\right)/gs; |
|
|
|
# log(3,4) means the log base 4 of 3 |
|
$string =~s/\\((?:sin|cos|tan|sec|csc|cot)(?:h)?)\\left\((.+?),(.+?)\\right\)/\\$1^{$3}\\left($2\\right)/gs; |
|
|
|
# int(3,a,b) integral from a to b of 3 |
|
$string =~s/\\int\\left\((.+?),(.+?),(.+?)\\right\)/\\int_{$2}^{$3}\\left($1\\right)/gs; |
|
|
|
# int( ... dx) -> ... |
|
$string =~s/\\int\\left\((.+?)d[a-z]\\right\)/$1/gs; |
|
|
|
# |
|
$string =~s/\\lim\\left\((.+?),(.+?),(.+?)\\right\)/\\lim_{$2\\to $3}$1/gs; |
|
return $string; |
|
} |
1; |
1; |
__END__ |
__END__ |
|
|
|
|
|
=pod |
|
|
|
=head1 NAME |
|
|
|
Apache::lontexconvert; |
|
|
|
=head1 SYNOPSIS |
|
|
|
Access to tth/ttm |
|
|
|
This is part of the LearningOnline Network with CAPA project |
|
described at http://www.lon-capa.org. |
|
|
|
|
|
=head1 SUBROUTINES |
|
|
|
=over |
|
|
|
=item init_tth() |
|
|
|
Header |
|
|
|
=item convert_real() |
|
|
|
we need this routine because &converted can get called from inside |
|
of the safespace (through &xmlparse('<m>stuff</m>') which doesn't |
|
allow the opcode for alarm, so we need to compile this before we get |
|
into the safe space since opcode checks only occur at compile time |
|
|
|
=item tth_converted() |
|
|
|
|
|
=item clean_out_math_mode() |
|
|
|
|
|
=item displaystyle() |
|
|
|
|
|
=item jsMath_converted() |
|
|
|
|
|
=item tex_engine() |
|
|
|
|
|
=item init_math_support() |
|
|
|
|
|
=item mimetex_converted() |
|
|
|
|
|
=item converted() |
|
|
|
|
|
=item to_convert() |
|
|
|
message display |
|
|
|
=item smiley() |
|
|
|
??? |
|
|
|
=item msgtexconverted() |
|
|
|
=item algebra() |
|
|
|
=item prepare_algebra() |
|
|
|
=item postprocess_algebra() |
|
|
|
=back |
|
|
|
=cut |
|
|
|
|
|
|