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

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

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