File:  [LON-CAPA] / loncom / xml / lonxml.pm
Revision 1.80: download - view: text, annotated - select for diffs
Mon May 28 18:27:46 2001 UTC (23 years ago) by harris41
Branches: MAIN
CVS tags: HEAD
add Math::Trig
add Math::Cephes qw(j0 j1 jn jv y0 y1 yn yv)

    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: 
   13: package Apache::lonxml; 
   14: use vars 
   15: qw(@pwd @outputstack $redirection $import @extlinks $metamode $evaluate %insertlist @namespace);
   16: use strict;
   17: use HTML::TokeParser;
   18: use Safe;
   19: use Safe::Hole;
   20: use Math::Trig;
   21: use Math::Cephes qw(j0 j1 jn jv y0 y1 yn yv);
   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:   return (<<ENDSCRIPT);
  103: <script language="JavaScript">
  104: // BEGIN LON-CAPA Internal
  105:     function LONCAPAreg() {
  106:        if (window.location.pathname!="/res/adm/pages/menu.html") {
  107: 	  menu=window.open("","LONCAPAmenu");
  108: 	  menu.currentURL=window.location.pathname;
  109:           menu.currentStale=0;
  110:        }
  111:     }
  112:   
  113:     function LONCAPAstale() {
  114:        if (window.location.pathname!="/res/adm/pages/menu.html") {
  115: 	  menu=window.open("","LONCAPAmenu");
  116:           menu.currentStale=1;
  117:        }
  118:     }
  119: // END LON-CAPA Internal
  120: </script>
  121: ENDSCRIPT
  122: }
  123: 
  124: sub loadevents() {
  125:     return 'LONCAPAreg();';
  126: }
  127: 
  128: sub unloadevents() {
  129:     return 'LONCAPAstale();';
  130: }
  131: 
  132: sub printalltags {
  133:   my $temp;
  134:   foreach $temp (sort keys %Apache::lonxml::alltags) {
  135:     &Apache::lonxml::debug("$temp -- $Apache::lonxml::alltags{$temp}");
  136:   }
  137: }
  138: 
  139: sub xmlparse {
  140: 
  141:  my ($target,$content_file_string,$safeinit,%style_for_target) = @_;
  142:  if ($target eq 'meta') {
  143:    $Apache::lonxml::redirection = 0;
  144:    $Apache::lonxml::metamode = 1;
  145:    $Apache::lonxml::evaluate = 1;
  146:    $Apache::lonxml::import = 0;
  147:  } elsif ($target eq 'grade') {
  148:    &startredirection;
  149:    $Apache::lonxml::metamode = 0;
  150:    $Apache::lonxml::evaluate = 1;
  151:    $Apache::lonxml::import = 1;
  152:  } elsif ($target eq 'modified') {
  153:    $Apache::lonxml::redirection = 0;
  154:    $Apache::lonxml::metamode = 0;
  155:    $Apache::lonxml::evaluate = 0;
  156:    $Apache::lonxml::import = 0;
  157:  } else {
  158:    $Apache::lonxml::redirection = 0;
  159:    $Apache::lonxml::metamode = 0;
  160:    $Apache::lonxml::evaluate = 1;
  161:    $Apache::lonxml::import = 1;
  162:  }
  163:  #&printalltags();
  164:  my @pars = ();
  165:  @Apache::lonxml::pwd=();
  166:  my $pwd=$ENV{'request.filename'};
  167:  $pwd =~ s:/[^/]*$::;
  168:  &newparser(\@pars,\$content_file_string,$pwd);
  169:  my $currentstring = '';
  170:  my $finaloutput = ''; 
  171:  my $newarg = '';
  172:  my $result;
  173: 
  174:  my $safeeval = new Safe;
  175:  my $safehole = new Safe::Hole;
  176:  $safeeval->permit("entereval");
  177:  $safeeval->permit(":base_math");
  178:  $safeeval->permit("sort");
  179:  $safeeval->deny(":base_io");
  180: # $safe->share_from('Math::Trig',['acsc','asin','acos','atan']);
  181:  $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
  182:  $safehole->wrap(\&Math::Trig::acsc,$safeeval,'&acsc');
  183:  $safehole->wrap(\&Math::Trig::acosec,$safeeval,'&acosec');
  184:  $safehole->wrap(\&Math::Trig::asec,$safeeval,'&asec');
  185:  $safehole->wrap(\&Math::Trig::acot,$safeeval,'&acot');
  186:  $safehole->wrap(\&Math::Trig::acotan,$safeeval,'&acotan');
  187:  $safehole->wrap(\&Math::Trig::acsch,$safeeval,'&acsch');
  188:  $safehole->wrap(\&Math::Trig::acosech,$safeeval,'&acosech');
  189:  $safehole->wrap(\&Math::Trig::asech,$safeeval,'&asech');
  190:  $safehole->wrap(\&Math::Trig::acoth,$safeeval,'&acoth');
  191:  $safehole->wrap(\&Math::Trig::acotanh,$safeeval,'&acotanh');
  192:  $safehole->wrap(\&Math::Trig::asin,$safeeval,'&asin');
  193:  $safehole->wrap(\&Math::Trig::acos,$safeeval,'&acos');
  194:  $safehole->wrap(\&Math::Trig::atan,$safeeval,'&atan');
  195:  $safehole->wrap(\&Math::Trig::asinh,$safeeval,'&asinh');
  196:  $safehole->wrap(\&Math::Trig::acosh,$safeeval,'&acosh');
  197:  $safehole->wrap(\&Math::Trig::atanh,$safeeval,'&atanh');
  198:  $safehole->wrap(\&Math::Trig::atan2,$safeeval,'&atan2');
  199:  $safehole->wrap(\&Math::Trig::csc,$safeeval,'&csc');
  200:  $safehole->wrap(\&Math::Trig::cosec,$safeeval,'&cosec');
  201:  $safehole->wrap(\&Math::Trig::sec,$safeeval,'&sec');
  202:  $safehole->wrap(\&Math::Trig::cot,$safeeval,'&cot');
  203:  $safehole->wrap(\&Math::Trig::cotan,$safeeval,'&cotan');
  204:  $safehole->wrap(\&Math::Trig::csch,$safeeval,'&csch');
  205:  $safehole->wrap(\&Math::Trig::cosech,$safeeval,'&cosech');
  206:  $safehole->wrap(\&Math::Trig::sech,$safeeval,'&sech');
  207:  $safehole->wrap(\&Math::Trig::coth,$safeeval,'&coth');
  208:  $safehole->wrap(\&Math::Trig::cotanh,$safeeval,'&cptanh');
  209:  $safehole->wrap(\&Math::Trig::deg2rad,$safeeval,'&deg2rad');
  210:  $safehole->wrap(\&Math::Trig::deg2grad,$safeeval,'&deg2grad');
  211:  $safehole->wrap(\&Math::Trig::grad2deg,$safeeval,'&grad2deg');
  212:  $safehole->wrap(\&Math::Trig::grad2rad,$safeeval,'&grad2rad');
  213:  $safehole->wrap(\&Math::Trig::rad2deg,$safeeval,'&rad2deg');
  214:  $safehole->wrap(\&Math::Trig::rad2grad,$safeeval,'&rad2grad');
  215:  $safehole->wrap(\&Math::Trig::pi,$safeeval,'&pi');
  216:  $safehole->wrap(\&Math::Cephes::erf,$safeeval,'&erf');
  217:  $safehole->wrap(\&Math::Cephes::erfc,$safeeval,'&erfc');
  218:  $safehole->wrap(\&Math::Cephes::j0,$safeeval,'&j0');
  219:  $safehole->wrap(\&Math::Cephes::j1,$safeeval,'&j1');
  220:  $safehole->wrap(\&Math::Cephes::jn,$safeeval,'&jn');
  221:  $safehole->wrap(\&Math::Cephes::jv,$safeeval,'&jv');
  222:  $safehole->wrap(\&Math::Cephes::y0,$safeeval,'&y0');
  223:  $safehole->wrap(\&Math::Cephes::y1,$safeeval,'&y1');
  224:  $safehole->wrap(\&Math::Cephes::yn,$safeeval,'&yn');
  225:  $safehole->wrap(\&Math::Cephes::yv,$safeeval,'&yv');
  226: 
  227: #need to inspect this class of ops
  228: # $safeeval->deny(":base_orig");
  229:  $safeinit .= ';$external::target='.$target.';';
  230:  $safeinit .= ';$external::randomseed='.&Apache::lonnet::rndseed().';';
  231:  &Apache::run::run($safeinit,$safeeval);
  232: #-------------------- Redefinition of the target in the case of compound target
  233: 
  234:  ($target, my @tenta) = split('&&',$target);
  235: 
  236:  my @stack = (); 
  237:  my @parstack = ();
  238:  &initdepth;
  239:  my $token;
  240:  while ( $#pars > -1 ) {
  241:    while ($token = $pars[$#pars]->get_token) {
  242:      if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
  243:        if ($metamode<1) { $result=$token->[1]; }
  244:      } elsif ($token->[0] eq 'PI') {
  245:        if ($metamode<1) { $result=$token->[2]; }
  246:      } elsif ($token->[0] eq 'S') {
  247:        # add tag to stack 	    
  248:        push (@stack,$token->[1]);
  249:        # add parameters list to another stack
  250:        push (@parstack,&parstring($token));
  251:        &increasedepth($token);       
  252:        if (exists $style_for_target{$token->[1]}) {
  253: 	 if ($Apache::lonxml::redirection) {
  254: 	   $Apache::lonxml::outputstack['-1'] .=  
  255: 	     &recurse($style_for_target{$token->[1]},$target,$safeeval,
  256: 		      \%style_for_target,@parstack);
  257: 	 } else {
  258: 	   $finaloutput .= &recurse($style_for_target{$token->[1]},$target,
  259: 				    $safeeval,\%style_for_target,@parstack);
  260: 	 }
  261:        } else {
  262: 	 $result = &callsub("start_$token->[1]", $target, $token,\@parstack,
  263: 			    \@pars, $safeeval, \%style_for_target);
  264:        }              
  265:      } elsif ($token->[0] eq 'E')  {
  266:        #clear out any tags that didn't end
  267:        while ($token->[1] ne $stack[$#stack] && ($#stack > -1)) {
  268: 	 &Apache::lonxml::warning("Unbalanced tags in resource $stack['-1']");
  269: 	 pop @stack;pop @parstack;&decreasedepth($token);
  270:        }
  271:        
  272:        if (exists $style_for_target{'/'."$token->[1]"}) {
  273: 	 if ($Apache::lonxml::redirection) {
  274: 	   $Apache::lonxml::outputstack['-1'] .=  
  275: 	     &recurse($style_for_target{'/'."$token->[1]"},
  276: 		      $target,$safeeval,\%style_for_target,@parstack);
  277: 	 } else {
  278: 	   $finaloutput .= &recurse($style_for_target{'/'."$token->[1]"},
  279: 				    $target,$safeeval,\%style_for_target,
  280: 				    @parstack);
  281: 	 }
  282: 
  283:        } else {
  284: 	 $result = &callsub("end_$token->[1]", $target, $token, \@parstack,
  285: 			    \@pars,$safeeval, \%style_for_target);
  286:        }
  287:      } else {
  288:        &Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
  289:      }
  290:      #evaluate variable refs in result
  291:      if ($result ne "") {
  292:        if ( $#parstack > -1 ) {
  293: 	 if ($Apache::lonxml::redirection) {
  294: 	   $Apache::lonxml::outputstack['-1'] .= 
  295: 	     &Apache::run::evaluate($result,$safeeval,$parstack[$#parstack]);
  296: 	 } else {
  297: 	   $finaloutput .= &Apache::run::evaluate($result,$safeeval,
  298: 						  $parstack[$#parstack]);
  299: 	 }
  300:        } else {
  301: 	 $finaloutput .= &Apache::run::evaluate($result,$safeeval,'');
  302:        }
  303:        $result = '';
  304:      } 
  305:      if ($token->[0] eq 'E') { 
  306:        pop @stack;pop @parstack;&decreasedepth($token);
  307:      }
  308:    }
  309:    pop @pars;
  310:    pop @Apache::lonxml::pwd;
  311:  }
  312: 
  313: # if ($target eq 'meta') {
  314: #   $finaloutput.=&endredirection;
  315: # }
  316: 
  317:   if (($ENV{'QUERY_STRING'}) && ($target eq 'web')) {
  318:       $finaloutput=&afterburn($finaloutput);
  319:   }
  320: 
  321:  return $finaloutput;
  322: }
  323: 
  324: 
  325: sub recurse {
  326:   
  327:   my @innerstack = (); 
  328:   my @innerparstack = ();
  329:   my ($newarg,$target,$safeeval,$style_for_target,@parstack) = @_;
  330:   my @pat = ();
  331:   &newparser(\@pat,\$newarg);
  332:   my $tokenpat;
  333:   my $partstring = '';
  334:   my $output='';
  335:   my $decls='';
  336:   while ( $#pat > -1 ) {
  337:     while  ($tokenpat = $pat[$#pat]->get_token) {
  338:       if (($tokenpat->[0] eq 'T') || ($tokenpat->[0] eq 'C') || ($tokenpat->[0] eq 'D') ) {
  339: 	if ($metamode<1) { $partstring=$tokenpat->[1]; }
  340:       } elsif ($tokenpat->[0] eq 'PI') {
  341: 	if ($metamode<1) { $partstring=$tokenpat->[2]; }
  342:       } elsif ($tokenpat->[0] eq 'S') {
  343: 	push (@innerstack,$tokenpat->[1]);
  344: 	push (@innerparstack,&parstring($tokenpat));
  345: 	&increasedepth($tokenpat);
  346: 	$partstring = &callsub("start_$tokenpat->[1]", 
  347: 			       $target, $tokenpat, \@innerparstack,
  348: 			       \@pat, $safeeval, $style_for_target);
  349:       } elsif ($tokenpat->[0] eq 'E') {
  350: 	#clear out any tags that didn't end
  351: 	while ($tokenpat->[1] ne $innerstack[$#innerstack] 
  352: 	       && ($#innerstack > -1)) {
  353: 	  &Apache::lonxml::warning("Unbalanced tags in resource $innerstack['-1']");
  354: 	  pop @innerstack;pop @innerparstack;&decreasedepth($tokenpat);
  355: 	}
  356: 	$partstring = &callsub("end_$tokenpat->[1]",
  357: 			       $target, $tokenpat, \@innerparstack,
  358: 			       \@pat, $safeeval, $style_for_target);
  359:       } else {
  360: 	&Apache::lonxml::error("Unknown token event :$tokenpat->[0]:$tokenpat->[1]:");
  361:       }
  362:       #pass both the variable to the style tag, and the tag we 
  363:       #are processing inside the <definedtag>
  364:       if ( $partstring ne "" ) {
  365: 	if ( $#parstack > -1 ) { 
  366: 	  if ( $#innerparstack > -1 ) { 
  367: 	    $decls= $parstack[$#parstack].$innerparstack[$#innerparstack];
  368: 	  } else {
  369: 	    $decls= $parstack[$#parstack];
  370: 	  }
  371: 	} else {
  372: 	  if ( $#innerparstack > -1 ) { 
  373: 	    $decls=$innerparstack[$#innerparstack];
  374: 	  } else {
  375: 	    $decls='';
  376: 	  }
  377: 	}
  378: 	$output .= &Apache::run::evaluate($partstring,$safeeval,$decls);
  379: 	$partstring = '';
  380:       }
  381:       if ($tokenpat->[0] eq 'E') { pop @innerstack;pop @innerparstack;
  382: 				 &decreasedepth($tokenpat);}
  383:     }
  384:     pop @pat;
  385:     pop @Apache::lonxml::pwd;
  386:   }
  387:   return $output;
  388: }
  389: 
  390: sub callsub {
  391:   my ($sub,$target,$token,$parstack,$parser,$safeeval,$style)=@_;
  392:   my $currentstring='';
  393:   my $nodefault;
  394:   {
  395:     my $sub1;
  396:     no strict 'refs';
  397:     if ($target eq 'edit' && $token->[0] eq 'S') {
  398:       $currentstring = &Apache::edit::tag_start($target,$token,$parstack,$parser,
  399: 						$safeeval,$style);
  400:     }
  401:     my $tag=$token->[1];
  402:     my $space=$Apache::lonxml::alltags{$tag};
  403:     if (!$space) {
  404: 	$tag=~tr/A-Z/a-z/;
  405: 	$sub=~tr/A-Z/a-z/;
  406: 	$space=$Apache::lonxml::alltags{$tag}
  407:     }
  408:     if ($space) {
  409:       #&Apache::lonxml::debug("Calling sub $sub in $space $metamode<br />\n");
  410:       $sub1="$space\:\:$sub";
  411:       $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
  412:       ($currentstring,$nodefault) = &$sub1($target,$token,$parstack,$parser,
  413: 					   $safeeval,$style);
  414:     } else {
  415:       #&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode<br />\n");
  416:       if ($metamode <1) {
  417: 	if (defined($token->[4]) && ($metamode < 1)) {
  418: 	  $currentstring = $token->[4];
  419: 	} else {
  420: 	  $currentstring = $token->[2];
  421: 	}
  422:       }
  423:     }
  424:     &Apache::lonxml::debug("nodefalt:$nodefault:");
  425:     if ($currentstring eq '' && $nodefault eq '') {
  426:       if ($target eq 'edit') {
  427: 	&Apache::lonxml::debug("doing default edit for $token->[1]");
  428: 	if ($token->[0] eq 'S') {
  429: 	  $currentstring = &Apache::edit::tag_start($target,$token);
  430: 	} elsif ($token->[0] eq 'E') {
  431: 	  $currentstring = &Apache::edit::tag_end($target,$token);
  432: 	}
  433:       } elsif ($target eq 'modified') {
  434: 	if ($token->[0] eq 'S') {
  435: 	  $currentstring = $token->[4];
  436: 	  $currentstring.=&Apache::edit::handle_insert();
  437: 	} else {
  438: 	  $currentstring = $token->[2];
  439: 	}
  440:       }
  441:     }
  442:     use strict 'refs';
  443:   }
  444:   return $currentstring;
  445: }
  446: 
  447: sub startredirection {
  448:   $Apache::lonxml::redirection++;
  449:   push (@Apache::lonxml::outputstack, '');
  450: }
  451: 
  452: sub endredirection {
  453:   if (!$Apache::lonxml::redirection) {
  454:     &Apache::lonxml::error("Endredirection was called, before a startredirection, perhaps you have unbalanced tags. Some debuging information:".join ":",caller);
  455:     return '';
  456:   }
  457:   $Apache::lonxml::redirection--;
  458:   pop @Apache::lonxml::outputstack;
  459: }
  460: 
  461: sub initdepth {
  462:   @Apache::lonxml::depthcounter=();
  463:   $Apache::lonxml::depth=-1;
  464:   $Apache::lonxml::olddepth=-1;
  465: }
  466: 
  467: sub increasedepth {
  468:   my ($token) = @_;
  469:   $Apache::lonxml::depth++;
  470:   $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
  471:   if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
  472:     $Apache::lonxml::olddepth=$Apache::lonxml::depth;
  473:   }
  474:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
  475:   &Apache::lonxml::debug("s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n");
  476: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
  477: }
  478: 
  479: sub decreasedepth {
  480:   my ($token) = @_;
  481:   $Apache::lonxml::depth--;
  482:   if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
  483:     $#Apache::lonxml::depthcounter--;
  484:     $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
  485:   }
  486:   if (  $Apache::lonxml::depth < -1) {
  487:     &Apache::lonxml::warning("Unbalanced tags in resource");   
  488:     $Apache::lonxml::depth='-1';
  489:   }
  490:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
  491:   &Apache::lonxml::debug("e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n");
  492: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
  493: }
  494: 
  495: sub get_all_text {
  496: 
  497:  my($tag,$pars)= @_;
  498:  my $depth=0;
  499:  my $token;
  500:  my $result='';
  501:  if ( $tag =~ m:^/: ) { 
  502:    my $tag=substr($tag,1); 
  503: #   &Apache::lonxml::debug("have:$tag:");
  504:    while (($depth >=0) && ($token = $pars->get_token)) {
  505: #     &Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]");
  506:      if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
  507:        $result.=$token->[1];
  508:      } elsif ($token->[0] eq 'PI') {
  509:        $result.=$token->[2];
  510:      } elsif ($token->[0] eq 'S') {
  511:        if ($token->[1] eq $tag) { $depth++; }
  512:        $result.=$token->[4];
  513:      } elsif ($token->[0] eq 'E')  {
  514:        if ( $token->[1] eq $tag) { $depth--; }
  515:        #skip sending back the last end tag
  516:        if ($depth > -1) { $result.=$token->[2]; } else {
  517: 	 $pars->unget_token($token);
  518:        }
  519:      }
  520:    }
  521:  } else {
  522:    while ($token = $pars->get_token) {
  523: #     &Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
  524:      if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
  525:        $result.=$token->[1];
  526:      } elsif ($token->[0] eq 'PI') {
  527:        $result.=$token->[2];
  528:      } elsif ($token->[0] eq 'S') {
  529:        if ( $token->[1] eq $tag) { 
  530: 	 $pars->unget_token($token); last;
  531:        } else {
  532: 	 $result.=$token->[4];
  533:        }
  534:      } elsif ($token->[0] eq 'E')  {
  535:        $result.=$token->[2];
  536:      }
  537:    }
  538:  }
  539: # &Apache::lonxml::debug("Exit:$result:");
  540:  return $result
  541: }
  542: 
  543: sub newparser {
  544:   my ($parser,$contentref,$dir) = @_;
  545:   push (@$parser,HTML::TokeParser->new($contentref));
  546:   $$parser['-1']->xml_mode('1');
  547:   if ( $dir eq '' ) {
  548:     push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
  549:   } else {
  550:     push (@Apache::lonxml::pwd, $dir);
  551:   } 
  552: #  &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
  553: #  &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
  554: }
  555: 
  556: sub parstring {
  557:   my ($token) = @_;
  558:   my $temp='';
  559:   map {
  560:     unless ($_=~/\W/) {
  561:       my $val=$token->[2]->{$_};
  562:       $val =~ s/([\%\@\\])/\\$1/g;
  563:       #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
  564:       $temp .= "my \$$_=\"$val\";"
  565:     }
  566:   } @{$token->[3]};
  567:   return $temp;
  568: }
  569: 
  570: sub writeallows {
  571:     my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
  572:     my $thisdir=$thisurl;
  573:     $thisdir=~s/\/[^\/]+$//;
  574:     my %httpref=();
  575:     map {
  576:        $httpref{'httpref.'.
  577:  	        &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl;              } @extlinks;
  578:     &Apache::lonnet::appenv(%httpref);
  579: }
  580: 
  581: #
  582: # Afterburner handles anchors, highlights and links
  583: #
  584: sub afterburn {
  585:     my $result=shift;
  586:     map {
  587:        my ($name, $value) = split(/=/,$_);
  588:        $value =~ tr/+/ /;
  589:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  590:        if (($name eq 'highlight')||($name eq 'anchor')||($name eq 'link')) {
  591:            unless ($ENV{'form.'.$name}) {
  592:               $ENV{'form.'.$name}=$value;
  593: 	   }
  594:        }
  595:     } (split(/&/,$ENV{'QUERY_STRING'}));
  596:     if ($ENV{'form.highlight'}) {
  597:         map {
  598:            my $anchorname=$_;
  599: 	   my $matchthis=$anchorname;
  600:            $matchthis=~s/\_+/\\s\+/g;
  601:            $result=~s/($matchthis)/\<font color=\"red\"\>$1\<\/font\>/gs;
  602:        } split(/\,/,$ENV{'form.highlight'});
  603:     }
  604:     if ($ENV{'form.link'}) {
  605:         map {
  606:            my ($anchorname,$linkurl)=split(/\>/,$_);
  607: 	   my $matchthis=$anchorname;
  608:            $matchthis=~s/\_+/\\s\+/g;
  609:            $result=~s/($matchthis)/\<a href=\"$linkurl\"\>$1\<\/a\>/gs;
  610:        } split(/\,/,$ENV{'form.link'});
  611:     }
  612:     if ($ENV{'form.anchor'}) {
  613:         my $anchorname=$ENV{'form.anchor'};
  614: 	my $matchthis=$anchorname;
  615:         $matchthis=~s/\_+/\\s\+/g;
  616:         $result=~s/($matchthis)/\<a name=\"$anchorname\"\>$1\<\/a\>/s;
  617:         $result.=(<<"ENDSCRIPT");
  618: <script>
  619:     document.location.hash='$anchorname';
  620: </script>
  621: ENDSCRIPT
  622:     }
  623:     return $result;
  624: }
  625: 
  626: sub storefile {
  627:     my ($file,$contents)=@_;
  628:     if (my $fh=Apache::File->new('>'.$file)) {
  629: 	print $fh $contents;
  630:         $fh->close();
  631:     }
  632: }
  633: 
  634: sub inserteditinfo {
  635:       my ($result,$filecontents)=@_;
  636:       unless ($filecontents) {
  637: 	  $filecontents=(<<SIMPLECONTENT);
  638: <html>
  639: <head>
  640: <title>
  641:                            Title of Document Goes Here
  642: </title>
  643: </head>
  644: <body bgcolor="#FFFFFF">
  645: 
  646:                            Body of Document Goes Here
  647: 
  648: </body>
  649: </html>
  650: SIMPLECONTENT
  651:       }
  652:       my $editheader='<a href="#editsection">Edit below</a><hr />';
  653:       my $editfooter=(<<ENDFOOTER);
  654: <hr />
  655: <a name="editsection" />
  656: <form method="post">
  657: <textarea cols="80" rows="40" name="filecont">$filecontents</textarea>
  658: <br />
  659: <input type="submit" name="savethisfile" value="Save this file" />
  660: </form>
  661: ENDFOOTER
  662:       $result=~s/(\<body[^\>]*\>)/$1$editheader/is;
  663:       $result=~s/(\<\/body\>)/$editfooter/is;
  664:       return $result;
  665: }
  666: 
  667: sub handler {
  668:   my $request=shift;
  669: 
  670:   my $target='web';
  671: 
  672:   $Apache::lonxml::debug=0;
  673: 
  674:   if ($ENV{'browser.mathml'}) {
  675:     $request->content_type('text/xml');
  676:   } else {
  677:     $request->content_type('text/html');
  678:   }
  679:   
  680:   $request->send_http_header;
  681:   
  682:   return OK if $request->header_only;
  683: 
  684: 
  685:   my $file=&Apache::lonnet::filelocation("",$request->uri);
  686: #
  687: # Edit action? Save file.
  688: #
  689:   unless ($ENV{'request.state'} eq 'published') {
  690:       if ($ENV{'form.savethisfile'}) {
  691: 	  &storefile($file,$ENV{'form.filecont'});
  692:       }
  693:   }
  694:   my %mystyle;
  695:   my $result = ''; 
  696:   my $filecontents=&Apache::lonnet::getfile($file);
  697:   if ($filecontents == -1) {
  698:     $result=(<<ENDNOTFOUND);
  699: <html>
  700: <head>
  701: <title>File not found</title>
  702: </head>
  703: <body bgcolor="#FFFFFF">
  704: <b>File not found: $file</b>
  705: </body>
  706: </html>
  707: ENDNOTFOUND
  708:     $filecontents='';
  709:   } else {
  710:     $result = &Apache::lonxml::xmlparse($target,$filecontents,'',%mystyle);
  711:   }
  712: 
  713: #
  714: # Edit action? Insert editing commands
  715: #
  716:   unless ($ENV{'request.state'} eq 'published') {
  717:       $result=&inserteditinfo($result,$filecontents);
  718:   }
  719: 
  720:   $request->print($result);
  721: 
  722:   writeallows($request->uri);
  723:   return OK;
  724: }
  725:  
  726: sub debug {
  727:   if ($Apache::lonxml::debug eq 1) {
  728:     print "DEBUG:".$_[0]."<br />\n";
  729:   }
  730: }
  731: 
  732: sub error {
  733:   if (($Apache::lonxml::debug eq 1) || ($ENV{'request.state'} eq 'construct') ) {
  734:     print "<b>ERROR:</b>".$_[0]."<br />\n";
  735:   } else {
  736:     print "<b>An Error occured while processing this resource. The instructor has been notified.</b> <br />";
  737:     #notify author
  738:     &Apache::lonmsg::author_res_msg($ENV{'request.filename'},$_[0]);
  739:     #notify course
  740:     if ( $ENV{'request.course.id'} ) {
  741:       my $users=$ENV{'course.'.$ENV{'request.course.id'}.'.comment.email'};
  742:       foreach my $user (split /\,/, $users) {
  743: 	($user,my $domain) = split /:/, $user;
  744: 	&Apache::lonmsg::user_normal_msg($user,$domain,"Error in $ENV{'request.filename'}",$_[0]);
  745:       }
  746:     }
  747: 
  748:     #FIXME probably shouldn't have me get everything forever.
  749:     &Apache::lonmsg::user_normal_msg('albertel','msu',"Error in $ENV{'request.filename'}",$_[0]);
  750:     #&Apache::lonmsg::user_normal_msg('albertel','103',"Error in $ENV{'request.filename'}",$_[0]);
  751:   }
  752: }
  753: 
  754: sub warning {
  755:   if ($ENV{'request.state'} eq 'construct') {
  756:     print "<b>W</b>ARNING<b>:</b>".$_[0]."<br />\n";
  757:   }
  758: }
  759: 
  760: sub register_insert {
  761:   my @data = split /\n/, &Apache::lonnet::getfile('/home/httpd/lonTabs/insertlist.tab');
  762:   my $i;
  763:   my $tagnum=0;
  764:   my @order;
  765:   for ($i=0;$i < $#data; $i++) {
  766:     my $line = $data[$i];
  767:     if ( $line =~ /^\#/ || $line =~ /^\s*\n/) { next; }
  768:     if ( $line =~ /TABLE/ ) { last; }
  769:     my ($tag,$descrip,$function,$show) = split(/,/, $line);
  770:     $insertlist{"$tagnum.tag"} = $tag;
  771:     $insertlist{"$tagnum.description"} = $descrip;
  772:     $insertlist{"$tagnum.function"} = $function;
  773:     $insertlist{"$tagnum.show"}= $show;
  774:     $tagnum++;
  775:   }
  776:   $i++; #skipping TABLE line
  777:   $tagnum = 0;
  778:   for (;$i < $#data;$i++) {
  779:     my $line = $data[$i];
  780:     my ($mnemonic,@which) = split(/ +/,$line);
  781:     my $tag = $insertlist{"$tagnum.tag"};
  782:     for (my $j=0;$j <$#which;$j++) {
  783:       if ( $which[$j] eq 'Y' ) {
  784: 	if ($insertlist{"$j.show"} ne 'no') {
  785: 	  push(@{ $insertlist{"$tag.which"} },$j);
  786: 	}
  787:       }
  788:     }
  789:     $tagnum++;
  790:   }
  791: }
  792: 1;
  793: __END__
  794: 
  795: 

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