--- loncom/xml/lonxml.pm 2005/11/17 20:04:19 1.390 +++ loncom/xml/lonxml.pm 2006/02/05 18:30:56 1.397 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # XML Parser Module # -# $Id: lonxml.pm,v 1.390 2005/11/17 20:04:19 albertel Exp $ +# $Id: lonxml.pm,v 1.397 2006/02/05 18:30:56 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -52,6 +52,7 @@ use Math::Random(); use Opcode(); use POSIX qw(strftime); use Time::HiRes qw( gettimeofday tv_interval ); +use Symbol(); sub register { my ($space,@taglist) = @_; @@ -148,6 +149,19 @@ $Apache::lonxml::post_evaluate=1; #a header message to emit in the case of any generated warning or errors $Apache::lonxml::warnings_error_header=''; +# Control whether or not LaTeX symbols should be substituted for their +# \ style equivalents...this may be turned off e.g. in an verbatim +# environment. + +$Apache::lonxml::substitute_LaTeX_symbols = 1; # Starts out on. + +sub enable_LaTeX_substitutions { + $Apache::lonxml::substitute_LaTeX_symbols = 1; +} +sub disable_LaTeX_substitutions { + $Apache::lonxml::substitute_LaTeX_symbols = 0; +} + sub xmlbegin { my ($style)=@_; my $output=''; @@ -363,13 +377,16 @@ sub xmlparse { &initdepth(); &init_alarm(); my $finaloutput = &inner_xmlparse($target,\@stack,\@parstack,\@pars, - $safeeval,\%style_for_target); + $safeeval,\%style_for_target,1); if ($env{'request.uri'}) { &writeallows($env{'request.uri'}); } &do_registered_ssi(); if ($Apache::lonxml::counter_changed) { &store_counter() } + + &clean_safespace($safeeval); + if ($env{'form.return_only_error_and_warning_counts'}) { return "$errorcount:$warningcount"; } @@ -378,6 +395,13 @@ sub xmlparse { sub latex_special_symbols { my ($string,$where)=@_; + # + # If e.g. in verbatim mode, then don't substitute. + # but return original string. + # + if (!($Apache::lonxml::substitute_LaTeX_symbols)) { + return $string; + } if ($where eq 'header') { $string =~ s/(\\|_|\^)/ /g; $string =~ s/(\$|%|\{|\})/\\$1/g; @@ -407,7 +431,7 @@ sub latex_special_symbols { } sub inner_xmlparse { - my ($target,$stack,$parstack,$pars,$safeeval,$style_for_target)=@_; + my ($target,$stack,$parstack,$pars,$safeeval,$style_for_target,$start)=@_; my $finaloutput = ''; my $result; my $token; @@ -522,7 +546,7 @@ sub inner_xmlparse { # $finaloutput.=&endredirection; # } - if ($target eq 'grade') { &endredirection(); } + if ( $start && $target eq 'grade') { &endredirection(); } if ( $Apache::lonxml::redirection > $startredirection) { while ($Apache::lonxml::redirection > $startredirection) { $finaloutput .= &endredirection(); @@ -625,6 +649,7 @@ sub setup_globals { @Apache::lonxml::ssi_info=(); $Apache::lonxml::post_evaluate=1; $Apache::lonxml::warnings_error_header=''; + $Apache::lonxml::substitute_LaTeX_symbols = 1; if ($target eq 'meta') { $Apache::lonxml::redirection = 0; $Apache::lonxml::metamode = 1; @@ -795,6 +820,34 @@ sub init_safespace { &initialize_rndseed($safeeval); } +sub clean_safespace { + my ($safeeval) = @_; + delete_package_recurse($safeeval->{Root}); +} + +sub delete_package_recurse { + my ($package) = @_; + my @subp; + { + no strict 'refs'; + while (my ($key,$val) = each(%{*{"$package\::"}})) { + if (!defined($val)) { next; } + local (*ENTRY) = $val; + if (defined *ENTRY{HASH} && $key =~ /::$/ && + $key ne "main::" && $key ne "::") + { + my ($p) = $package ne "main" ? "$package\::" : ""; + ($p .= $key) =~ s/::$//; + push(@subp,$p); + } + } + } + foreach my $p (@subp) { + delete_package_recurse($p); + } + Symbol::delete_package($package); +} + sub initialize_rndseed { my ($safeeval)=@_; my $rndseed; @@ -961,7 +1014,10 @@ sub increment_counter { } sub init_counter { - if (defined($env{'form.counter'})) { + if ($env{'request.state'} eq 'construct') { + $Apache::lonxml::counter=1; + $Apache::lonxml::counter_changed=1; + } elsif (defined($env{'form.counter'})) { $Apache::lonxml::counter=$env{'form.counter'}; $Apache::lonxml::counter_changed=0; } else { @@ -1402,7 +1458,7 @@ ENDNOTFOUND undef($Apache::lonhomework::parsing_a_task); &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'}, ['rawmode']); - if ($env{'rawmode'}) { $result = $filecontents; } + if ($env{'form.rawmode'}) { $result = $filecontents; } } }