--- loncom/xml/lonxml.pm 2003/10/27 19:00:11 1.288 +++ loncom/xml/lonxml.pm 2004/03/09 20:37:58 1.309 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # XML Parser Module # -# $Id: lonxml.pm,v 1.288 2003/10/27 19:00:11 sakharuk Exp $ +# $Id: lonxml.pm,v 1.309 2004/03/09 20:37:58 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -167,6 +167,9 @@ $Apache::lonxml::style_end_values=''; #should we do the postag variable interpolation $Apache::lonxml::post_evaluate=1; +#a header message to emit in the case of any generated warning or errors +$Apache::lonxml::warnings_error_header=''; + sub xmlbegin { my $output=''; if ($ENV{'browser.mathml'}) { @@ -189,7 +192,7 @@ sub xmlend { $mode='problem'; $status=$Apache::inputtags::status[-1]; } - return &Apache::lonfeedback::list_discussion().''; + return &Apache::lonfeedback::list_discussion($mode,$status).''; } sub tokeninputfield { @@ -337,6 +340,13 @@ sub xmlparse { &Apache::style::styleparser($target,$styletext)); } } + } elsif ($ENV{'construct.style'} && ($ENV{'request.state'} eq 'construct')) { + my $location=&Apache::lonnet::filelocation('',$ENV{'construct.style'}); + my $styletext=&Apache::lonnet::getfile($location); + if ($styletext ne '-1') { + %style_for_target = (%style_for_target, + &Apache::style::styleparser($target,$styletext)); + } } #&printalltags(); my @pars = (); @@ -395,10 +405,13 @@ sub latex_special_symbols { $string =~ s/_/ /g; } else { $string=~s/\\ /\\char92 /g; - $string=~s/\^/\\\^ /g; + $string=~s/\^/\\\^\\strut /g; $string=~s/\~/\\char126 /g; - $string=~s/(&[^A-Za-z\#])/\\$1/g; - $string=~s/([^&])\#/$1\\#/g; + #fixup & if it doesn't look like + # { or α + $string=~s/(&(?!((\#[0-9]+)|([a-z][a-z0-9]*));))/\\$1/gi; + $string=~s/([^&\\])\#/$1\\#/g; + $string=~s/\#\#/\#\\#/g; $string=~s/(\$|_|{|})/\\$1/g; $string=~s/\\char92 /\\texttt{\\char92}/g; $string=~s/(>|<)/\$$1\$/g; #more or less @@ -616,6 +629,7 @@ sub setup_globals { @Apache::lonxml::extlinks=(); @Apache::lonxml::ssi_info=(); $Apache::lonxml::post_evaluate=1; + $Apache::lonxml::warnings_error_header=''; if ($target eq 'meta') { $Apache::lonxml::redirection = 0; $Apache::lonxml::metamode = 1; @@ -736,6 +750,7 @@ sub init_safespace { $safehole->wrap(\&Math::Random::random_set_seed_from_phrase,$safeeval,'&random_set_seed_from_phrase'); $safehole->wrap(\&Math::Random::random_get_seed,$safeeval,'&random_get_seed'); $safehole->wrap(\&Math::Random::random_set_seed,$safeeval,'&random_set_seed'); + $safehole->wrap(\&Apache::lonxml::error,$safeeval,'&LONCAPA_INTERNAL_ERROR'); #need to inspect this class of ops # $safeeval->deny(":base_orig"); @@ -746,6 +761,28 @@ sub init_safespace { $safeinit .= ';$external::randomseed='.$rndseed.';'; &Apache::lonxml::debug("Setting rndseed to $rndseed"); &Apache::run::run($safeinit,$safeeval); + + my $subroutine=<<'EVALUATESUB'; +sub __LC_INTERNAL_EVALUATE__ { + my ($__LC__a,$__LC__b,$__LC__c)=@_; + my $__LC__prefix; + while(1){ + { + use strict; + no strict "vars"; + if (eval(defined(eval($__LC__a.$__LC__b)))) { + return $__LC__prefix.eval($__LC__a.$__LC__b.$__LC__c); + } + } + $__LC__prefix.=substr($__LC__a,0,1,""); + if ($__LC__a!~/^(\$|&|\#)/) { last; } + } + return $__LC__prefix.$__LC__a.$__LC__b.$__LC__c; +} +EVALUATESUB + $safeeval->permit("require"); + $safeeval->reval($subroutine); + $safeeval->deny("require"); } sub default_homework_load { @@ -845,14 +882,12 @@ sub get_all_text_unbalanced { sub increment_counter { my ($increment) = @_; - if ($ENV{'form.freeze_counter'} ne 'yes') { - if (defined($increment) && $increment gt 0) { - $Apache::lonxml::counter+=$increment; - } else { - $Apache::lonxml::counter++; - } - $Apache::lonxml::counter_changed=1; + if (defined($increment) && $increment gt 0) { + $Apache::lonxml::counter+=$increment; + } else { + $Apache::lonxml::counter++; } + $Apache::lonxml::counter_changed=1; } sub init_counter { @@ -1062,6 +1097,7 @@ ENDSCRIPT sub storefile { my ($file,$contents)=@_; + &Apache::lonnet::correct_line_ends(\$contents); if (my $fh=Apache::File->new('>'.$file)) { print $fh $contents; $fh->close(); @@ -1122,8 +1158,8 @@ sub inserteditinfo { 'ed' => 'Edit'); my $buttons=(< - + + BUTTONS my $editfooter=(< @@ -1201,8 +1237,10 @@ sub handler { unless ($ENV{'request.state'} eq 'published') { if (($ENV{'form.savethisfile'}) || ($ENV{'form.attemptclean'})) { if (&storefile($file,$ENV{'form.filecont'})) { - $request->print("".&mt('Updated').": ". -&Apache::lonlocal::locallocaltime(time)." "); + &Apache::lonxml::info("". + &mt('Updated').": ". + &Apache::lonlocal::locallocaltime(time). + " "); } } } @@ -1254,7 +1292,9 @@ ENDNOTFOUND if ($ENV{'form.editmode'} && (!($ENV{'form.viewmode'}))) { my $displayfile=$request->uri; $displayfile=~s/^\/[^\/]*//; - $result='

