Annotation of loncom/xml/lonxml.pm, revision 1.72

1.2       sakharuk    1: # The LearningOnline Network with CAPA
1.3       sakharuk    2: # XML Parser Module 
1.2       sakharuk    3: #
1.3       sakharuk    4: # last modified 06/26/00 by Alexander Sakharuk
1.33      www         5: # 11/6 Gerd Kortemeyer
1.45      www         6: # 6/1/1 Gerd Kortemeyer
1.56      albertel    7: # 2/21,3/13 Guy
1.68      www         8: # 3/29,5/4 Gerd Kortemeyer
1.2       sakharuk    9: 
1.4       albertel   10: package Apache::lonxml; 
1.33      www        11: use vars 
1.72    ! albertel   12: qw(@pwd @outputstack $redirection $import @extlinks $metamode $evaluate);
1.1       sakharuk   13: use strict;
                     14: use HTML::TokeParser;
1.3       sakharuk   15: use Safe;
1.40      albertel   16: use Safe::Hole;
1.13      albertel   17: use Opcode;
1.72    ! albertel   18: 
        !            19: sub register {
        !            20:   my $space;
        !            21:   my @taglist;
        !            22:   my $temptag;
        !            23:   ($space,@taglist) = @_;
        !            24:   foreach $temptag (@taglist) {
        !            25:     $Apache::lonxml::alltags{$temptag}=$space;
        !            26:   }
        !            27: }
        !            28: 
1.46      www        29: use Apache::Constants qw(:common);
1.71      www        30: use Apache::lontexconvert;
1.72    ! albertel   31: use Apache::style;
        !            32: use Apache::run;
        !            33: use Apache::londefdef;
        !            34: use Apache::scripttag;
        !            35: use Apache::edit;
        !            36: #==================================================   Main subroutine: xmlparse  
        !            37: #debugging control, to turn on debugging modify the correct handler
        !            38: $Apache::lonxml::debug=0;
        !            39: 
        !            40: #path to the directory containing the file currently being processed
        !            41: @pwd=();
        !            42: 
        !            43: #these two are used for capturing a subset of the output for later processing,
        !            44: #don't touch them directly use &startredirection and &endredirection
        !            45: @outputstack = ();
        !            46: $redirection = 0;
        !            47: 
        !            48: #controls wheter the <import> tag actually does
        !            49: $import = 1;
        !            50: @extlinks=();
        !            51: 
        !            52: # meta mode is a bit weird only some output is to be turned off
        !            53: #<output> tag turns metamode off (defined in londefdef.pm)
        !            54: $metamode = 0;
        !            55: 
        !            56: # turns on and of run::evaluate actually derefencing var refs
        !            57: $evaluate = 1;
1.7       albertel   58: 
1.68      www        59: 
                     60: sub xmlbegin {
                     61:   my $output='';
                     62:   if ($ENV{'browser.mathml'}) {
                     63:       $output='<?xml version="1.0"?>'
                     64:             .'<?xml-stylesheet type="text/css" href="/adm/MathML/mathml.css"?>'
                     65:             .'<!DOCTYPE html SYSTEM "/adm/MathML/mathml.dtd" '
                     66:             .'[<!ENTITY mathns "http://www.w3.org/1998/Math/MathML">]>'
                     67:             .'<html xmlns:math="http://www.w3.org/1998/Math/MathML" ' 
                     68: 		.'xmlns="http://www.w3.org/TR/REC-html40">';
                     69:   } else {
                     70:       $output='<html>';
                     71:   }
                     72:   return $output;
                     73: }
                     74: 
                     75: sub xmlend {
                     76:     return '</html>';
                     77: }
                     78: 
1.70      www        79: sub fontsettings() {
                     80:     my $headerstring='';
                     81:     if (($ENV{'browser.os'} eq 'mac') && (!$ENV{'browser.mathml'})) { 
                     82:          $headerstring.=
                     83:              '<meta Content-Type="text/html; charset=x-mac-roman">';
                     84:     }
                     85:     return $headerstring;
                     86: }
                     87: 
