File:  [LON-CAPA] / loncom / xml / lonxml.pm
Revision 1.89: download - view: text, annotated - select for diffs
Sat Jun 9 12:32:42 2001 UTC (22 years, 11 months ago) by www
Branches: MAIN
CVS tags: HEAD
Cleans up rows of buttons for homework menu

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

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