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

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

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