--- loncom/xml/lonxml.pm 2003/04/03 22:25:49 1.245 +++ loncom/xml/lonxml.pm 2003/08/06 17:00:30 1.269 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # XML Parser Module # -# $Id: lonxml.pm,v 1.245 2003/04/03 22:25:49 albertel Exp $ +# $Id: lonxml.pm,v 1.269 2003/08/06 17:00:30 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -70,6 +70,7 @@ use Math::Cephes(); use Math::Random(); use Opcode(); + sub register { my ($space,@taglist) = @_; foreach my $temptag (@taglist) { @@ -95,6 +96,8 @@ use Apache::run(); use Apache::londefdef(); use Apache::scripttag(); use Apache::edit(); +use Apache::inputtags(); +use Apache::outputtags(); use Apache::lonnet(); use Apache::File(); use Apache::loncommon(); @@ -151,6 +154,10 @@ $Apache::lonxml::counter_changed=0; #internal check on whether to look at style defs $Apache::lonxml::usestyle=1; +#locations used to store the parameter string for style substitutions +$Apache::lonxml::style_values=''; +$Apache::lonxml::style_end_values=''; + sub xmlbegin { my $output=''; if ($ENV{'browser.mathml'}) { @@ -380,8 +387,11 @@ sub printtokenheader { sub fontsettings() { my $headerstring=''; if (($ENV{'browser.os'} eq 'mac') && (!$ENV{'browser.mathml'})) { - $headerstring.= - ''; + $headerstring.= + ''; + } elsif (!$ENV{'browser.mathml'} && $ENV{'browser.unicode'}) { + $headerstring.= + ''; } return $headerstring; } @@ -418,8 +428,7 @@ sub xmlparse { } } } - - #&printalltags(); +#&printalltags(); my @pars = (); my $pwd=$ENV{'request.filename'}; $pwd =~ s:/[^/]*$::; @@ -438,6 +447,7 @@ sub xmlparse { my $finaloutput = &inner_xmlparse($target,\@stack,\@parstack,\@pars, $safeeval,\%style_for_target); + if ($ENV{'request.uri'}) { &writeallows($ENV{'request.uri'}); } @@ -475,13 +485,14 @@ sub latex_special_symbols { $current_token=~s/\\ /\\char92 /g; $current_token=~s/\^/\\char94 /g; $current_token=~s/\~/\\char126 /g; - $current_token=~s/(&[^a-z\#])/\\$1/g; + $current_token=~s/(&[^A-Za-z\#])/\\$1/g; $current_token=~s/([^&])\#/$1\\#/g; $current_token=~s/(\$|_|{|})/\\$1/g; $current_token=~s/\\char92 /\\texttt{\\char92}/g; $current_token=~s/(>|<)/\$$1\$/g; #more or less if ($current_token=~m/\d%/) {$current_token =~ s/(\d)%/$1\\%/g;} #percent after digit if ($current_token=~m/\s%/) {$current_token =~ s/(\s)%/$1\\%/g;} #persent after space + if ($current_token eq '%.') {$current_token = '\%.';} #persent at the end of statement } return $current_token; } @@ -491,9 +502,10 @@ sub inner_xmlparse { my $finaloutput = ''; my $result; my $token; + my $dontpop=0; while ( $#$pars > -1 ) { while ($token = $$pars['-1']->get_token) { - if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) { + if (($token->[0] eq 'T') || ($token->[0] eq 'C') ) { if ($metamode<1) { my $text=$token->[1]; if ($token->[0] eq 'C' && $target eq 'tex') { @@ -502,8 +514,13 @@ sub inner_xmlparse { } $result.=$text; } + } elsif (($token->[0] eq 'D')) { + if ($metamode<1 && $target eq 'web') { + my $text=$token->[1]; + $result.=$text; + } } elsif ($token->[0] eq 'PI') { - if ($metamode<1) { + if ($metamode<1 && $target eq 'web') { $result=$token->[2]; } } elsif ($token->[0] eq 'S') { @@ -518,48 +535,57 @@ sub inner_xmlparse { my $string=$$style_for_target{$token->[1]}. ''; &Apache::lonxml::newparser($pars,\$string); + $Apache::lonxml::style_values=$$parstack[-1]; + $Apache::lonxml::style_end_values=$$parstack[-1]; } else { $result = &callsub("start_$token->[1]", $target, $token, $stack, $parstack, $pars, $safeeval, $style_for_target); } } elsif ($token->[0] eq 'E') { - #clear out any tags that didn't end - while ($token->[1] ne $$stack['-1'] && ($#$stack > -1)) { - my $lasttag=$$stack[-1]; - if ($token->[1] =~ /^$lasttag$/i) { - &Apache::lonxml::warning('Using tag </'.$token->[1].'> on line '.$token->[3].' as end tag to <'.$$stack[-1].'>'); - last; - } else { - &Apache::lonxml::warning('Found tag </'.$token->[1].'> on line '.$token->[3].' when looking for </'.$$stack[-1].'> in file'); - &end_tag($stack,$parstack,$token); - } - } - if ($Apache::lonxml::usestyle && exists($$style_for_target{'/'."$token->[1]"})) { $Apache::lonxml::usestyle=0; my $string=$$style_for_target{'/'.$token->[1]}. - ''; + ''; &Apache::lonxml::newparser($pars,\$string); + $Apache::lonxml::style_values=$Apache::lonxml::style_end_values; + $Apache::lonxml::style_end_values=''; + $dontpop=1; } else { - $result = &callsub("end_$token->[1]", $target, $token, $stack, - $parstack, $pars,$safeeval, $style_for_target); + #clear out any tags that didn't end + while ($token->[1] ne $$stack['-1'] && ($#$stack > -1)) { + my $lasttag=$$stack[-1]; + if ($token->[1] =~ /^$lasttag$/i) { + &Apache::lonxml::warning('Using tag </'.$token->[1].'> on line '.$token->[3].' as end tag to <'.$$stack[-1].'>'); + last; + } else { + &Apache::lonxml::warning('Found tag </'.$token->[1].'> on line '.$token->[3].' when looking for </'.$$stack[-1].'> in file'); + &end_tag($stack,$parstack,$token); + } + } + $result = &callsub("end_$token->[1]", $target, $token, $stack, + $parstack, $pars,$safeeval, $style_for_target); } } else { &Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:"); } #evaluate variable refs in result if ($result ne "") { + my $extras; + if (!$Apache::lonxml::usestyle) { + $extras=$Apache::lonxml::style_values; + } if ( $#$parstack > -1 ) { - $result=&Apache::run::evaluate($result,$safeeval,$$parstack[-1]); + $result=&Apache::run::evaluate($result,$safeeval,$extras.$$parstack[-1]); } else { - $result= &Apache::run::evaluate($result,$safeeval,''); + $result= &Apache::run::evaluate($result,$safeeval,$extras); } } if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) { - if ($target eq 'tex') { - $result=&latex_special_symbols($result,$stack,$parstack); - } + #Style file definitions should be correct + if ($target eq 'tex' && ($Apache::lonxml::usestyle)) { + $result=&latex_special_symbols($result,$stack,$parstack); + } } # Encode any high ASCII characters @@ -573,9 +599,10 @@ sub inner_xmlparse { } $result = ''; - if ($token->[0] eq 'E') { + if ($token->[0] eq 'E' && !$dontpop) { &end_tag($stack,$parstack,$token); } + $dontpop=0; } if ($#$pars > -1) { pop @$pars; @@ -718,6 +745,7 @@ sub init_safespace { $safeeval->permit("sort"); $safeeval->deny(":base_io"); $safehole->wrap(\&Apache::scripttag::xmlparse,$safeeval,'&xmlparse'); + $safehole->wrap(\&Apache::outputtags::multipart,$safeeval,'&multipart'); $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT'); $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin'); @@ -799,6 +827,7 @@ sub init_safespace { my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser(); $rndseed=&Apache::lonnet::rndseed($symb,$courseid,$domain,$name); $safeinit .= ';$external::randomseed='.$rndseed.';'; + &Apache::lonxml::debug("Setting rndseed to $rndseed"); &Apache::run::run($safeinit,$safeeval); } @@ -898,18 +927,23 @@ sub get_all_text_unbalanced { } sub increment_counter { - $Apache::lonxml::counter++; + my ($increment) = @_; + if (defined($increment) && $increment gt 0) { + $Apache::lonxml::counter+=$increment; + } else { + $Apache::lonxml::counter++; + } $Apache::lonxml::counter_changed=1; } sub init_counter { if (defined($ENV{'form.counter'})) { $Apache::lonxml::counter=$ENV{'form.counter'}; + $Apache::lonxml::counter_changed=0; } else { $Apache::lonxml::counter=1; - &store_counter(); + $Apache::lonxml::counter_changed=1; } - $Apache::lonxml::counter_changed=0; } sub store_counter { @@ -1006,8 +1040,6 @@ sub newparser { } else { push (@Apache::lonxml::pwd, $dir); } -# &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd"); -# &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]"); } sub parstring { @@ -1018,7 +1050,7 @@ sub parstring { my $val=$token->[2]->{$_}; $val =~ s/([\%\@\\\"\'])/\\$1/g; #if ($val =~ m/^[\%\@]/) { $val="\\".$val; } - $temp .= "my \$$_=\"$val\";" + $temp .= "my \$$_=\"$val\";"; } } return $temp; @@ -1111,13 +1143,8 @@ sub inserteditinfo { my ($result,$filecontents)=@_; $filecontents = &HTML::Entities::encode($filecontents); # my $editheader='Edit below
'; - my $xml_help = '
'. - &Apache::loncommon::help_open_topic("Greek_Symbols",'Greek Symbols', - undef,undef,600) - .''. - &Apache::loncommon::help_open_topic("Other_Symbols",'Other Symbols', - undef,undef,600) - .'
'; + my $xml_help = Apache::loncommon::helpLatexCheatsheet(); + my $titledisplay=&display_title(); my $buttons=(< @@ -1135,6 +1162,7 @@ $buttons