1.68      www        88: sub registerurl {
                     89:   return (<<ENDSCRIPT);
                     90: <script language="JavaScript">
1.71      www        91: // BEGIN LON-CAPA Internal
1.69      www        92:     function LONCAPAreg() {
                     93:        if (window.location.pathname!="/res/adm/pages/menu.html") {
                     94: 	  menu=window.open("","LONCAPAmenu");
                     95: 	  menu.currentURL=window.location.pathname;
                     96:           menu.currentStale=0;
                     97:        }
                     98:     }
                     99:   
                    100:     function LONCAPAstale() {
                    101:        if (window.location.pathname!="/res/adm/pages/menu.html") {
                    102: 	  menu=window.open("","LONCAPAmenu");
                    103:           menu.currentStale=1;
                    104:        }
1.68      www       105:     }
1.71      www       106: // END LON-CAPA Internal
1.68      www       107: </script>
                    108: ENDSCRIPT
1.69      www       109: }
                    110: 
                    111: sub loadevents() {
                    112:     return 'LONCAPAreg();';
                    113: }
                    114: 
                    115: sub unloadevents() {
                    116:     return 'LONCAPAstale();';
1.68      www       117: }
                    118: 
1.48      albertel  119: sub printalltags {
                    120:   my $temp;
                    121:   foreach $temp (sort keys %Apache::lonxml::alltags) {
1.64      albertel  122:     &Apache::lonxml::debug("$temp -- $Apache::lonxml::alltags{$temp}");
1.48      albertel  123:   }
                    124: }
