File:  [LON-CAPA] / loncom / xml / lonxml.pm
Revision 1.84: download - view: text, annotated - select for diffs
Sat Jun 2 03:59:59 2001 UTC (22 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- added new arg $tagstack to all start_ or end_ routines

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

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