File:  [LON-CAPA] / loncom / xml / lonxml.pm
Revision 1.85: download - view: text, annotated - select for diffs
Sat Jun 2 19:55:46 2001 UTC (23 years ago) by www
Branches: MAIN
CVS tags: HEAD
Get rid of menu checking on itself

    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 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:   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:           menu.clearbut(3,1);
  111:           menu.switchbutton
  112:        (8,1,'eval.gif','evaluate','this','gopost("/adm/evaluate",currentURL)');
  113:           menu.switchbutton
  114:     (8,2,'fdbk.gif','feedback','on this','gopost("/adm/feedback",currentURL)');
  115:           menu.switchbutton
  116:      (8,3,'prt.gif','prepare','printout','gopost("/adm/printout",currentURL)');
  117:           menu.switchbutton
  118:        (2,1,'back.gif','backward','','gopost("/adm/flip","back:"+currentURL)');
  119:           menu.switchbutton
  120:      (2,3,'forw.gif','forward','','gopost("/adm/flip","forward:"+currentURL)');
  121:        }
  122:     }
  123:   
  124:     function LONCAPAstale() {
  125:        if (window.location.pathname!="/res/adm/pages/menu.html") {
  126: 	  menu=window.open("","LONCAPAmenu");
  127:           menu.currentStale=1;
  128:           menu.clearbut(2,1);
  129:           menu.clearbut(2,3);
  130:           menu.clearbut(8,1);
  131:           menu.clearbut(8,2);
  132:           menu.clearbut(8,3);
  133:           menu.switchbutton
  134:             (3,1,'reload.gif','return','location','go(currentURL)');
  135:        }
  136:     }
  137: // END LON-CAPA Internal
  138: </script>
  139: ENDSCRIPT
  140: }
  141: 
  142: sub loadevents() {
  143:     return 'LONCAPAreg();';
  144: }
  145: 
  146: sub unloadevents() {
  147:     return 'LONCAPAstale();';
  148: }
  149: 
  150: sub printalltags {
  151:   my $temp;
  152:   foreach $temp (sort keys %Apache::lonxml::alltags) {
  153:     &Apache::lonxml::debug("$temp -- $Apache::lonxml::alltags{$temp}");
  154:   }
  155: }
  156: 
  157: sub xmlparse {
  158: 
  159:  my ($target,$content_file_string,$safeinit,%style_for_target) = @_;
  160:  if ($target eq 'meta') {
  161:    $Apache::lonxml::redirection = 0;
  162:    $Apache::lonxml::metamode = 1;
  163:    $Apache::lonxml::evaluate = 1;
  164:    $Apache::lonxml::import = 0;
  165:  } elsif ($target eq 'grade') {
  166:    &startredirection;
  167:    $Apache::lonxml::metamode = 0;
  168:    $Apache::lonxml::evaluate = 1;
  169:    $Apache::lonxml::import = 1;
  170:  } elsif ($target eq 'modified') {
  171:    $Apache::lonxml::redirection = 0;
  172:    $Apache::lonxml::metamode = 0;
  173:    $Apache::lonxml::evaluate = 0;
  174:    $Apache::lonxml::import = 0;
  175:  } else {
  176:    $Apache::lonxml::redirection = 0;
  177:    $Apache::lonxml::metamode = 0;
  178:    $Apache::lonxml::evaluate = 1;
  179:    $Apache::lonxml::import = 1;
  180:  }
  181:  #&printalltags();
  182:  my @pars = ();
  183:  @Apache::lonxml::pwd=();
  184:  my $pwd=$ENV{'request.filename'};
  185:  $pwd =~ s:/[^/]*$::;
  186:  &newparser(\@pars,\$content_file_string,$pwd);
  187:  my $currentstring = '';
  188:  my $finaloutput = ''; 
  189:  my $newarg = '';
  190:  my $result;
  191: 
  192:  my $safeeval = new Safe;
  193:  my $safehole = new Safe::Hole;
  194:  &init_safespace($target,$safeeval,$safehole,$safeinit);
  195: #-------------------- Redefinition of the target in the case of compound target
  196: 
  197:  ($target, my @tenta) = split('&&',$target);
  198: 
  199:  my @stack = (); 
  200:  my @parstack = ();
  201:  &initdepth;
  202:  my $token;
  203:  while ( $#pars > -1 ) {
  204:    while ($token = $pars[$#pars]->get_token) {
  205:      if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
  206:        if ($metamode<1) { $result=$token->[1]; }
  207:      } elsif ($token->[0] eq 'PI') {
  208:        if ($metamode<1) { $result=$token->[2]; }
  209:      } elsif ($token->[0] eq 'S') {
  210:        # add tag to stack 	    
  211:        push (@stack,$token->[1]);
  212:        # add parameters list to another stack
  213:        push (@parstack,&parstring($token));
  214:        &increasedepth($token);       
  215:        if (exists $style_for_target{$token->[1]}) {
  216: 	 if ($Apache::lonxml::redirection) {
  217: 	   $Apache::lonxml::outputstack['-1'] .=  
  218: 	     &recurse($style_for_target{$token->[1]},$target,$safeeval,
  219: 		      \%style_for_target,@parstack);
  220: 	 } else {
  221: 	   $finaloutput .= &recurse($style_for_target{$token->[1]},$target,
  222: 				    $safeeval,\%style_for_target,@parstack);
  223: 	 }
  224:        } else {
  225: 	 $result = &callsub("start_$token->[1]", $target, $token, \@stack,
  226: 			    \@parstack, \@pars, $safeeval, \%style_for_target);
  227:        }              
  228:      } elsif ($token->[0] eq 'E')  {
  229:        #clear out any tags that didn't end
  230:        while ($token->[1] ne $stack[$#stack] && ($#stack > -1)) {
  231: 	 &Apache::lonxml::warning("Unbalanced tags in resource $stack['-1']");
  232: 	 pop @stack;pop @parstack;&decreasedepth($token);
  233:        }
  234:        
  235:        if (exists $style_for_target{'/'."$token->[1]"}) {
  236: 	 if ($Apache::lonxml::redirection) {
  237: 	   $Apache::lonxml::outputstack['-1'] .=  
  238: 	     &recurse($style_for_target{'/'."$token->[1]"},
  239: 		      $target,$safeeval,\%style_for_target,@parstack);
  240: 	 } else {
  241: 	   $finaloutput .= &recurse($style_for_target{'/'."$token->[1]"},
  242: 				    $target,$safeeval,\%style_for_target,
  243: 				    @parstack);
  244: 	 }
  245: 
  246:        } else {
  247: 	 $result = &callsub("end_$token->[1]", $target, $token, \@stack, 
  248: 			    \@parstack, \@pars,$safeeval, \%style_for_target);
  249:        }
  250:      } else {
  251:        &Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
  252:      }
  253:      #evaluate variable refs in result
  254:      if ($result ne "") {
  255:        if ( $#parstack > -1 ) {
  256: 	 if ($Apache::lonxml::redirection) {
  257: 	   $Apache::lonxml::outputstack['-1'] .= 
  258: 	     &Apache::run::evaluate($result,$safeeval,$parstack[$#parstack]);
  259: 	 } else {
  260: 	   $finaloutput .= &Apache::run::evaluate($result,$safeeval,
  261: 						  $parstack[$#parstack]);
  262: 	 }
  263:        } else {
  264: 	 $finaloutput .= &Apache::run::evaluate($result,$safeeval,'');
  265:        }
  266:        $result = '';
  267:      } 
  268:      if ($token->[0] eq 'E') { 
  269:        pop @stack;pop @parstack;&decreasedepth($token);
  270:      }
  271:    }
  272:    pop @pars;
  273:    pop @Apache::lonxml::pwd;
  274:  }
  275: 
  276: # if ($target eq 'meta') {
  277: #   $finaloutput.=&endredirection;
  278: # }
  279: 
  280:   if (($ENV{'QUERY_STRING'}) && ($target eq 'web')) {
  281:       $finaloutput=&afterburn($finaloutput);
  282:   }
  283: 
  284:  return $finaloutput;
  285: }
  286: 
  287: 
  288: sub recurse {
  289:   
  290:   my @innerstack = (); 
  291:   my @innerparstack = ();
  292:   my ($newarg,$target,$safeeval,$style_for_target,@parstack) = @_;
  293:   my @pat = ();
  294:   &newparser(\@pat,\$newarg);
  295:   my $tokenpat;
  296:   my $partstring = '';
  297:   my $output='';
  298:   my $decls='';
  299:   while ( $#pat > -1 ) {
  300:     while  ($tokenpat = $pat[$#pat]->get_token) {
  301:       if (($tokenpat->[0] eq 'T') || ($tokenpat->[0] eq 'C') || ($tokenpat->[0] eq 'D') ) {
  302: 	if ($metamode<1) { $partstring=$tokenpat->[1]; }
  303:       } elsif ($tokenpat->[0] eq 'PI') {
  304: 	if ($metamode<1) { $partstring=$tokenpat->[2]; }
  305:       } elsif ($tokenpat->[0] eq 'S') {
  306: 	push (@innerstack,$tokenpat->[1]);
  307: 	push (@innerparstack,&parstring($tokenpat));
  308: 	&increasedepth($tokenpat);
  309: 	$partstring = &callsub("start_$tokenpat->[1]", $target, $tokenpat,
  310: 			       \@innerstack, \@innerparstack, \@pat,
  311: 			       $safeeval, $style_for_target);
  312:       } elsif ($tokenpat->[0] eq 'E') {
  313: 	#clear out any tags that didn't end
  314: 	while ($tokenpat->[1] ne $innerstack[$#innerstack] 
  315: 	       && ($#innerstack > -1)) {
  316: 	  &Apache::lonxml::warning("Unbalanced tags in resource $innerstack['-1']");
  317: 	  pop @innerstack;pop @innerparstack;&decreasedepth($tokenpat);
  318: 	}
  319: 	$partstring = &callsub("end_$tokenpat->[1]", $target, $tokenpat,
  320: 			       \@innerstack, \@innerparstack, \@pat,
  321: 			       $safeeval, $style_for_target);
  322:       } else {
  323: 	&Apache::lonxml::error("Unknown token event :$tokenpat->[0]:$tokenpat->[1]:");
  324:       }
  325:       #pass both the variable to the style tag, and the tag we 
  326:       #are processing inside the <definedtag>
  327:       if ( $partstring ne "" ) {
  328: 	if ( $#parstack > -1 ) { 
  329: 	  if ( $#innerparstack > -1 ) { 
  330: 	    $decls= $parstack[$#parstack].$innerparstack[$#innerparstack];
  331: 	  } else {
  332: 	    $decls= $parstack[$#parstack];
  333: 	  }
  334: 	} else {
  335: 	  if ( $#innerparstack > -1 ) { 
  336: 	    $decls=$innerparstack[$#innerparstack];
  337: 	  } else {
  338: 	    $decls='';
  339: 	  }
  340: 	}
  341: 	$output .= &Apache::run::evaluate($partstring,$safeeval,$decls);
  342: 	$partstring = '';
  343:       }
  344:       if ($tokenpat->[0] eq 'E') { pop @innerstack;pop @innerparstack;
  345: 				 &decreasedepth($tokenpat);}
  346:     }
  347:     pop @pat;
  348:     pop @Apache::lonxml::pwd;
  349:   }
  350:   return $output;
  351: }
  352: 
  353: sub callsub {
  354:   my ($sub,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  355:   my $currentstring='';
  356:   my $nodefault;
  357:   {
  358:     my $sub1;
  359:     no strict 'refs';
  360:     if ($target eq 'edit' && $token->[0] eq 'S') {
  361:       $currentstring = &Apache::edit::tag_start($target,$token,$tagstack,
  362: 						$parstack,$parser,
  363: 						$safeeval,$style);
  364:     }
  365:     my $tag=$token->[1];
  366:     my $space=$Apache::lonxml::alltags{$tag};
  367:     if (!$space) {
  368: 	$tag=~tr/A-Z/a-z/;
  369: 	$sub=~tr/A-Z/a-z/;
  370: 	$space=$Apache::lonxml::alltags{$tag}
  371:     }
  372:     if ($space) {
  373:       #&Apache::lonxml::debug("Calling sub $sub in $space $metamode<br />\n");
  374:       $sub1="$space\:\:$sub";
  375:       $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
  376:       ($currentstring,$nodefault) = &$sub1($target,$token,$tagstack,
  377: 					   $parstack,$parser,$safeeval,
  378: 					   $style);
  379:     } else {
  380:       #&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode<br />\n");
  381:       if ($metamode <1) {
  382: 	if (defined($token->[4]) && ($metamode < 1)) {
  383: 	  $currentstring = $token->[4];
  384: 	} else {
  385: 	  $currentstring = $token->[2];
  386: 	}
  387:       }
  388:     }
  389: #    &Apache::lonxml::debug("nodefalt:$nodefault:");
  390:     if ($currentstring eq '' && $nodefault eq '') {
  391:       if ($target eq 'edit') {
  392: 	&Apache::lonxml::debug("doing default edit for $token->[1]");
  393: 	if ($token->[0] eq 'S') {
  394: 	  $currentstring = &Apache::edit::tag_start($target,$token);
  395: 	} elsif ($token->[0] eq 'E') {
  396: 	  $currentstring = &Apache::edit::tag_end($target,$token);
  397: 	}
  398:       } elsif ($target eq 'modified') {
  399: 	if ($token->[0] eq 'S') {
  400: 	  $currentstring = $token->[4];
  401: 	  $currentstring.=&Apache::edit::handle_insert();
  402: 	} else {
  403: 	  $currentstring = $token->[2];
  404: 	}
  405:       }
  406:     }
  407:     use strict 'refs';
  408:   }
  409:   return $currentstring;
  410: }
  411: 
  412: sub init_safespace {
  413:   my ($target,$safeeval,$safehole,$safeinit) = @_;
  414:   $safeeval->permit("entereval");
  415:   $safeeval->permit(":base_math");
  416:   $safeeval->permit("sort");
  417:   $safeeval->deny(":base_io");
  418:   $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
  419:   
  420:   $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin');
  421:   $safehole->wrap(\&Math::Cephes::acos,$safeeval,'&acos');
  422:   $safehole->wrap(\&Math::Cephes::atan,$safeeval,'&atan');
  423:   $safehole->wrap(\&Math::Cephes::sinh,$safeeval,'&sinh');
  424:   $safehole->wrap(\&Math::Cephes::cosh,$safeeval,'&cosh');
  425:   $safehole->wrap(\&Math::Cephes::tanh,$safeeval,'&tanh');
  426:   $safehole->wrap(\&Math::Cephes::asinh,$safeeval,'&asinh');
  427:   $safehole->wrap(\&Math::Cephes::acosh,$safeeval,'&acosh');
  428:   $safehole->wrap(\&Math::Cephes::atanh,$safeeval,'&atanh');
  429:   $safehole->wrap(\&Math::Cephes::erf,$safeeval,'&erf');
  430:   $safehole->wrap(\&Math::Cephes::erfc,$safeeval,'&erfc');
  431:   $safehole->wrap(\&Math::Cephes::j0,$safeeval,'&j0');
  432:   $safehole->wrap(\&Math::Cephes::j1,$safeeval,'&j1');
  433:   $safehole->wrap(\&Math::Cephes::jn,$safeeval,'&jn');
  434:   $safehole->wrap(\&Math::Cephes::jv,$safeeval,'&jv');
  435:   $safehole->wrap(\&Math::Cephes::y0,$safeeval,'&y0');
  436:   $safehole->wrap(\&Math::Cephes::y1,$safeeval,'&y1');
  437:   $safehole->wrap(\&Math::Cephes::yn,$safeeval,'&yn');
  438:   $safehole->wrap(\&Math::Cephes::yv,$safeeval,'&yv');
  439:   
  440: #need to inspect this class of ops
  441: # $safeeval->deny(":base_orig");
  442:   $safeinit .= ';$external::target='.$target.';';
  443:   $safeinit .= ';$external::randomseed='.&Apache::lonnet::rndseed().';';
  444:   &Apache::run::run($safeinit,$safeeval);
  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 get_param {
  761:   my ($param,$parstack,$safeeval,$context) = @_;
  762:   if ( ! $context ) { $context = -1; }
  763:   my $args ='';
  764:   if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
  765:   return &Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
  766: }
  767: 
  768: sub register_insert {
  769:   my @data = split /\n/, &Apache::lonnet::getfile('/home/httpd/lonTabs/insertlist.tab');
  770:   my $i;
  771:   my $tagnum=0;
  772:   my @order;
  773:   for ($i=0;$i < $#data; $i++) {
  774:     my $line = $data[$i];
  775:     if ( $line =~ /^\#/ || $line =~ /^\s*\n/) { next; }
  776:     if ( $line =~ /TABLE/ ) { last; }
  777:     my ($tag,$descrip,$function,$show) = split(/,/, $line);
  778:     $insertlist{"$tagnum.tag"} = $tag;
  779:     $insertlist{"$tagnum.description"} = $descrip;
  780:     $insertlist{"$tagnum.function"} = $function;
  781:     $insertlist{"$tagnum.show"}= $show;
  782:     $tagnum++;
  783:   }
  784:   $i++; #skipping TABLE line
  785:   $tagnum = 0;
  786:   for (;$i < $#data;$i++) {
  787:     my $line = $data[$i];
  788:     my ($mnemonic,@which) = split(/ +/,$line);
  789:     my $tag = $insertlist{"$tagnum.tag"};
  790:     for (my $j=0;$j <$#which;$j++) {
  791:       if ( $which[$j] eq 'Y' ) {
  792: 	if ($insertlist{"$j.show"} ne 'no') {
  793: 	  push(@{ $insertlist{"$tag.which"} },$j);
  794: 	}
  795:       }
  796:     }
  797:     $tagnum++;
  798:   }
  799: }
  800: 1;
  801: __END__
  802: 
  803: 

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