--- loncom/xml/lonxml.pm 2003/08/06 17:00:30 1.269 +++ loncom/xml/lonxml.pm 2003/08/11 20:05:53 1.270 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # XML Parser Module # -# $Id: lonxml.pm,v 1.269 2003/08/06 17:00:30 albertel Exp $ +# $Id: lonxml.pm,v 1.270 2003/08/11 20:05:53 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -952,83 +952,105 @@ sub store_counter { } sub get_all_text { - my($tag,$pars)= @_; - &Apache::lonxml::debug("Got a ".ref($pars)); - my $gotfullstack=1; - if (ref($pars) ne 'ARRAY') { - $gotfullstack=0; - $pars=[$pars]; - } - my $depth=0; - my $token; - my $result=''; - if ( $tag =~ m:^/: ) { - my $tag=substr($tag,1); - #&Apache::lonxml::debug("have:$tag:"); - my $top_empty=0; - while (($depth >=0) && ($#$pars > -1) && (!$top_empty)) { - while (($depth >=0) && ($token = $$pars[-1]->get_token)) { - #&Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]:".$#$pars.":".$#Apache::lonxml::pwd); - if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) { - $result.=$token->[1]; - } elsif ($token->[0] eq 'PI') { - $result.=$token->[2]; - } elsif ($token->[0] eq 'S') { - if ($token->[1] =~ /^$tag$/i) { $depth++; } - $result.=$token->[4]; - } elsif ($token->[0] eq 'E') { - if ( $token->[1] =~ /^$tag$/i) { $depth--; } - #skip sending back the last end tag - if ($depth > -1) { $result.=$token->[2]; } else { - $$pars[-1]->unget_token($token); - } - } - } - if (($depth >=0) && ($#$pars == 0) ) { $top_empty=1; } - if (($depth >=0) && ($#$pars > 0) ) { - pop(@$pars); - pop(@Apache::lonxml::pwd); - } - } - if ($top_empty && $depth >= 0) { - #never found the end tag ran out of text, throw error send back blank - &error('Never found end tag for <'.$tag.'>'); - if ($gotfullstack) { - my $newstring=''.$result; - &Apache::lonxml::newparser($pars,\$newstring); - } - $result=''; - } - } else { - while ($#$pars > -1) { - while ($token = $$pars[-1]->get_token) { - #&Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]"); - if (($token->[0] eq 'T')||($token->[0] eq 'C')|| - ($token->[0] eq 'D')) { - $result.=$token->[1]; - } elsif ($token->[0] eq 'PI') { - $result.=$token->[2]; - } elsif ($token->[0] eq 'S') { - if ( $token->[1] =~ /^$tag$/i) { - $$pars[-1]->unget_token($token); last; - } else { - $result.=$token->[4]; - } - } elsif ($token->[0] eq 'E') { - $result.=$token->[2]; - } - } - if (($#$pars > 0) ) { - pop(@$pars); - pop(@Apache::lonxml::pwd); - } else { last; } - } - } - if ($result =~ m||) { - $Apache::lonxml::usestyle=1; - } - #&Apache::lonxml::debug("Exit:$result:"); - return $result + my($tag,$pars,$style)= @_; + &Apache::lonxml::debug("Got a ".ref($pars)); + my $gotfullstack=1; + if (ref($pars) ne 'ARRAY') { + $gotfullstack=0; + $pars=[$pars]; + } + &Apache::lonxml::debug("Got a ".ref($style)); + if (ref($style) ne 'HASH') { + $style={}; + } else { + &Apache::lonhomework::showhash(%$style); + } + my $depth=0; + my $token; + my $result=''; + if ( $tag =~ m:^/: ) { + my $tag=substr($tag,1); + #&Apache::lonxml::debug("have:$tag:"); + my $top_empty=0; + while (($depth >=0) && ($#$pars > -1) && (!$top_empty)) { + while (($depth >=0) && ($token = $$pars[-1]->get_token)) { + #&Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]:".$#$pars.":".$#Apache::lonxml::pwd); + if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) { + $result.=$token->[1]; + } elsif ($token->[0] eq 'PI') { + $result.=$token->[2]; + } elsif ($token->[0] eq 'S') { + if ($token->[1] =~ /^$tag$/i) { $depth++; } + if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_ON$/i) { $Apache::lonxml::usestyle=1; } + if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_OFF$/i) { $Apache::lonxml::usestyle=0; } + $result.=$token->[4]; + } elsif ($token->[0] eq 'E') { + if ( $token->[1] =~ /^$tag$/i) { $depth--; } + #skip sending back the last end tag + if ($depth == 0 && exists($$style{'/'.$token->[1]})) { + my $string= + ''. + $$style{'/'.$token->[1]}. + $token->[2]. + ''; + &Apache::lonxml::newparser($pars,\$string); + #&Apache::lonxml::debug("reParsing $string"); + next; + } + if ($depth > -1) { + $result.=$token->[2]; + } else { + $$pars[-1]->unget_token($token); + } + } + } + if (($depth >=0) && ($#$pars == 0) ) { $top_empty=1; } + if (($depth >=0) && ($#$pars > 0) ) { + pop(@$pars); + pop(@Apache::lonxml::pwd); + } + } + if ($top_empty && $depth >= 0) { + #never found the end tag ran out of text, throw error send back blank + &error('Never found end tag for <'.$tag. + '> current string
'.
+		   &HTML::Entities::encode($result).
+		   '
'); + if ($gotfullstack) { + my $newstring=''.$result; + &Apache::lonxml::newparser($pars,\$newstring); + } + $result=''; + } + } else { + while ($#$pars > -1) { + while ($token = $$pars[-1]->get_token) { + #&Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]"); + if (($token->[0] eq 'T')||($token->[0] eq 'C')|| + ($token->[0] eq 'D')) { + $result.=$token->[1]; + } elsif ($token->[0] eq 'PI') { + $result.=$token->[2]; + } elsif ($token->[0] eq 'S') { + if ( $token->[1] =~ /^$tag$/i) { + $$pars[-1]->unget_token($token); last; + } else { + $result.=$token->[4]; + } + if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_ON$/i) { $Apache::lonxml::usestyle=1; } + if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_OFF$/i) { $Apache::lonxml::usestyle=0; } + } elsif ($token->[0] eq 'E') { + $result.=$token->[2]; + } + } + if (($#$pars > 0) ) { + pop(@$pars); + pop(@Apache::lonxml::pwd); + } else { last; } + } + } + #&Apache::lonxml::debug("Exit:$result:"); + return $result } sub newparser {