1.31      sakharuk  125: 
1.3       sakharuk  126: sub xmlparse {
                    127: 
1.18      albertel  128:  my ($target,$content_file_string,$safeinit,%style_for_target) = @_;
1.41      albertel  129:  if ($target eq 'meta') {
1.59      albertel  130:    $Apache::lonxml::redirection = 0;
                    131:    $Apache::lonxml::metamode = 1;
1.72    ! albertel  132:    $Apache::lonxml::evaluate = 1;
1.55      albertel  133:    $Apache::lonxml::import = 0;
1.47      albertel  134:  } elsif ($target eq 'grade') {
1.55      albertel  135:    &startredirection;
1.59      albertel  136:    $Apache::lonxml::metamode = 0;
1.72    ! albertel  137:    $Apache::lonxml::evaluate = 1;
1.55      albertel  138:    $Apache::lonxml::import = 1;
1.72    ! albertel  139:  } elsif ($target eq 'modified') {
        !           140:    $Apache::lonxml::redirection = 0;
        !           141:    $Apache::lonxml::metamode = 0;
        !           142:    $Apache::lonxml::evaluate = 0;
        !           143:    $Apache::lonxml::import = 0;
1.44      albertel  144:  } else {
1.72    ! albertel  145:    $Apache::lonxml::redirection = 0;
1.59      albertel  146:    $Apache::lonxml::metamode = 0;
1.72    ! albertel  147:    $Apache::lonxml::evaluate = 1;
1.55      albertel  148:    $Apache::lonxml::import = 1;
1.32      sakharuk  149:  }
1.48      albertel  150:  #&printalltags();
1.16      albertel  151:  my @pars = ();
1.23      albertel  152:  @Apache::lonxml::pwd=();
                    153:  my $pwd=$ENV{'request.filename'};
                    154:  $pwd =~ s:/[^/]*$::;
                    155:  &newparser(\@pars,\$content_file_string,$pwd);
1.3       sakharuk  156:  my $currentstring = '';
                    157:  my $finaloutput = ''; 
                    158:  my $newarg = '';
1.16      albertel  159:  my $result;
1.24      sakharuk  160: 
1.3       sakharuk  161:  my $safeeval = new Safe;
1.40      albertel  162:  my $safehole = new Safe::Hole;
1.6       albertel  163:  $safeeval->permit("entereval");
1.13      albertel  164:  $safeeval->permit(":base_math");
1.19      albertel  165:  $safeeval->deny(":base_io");
1.40      albertel  166:  $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
1.19      albertel  167: #need to inspect this class of ops
                    168: # $safeeval->deny(":base_orig");
1.21      albertel  169:  $safeinit .= ';$external::target='.$target.';';
1.26      albertel  170:  $safeinit .= ';$external::randomseed='.&Apache::lonnet::rndseed().';';
1.21      albertel  171:  &Apache::run::run($safeinit,$safeeval);
1.3       sakharuk  172: #-------------------- Redefinition of the target in the case of compound target
                    173: 
                    174:  ($target, my @tenta) = split('&&',$target);
                    175: 
                    176:  my @stack = (); 
                    177:  my @parstack = ();
1.17      albertel  178:  &initdepth;
1.3       sakharuk  179:  my $token;
1.16      albertel  180:  while ( $#pars > -1 ) {
                    181:    while ($token = $pars[$#pars]->get_token) {
1.57      albertel  182:      if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
1.61      albertel  183:        if ($metamode<1) { $result=$token->[1]; }
1.57      albertel  184:      } elsif ($token->[0] eq 'PI') {
1.61      albertel  185:        if ($metamode<1) { $result=$token->[2]; }
1.16      albertel  186:      } elsif ($token->[0] eq 'S') {
                    187:        # add tag to stack 	    
                    188:        push (@stack,$token->[1]);
                    189:        # add parameters list to another stack
                    190:        push (@parstack,&parstring($token));
1.19      albertel  191:        &increasedepth($token);       
1.16      albertel  192:        if (exists $style_for_target{$token->[1]}) {
1.61      albertel  193: 	 if ($Apache::lonxml::redirection) {
1.55      albertel  194: 	   $Apache::lonxml::outputstack['-1'] .=  
                    195: 	     &recurse($style_for_target{$token->[1]},$target,$safeeval,
                    196: 		      \%style_for_target,@parstack);
1.41      albertel  197: 	 } else {
1.55      albertel  198: 	   $finaloutput .= &recurse($style_for_target{$token->[1]},$target,
                    199: 				    $safeeval,\%style_for_target,@parstack);
1.41      albertel  200: 	 }
1.16      albertel  201:        } else {
1.17      albertel  202: 	 $result = &callsub("start_$token->[1]", $target, $token,\@parstack,
1.41      albertel  203: 			    \@pars, $safeeval, \%style_for_target);
1.16      albertel  204:        }              
                    205:      } elsif ($token->[0] eq 'E')  {
                    206:        #clear out any tags that didn't end
1.55      albertel  207:        while ($token->[1] ne $stack[$#stack] && ($#stack > -1)) {
                    208: 	 &Apache::lonxml::warning("Unbalanced tags in resource $stack['-1']");
1.43      albertel  209: 	 pop @stack;pop @parstack;&decreasedepth($token);
                    210:        }
1.16      albertel  211:        
                    212:        if (exists $style_for_target{'/'."$token->[1]"}) {
1.61      albertel  213: 	 if ($Apache::lonxml::redirection) {
1.55      albertel  214: 	   $Apache::lonxml::outputstack['-1'] .=  
                    215: 	     &recurse($style_for_target{'/'."$token->[1]"},
                    216: 		      $target,$safeeval,\%style_for_target,@parstack);
                    217: 	 } else {
                    218: 	   $finaloutput .= &recurse($style_for_target{'/'."$token->[1]"},
                    219: 				    $target,$safeeval,\%style_for_target,
                    220: 				    @parstack);
                    221: 	 }
1.59      albertel  222: 
1.16      albertel  223:        } else {
1.17      albertel  224: 	 $result = &callsub("end_$token->[1]", $target, $token, \@parstack,
1.55      albertel  225: 			    \@pars,$safeeval, \%style_for_target);
1.13      albertel  226:        }
1.57      albertel  227:      } else {
                    228:        &Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
1.16      albertel  229:      }
1.55      albertel  230:      #evaluate variable refs in result
1.25      sakharuk  231:      if ($result ne "") {
1.24      sakharuk  232:        if ( $#parstack > -1 ) {
1.55      albertel  233: 	 if ($Apache::lonxml::redirection) {
                    234: 	   $Apache::lonxml::outputstack['-1'] .= 
                    235: 	     &Apache::run::evaluate($result,$safeeval,$parstack[$#parstack]);
                    236: 	 } else {
                    237: 	   $finaloutput .= &Apache::run::evaluate($result,$safeeval,
                    238: 						  $parstack[$#parstack]);
                    239: 	 }
1.16      albertel  240:        } else {
                    241: 	 $finaloutput .= &Apache::run::evaluate($result,$safeeval,'');
1.13      albertel  242:        }
1.16      albertel  243:        $result = '';
1.55      albertel  244:      } 
                    245:      if ($token->[0] eq 'E') { 
                    246:        pop @stack;pop @parstack;&decreasedepth($token);
1.2       sakharuk  247:      }
1.5       albertel  248:    }
1.16      albertel  249:    pop @pars;
1.23      albertel  250:    pop @Apache::lonxml::pwd;
1.3       sakharuk  251:  }
1.24      sakharuk  252: 
1.59      albertel  253: # if ($target eq 'meta') {
                    254: #   $finaloutput.=&endredirection;
                    255: # }
1.67      www       256: 
                    257:   if (($ENV{'QUERY_STRING'}) && ($target eq 'web')) {
                    258:       $finaloutput=&afterburn($finaloutput);
                    259:   }
                    260: 
1.3       sakharuk  261:  return $finaloutput;
1.15      albertel  262: }
                    263: 
1.67      www       264: 
1.15      albertel  265: sub recurse {
                    266:   
                    267:   my @innerstack = (); 
                    268:   my @innerparstack = ();
                    269:   my ($newarg,$target,$safeeval,$style_for_target,@parstack) = @_;
1.16      albertel  270:   my @pat = ();
1.23      albertel  271:   &newparser(\@pat,\$newarg);
1.15      albertel  272:   my $tokenpat;
                    273:   my $partstring = '';
                    274:   my $output='';
1.16      albertel  275:   my $decls='';
                    276:   while ( $#pat > -1 ) {
                    277:     while  ($tokenpat = $pat[$#pat]->get_token) {
1.57      albertel  278:       if (($tokenpat->[0] eq 'T') || ($tokenpat->[0] eq 'C') || ($tokenpat->[0] eq 'D') ) {
1.61      albertel  279: 	if ($metamode<1) { $partstring=$tokenpat->[1]; }
1.57      albertel  280:       } elsif ($tokenpat->[0] eq 'PI') {
1.61      albertel  281: 	if ($metamode<1) { $partstring=$tokenpat->[2]; }
1.16      albertel  282:       } elsif ($tokenpat->[0] eq 'S') {
                    283: 	push (@innerstack,$tokenpat->[1]);
                    284: 	push (@innerparstack,&parstring($tokenpat));
1.19      albertel  285: 	&increasedepth($tokenpat);
1.16      albertel  286: 	$partstring = &callsub("start_$tokenpat->[1]", 
                    287: 			       $target, $tokenpat, \@innerparstack,
                    288: 			       \@pat, $safeeval, $style_for_target);
                    289:       } elsif ($tokenpat->[0] eq 'E') {
                    290: 	#clear out any tags that didn't end
                    291: 	while ($tokenpat->[1] ne $innerstack[$#innerstack] 
1.43      albertel  292: 	       && ($#innerstack > -1)) {
1.49      albertel  293: 	  &Apache::lonxml::warning("Unbalanced tags in resource $innerstack['-1']");
1.43      albertel  294: 	  pop @innerstack;pop @innerparstack;&decreasedepth($tokenpat);
                    295: 	}
1.16      albertel  296: 	$partstring = &callsub("end_$tokenpat->[1]",
                    297: 			       $target, $tokenpat, \@innerparstack,
                    298: 			       \@pat, $safeeval, $style_for_target);
1.57      albertel  299:       } else {
                    300: 	&Apache::lonxml::error("Unknown token event :$tokenpat->[0]:$tokenpat->[1]:");
1.16      albertel  301:       }
                    302:       #pass both the variable to the style tag, and the tag we 
                    303:       #are processing inside the <definedtag>
                    304:       if ( $partstring ne "" ) {
                    305: 	if ( $#parstack > -1 ) { 
                    306: 	  if ( $#innerparstack > -1 ) { 
                    307: 	    $decls= $parstack[$#parstack].$innerparstack[$#innerparstack];
                    308: 	  } else {
                    309: 	    $decls= $parstack[$#parstack];
                    310: 	  }
                    311: 	} else {
                    312: 	  if ( $#innerparstack > -1 ) { 
                    313: 	    $decls=$innerparstack[$#innerparstack];
                    314: 	  } else {
                    315: 	    $decls='';
                    316: 	  }
                    317: 	}
                    318: 	$output .= &Apache::run::evaluate($partstring,$safeeval,$decls);
                    319: 	$partstring = '';
                    320:       }
1.17      albertel  321:       if ($tokenpat->[0] eq 'E') { pop @innerstack;pop @innerparstack;
1.19      albertel  322: 				 &decreasedepth($tokenpat);}
1.15      albertel  323:     }
1.16      albertel  324:     pop @pat;
1.23      albertel  325:     pop @Apache::lonxml::pwd;
1.15      albertel  326:   }
                    327:   return $output;
1.7       albertel  328: }
                    329: 
                    330: sub callsub {
1.14      albertel  331:   my ($sub,$target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.7       albertel  332:   my $currentstring='';
1.72    ! albertel  333:   my $nodefault;
1.7       albertel  334:   {
1.59      albertel  335:     my $sub1;
1.7       albertel  336:     no strict 'refs';
1.59      albertel  337:     if ($target eq 'edit' && $token->[0] eq 'S') {
                    338:       $currentstring = &Apache::edit::tag_start($target,$token,$parstack,$parser,
                    339: 						$safeeval,$style);
                    340:     }
1.68      www       341:     my $tag=$token->[1];
                    342:     my $space=$Apache::lonxml::alltags{$tag};
                    343:     if (!$space) {
                    344: 	$tag=~tr/A-Z/a-z/;
                    345: 	$sub=~tr/A-Z/a-z/;
                    346: 	$space=$Apache::lonxml::alltags{$tag}
                    347:     }
                    348:     if ($space) {
1.72    ! albertel  349:       #&Apache::lonxml::debug("Calling sub $sub in $space $metamode<br />\n");
1.24      sakharuk  350:       $sub1="$space\:\:$sub";
1.17      albertel  351:       $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
1.72    ! albertel  352:       ($currentstring,$nodefault) = &$sub1($target,$token,$parstack,$parser,
        !           353: 					   $safeeval,$style);
1.7       albertel  354:     } else {
1.72    ! albertel  355:       #&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode<br />\n");
1.62      sakharuk  356:       if ($metamode <1) {
                    357: 	if (defined($token->[4]) && ($metamode < 1)) {
1.72    ! albertel  358: 	  $currentstring = $token->[4];
1.62      sakharuk  359: 	} else {
1.72    ! albertel  360: 	  $currentstring = $token->[2];
1.62      sakharuk  361: 	}
1.7       albertel  362:       }
1.59      albertel  363:     }
1.72    ! albertel  364:     &Apache::lonxml::debug("nodefalt:$nodefault:");
        !           365:     if ($currentstring eq '' && $nodefault eq '') {
        !           366:       if ($target eq 'edit') {
        !           367: 	if ($token->[0] eq 'S') {
        !           368: 	  $currentstring = &Apache::edit::tag_start($token,$target);
        !           369: 	} elsif ($token->[0] eq 'E') {
        !           370: 	  $currentstring = &Apache::edit::tag_end($token,$target);
        !           371: 	}
        !           372:       } elsif ($target eq 'modified') {
        !           373: 	if ($token->[0] eq 'S') {
        !           374: 	  $currentstring = $token->[4];
        !           375: 	} else {
        !           376: 	  $currentstring = $token->[2];
        !           377: 	}
        !           378:       }
1.7       albertel  379:     }
                    380:     use strict 'refs';
                    381:   }
                    382:   return $currentstring;
1.17      albertel  383: }
                    384: 
1.55      albertel  385: sub startredirection {
                    386:   $Apache::lonxml::redirection++;
                    387:   push (@Apache::lonxml::outputstack, '');
                    388: }
                    389: 
                    390: sub endredirection {
                    391:   if (!$Apache::lonxml::redirection) {
1.72    ! albertel  392:     &Apache::lonxml::error("Endredirection was called, before a startredirection, perhaps you have unbalanced tags. Some debuging information:".join ":",caller);
1.55      albertel  393:     return '';
                    394:   }
                    395:   $Apache::lonxml::redirection--;
                    396:   pop @Apache::lonxml::outputstack;
                    397: }
                    398: 
1.17      albertel  399: sub initdepth {
                    400:   @Apache::lonxml::depthcounter=();
                    401:   $Apache::lonxml::depth=-1;
                    402:   $Apache::lonxml::olddepth=-1;
                    403: }
                    404: 
                    405: sub increasedepth {
1.19      albertel  406:   my ($token) = @_;
1.17      albertel  407:   $Apache::lonxml::depth++;
                    408:   $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
                    409:   if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
                    410:     $Apache::lonxml::olddepth=$Apache::lonxml::depth;
                    411:   }
1.42      albertel  412:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
1.64      albertel  413:   &Apache::lonxml::debug("s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n");
1.54      albertel  414: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
1.17      albertel  415: }
                    416: 
                    417: sub decreasedepth {
1.19      albertel  418:   my ($token) = @_;
1.17      albertel  419:   $Apache::lonxml::depth--;
1.36      albertel  420:   if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
                    421:     $#Apache::lonxml::depthcounter--;
                    422:     $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
                    423:   }
1.43      albertel  424:   if (  $Apache::lonxml::depth < -1) {
1.49      albertel  425:     &Apache::lonxml::warning("Unbalanced tags in resource");   
1.43      albertel  426:     $Apache::lonxml::depth='-1';
                    427:   }
1.42      albertel  428:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
1.64      albertel  429:   &Apache::lonxml::debug("e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n");
1.54      albertel  430: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
1.1       sakharuk  431: }
1.19      albertel  432: 
                    433: sub get_all_text {
                    434: 
                    435:  my($tag,$pars)= @_;
                    436:  my $depth=0;
                    437:  my $token;
                    438:  my $result='';
1.57      albertel  439:  if ( $tag =~ m:^/: ) { 
                    440:    my $tag=substr($tag,1); 
                    441: #   &Apache::lonxml::debug("have:$tag:");
                    442:    while (($depth >=0) && ($token = $pars->get_token)) {
                    443: #     &Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]");
                    444:      if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
                    445:        $result.=$token->[1];
                    446:      } elsif ($token->[0] eq 'PI') {
                    447:        $result.=$token->[2];
                    448:      } elsif ($token->[0] eq 'S') {
                    449:        if ($token->[1] eq $tag) { $depth++; }
                    450:        $result.=$token->[4];
                    451:      } elsif ($token->[0] eq 'E')  {
                    452:        if ( $token->[1] eq $tag) { $depth--; }
                    453:        #skip sending back the last end tag
                    454:        if ($depth > -1) { $result.=$token->[2]; } else {
                    455: 	 $pars->unget_token($token);
                    456:        }
                    457:      }
                    458:    }
                    459:  } else {
                    460:    while ($token = $pars->get_token) {
                    461: #     &Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
                    462:      if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
                    463:        $result.=$token->[1];
                    464:      } elsif ($token->[0] eq 'PI') {
                    465:        $result.=$token->[2];
                    466:      } elsif ($token->[0] eq 'S') {
                    467:        if ( $token->[1] eq $tag) { 
                    468: 	 $pars->unget_token($token); last;
                    469:        } else {
                    470: 	 $result.=$token->[4];
                    471:        }
                    472:      } elsif ($token->[0] eq 'E')  {
                    473:        $result.=$token->[2];
1.36      albertel  474:      }
1.19      albertel  475:    }
                    476:  }
1.49      albertel  477: # &Apache::lonxml::debug("Exit:$result:");
1.19      albertel  478:  return $result
                    479: }
                    480: 
1.23      albertel  481: sub newparser {
                    482:   my ($parser,$contentref,$dir) = @_;
                    483:   push (@$parser,HTML::TokeParser->new($contentref));
1.56      albertel  484:   $$parser['-1']->xml_mode('1');
1.23      albertel  485:   if ( $dir eq '' ) {
                    486:     push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
                    487:   } else {
                    488:     push (@Apache::lonxml::pwd, $dir);
                    489:   } 
                    490: #  &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
                    491: #  &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
                    492: }
1.1       sakharuk  493: 
1.8       albertel  494: sub parstring {
                    495:   my ($token) = @_;
                    496:   my $temp='';
1.20      albertel  497:   map {
1.35      www       498:     unless ($_=~/\W/) {
1.42      albertel  499:       my $val=$token->[2]->{$_};
1.53      albertel  500:       $val =~ s/([\%\@\\])/\\$1/g;
1.51      albertel  501:       #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
1.42      albertel  502:       $temp .= "my \$$_=\"$val\";"
1.20      albertel  503:     }
                    504:   } @{$token->[3]};
1.8       albertel  505:   return $temp;
                    506: }
1.22      albertel  507: 
1.34      www       508: sub writeallows {
                    509:     my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
                    510:     my $thisdir=$thisurl;
                    511:     $thisdir=~s/\/[^\/]+$//;
                    512:     my %httpref=();
                    513:     map {
                    514:        $httpref{'httpref.'.
                    515:  	        &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl;              } @extlinks;
                    516:     &Apache::lonnet::appenv(%httpref);
                    517: }
                    518: 
1.66      www       519: #
                    520: # Afterburner handles anchors, highlights and links
                    521: #
                    522: sub afterburn {
                    523:     my $result=shift;
                    524:     map {
                    525:        my ($name, $value) = split(/=/,$_);
                    526:        $value =~ tr/+/ /;
                    527:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
                    528:        if (($name eq 'highlight')||($name eq 'anchor')||($name eq 'link')) {
                    529:            unless ($ENV{'form.'.$name}) {
                    530:               $ENV{'form.'.$name}=$value;
                    531: 	   }
                    532:        }
                    533:     } (split(/&/,$ENV{'QUERY_STRING'}));
                    534:     if ($ENV{'form.highlight'}) {
                    535:         map {
                    536:            my $anchorname=$_;
                    537: 	   my $matchthis=$anchorname;
                    538:            $matchthis=~s/\_+/\\s\+/g;
                    539:            $result=~s/($matchthis)/\<font color=\"red\"\>$1\<\/font\>/gs;
                    540:        } split(/\,/,$ENV{'form.highlight'});
                    541:     }
                    542:     if ($ENV{'form.link'}) {
                    543:         map {
                    544:            my ($anchorname,$linkurl)=split(/\>/,$_);
                    545: 	   my $matchthis=$anchorname;
                    546:            $matchthis=~s/\_+/\\s\+/g;
                    547:            $result=~s/($matchthis)/\<a href=\"$linkurl\"\>$1\<\/a\>/gs;
                    548:        } split(/\,/,$ENV{'form.link'});
                    549:     }
                    550:     if ($ENV{'form.anchor'}) {
                    551:         my $anchorname=$ENV{'form.anchor'};
                    552: 	my $matchthis=$anchorname;
                    553:         $matchthis=~s/\_+/\\s\+/g;
                    554:         $result=~s/($matchthis)/\<a name=\"$anchorname\"\>$1\<\/a\>/s;
                    555:         $result.=(<<"ENDSCRIPT");
                    556: <script>
                    557:     document.location.hash='$anchorname';
                    558: </script>
                    559: ENDSCRIPT
                    560:     }
                    561:     return $result;
                    562: }
                    563: 
1.24      sakharuk  564: sub handler {
                    565:   my $request=shift;
1.68      www       566: 
1.64      albertel  567:   my $target='web';
1.68      www       568: 
1.65      albertel  569:   $Apache::lonxml::debug=0;
1.68      www       570: 
1.25      sakharuk  571:   if ($ENV{'browser.mathml'}) {
1.27      albertel  572:     $request->content_type('text/xml');
                    573:   } else {
                    574:     $request->content_type('text/html');
1.25      sakharuk  575:   }
1.64      albertel  576:   
1.29      sakharuk  577: #  $request->print(<<ENDHEADER);
                    578: #<html>
                    579: #<head>
                    580: #<title>Just test</title>
                    581: #</head>
                    582: #<body bgcolor="#FFFFFF">
                    583: #ENDHEADER
                    584: #  &Apache::lonhomework::send_header($request);
1.27      albertel  585:   $request->send_http_header;
1.64      albertel  586:   
1.45      www       587:   return OK if $request->header_only;
1.27      albertel  588: 
                    589: 
1.50      albertel  590:   my $file=&Apache::lonnet::filelocation("",$request->uri);
1.24      sakharuk  591:   my %mystyle;
1.50      albertel  592:   my $result = ''; 
                    593:   my $filecontents=&Apache::lonnet::getfile($file);
                    594:   if ($filecontents == -1) {
                    595:     &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
                    596:     $filecontents='';
                    597:   } else {
                    598:     $result = &Apache::lonxml::xmlparse($target,$filecontents,'',%mystyle);
1.66      www       599:   }
1.50      albertel  600: 
1.67      www       601:   $request->print($result);
1.64      albertel  602: 
1.34      www       603:   writeallows($request->uri);
1.45      www       604:   return OK;
1.24      sakharuk  605: }
                    606:  
1.22      albertel  607: sub debug {
                    608:   if ($Apache::lonxml::debug eq 1) {
1.54      albertel  609:     print "DEBUG:".$_[0]."<br />\n";
1.22      albertel  610:   }
                    611: }
1.49      albertel  612: 
1.22      albertel  613: sub error {
1.52      albertel  614:   if ($Apache::lonxml::debug eq 1) {
1.55      albertel  615:     print "<b>ERROR:</b>".$_[0]."<br />\n";
1.52      albertel  616:   } else {
                    617:     print "<b>An Error occured while processing this resource. The instructor has been notified.</b> <br />";
                    618:     #notify author
                    619:     &Apache::lonmsg::author_res_msg($ENV{'request.filename'},$_[0]);
                    620:     #notify course
                    621:     if ( $ENV{'request.course.id'} ) {
                    622:       my $users=$ENV{'course.'.$ENV{'request.course.id'}.'.comment.email'};
                    623:       foreach my $user (split /\,/, $users) {
                    624: 	($user,my $domain) = split /:/, $user;
1.54      albertel  625: 	&Apache::lonmsg::user_normal_msg($user,$domain,"Error in $ENV{'request.filename'}",$_[0]);
1.52      albertel  626:       }
                    627:     }
                    628:     
                    629:     #FIXME probably shouldn't have me get everything forever.
1.54      albertel  630:     &Apache::lonmsg::user_normal_msg('albertel','msu',"Error in $ENV{'request.filename'}",$_[0]);
                    631:     #&Apache::lonmsg::user_normal_msg('albertel','103',"Error in $ENV{'request.filename'}",$_[0]);   
1.52      albertel  632:   }
1.22      albertel  633: }
1.49      albertel  634: 
1.22      albertel  635: sub warning {
1.72    ! albertel  636:   #if ($Apache::lonxml::debug eq 1) {
1.55      albertel  637:     print "<b>W</b>ARNING<b>:</b>".$_[0]."<br />\n";
1.72    ! albertel  638:  # }
1.22      albertel  639: }
                    640: 
1.1       sakharuk  641: 1;
                    642: __END__
1.68      www       643: 
                    644: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>