'.$displayfile. + $result=''. + &Apache::lonxml::message_location().'

'. + $displayfile. '

'; $result=&inserteditinfo($result,$filecontents,$filetype); } @@ -1262,7 +1302,7 @@ ENDNOTFOUND if ($filetype eq 'html') { writeallows($request->uri); } - + &Apache::lonxml::add_messages(\$result); $request->print($result); return OK; @@ -1282,19 +1322,27 @@ sub display_title { } sub debug { - if ($Apache::lonxml::debug eq 1) { - $|=1; - print('DEBUG:'.&HTML::Entities::encode($_[0])."\n"); - } + if ($Apache::lonxml::debug eq "1") { + $|=1; + my $request=$Apache::lonxml::request; + if (!$request) { $request=Apache->request; } + $request->print('
DEBUG:'.&HTML::Entities::encode($_[0])."
\n"); + } } sub error { $errorcount++; + my $request=$Apache::lonxml::request; + if (!$request) { $request=Apache->request; } if (($Apache::lonxml::debug eq 1) || ($ENV{'request.state'} eq 'construct') ) { # If printing in construction space, put the error inside

-    print "ERROR:".join("\n",@_)."\n";
+      push(@Apache::lonxml::error_messages,
+	   $Apache::lonxml::warnings_error_header.
+	   "ERROR:".join("
\n",@_)."
\n"); + $Apache::lonxml::warnings_error_header=''; } else { - print "An Error occured while processing this resource. The instructor has been notified.
"; + push(@Apache::lonxml::error_messages, + "An Error occured while processing this resource. The instructor has been notified.
"); #notify author &Apache::lonmsg::author_res_msg($ENV{'request.filename'},join('
',@_)); #notify course @@ -1307,21 +1355,46 @@ sub error { "Error [$declutter]",join('
',@_)); } } - - #FIXME probably shouldn't have me get everything forever. - &Apache::lonmsg::user_normal_msg('albertel','msu',"Error in $ENV{'request.filename'}",join('
',@_)); - #&Apache::lonmsg::user_normal_msg('albertel','103',"Error in $ENV{'request.filename'}",$_[0]); } } sub warning { - $warningcount++; + $warningcount++; - if ($ENV{'form.grade_target'} ne 'tex') { - if ($ENV{'request.state'} eq 'construct' || $Apache::lonxml::debug) { - print "WARNING:".join('
',@_)."
\n"; - } - } + if ($ENV{'form.grade_target'} ne 'tex') { + if ($ENV{'request.state'} eq 'construct' || $Apache::lonxml::debug) { + my $request=$Apache::lonxml::request; + if (!$request) { $request=Apache->request; } + push(@Apache::lonxml::warning_messages, + $Apache::lonxml::warnings_error_header. + "WARNING:".join('
',@_)."
\n"); + $Apache::lonxml::warnings_error_header=''; + } + } +} + +sub info { + if ($ENV{'form.grade_target'} ne 'tex' + && $ENV{'request.state'} eq 'construct') { + push(@Apache::lonxml::info_messages,join('
',@_)."
\n"); + } +} + +sub message_location { + return '__LONCAPA_INTERNAL_MESSAGE_LOCATION__'; +} + +sub add_messages { + my ($msg)=@_; + my $result=join(' ', + @Apache::lonxml::info_messages, + @Apache::lonxml::error_messages, + @Apache::lonxml::warning_messages); + undef(@Apache::lonxml::info_messages); + undef(@Apache::lonxml::error_messages); + undef(@Apache::lonxml::warning_messages); + $$msg=~s/__LONCAPA_INTERNAL_MESSAGE_LOCATION__/$result/; + $$msg=~s/__LONCAPA_INTERNAL_MESSAGE_LOCATION__//g; } sub get_param { @@ -1329,6 +1402,9 @@ sub get_param { if ( ! $context ) { $context = -1; } my $args =''; if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; } + if ( ! $Apache::lonxml::usestyle ) { + $args=$Apache::lonxml::style_values.$args; + } if ( ! $args ) { return undef; } if ( $case_insensitive ) { if ($args =~ s/(my \$)(\Q$param\E)(=\")/$1.lc($2).$3/ei) { @@ -1352,6 +1428,9 @@ sub get_param_var { if ( ! $context ) { $context = -1; } my $args =''; if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; } + if ( ! $Apache::lonxml::usestyle ) { + $args=$Apache::lonxml::style_values.$args; + } &Apache::lonxml::debug("Args are $args param is $param"); if ($case_insensitive) { if (! ($args=~s/(my \$)(\Q$param\E)(=\")/$1.lc($2).$3/ei)) {