File:  [LON-CAPA] / loncom / xml / lonxml.pm
Revision 1.86: download - view: text, annotated - select for diffs
Sun Jun 3 18:33:54 2001 UTC (23 years ago) by www
Branches: MAIN
CVS tags: HEAD
Less flickering in Remote when flipping pages

    1: # The LearningOnline Network with CAPA
    2: # XML Parser Module 
    3: #
    4: # last modified 06/26/00 by Alexander Sakharuk
    5: # 11/6 Gerd Kortemeyer
    6: # 6/1/1 Gerd Kortemeyer
    7: # 2/21,3/13 Guy
    8: # 3/29,5/4 Gerd Kortemeyer
    9: # 5/10 Scott Harrison
   10: # 5/26 Gerd Kortemeyer
   11: # 5/27 H. K. Ng
   12: # 6/2,6/3 Gerd Kortemeyer
   13: 
   14: package Apache::lonxml; 
   15: use vars 
   16: qw(@pwd @outputstack $redirection $import @extlinks $metamode $evaluate %insertlist @namespace);
   17: use strict;
   18: use HTML::TokeParser;
   19: use Safe;
   20: use Safe::Hole;
   21: use Math::Cephes qw(:trigs :hypers :bessels erf erfc);
   22: use Opcode;
   23: 
   24: sub register {
   25:   my $space;
   26:   my @taglist;
   27:   my $temptag;
   28:   ($space,@taglist) = @_;
   29:   foreach $temptag (@taglist) {
   30:     $Apache::lonxml::alltags{$temptag}=$space;
   31:   }
   32: }
   33: 
   34: use Apache::Constants qw(:common);
   35: use Apache::lontexconvert;
   36: use Apache::style;
   37: use Apache::run;
   38: use Apache::londefdef;
   39: use Apache::scripttag;
   40: use Apache::edit;
   41: use Apache::lonnet;
   42: use Apache::File;
   43: 
   44: #==================================================   Main subroutine: xmlparse  
   45: #debugging control, to turn on debugging modify the correct handler
   46: $Apache::lonxml::debug=0;
   47: 
   48: #path to the directory containing the file currently being processed
   49: @pwd=();
   50: 
   51: #these two are used for capturing a subset of the output for later processing,
   52: #don't touch them directly use &startredirection and &endredirection
   53: @outputstack = ();
   54: $redirection = 0;
   55: 
   56: #controls wheter the <import> tag actually does
   57: $import = 1;
   58: @extlinks=();
   59: 
   60: # meta mode is a bit weird only some output is to be turned off
   61: #<output> tag turns metamode off (defined in londefdef.pm)
   62: $metamode = 0;
   63: 
   64: # turns on and of run::evaluate actually derefencing var refs
   65: $evaluate = 1;
   66: 
   67: # data structure for eidt mode, determines what tags can go into what other tags
   68: %insertlist=();
   69: 
   70: #stores the list of active tag namespaces
   71: @namespace=();
   72: 
   73: sub xmlbegin {
   74:   my $output='';
   75:   if ($ENV{'browser.mathml'}) {
   76:       $output='<?xml version="1.0"?>'
   77:             .'<?xml-stylesheet type="text/css" href="/adm/MathML/mathml.css"?>'
   78:             .'<!DOCTYPE html SYSTEM "/adm/MathML/mathml.dtd" '
   79:             .'[<!ENTITY mathns "http://www.w3.org/1998/Math/MathML">]>'
   80:             .'<html xmlns:math="http://www.w3.org/1998/Math/MathML" ' 
   81: 		.'xmlns="http://www.w3.org/TR/REC-html40">';
   82:   } else {
   83:       $output='<html>';
   84:   }
   85:   return $output;
   86: }
   87: 
   88: sub xmlend {
   89:     return '</html>';
   90: }
   91: 
   92: sub fontsettings() {
   93:     my $headerstring='';
   94:     if (($ENV{'browser.os'} eq 'mac') && (!$ENV{'browser.mathml'})) { 
   95:          $headerstring.=
   96:              '<meta Content-Type="text/html; charset=x-mac-roman">';
   97:     }
   98:     return $headerstring;
   99: }
  100: 
  101: sub registerurl {
  102:     if ($ENV{'REQUEST_URI'}!~/^\/(res\/)*adm\//) {
  103: 	return (<<ENDREGTHIS);
  104: 
  105: <script language="JavaScript">
  106: // BEGIN LON-CAPA Internal
  107: 
  108:     function LONCAPAreg() {
  109: 	  menu=window.open("","LONCAPAmenu");
  110:           menu.clearTimeout(menu.menucltim);
  111: 	  menu.currentURL=window.location.pathname;
  112:           menu.currentStale=0;
  113:           menu.clearbut(3,1);
  114:           menu.switchbutton
  115:        (8,1,'eval.gif','evaluate','this','gopost("/adm/evaluate",currentURL)');
  116:           menu.switchbutton
  117:     (8,2,'fdbk.gif','feedback','on this','gopost("/adm/feedback",currentURL)');
  118:           menu.switchbutton
  119:      (8,3,'prt.gif','prepare','printout','gopost("/adm/printout",currentURL)');
  120:           menu.switchbutton
  121:        (2,1,'back.gif','backward','','gopost("/adm/flip","back:"+currentURL)');
  122:           menu.switchbutton
  123:      (2,3,'forw.gif','forward','','gopost("/adm/flip","forward:"+currentURL)');
  124:     }
  125: 
  126:     function LONCAPAstale() {
  127: 	  menu=window.open("","LONCAPAmenu");
  128:           menu.currentStale=1;
  129:           menu.switchbutton
  130:             (3,1,'reload.gif','return','location','go(currentURL)');
  131:           menu.menucltim=menu.setTimeout(
  132:  'clearbut(2,1);clearbut(2,3);clearbut(8,1);clearbut(8,2);clearbut(8,3);',
  133: 			  2000);
  134: 
  135:     }
  136: 
  137: // END LON-CAPA Internal
  138: </script>
  139: ENDREGTHIS
  140: 
  141:     } else {
  142:         return (<<ENDDONOTREGTHIS);
  143: 
  144: <script language="JavaScript">
  145: // BEGIN LON-CAPA Internal
  146: 
  147:     function LONCAPAreg() {
  148: 	  menu=window.open("","LONCAPAmenu");
  149:           menu.currentStale=1;
  150:           menu.clearbut(2,1);
  151:           menu.clearbut(2,3);
  152:           menu.clearbut(8,1);
  153:           menu.clearbut(8,2);
  154:           menu.clearbut(8,3);
  155:           if (menu.currentURL) {
  156:              menu.switchbutton
  157:               (3,1,'reload.gif','return','location','go(currentURL)');
  158:  	  } else {
  159: 	      menu.clearbut(3,1);
  160:           }
  161:     }
  162: 
  163:     function LONCAPAstale() {
  164:     }
  165: 
  166: // END LON-CAPA Internal
  167: </script>
  168: ENDDONOTREGTHIS
  169: 
  170:     }
  171: }
  172: 
  173: sub loadevents() {
  174:     return 'LONCAPAreg();';
  175: }
  176: 
  177: sub unloadevents() {
  178:     return 'LONCAPAstale();';
  179: }
  180: 
  181: sub printalltags {
  182:   my $temp;
  183:   foreach $temp (sort keys %Apache::lonxml::alltags) {
  184:     &Apache::lonxml::debug("$temp -- $Apache::lonxml::alltags{$temp}");
  185:   }
  186: }
  187: 
  188: sub xmlparse {
  189: 
  190:  my ($target,$content_file_string,$safeinit,%style_for_target) = @_;
  191:  if ($target eq 'meta') {
  192:    $Apache::lonxml::redirection = 0;
  193:    $Apache::lonxml::metamode = 1;
  194:    $Apache::lonxml::evaluate = 1;
  195:    $Apache::lonxml::import = 0;
  196:  } elsif ($target eq 'grade') {
  197:    &startredirection;
  198:    $Apache::lonxml::metamode = 0;
  199:    $Apache::lonxml::evaluate = 1;
  200:    $Apache::lonxml::import = 1;
  201:  } elsif ($target eq 'modified') {
  202:    $Apache::lonxml::redirection = 0;
  203:    $Apache::lonxml::metamode = 0;
  204:    $Apache::lonxml::evaluate = 0;
  205:    $Apache::lonxml::import = 0;
  206:  } else {
  207:    $Apache::lonxml::redirection = 0;
  208:    $Apache::lonxml::metamode = 0;
  209:    $Apache::lonxml::evaluate = 1;
  210:    $Apache::lonxml::import = 1;
  211:  }
  212:  #&printalltags();
  213:  my @pars = ();
  214:  @Apache::lonxml::pwd=();
  215:  my $pwd=$ENV{'request.filename'};
  216:  $pwd =~ s:/[^/]*$::;
  217:  &newparser(\@pars,\$content_file_string,$pwd);
  218:  my $currentstring = '';
  219:  my $finaloutput = ''; 
  220:  my $newarg = '';
  221:  my $result;
  222: 
  223:  my $safeeval = new Safe;
  224:  my $safehole = new Safe::Hole;
  225:  &init_safespace($target,$safeeval,$safehole,$safeinit);
  226: #-------------------- Redefinition of the target in the case of compound target
  227: 
  228:  ($target, my @tenta) = split('&&',$target);
  229: 
  230:  my @stack = (); 
  231:  my @parstack = ();
  232:  &initdepth;
  233:  my $token;
  234:  while ( $#pars > -1 ) {
  235:    while ($token = $pars[$#pars]->get_token) {
  236:      if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
  237:        if ($metamode<1) { $result=$token->[1]; }
  238:      } elsif ($token->[0] eq 'PI') {
  239:        if ($metamode<1) { $result=$token->[2]; }
  240:      } elsif ($token->[0] eq 'S') {
  241:        # add tag to stack 	    
  242:        push (@stack,$token->[1]);
  243:        # add parameters list to another stack
  244:        push (@parstack,&parstring($token));
  245:        &increasedepth($token);       
  246:        if (exists $style_for_target{$token->[1]}) {
  247: 	 if ($Apache::lonxml::redirection) {
  248: 	   $Apache::lonxml::outputstack['-1'] .=  
  249: 	     &recurse($style_for_target{$token->[1]},$target,$safeeval,
  250: 		      \%style_for_target,@parstack);
  251: 	 } else {
  252: 	   $finaloutput .= &recurse($style_for_target{$token->[1]},$target,
  253: 				    $safeeval,\%style_for_target,@parstack);
  254: 	 }
  255:        } else {
  256: 	 $result = &callsub("start_$token->[1]", $target, $token, \@stack,
  257: 			    \@parstack, \@pars, $safeeval, \%style_for_target);
  258:        }              
  259:      } elsif ($token->[0] eq 'E')  {
  260:        #clear out any tags that didn't end
  261:        while ($token->[1] ne $stack[$#stack] && ($#stack > -1)) {
  262: 	 &Apache::lonxml::warning("Unbalanced tags in resource $stack['-1']");
  263: 	 pop @stack;pop @parstack;&decreasedepth($token);
  264:        }
  265:        
  266:        if (exists $style_for_target{'/'."$token->[1]"}) {
  267: 	 if ($Apache::lonxml::redirection) {
  268: 	   $Apache::lonxml::outputstack['-1'] .=  
  269: 	     &recurse($style_for_target{'/'."$token->[1]"},
  270: 		      $target,$safeeval,\%style_for_target,@parstack);
  271: 	 } else {
  272: 	   $finaloutput .= &recurse($style_for_target{'/'."$token->[1]"},
  273: 				    $target,$safeeval,\%style_for_target,
  274: 				    @parstack);
  275: 	 }
  276: 
  277:        } else {
  278: 	 $result = &callsub("end_$token->[1]", $target, $token, \@stack, 
  279: 			    \@parstack, \@pars,$safeeval, \%style_for_target);
  280:        }
  281:      } else {
  282:        &Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
  283:      }
  284:      #evaluate variable refs in result
  285:      if ($result ne "") {
  286:        if ( $#parstack > -1 ) {
  287: 	 if ($Apache::lonxml::redirection) {
  288: 	   $Apache::lonxml::outputstack['-1'] .= 
  289: 	     &Apache::run::evaluate($result,$safeeval,$parstack[$#parstack]);
  290: 	 } else {
  291: 	   $finaloutput .= &Apache::run::evaluate($result,$safeeval,
  292: 						  $parstack[$#parstack]);
  293: 	 }
  294:        } else {
  295: 	 $finaloutput .= &Apache::run::evaluate($result,$safeeval,'');
  296:        }
  297:        $result = '';
  298:      } 
  299:      if ($token->[0] eq 'E') { 
  300:        pop @stack;pop @parstack;&decreasedepth($token);
  301:      }
  302:    }
  303:    pop @pars;
  304:    pop @Apache::lonxml::pwd;
  305:  }
  306: 
  307: # if ($target eq 'meta') {
  308: #   $finaloutput.=&endredirection;
  309: # }
  310: 
  311:   if (($ENV{'QUERY_STRING'}) && ($target eq 'web')) {
  312:       $finaloutput=&afterburn($finaloutput);
  313:   }
  314: 
  315:  return $finaloutput;
  316: }
  317: 
  318: 
  319: sub recurse {
  320:   
  321:   my @innerstack = (); 
  322:   my @innerparstack = ();
  323:   my ($newarg,$target,$safeeval,$style_for_target,@parstack) = @_;
  324:   my @pat = ();
  325:   &newparser(\@pat,\$newarg);
  326:   my $tokenpat;
  327:   my $partstring = '';
  328:   my $output='';
  329:   my $decls='';
  330:   while ( $#pat > -1 ) {
  331:     while  ($tokenpat = $pat[$#pat]->get_token) {
  332:       if (($tokenpat->[0] eq 'T') || ($tokenpat->[0] eq 'C') || ($tokenpat->[0] eq 'D') ) {
  333: 	if ($metamode<1) { $partstring=$tokenpat->[1]; }
  334:       } elsif ($tokenpat->[0] eq 'PI') {
  335: 	if ($metamode<1) { $partstring=$tokenpat->[2]; }
  336:       } elsif ($tokenpat->[0] eq 'S') {
  337: 	push (@innerstack,$tokenpat->[1]);
  338: 	push (@innerparstack,&parstring($tokenpat));
  339: 	&increasedepth($tokenpat);
  340: 	$partstring = &callsub("start_$tokenpat->[1]", $target, $tokenpat,
  341: 			       \@innerstack, \@innerparstack, \@pat,
  342: 			       $safeeval, $style_for_target);
  343:       } elsif ($tokenpat->[0] eq 'E') {
  344: 	#clear out any tags that didn't end
  345: 	while ($tokenpat->[1] ne $innerstack[$#innerstack] 
  346: 	       && ($#innerstack > -1)) {
  347: 	  &Apache::lonxml::warning("Unbalanced tags in resource $innerstack['-1']");
  348: 	  pop @innerstack;pop @innerparstack;&decreasedepth($tokenpat);
  349: 	}
  350: 	$partstring = &callsub("end_$tokenpat->[1]", $target, $tokenpat,
  351: 			       \@innerstack, \@innerparstack, \@pat,
  352: 			       $safeeval, $style_for_target);
  353:       } else {
  354: 	&Apache::lonxml::error("Unknown token event :$tokenpat->[0]:$tokenpat->[1]:");
  355:       }
  356:       #pass both the variable to the style tag, and the tag we 
  357:       #are processing inside the <definedtag>
  358:       if ( $partstring ne "" ) {
  359: 	if ( $#parstack > -1 ) { 
  360: 	  if ( $#innerparstack > -1 ) { 
  361: 	    $decls= $parstack[$#parstack].$innerparstack[$#innerparstack];
  362: 	  } else {
  363: 	    $decls= $parstack[$#parstack];
  364: 	  }
  365: 	} else {
  366: 	  if ( $#innerparstack > -1 ) { 
  367: 	    $decls=$innerparstack[$#innerparstack];
  368: 	  } else {
  369: 	    $decls='';
  370: 	  }
  371: 	}
  372: 	$output .= &Apache::run::evaluate($partstring,$safeeval,$decls);
  373: 	$partstring = '';
  374:       }
  375:       if ($tokenpat->[0] eq 'E') { pop @innerstack;pop @innerparstack;
  376: 				 &decreasedepth($tokenpat);}
  377:     }
  378:     pop @pat;
  379:     pop @Apache::lonxml::pwd;
  380:   }
  381:   return $output;
  382: }
  383: 
  384: sub callsub {
  385:   my ($sub,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  386:   my $currentstring='';
  387:   my $nodefault;
  388:   {
  389:     my $sub1;
  390:     no strict 'refs';
  391:     if ($target eq 'edit' && $token->[0] eq 'S') {
  392:       $currentstring = &Apache::edit::tag_start($target,$token,$tagstack,
  393: 						$parstack,$parser,
  394: 						$safeeval,$style);
  395:     }
  396:     my $tag=$token->[1];
  397:     my $space=$Apache::lonxml::alltags{$tag};
  398:     if (!$space) {
  399: 	$tag=~tr/A-Z/a-z/;
  400: 	$sub=~tr/A-Z/a-z/;
  401: 	$space=$Apache::lonxml::alltags{$tag}
  402:     }
  403:     if ($space) {
  404:       #&Apache::lonxml::debug("Calling sub $sub in $space $metamode<br />\n");
  405:       $sub1="$space\:\:$sub";
  406:       $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
  407:       ($currentstring,$nodefault) = &$sub1($target,$token,$tagstack,
  408: 					   $parstack,$parser,$safeeval,
  409: 					   $style);
  410:     } else {
  411:       #&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode<br />\n");
  412:       if ($metamode <1) {
  413: 	if (defined($token->[4]) && ($metamode < 1)) {
  414: 	  $currentstring = $token->[4];
  415: 	} else {
  416: 	  $currentstring = $token->[2];
  417: 	}
  418:       }
  419:     }
  420: #    &Apache::lonxml::debug("nodefalt:$nodefault:");
  421:     if ($currentstring eq '' && $nodefault eq '') {
  422:       if ($target eq 'edit') {
  423: 	&Apache::lonxml::debug("doing default edit for $token->[1]");
  424: 	if ($token->[0] eq 'S') {
  425: 	  $currentstring = &Apache::edit::tag_start($target,$token);
  426: 	} elsif ($token->[0] eq 'E') {
  427: 	  $currentstring = &Apache::edit::tag_end($target,$token);
  428: 	}
  429:       } elsif ($target eq 'modified') {
  430: 	if ($token->[0] eq 'S') {
  431: 	  $currentstring = $token->[4];
  432: 	  $currentstring.=&Apache::edit::handle_insert();
  433: 	} else {
  434: 	  $currentstring = $token->[2];
  435: 	}
  436:       }
  437:     }
  438:     use strict 'refs';
  439:   }
  440:   return $currentstring;
  441: }
  442: 
  443: sub init_safespace {
  444:   my ($target,$safeeval,$safehole,$safeinit) = @_;
  445:   $safeeval->permit("entereval");
  446:   $safeeval->permit(":base_math");
  447:   $safeeval->permit("sort");
  448:   $safeeval->deny(":base_io");
  449:   $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
  450:   
  451:   $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin');
  452:   $safehole->wrap(\&Math::Cephes::acos,$safeeval,'&acos');
  453:   $safehole->wrap(\&Math::Cephes::atan,$safeeval,'&atan');
  454:   $safehole->wrap(\&Math::Cephes::sinh,$safeeval,'&sinh');
  455:   $safehole->wrap(\&Math::Cephes::cosh,$safeeval,'&cosh');
  456:   $safehole->wrap(\&Math::Cephes::tanh,$safeeval,'&tanh');
  457:   $safehole->wrap(\&Math::Cephes::asinh,$safeeval,'&asinh');
  458:   $safehole->wrap(\&Math::Cephes::acosh,$safeeval,'&acosh');
  459:   $safehole->wrap(\&Math::Cephes::atanh,$safeeval,'&atanh');
  460:   $safehole->wrap(\&Math::Cephes::erf,$safeeval,'&erf');
  461:   $safehole->wrap(\&Math::Cephes::erfc,$safeeval,'&erfc');
  462:   $safehole->wrap(\&Math::Cephes::j0,$safeeval,'&j0');
  463:   $safehole->wrap(\&Math::Cephes::j1,$safeeval,'&j1');
  464:   $safehole->wrap(\&Math::Cephes::jn,$safeeval,'&jn');
  465:   $safehole->wrap(\&Math::Cephes::jv,$safeeval,'&jv');
  466:   $safehole->wrap(\&Math::Cephes::y0,$safeeval,'&y0');
  467:   $safehole->wrap(\&Math::Cephes::y1,$safeeval,'&y1');
  468:   $safehole->wrap(\&Math::Cephes::yn,$safeeval,'&yn');
  469:   $safehole->wrap(\&Math::Cephes::yv,$safeeval,'&yv');
  470:   
  471: #need to inspect this class of ops
  472: # $safeeval->deny(":base_orig");
  473:   $safeinit .= ';$external::target='.$target.';';
  474:   $safeinit .= ';$external::randomseed='.&Apache::lonnet::rndseed().';';
  475:   &Apache::run::run($safeinit,$safeeval);
  476: }
  477: 
  478: sub startredirection {
  479:   $Apache::lonxml::redirection++;
  480:   push (@Apache::lonxml::outputstack, '');
  481: }
  482: 
  483: sub endredirection {
  484:   if (!$Apache::lonxml::redirection) {
  485:     &Apache::lonxml::error("Endredirection was called, before a startredirection, perhaps you have unbalanced tags. Some debuging information:".join ":",caller);
  486:     return '';
  487:   }
  488:   $Apache::lonxml::redirection--;
  489:   pop @Apache::lonxml::outputstack;
  490: }
  491: 
  492: sub initdepth {
  493:   @Apache::lonxml::depthcounter=();
  494:   $Apache::lonxml::depth=-1;
  495:   $Apache::lonxml::olddepth=-1;
  496: }
  497: 
  498: sub increasedepth {
  499:   my ($token) = @_;
  500:   $Apache::lonxml::depth++;
  501:   $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
  502:   if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
  503:     $Apache::lonxml::olddepth=$Apache::lonxml::depth;
  504:   }
  505:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
  506:   &Apache::lonxml::debug("s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n");
  507: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
  508: }
  509: 
  510: sub decreasedepth {
  511:   my ($token) = @_;
  512:   $Apache::lonxml::depth--;
  513:   if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
  514:     $#Apache::lonxml::depthcounter--;
  515:     $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
  516:   }
  517:   if (  $Apache::lonxml::depth < -1) {
  518:     &Apache::lonxml::warning("Unbalanced tags in resource");   
  519:     $Apache::lonxml::depth='-1';
  520:   }
  521:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
  522:   &Apache::lonxml::debug("e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n");
  523: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
  524: }
  525: 
  526: sub get_all_text {
  527: 
  528:  my($tag,$pars)= @_;
  529:  my $depth=0;
  530:  my $token;
  531:  my $result='';
  532:  if ( $tag =~ m:^/: ) { 
  533:    my $tag=substr($tag,1); 
  534: #   &Apache::lonxml::debug("have:$tag:");
  535:    while (($depth >=0) && ($token = $pars->get_token)) {
  536: #     &Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]");
  537:      if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
  538:        $result.=$token->[1];
  539:      } elsif ($token->[0] eq 'PI') {
  540:        $result.=$token->[2];
  541:      } elsif ($token->[0] eq 'S') {
  542:        if ($token->[1] eq $tag) { $depth++; }
  543:        $result.=$token->[4];
  544:      } elsif ($token->[0] eq 'E')  {
  545:        if ( $token->[1] eq $tag) { $depth--; }
  546:        #skip sending back the last end tag
  547:        if ($depth > -1) { $result.=$token->[2]; } else {
  548: 	 $pars->unget_token($token);
  549:        }
  550:      }
  551:    }
  552:  } else {
  553:    while ($token = $pars->get_token) {
  554: #     &Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
  555:      if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
  556:        $result.=$token->[1];
  557:      } elsif ($token->[0] eq 'PI') {
  558:        $result.=$token->[2];
  559:      } elsif ($token->[0] eq 'S') {
  560:        if ( $token->[1] eq $tag) { 
  561: 	 $pars->unget_token($token); last;
  562:        } else {
  563: 	 $result.=$token->[4];
  564:        }
  565:      } elsif ($token->[0] eq 'E')  {
  566:        $result.=$token->[2];
  567:      }
  568:    }
  569:  }
  570: # &Apache::lonxml::debug("Exit:$result:");
  571:  return $result
  572: }
  573: 
  574: sub newparser {
  575:   my ($parser,$contentref,$dir) = @_;
  576:   push (@$parser,HTML::TokeParser->new($contentref));
  577:   $$parser['-1']->xml_mode('1');
  578:   if ( $dir eq '' ) {
  579:     push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
  580:   } else {
  581:     push (@Apache::lonxml::pwd, $dir);
  582:   } 
  583: #  &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
  584: #  &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
  585: }
  586: 
  587: sub parstring {
  588:   my ($token) = @_;
  589:   my $temp='';
  590:   map {
  591:     unless ($_=~/\W/) {
  592:       my $val=$token->[2]->{$_};
  593:       $val =~ s/([\%\@\\])/\\$1/g;
  594:       #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
  595:       $temp .= "my \$$_=\"$val\";"
  596:     }
  597:   } @{$token->[3]};
  598:   return $temp;
  599: }
  600: 
  601: sub writeallows {
  602:     my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
  603:     my $thisdir=$thisurl;
  604:     $thisdir=~s/\/[^\/]+$//;
  605:     my %httpref=();
  606:     map {
  607:        $httpref{'httpref.'.
  608:  	        &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl;              } @extlinks;
  609:     &Apache::lonnet::appenv(%httpref);
  610: }
  611: 
  612: #
  613: # Afterburner handles anchors, highlights and links
  614: #
  615: sub afterburn {
  616:     my $result=shift;
  617:     map {
  618:        my ($name, $value) = split(/=/,$_);
  619:        $value =~ tr/+/ /;
  620:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  621:        if (($name eq 'highlight')||($name eq 'anchor')||($name eq 'link')) {
  622:            unless ($ENV{'form.'.$name}) {
  623:               $ENV{'form.'.$name}=$value;
  624: 	   }
  625:        }
  626:     } (split(/&/,$ENV{'QUERY_STRING'}));
  627:     if ($ENV{'form.highlight'}) {
  628:         map {
  629:            my $anchorname=$_;
  630: 	   my $matchthis=$anchorname;
  631:            $matchthis=~s/\_+/\\s\+/g;
  632:            $result=~s/($matchthis)/\<font color=\"red\"\>$1\<\/font\>/gs;
  633:        } split(/\,/,$ENV{'form.highlight'});
  634:     }
  635:     if ($ENV{'form.link'}) {
  636:         map {
  637:            my ($anchorname,$linkurl)=split(/\>/,$_);
  638: 	   my $matchthis=$anchorname;
  639:            $matchthis=~s/\_+/\\s\+/g;
  640:            $result=~s/($matchthis)/\<a href=\"$linkurl\"\>$1\<\/a\>/gs;
  641:        } split(/\,/,$ENV{'form.link'});
  642:     }
  643:     if ($ENV{'form.anchor'}) {
  644:         my $anchorname=$ENV{'form.anchor'};
  645: 	my $matchthis=$anchorname;
  646:         $matchthis=~s/\_+/\\s\+/g;
  647:         $result=~s/($matchthis)/\<a name=\"$anchorname\"\>$1\<\/a\>/s;
  648:         $result.=(<<"ENDSCRIPT");
  649: <script>
  650:     document.location.hash='$anchorname';
  651: </script>
  652: ENDSCRIPT
  653:     }
  654:     return $result;
  655: }
  656: 
  657: sub storefile {
  658:     my ($file,$contents)=@_;
  659:     if (my $fh=Apache::File->new('>'.$file)) {
  660: 	print $fh $contents;
  661:         $fh->close();
  662:     }
  663: }
  664: 
  665: sub inserteditinfo {
  666:       my ($result,$filecontents)=@_;
  667:       unless ($filecontents) {
  668: 	  $filecontents=(<<SIMPLECONTENT);
  669: <html>
  670: <head>
  671: <title>
  672:                            Title of Document Goes Here
  673: </title>
  674: </head>
  675: <body bgcolor="#FFFFFF">
  676: 
  677:                            Body of Document Goes Here
  678: 
  679: </body>
  680: </html>
  681: SIMPLECONTENT
  682:       }
  683:       my $editheader='<a href="#editsection">Edit below</a><hr />';
  684:       my $editfooter=(<<ENDFOOTER);
  685: <hr />
  686: <a name="editsection" />
  687: <form method="post">
  688: <textarea cols="80" rows="40" name="filecont">$filecontents</textarea>
  689: <br />
  690: <input type="submit" name="savethisfile" value="Save this file" />
  691: </form>
  692: ENDFOOTER
  693:       $result=~s/(\<body[^\>]*\>)/$1$editheader/is;
  694:       $result=~s/(\<\/body\>)/$editfooter/is;
  695:       return $result;
  696: }
  697: 
  698: sub handler {
  699:   my $request=shift;
  700: 
  701:   my $target='web';
  702: 
  703:   $Apache::lonxml::debug=0;
  704: 
  705:   if ($ENV{'browser.mathml'}) {
  706:     $request->content_type('text/xml');
  707:   } else {
  708:     $request->content_type('text/html');
  709:   }
  710:   
  711:   $request->send_http_header;
  712:   
  713:   return OK if $request->header_only;
  714: 
  715: 
  716:   my $file=&Apache::lonnet::filelocation("",$request->uri);
  717: #
  718: # Edit action? Save file.
  719: #
  720:   unless ($ENV{'request.state'} eq 'published') {
  721:       if ($ENV{'form.savethisfile'}) {
  722: 	  &storefile($file,$ENV{'form.filecont'});
  723:       }
  724:   }
  725:   my %mystyle;
  726:   my $result = ''; 
  727:   my $filecontents=&Apache::lonnet::getfile($file);
  728:   if ($filecontents == -1) {
  729:     $result=(<<ENDNOTFOUND);
  730: <html>
  731: <head>
  732: <title>File not found</title>
  733: </head>
  734: <body bgcolor="#FFFFFF">
  735: <b>File not found: $file</b>
  736: </body>
  737: </html>
  738: ENDNOTFOUND
  739:     $filecontents='';
  740:   } else {
  741:     $result = &Apache::lonxml::xmlparse($target,$filecontents,'',%mystyle);
  742:   }
  743: 
  744: #
  745: # Edit action? Insert editing commands
  746: #
  747:   unless ($ENV{'request.state'} eq 'published') {
  748:       $result=&inserteditinfo($result,$filecontents);
  749:   }
  750: 
  751:   $request->print($result);
  752: 
  753:   writeallows($request->uri);
  754:   return OK;
  755: }
  756:  
  757: sub debug {
  758:   if ($Apache::lonxml::debug eq 1) {
  759:     print "DEBUG:".$_[0]."<br />\n";
  760:   }
  761: }
  762: 
  763: sub error {
  764:   if (($Apache::lonxml::debug eq 1) || ($ENV{'request.state'} eq 'construct') ) {
  765:     print "<b>ERROR:</b>".$_[0]."<br />\n";
  766:   } else {
  767:     print "<b>An Error occured while processing this resource. The instructor has been notified.</b> <br />";
  768:     #notify author
  769:     &Apache::lonmsg::author_res_msg($ENV{'request.filename'},$_[0]);
  770:     #notify course
  771:     if ( $ENV{'request.course.id'} ) {
  772:       my $users=$ENV{'course.'.$ENV{'request.course.id'}.'.comment.email'};
  773:       foreach my $user (split /\,/, $users) {
  774: 	($user,my $domain) = split /:/, $user;
  775: 	&Apache::lonmsg::user_normal_msg($user,$domain,"Error in $ENV{'request.filename'}",$_[0]);
  776:       }
  777:     }
  778: 
  779:     #FIXME probably shouldn't have me get everything forever.
  780:     &Apache::lonmsg::user_normal_msg('albertel','msu',"Error in $ENV{'request.filename'}",$_[0]);
  781:     #&Apache::lonmsg::user_normal_msg('albertel','103',"Error in $ENV{'request.filename'}",$_[0]);
  782:   }
  783: }
  784: 
  785: sub warning {
  786:   if ($ENV{'request.state'} eq 'construct') {
  787:     print "<b>W</b>ARNING<b>:</b>".$_[0]."<br />\n";
  788:   }
  789: }
  790: 
  791: sub get_param {
  792:   my ($param,$parstack,$safeeval,$context) = @_;
  793:   if ( ! $context ) { $context = -1; }
  794:   my $args ='';
  795:   if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
  796:   return &Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
  797: }
  798: 
  799: sub register_insert {
  800:   my @data = split /\n/, &Apache::lonnet::getfile('/home/httpd/lonTabs/insertlist.tab');
  801:   my $i;
  802:   my $tagnum=0;
  803:   my @order;
  804:   for ($i=0;$i < $#data; $i++) {
  805:     my $line = $data[$i];
  806:     if ( $line =~ /^\#/ || $line =~ /^\s*\n/) { next; }
  807:     if ( $line =~ /TABLE/ ) { last; }
  808:     my ($tag,$descrip,$function,$show) = split(/,/, $line);
  809:     $insertlist{"$tagnum.tag"} = $tag;
  810:     $insertlist{"$tagnum.description"} = $descrip;
  811:     $insertlist{"$tagnum.function"} = $function;
  812:     $insertlist{"$tagnum.show"}= $show;
  813:     $tagnum++;
  814:   }
  815:   $i++; #skipping TABLE line
  816:   $tagnum = 0;
  817:   for (;$i < $#data;$i++) {
  818:     my $line = $data[$i];
  819:     my ($mnemonic,@which) = split(/ +/,$line);
  820:     my $tag = $insertlist{"$tagnum.tag"};
  821:     for (my $j=0;$j <$#which;$j++) {
  822:       if ( $which[$j] eq 'Y' ) {
  823: 	if ($insertlist{"$j.show"} ne 'no') {
  824: 	  push(@{ $insertlist{"$tag.which"} },$j);
  825: 	}
  826:       }
  827:     }
  828:     $tagnum++;
  829:   }
  830: }
  831: 1;
  832: __END__
  833: 
  834: 

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