$buttons
+$titledisplay ENDFOOTER # $result=~s/(\]*\>)/$1$editheader/is; $result=~s/(\<\/body\>)/$editfooter/is; @@ -1169,37 +1197,37 @@ sub get_target { } sub handler { - my $request=shift; - - my $target=&get_target(); - - $Apache::lonxml::debug=0; - - if ($ENV{'browser.mathml'}) { - $request->content_type('text/xml'); - } else { - $request->content_type('text/html'); - } - &Apache::loncommon::no_cache($request); - $request->send_http_header; - - return OK if $request->header_only; + my $request=shift; + + my $target=&get_target(); + + $Apache::lonxml::debug=$ENV{'user.debug'}; + + if ($ENV{'browser.mathml'}) { + $request->content_type('text/xml'); + } else { + $request->content_type('text/html'); + } + &Apache::loncommon::no_cache($request); + $request->send_http_header; + + return OK if $request->header_only; - my $file=&Apache::lonnet::filelocation("",$request->uri); + my $file=&Apache::lonnet::filelocation("",$request->uri); # # Edit action? Save file. # - unless ($ENV{'request.state'} eq 'published') { - if (($ENV{'form.savethisfile'}) || ($ENV{'form.attemptclean'})) { - &storefile($file,$ENV{'form.filecont'}); - } - } - my %mystyle; - my $result = ''; - my $filecontents=&Apache::lonnet::getfile($file); - if ($filecontents eq -1) { - $result=(< File not found @@ -1210,40 +1238,58 @@ sub handler { ENDNOTFOUND $filecontents=''; - if ($ENV{'request.state'} ne 'published') { - $filecontents=&createnewhtml(); - $ENV{'form.editmode'}='Edit'; #force edit mode - } - } else { - unless ($ENV{'request.state'} eq 'published') { - if ($ENV{'form.attemptclean'}) { - $filecontents=&htmlclean($filecontents,1); - } - } - if (!$ENV{'form.editmode'} || $ENV{'form.viewmode'}) { - $result = &Apache::lonxml::xmlparse($request,$target,$filecontents, - '',%mystyle); + if ($ENV{'request.state'} ne 'published') { + $filecontents=&createnewhtml(); + $ENV{'form.editmode'}='Edit'; #force edit mode + } + } else { + unless ($ENV{'request.state'} eq 'published') { + if ($ENV{'form.attemptclean'}) { + $filecontents=&htmlclean($filecontents,1); + } +# +# we are in construction space, see if edit mode forced + &Apache::loncommon::get_unprocessed_cgi + ($ENV{'QUERY_STRING'},['editmode']); + } + if (!$ENV{'form.editmode'} || $ENV{'form.viewmode'}) { + $result = &Apache::lonxml::xmlparse($request,$target,$filecontents, + '',%mystyle); + } } - } - + # # Edit action? Insert editing commands # - unless ($ENV{'request.state'} eq 'published') { - if ($ENV{'form.editmode'} && (!($ENV{'form.viewmode'}))) { - my $displayfile=$request->uri; - $displayfile=~s/^\/[^\/]*//; - $result='

