File:  [LON-CAPA] / loncom / xml / lonxml.pm
Revision 1.72: download - view: text, annotated - select for diffs
Fri May 4 21:17:24 2001 UTC (23 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: HEAD
- large changes to start generating a editing enviroment
- <script> <scriptlib> <parserlib> work in edit in envrioment
- callsub needs to be reorganized
- added evaluate global to lonxml, turns off evaluation of $var

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

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