File:  [LON-CAPA] / loncom / xml / lonxml.pm
Revision 1.83: download - view: text, annotated - select for diffs
Thu May 31 22:38:36 2001 UTC (23 years ago) by albertel
Branches: MAIN
CVS tags: HEAD
 - adding get_param calls to replace the junk that went before

    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,\@parstack,
  207: 			    \@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, \@parstack,
  229: 			    \@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]", 
  291: 			       $target, $tokenpat, \@innerparstack,
  292: 			       \@pat, $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]",
  301: 			       $target, $tokenpat, \@innerparstack,
  302: 			       \@pat, $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,$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,$parstack,$parser,
  343: 						$safeeval,$style);
  344:     }
  345:     my $tag=$token->[1];
  346:     my $space=$Apache::lonxml::alltags{$tag};
  347:     if (!$space) {
  348: 	$tag=~tr/A-Z/a-z/;
  349: 	$sub=~tr/A-Z/a-z/;
  350: 	$space=$Apache::lonxml::alltags{$tag}
  351:     }
  352:     if ($space) {
  353:       #&Apache::lonxml::debug("Calling sub $sub in $space $metamode<br />\n");
  354:       $sub1="$space\:\:$sub";
  355:       $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
  356:       ($currentstring,$nodefault) = &$sub1($target,$token,$parstack,$parser,
  357: 					   $safeeval,$style);
  358:     } else {
  359:       #&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode<br />\n");
  360:       if ($metamode <1) {
  361: 	if (defined($token->[4]) && ($metamode < 1)) {
  362: 	  $currentstring = $token->[4];
  363: 	} else {
  364: 	  $currentstring = $token->[2];
  365: 	}
  366:       }
  367:     }
  368: #    &Apache::lonxml::debug("nodefalt:$nodefault:");
  369:     if ($currentstring eq '' && $nodefault eq '') {
  370:       if ($target eq 'edit') {
  371: 	&Apache::lonxml::debug("doing default edit for $token->[1]");
  372: 	if ($token->[0] eq 'S') {
  373: 	  $currentstring = &Apache::edit::tag_start($target,$token);
  374: 	} elsif ($token->[0] eq 'E') {
  375: 	  $currentstring = &Apache::edit::tag_end($target,$token);
  376: 	}
  377:       } elsif ($target eq 'modified') {
  378: 	if ($token->[0] eq 'S') {
  379: 	  $currentstring = $token->[4];
  380: 	  $currentstring.=&Apache::edit::handle_insert();
  381: 	} else {
  382: 	  $currentstring = $token->[2];
  383: 	}
  384:       }
  385:     }
  386:     use strict 'refs';
  387:   }
  388:   return $currentstring;
  389: }
  390: 
  391: sub init_safespace {
  392:   my ($target,$safeeval,$safehole,$safeinit) = @_;
  393:   $safeeval->permit("entereval");
  394:   $safeeval->permit(":base_math");
  395:   $safeeval->permit("sort");
  396:   $safeeval->deny(":base_io");
  397:   $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
  398:   
  399:   $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin');
  400:   $safehole->wrap(\&Math::Cephes::acos,$safeeval,'&acos');
  401:   $safehole->wrap(\&Math::Cephes::atan,$safeeval,'&atan');
  402:   $safehole->wrap(\&Math::Cephes::sinh,$safeeval,'&sinh');
  403:   $safehole->wrap(\&Math::Cephes::cosh,$safeeval,'&cosh');
  404:   $safehole->wrap(\&Math::Cephes::tanh,$safeeval,'&tanh');
  405:   $safehole->wrap(\&Math::Cephes::asinh,$safeeval,'&asinh');
  406:   $safehole->wrap(\&Math::Cephes::acosh,$safeeval,'&acosh');
  407:   $safehole->wrap(\&Math::Cephes::atanh,$safeeval,'&atanh');
  408:   $safehole->wrap(\&Math::Cephes::erf,$safeeval,'&erf');
  409:   $safehole->wrap(\&Math::Cephes::erfc,$safeeval,'&erfc');
  410:   $safehole->wrap(\&Math::Cephes::j0,$safeeval,'&j0');
  411:   $safehole->wrap(\&Math::Cephes::j1,$safeeval,'&j1');
  412:   $safehole->wrap(\&Math::Cephes::jn,$safeeval,'&jn');
  413:   $safehole->wrap(\&Math::Cephes::jv,$safeeval,'&jv');
  414:   $safehole->wrap(\&Math::Cephes::y0,$safeeval,'&y0');
  415:   $safehole->wrap(\&Math::Cephes::y1,$safeeval,'&y1');
  416:   $safehole->wrap(\&Math::Cephes::yn,$safeeval,'&yn');
  417:   $safehole->wrap(\&Math::Cephes::yv,$safeeval,'&yv');
  418:   
  419: #need to inspect this class of ops
  420: # $safeeval->deny(":base_orig");
  421:   $safeinit .= ';$external::target='.$target.';';
  422:   $safeinit .= ';$external::randomseed='.&Apache::lonnet::rndseed().';';
  423:   &Apache::run::run($safeinit,$safeeval);
  424: }
  425: 
  426: sub startredirection {
  427:   $Apache::lonxml::redirection++;
  428:   push (@Apache::lonxml::outputstack, '');
  429: }
  430: 
  431: sub endredirection {
  432:   if (!$Apache::lonxml::redirection) {
  433:     &Apache::lonxml::error("Endredirection was called, before a startredirection, perhaps you have unbalanced tags. Some debuging information:".join ":",caller);
  434:     return '';
  435:   }
  436:   $Apache::lonxml::redirection--;
  437:   pop @Apache::lonxml::outputstack;
  438: }
  439: 
  440: sub initdepth {
  441:   @Apache::lonxml::depthcounter=();
  442:   $Apache::lonxml::depth=-1;
  443:   $Apache::lonxml::olddepth=-1;
  444: }
  445: 
  446: sub increasedepth {
  447:   my ($token) = @_;
  448:   $Apache::lonxml::depth++;
  449:   $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
  450:   if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
  451:     $Apache::lonxml::olddepth=$Apache::lonxml::depth;
  452:   }
  453:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
  454:   &Apache::lonxml::debug("s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n");
  455: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
  456: }
  457: 
  458: sub decreasedepth {
  459:   my ($token) = @_;
  460:   $Apache::lonxml::depth--;
  461:   if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
  462:     $#Apache::lonxml::depthcounter--;
  463:     $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
  464:   }
  465:   if (  $Apache::lonxml::depth < -1) {
  466:     &Apache::lonxml::warning("Unbalanced tags in resource");   
  467:     $Apache::lonxml::depth='-1';
  468:   }
  469:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
  470:   &Apache::lonxml::debug("e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n");
  471: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
  472: }
  473: 
  474: sub get_all_text {
  475: 
  476:  my($tag,$pars)= @_;
  477:  my $depth=0;
  478:  my $token;
  479:  my $result='';
  480:  if ( $tag =~ m:^/: ) { 
  481:    my $tag=substr($tag,1); 
  482: #   &Apache::lonxml::debug("have:$tag:");
  483:    while (($depth >=0) && ($token = $pars->get_token)) {
  484: #     &Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]");
  485:      if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
  486:        $result.=$token->[1];
  487:      } elsif ($token->[0] eq 'PI') {
  488:        $result.=$token->[2];
  489:      } elsif ($token->[0] eq 'S') {
  490:        if ($token->[1] eq $tag) { $depth++; }
  491:        $result.=$token->[4];
  492:      } elsif ($token->[0] eq 'E')  {
  493:        if ( $token->[1] eq $tag) { $depth--; }
  494:        #skip sending back the last end tag
  495:        if ($depth > -1) { $result.=$token->[2]; } else {
  496: 	 $pars->unget_token($token);
  497:        }
  498:      }
  499:    }
  500:  } else {
  501:    while ($token = $pars->get_token) {
  502: #     &Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
  503:      if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
  504:        $result.=$token->[1];
  505:      } elsif ($token->[0] eq 'PI') {
  506:        $result.=$token->[2];
  507:      } elsif ($token->[0] eq 'S') {
  508:        if ( $token->[1] eq $tag) { 
  509: 	 $pars->unget_token($token); last;
  510:        } else {
  511: 	 $result.=$token->[4];
  512:        }
  513:      } elsif ($token->[0] eq 'E')  {
  514:        $result.=$token->[2];
  515:      }
  516:    }
  517:  }
  518: # &Apache::lonxml::debug("Exit:$result:");
  519:  return $result
  520: }
  521: 
  522: sub newparser {
  523:   my ($parser,$contentref,$dir) = @_;
  524:   push (@$parser,HTML::TokeParser->new($contentref));
  525:   $$parser['-1']->xml_mode('1');
  526:   if ( $dir eq '' ) {
  527:     push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
  528:   } else {
  529:     push (@Apache::lonxml::pwd, $dir);
  530:   } 
  531: #  &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
  532: #  &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
  533: }
  534: 
  535: sub parstring {
  536:   my ($token) = @_;
  537:   my $temp='';
  538:   map {
  539:     unless ($_=~/\W/) {
  540:       my $val=$token->[2]->{$_};
  541:       $val =~ s/([\%\@\\])/\\$1/g;
  542:       #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
  543:       $temp .= "my \$$_=\"$val\";"
  544:     }
  545:   } @{$token->[3]};
  546:   return $temp;
  547: }
  548: 
  549: sub writeallows {
  550:     my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
  551:     my $thisdir=$thisurl;
  552:     $thisdir=~s/\/[^\/]+$//;
  553:     my %httpref=();
  554:     map {
  555:        $httpref{'httpref.'.
  556:  	        &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl;              } @extlinks;
  557:     &Apache::lonnet::appenv(%httpref);
  558: }
  559: 
  560: #
  561: # Afterburner handles anchors, highlights and links
  562: #
  563: sub afterburn {
  564:     my $result=shift;
  565:     map {
  566:        my ($name, $value) = split(/=/,$_);
  567:        $value =~ tr/+/ /;
  568:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  569:        if (($name eq 'highlight')||($name eq 'anchor')||($name eq 'link')) {
  570:            unless ($ENV{'form.'.$name}) {
  571:               $ENV{'form.'.$name}=$value;
  572: 	   }
  573:        }
  574:     } (split(/&/,$ENV{'QUERY_STRING'}));
  575:     if ($ENV{'form.highlight'}) {
  576:         map {
  577:            my $anchorname=$_;
  578: 	   my $matchthis=$anchorname;
  579:            $matchthis=~s/\_+/\\s\+/g;
  580:            $result=~s/($matchthis)/\<font color=\"red\"\>$1\<\/font\>/gs;
  581:        } split(/\,/,$ENV{'form.highlight'});
  582:     }
  583:     if ($ENV{'form.link'}) {
  584:         map {
  585:            my ($anchorname,$linkurl)=split(/\>/,$_);
  586: 	   my $matchthis=$anchorname;
  587:            $matchthis=~s/\_+/\\s\+/g;
  588:            $result=~s/($matchthis)/\<a href=\"$linkurl\"\>$1\<\/a\>/gs;
  589:        } split(/\,/,$ENV{'form.link'});
  590:     }
  591:     if ($ENV{'form.anchor'}) {
  592:         my $anchorname=$ENV{'form.anchor'};
  593: 	my $matchthis=$anchorname;
  594:         $matchthis=~s/\_+/\\s\+/g;
  595:         $result=~s/($matchthis)/\<a name=\"$anchorname\"\>$1\<\/a\>/s;
  596:         $result.=(<<"ENDSCRIPT");
  597: <script>
  598:     document.location.hash='$anchorname';
  599: </script>
  600: ENDSCRIPT
  601:     }
  602:     return $result;
  603: }
  604: 
  605: sub storefile {
  606:     my ($file,$contents)=@_;
  607:     if (my $fh=Apache::File->new('>'.$file)) {
  608: 	print $fh $contents;
  609:         $fh->close();
  610:     }
  611: }
  612: 
  613: sub inserteditinfo {
  614:       my ($result,$filecontents)=@_;
  615:       unless ($filecontents) {
  616: 	  $filecontents=(<<SIMPLECONTENT);
  617: <html>
  618: <head>
  619: <title>
  620:                            Title of Document Goes Here
  621: </title>
  622: </head>
  623: <body bgcolor="#FFFFFF">
  624: 
  625:                            Body of Document Goes Here
  626: 
  627: </body>
  628: </html>
  629: SIMPLECONTENT
  630:       }
  631:       my $editheader='<a href="#editsection">Edit below</a><hr />';
  632:       my $editfooter=(<<ENDFOOTER);
  633: <hr />
  634: <a name="editsection" />
  635: <form method="post">
  636: <textarea cols="80" rows="40" name="filecont">$filecontents</textarea>
  637: <br />
  638: <input type="submit" name="savethisfile" value="Save this file" />
  639: </form>
  640: ENDFOOTER
  641:       $result=~s/(\<body[^\>]*\>)/$1$editheader/is;
  642:       $result=~s/(\<\/body\>)/$editfooter/is;
  643:       return $result;
  644: }
  645: 
  646: sub handler {
  647:   my $request=shift;
  648: 
  649:   my $target='web';
  650: 
  651:   $Apache::lonxml::debug=0;
  652: 
  653:   if ($ENV{'browser.mathml'}) {
  654:     $request->content_type('text/xml');
  655:   } else {
  656:     $request->content_type('text/html');
  657:   }
  658:   
  659:   $request->send_http_header;
  660:   
  661:   return OK if $request->header_only;
  662: 
  663: 
  664:   my $file=&Apache::lonnet::filelocation("",$request->uri);
  665: #
  666: # Edit action? Save file.
  667: #
  668:   unless ($ENV{'request.state'} eq 'published') {
  669:       if ($ENV{'form.savethisfile'}) {
  670: 	  &storefile($file,$ENV{'form.filecont'});
  671:       }
  672:   }
  673:   my %mystyle;
  674:   my $result = ''; 
  675:   my $filecontents=&Apache::lonnet::getfile($file);
  676:   if ($filecontents == -1) {
  677:     $result=(<<ENDNOTFOUND);
  678: <html>
  679: <head>
  680: <title>File not found</title>
  681: </head>
  682: <body bgcolor="#FFFFFF">
  683: <b>File not found: $file</b>
  684: </body>
  685: </html>
  686: ENDNOTFOUND
  687:     $filecontents='';
  688:   } else {
  689:     $result = &Apache::lonxml::xmlparse($target,$filecontents,'',%mystyle);
  690:   }
  691: 
  692: #
  693: # Edit action? Insert editing commands
  694: #
  695:   unless ($ENV{'request.state'} eq 'published') {
  696:       $result=&inserteditinfo($result,$filecontents);
  697:   }
  698: 
  699:   $request->print($result);
  700: 
  701:   writeallows($request->uri);
  702:   return OK;
  703: }
  704:  
  705: sub debug {
  706:   if ($Apache::lonxml::debug eq 1) {
  707:     print "DEBUG:".$_[0]."<br />\n";
  708:   }
  709: }
  710: 
  711: sub error {
  712:   if (($Apache::lonxml::debug eq 1) || ($ENV{'request.state'} eq 'construct') ) {
  713:     print "<b>ERROR:</b>".$_[0]."<br />\n";
  714:   } else {
  715:     print "<b>An Error occured while processing this resource. The instructor has been notified.</b> <br />";
  716:     #notify author
  717:     &Apache::lonmsg::author_res_msg($ENV{'request.filename'},$_[0]);
  718:     #notify course
  719:     if ( $ENV{'request.course.id'} ) {
  720:       my $users=$ENV{'course.'.$ENV{'request.course.id'}.'.comment.email'};
  721:       foreach my $user (split /\,/, $users) {
  722: 	($user,my $domain) = split /:/, $user;
  723: 	&Apache::lonmsg::user_normal_msg($user,$domain,"Error in $ENV{'request.filename'}",$_[0]);
  724:       }
  725:     }
  726: 
  727:     #FIXME probably shouldn't have me get everything forever.
  728:     &Apache::lonmsg::user_normal_msg('albertel','msu',"Error in $ENV{'request.filename'}",$_[0]);
  729:     #&Apache::lonmsg::user_normal_msg('albertel','103',"Error in $ENV{'request.filename'}",$_[0]);
  730:   }
  731: }
  732: 
  733: sub warning {
  734:   if ($ENV{'request.state'} eq 'construct') {
  735:     print "<b>W</b>ARNING<b>:</b>".$_[0]."<br />\n";
  736:   }
  737: }
  738: 
  739: sub get_param {
  740:   my ($param,$parstack,$safeeval,$context) = @_;
  741:   if ( ! $context ) { $context = -1; }
  742:   my $args ='';
  743:   if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
  744:   return &Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
  745: }
  746: 
  747: sub register_insert {
  748:   my @data = split /\n/, &Apache::lonnet::getfile('/home/httpd/lonTabs/insertlist.tab');
  749:   my $i;
  750:   my $tagnum=0;
  751:   my @order;
  752:   for ($i=0;$i < $#data; $i++) {
  753:     my $line = $data[$i];
  754:     if ( $line =~ /^\#/ || $line =~ /^\s*\n/) { next; }
  755:     if ( $line =~ /TABLE/ ) { last; }
  756:     my ($tag,$descrip,$function,$show) = split(/,/, $line);
  757:     $insertlist{"$tagnum.tag"} = $tag;
  758:     $insertlist{"$tagnum.description"} = $descrip;
  759:     $insertlist{"$tagnum.function"} = $function;
  760:     $insertlist{"$tagnum.show"}= $show;
  761:     $tagnum++;
  762:   }
  763:   $i++; #skipping TABLE line
  764:   $tagnum = 0;
  765:   for (;$i < $#data;$i++) {
  766:     my $line = $data[$i];
  767:     my ($mnemonic,@which) = split(/ +/,$line);
  768:     my $tag = $insertlist{"$tagnum.tag"};
  769:     for (my $j=0;$j <$#which;$j++) {
  770:       if ( $which[$j] eq 'Y' ) {
  771: 	if ($insertlist{"$j.show"} ne 'no') {
  772: 	  push(@{ $insertlist{"$tag.which"} },$j);
  773: 	}
  774:       }
  775:     }
  776:     $tagnum++;
  777:   }
  778: }
  779: 1;
  780: __END__
  781: 
  782: 

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