'.$displayfile. - '

'; - $result=&inserteditinfo($result,$filecontents); + unless ($ENV{'request.state'} eq 'published') { + if ($ENV{'form.editmode'} && (!($ENV{'form.viewmode'}))) { + my $displayfile=$request->uri; + $displayfile=~s/^\/[^\/]*//; + $result='

'.$displayfile. + '

'; + $result=&inserteditinfo($result,$filecontents); + } } - } - - writeallows($request->uri); - - $request->print($result); - - return OK; + + writeallows($request->uri); + + + $request->print($result); + + return OK; +} + +sub display_title { + my $result; + if ($ENV{'request.state'} eq 'construct') { + my $title=&Apache::lonnet::gettitle(); + if (!defined($title) || $title eq '') { + $title = $ENV{'request.filename'}; + $title = substr($title, rindex($title, '/') + 1); + } + $result = ""; + } + return $result; } sub debug { @@ -1281,8 +1327,11 @@ sub error { sub warning { $warningcount++; - if ($ENV{'request.state'} eq 'construct') { - print "WARNING:".join('
',@_)."
\n"; + + if ($ENV{'form.grade_target'} ne 'tex') { + if ($ENV{'request.state'} eq 'construct' || $Apache::lonxml::debug) { + print "WARNING:".join('
',@_)."
\n"; + } } } @@ -1344,7 +1393,7 @@ sub register_insert { my $line = $data[$i]; if ( $line =~ /^\#/ || $line =~ /^\s*\n/) { next; } if ( $line =~ /TABLE/ ) { last; } - my ($tag,$descrip,$color,$function,$show) = split(/,/, $line); + my ($tag,$descrip,$color,$function,$show,$helpfile,$helpdesc) = split(/,/, $line); if ($tag) { $insertlist{"$tagnum.tag"} = $tag; $insertlist{"$tagnum.description"} = $descrip; @@ -1352,6 +1401,8 @@ sub register_insert { $insertlist{"$tagnum.function"} = $function; if (!defined($show)) { $show='yes'; } $insertlist{"$tagnum.show"}= $show; + $insertlist{"$tagnum.helpfile"} = $helpfile; + $insertlist{"$tagnum.helpdesc"} = $helpdesc; $insertlist{"$tag.num"}=$tagnum; $tagnum++; } @@ -1386,15 +1437,30 @@ sub description { return $insertlist{$tagnum.'.description'}; } +# Returns a list containing the help file, and the description +sub helpinfo { + my ($token)=@_; + my $tagnum; + my $tag=$token->[1]; + foreach my $namespace (reverse @Apache::lonxml::namespace) { + my $testtag=$namespace.'::'.$tag; + $tagnum=$insertlist{"$testtag.num"}; + if (defined($tagnum)) { last; } + } + if (!defined ($tagnum)) { $tagnum=$Apache::lonxml::insertlist{"$tag.num"}; } + return ($insertlist{$tagnum.'.helpfile'}, $insertlist{$tagnum.'.helpdesc'}); +} + # ----------------------------------------------------------------- whichuser # returns a list of $symb, $courseid, $domain, $name that is correct for # calls to lonnet functions for this setup. # - looks for form.grade_ parameters sub whichuser { + my ($passedsymb)=@_; my ($symb,$courseid,$domain,$name,$publicuser); if (defined($ENV{'form.grade_symb'})) { my $tmp_courseid=$ENV{'form.grade_courseid'}; - my $allowed=&Apache::lonnet::allowed('mgr',$tmp_courseid); + my $allowed=&Apache::lonnet::allowed('vgr',$tmp_courseid); if ($allowed) { $symb=$ENV{'form.grade_symb'}; $courseid=$ENV{'form.grade_courseid'}; @@ -1402,7 +1468,11 @@ sub whichuser { $name=$ENV{'form.grade_username'}; } } else { - $symb=&Apache::lonnet::symbread(); + if (!$passedsymb) { + $symb=&Apache::lonnet::symbread(); + } else { + $symb=$passedsymb; + } $courseid=$ENV{'request.course.id'}; $domain=$ENV{'user.domain'}; $name=$ENV{'user.name'};