File:  [LON-CAPA] / loncom / xml / lonxml.pm
Revision 1.63: download - view: text, annotated - select for diffs
Tue Mar 27 16:57:20 2001 UTC (23 years, 2 months ago) by sakharuk
Branches: MAIN
CVS tags: HEAD

r changes for tex target

    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: 
    9: package Apache::lonxml; 
   10: use vars 
   11: qw(@pwd @outputstack $redirection $import @extlinks $metamode);
   12: use strict;
   13: use HTML::TokeParser;
   14: use Safe;
   15: use Safe::Hole;
   16: use Opcode;
   17: use Apache::Constants qw(:common);
   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: sub printalltags {
   30:   my $temp;
   31:   foreach $temp (sort keys %Apache::lonxml::alltags) {
   32: #    &Apache::lonxml::debug("$temp -- $Apache::lonxml::alltags{$temp}");
   33:   }
   34: }
   35: use Apache::style;
   36: use Apache::lontexconvert;
   37: use Apache::run;
   38: use Apache::londefdef;
   39: use Apache::scripttag;
   40: use Apache::edit;
   41: #==================================================   Main subroutine: xmlparse  
   42: @pwd=();
   43: @outputstack = ();
   44: $redirection = 0;
   45: $import = 1;
   46: @extlinks=();
   47: $metamode = 0;
   48: 
   49: sub xmlparse {
   50: 
   51:  my ($target,$content_file_string,$safeinit,%style_for_target) = @_;
   52:  if ($target eq 'meta') {
   53:    # meta mode is a bit weird only some output is to be turned off
   54:    #<output> tag turns metamode off (defined in londefdef.pm)
   55:    $Apache::lonxml::redirection = 0;
   56:    $Apache::lonxml::metamode = 1;
   57:    $Apache::lonxml::import = 0;
   58:  } elsif ($target eq 'grade') {
   59:    &startredirection;
   60:    $Apache::lonxml::metamode = 0;
   61:    $Apache::lonxml::import = 1;
   62:  } else {
   63:    $Apache::lonxml::metamode = 0;
   64:    $Apache::lonxml::redirection = 0;
   65:    $Apache::lonxml::import = 1;
   66:  }
   67:  #&printalltags();
   68:  my @pars = ();
   69:  @Apache::lonxml::pwd=();
   70:  my $pwd=$ENV{'request.filename'};
   71:  $pwd =~ s:/[^/]*$::;
   72:  &newparser(\@pars,\$content_file_string,$pwd);
   73:  my $currentstring = '';
   74:  my $finaloutput = ''; 
   75:  my $newarg = '';
   76:  my $result;
   77: 
   78:  my $safeeval = new Safe;
   79:  my $safehole = new Safe::Hole;
   80:  $safeeval->permit("entereval");
   81:  $safeeval->permit(":base_math");
   82:  $safeeval->deny(":base_io");
   83:  $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
   84: #need to inspect this class of ops
   85: # $safeeval->deny(":base_orig");
   86:  $safeinit .= ';$external::target='.$target.';';
   87:  $safeinit .= ';$external::randomseed='.&Apache::lonnet::rndseed().';';
   88:  &Apache::run::run($safeinit,$safeeval);
   89: #-------------------- Redefinition of the target in the case of compound target
   90: 
   91:  ($target, my @tenta) = split('&&',$target);
   92: 
   93:  my @stack = (); 
   94:  my @parstack = ();
   95:  &initdepth;
   96:  my $token;
   97:  while ( $#pars > -1 ) {
   98:    while ($token = $pars[$#pars]->get_token) {
   99:      if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
  100:        if ($metamode<1) { $result=$token->[1]; }
  101:      } elsif ($token->[0] eq 'PI') {
  102:        if ($metamode<1) { $result=$token->[2]; }
  103:      } elsif ($token->[0] eq 'S') {
  104:        # add tag to stack 	    
  105:        push (@stack,$token->[1]);
  106:        # add parameters list to another stack
  107:        push (@parstack,&parstring($token));
  108:        &increasedepth($token);       
  109: #       &Apache::lonxml::debug("Checking for $token->[1] style");
  110:        if (exists $style_for_target{$token->[1]}) {
  111: #	   &Apache::lonxml::debug("Found $token->[1] style");
  112: 	 if ($Apache::lonxml::redirection) {
  113: 	   $Apache::lonxml::outputstack['-1'] .=  
  114: 	     &recurse($style_for_target{$token->[1]},$target,$safeeval,
  115: 		      \%style_for_target,@parstack);
  116: 	 } else {
  117: 	   $finaloutput .= &recurse($style_for_target{$token->[1]},$target,
  118: 				    $safeeval,\%style_for_target,@parstack);
  119: 	 }
  120:        } else {
  121: #	   &Apache::lonxml::debug("No style for for $token->[1]");
  122: 	 $result = &callsub("start_$token->[1]", $target, $token,\@parstack,
  123: 			    \@pars, $safeeval, \%style_for_target);
  124:        }              
  125:      } elsif ($token->[0] eq 'E')  {
  126:        #clear out any tags that didn't end
  127:        while ($token->[1] ne $stack[$#stack] && ($#stack > -1)) {
  128: 	 &Apache::lonxml::warning("Unbalanced tags in resource $stack['-1']");
  129: 	 pop @stack;pop @parstack;&decreasedepth($token);
  130:        }
  131:        
  132:        if (exists $style_for_target{'/'."$token->[1]"}) {
  133: 	 if ($Apache::lonxml::redirection) {
  134: 	   $Apache::lonxml::outputstack['-1'] .=  
  135: 	     &recurse($style_for_target{'/'."$token->[1]"},
  136: 		      $target,$safeeval,\%style_for_target,@parstack);
  137: 	 } else {
  138: 	   $finaloutput .= &recurse($style_for_target{'/'."$token->[1]"},
  139: 				    $target,$safeeval,\%style_for_target,
  140: 				    @parstack);
  141: 	 }
  142: 
  143:        } else {
  144: 	 $result = &callsub("end_$token->[1]", $target, $token, \@parstack,
  145: 			    \@pars,$safeeval, \%style_for_target);
  146:        }
  147:      } else {
  148:        &Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
  149:      }
  150:      #evaluate variable refs in result
  151:      if ($result ne "") {
  152:        if ( $#parstack > -1 ) {
  153: 	 if ($Apache::lonxml::redirection) {
  154: 	   $Apache::lonxml::outputstack['-1'] .= 
  155: 	     &Apache::run::evaluate($result,$safeeval,$parstack[$#parstack]);
  156: 	 } else {
  157: 	   $finaloutput .= &Apache::run::evaluate($result,$safeeval,
  158: 						  $parstack[$#parstack]);
  159: 	 }
  160:        } else {
  161: 	 $finaloutput .= &Apache::run::evaluate($result,$safeeval,'');
  162:        }
  163:        $result = '';
  164:      } 
  165:      if ($token->[0] eq 'E') { 
  166:        pop @stack;pop @parstack;&decreasedepth($token);
  167:      }
  168:    }
  169:    pop @pars;
  170:    pop @Apache::lonxml::pwd;
  171:  }
  172: 
  173: # if ($target eq 'meta') {
  174: #   $finaloutput.=&endredirection;
  175: # }
  176:  return $finaloutput;
  177: }
  178: 
  179: sub recurse {
  180:   
  181:   my @innerstack = (); 
  182:   my @innerparstack = ();
  183:   my ($newarg,$target,$safeeval,$style_for_target,@parstack) = @_;
  184:   my @pat = ();
  185:   &newparser(\@pat,\$newarg);
  186:   my $tokenpat;
  187:   my $partstring = '';
  188:   my $output='';
  189:   my $decls='';
  190:   while ( $#pat > -1 ) {
  191:     while  ($tokenpat = $pat[$#pat]->get_token) {
  192:       if (($tokenpat->[0] eq 'T') || ($tokenpat->[0] eq 'C') || ($tokenpat->[0] eq 'D') ) {
  193: 	if ($metamode<1) { $partstring=$tokenpat->[1]; }
  194:       } elsif ($tokenpat->[0] eq 'PI') {
  195: 	if ($metamode<1) { $partstring=$tokenpat->[2]; }
  196:       } elsif ($tokenpat->[0] eq 'S') {
  197: 	push (@innerstack,$tokenpat->[1]);
  198: 	push (@innerparstack,&parstring($tokenpat));
  199: 	&increasedepth($tokenpat);
  200: 	$partstring = &callsub("start_$tokenpat->[1]", 
  201: 			       $target, $tokenpat, \@innerparstack,
  202: 			       \@pat, $safeeval, $style_for_target);
  203:       } elsif ($tokenpat->[0] eq 'E') {
  204: 	#clear out any tags that didn't end
  205: 	while ($tokenpat->[1] ne $innerstack[$#innerstack] 
  206: 	       && ($#innerstack > -1)) {
  207: 	  &Apache::lonxml::warning("Unbalanced tags in resource $innerstack['-1']");
  208: 	  pop @innerstack;pop @innerparstack;&decreasedepth($tokenpat);
  209: 	}
  210: 	$partstring = &callsub("end_$tokenpat->[1]",
  211: 			       $target, $tokenpat, \@innerparstack,
  212: 			       \@pat, $safeeval, $style_for_target);
  213:       } else {
  214: 	&Apache::lonxml::error("Unknown token event :$tokenpat->[0]:$tokenpat->[1]:");
  215:       }
  216:       #pass both the variable to the style tag, and the tag we 
  217:       #are processing inside the <definedtag>
  218:       if ( $partstring ne "" ) {
  219: 	if ( $#parstack > -1 ) { 
  220: 	  if ( $#innerparstack > -1 ) { 
  221: 	    $decls= $parstack[$#parstack].$innerparstack[$#innerparstack];
  222: 	  } else {
  223: 	    $decls= $parstack[$#parstack];
  224: 	  }
  225: 	} else {
  226: 	  if ( $#innerparstack > -1 ) { 
  227: 	    $decls=$innerparstack[$#innerparstack];
  228: 	  } else {
  229: 	    $decls='';
  230: 	  }
  231: 	}
  232: 	$output .= &Apache::run::evaluate($partstring,$safeeval,$decls);
  233: 	$partstring = '';
  234:       }
  235:       if ($tokenpat->[0] eq 'E') { pop @innerstack;pop @innerparstack;
  236: 				 &decreasedepth($tokenpat);}
  237:     }
  238:     pop @pat;
  239:     pop @Apache::lonxml::pwd;
  240:   }
  241:   return $output;
  242: }
  243: 
  244: sub callsub {
  245:   my ($sub,$target,$token,$parstack,$parser,$safeeval,$style)=@_;
  246:   my $currentstring='';
  247:   {
  248:     my $sub1;
  249:     no strict 'refs';
  250:     if ($target eq 'edit' && $token->[0] eq 'S') {
  251:       $currentstring = &Apache::edit::tag_start($target,$token,$parstack,$parser,
  252: 						$safeeval,$style);
  253:     }
  254:     if (my $space=$Apache::lonxml::alltags{$token->[1]}) {
  255: #      &Apache::lonxml::debug("Calling sub $sub in $space $metamode<br />\n");
  256:       $sub1="$space\:\:$sub";
  257:       $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
  258:       $currentstring .= &$sub1($target,$token,$parstack,$parser,
  259: 			     $safeeval,$style);
  260:     } else {
  261: #      &Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode<br />\n");
  262:       if ($metamode <1) {
  263: 	if (defined($token->[4]) && ($metamode < 1)) {
  264: 	  $currentstring .= $token->[4];
  265: 	} else {
  266: 	  $currentstring .= $token->[2];
  267: 	}
  268:       }
  269:     }
  270:     if ($target eq 'edit' && $token->[0] eq 'E') {
  271:       $currentstring .= &Apache::edit::tag_end($target,$token,$parstack,$parser,
  272: 						$safeeval,$style);
  273:     }
  274:     use strict 'refs';
  275:   }
  276:   return $currentstring;
  277: }
  278: 
  279: sub startredirection {
  280:   $Apache::lonxml::redirection++;
  281:   push (@Apache::lonxml::outputstack, '');
  282: }
  283: 
  284: sub endredirection {
  285:   if (!$Apache::lonxml::redirection) {
  286:     &Apache::lonxml::error("Endredirection was called, before a startredirection, perhaps you have unbalanced tags. Some debuggin information:".join ":",caller);
  287:     return '';
  288:   }
  289:   $Apache::lonxml::redirection--;
  290:   pop @Apache::lonxml::outputstack;
  291: }
  292: 
  293: sub initdepth {
  294:   @Apache::lonxml::depthcounter=();
  295:   $Apache::lonxml::depth=-1;
  296:   $Apache::lonxml::olddepth=-1;
  297: }
  298: 
  299: sub increasedepth {
  300:   my ($token) = @_;
  301:   $Apache::lonxml::depth++;
  302:   $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
  303:   if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
  304:     $Apache::lonxml::olddepth=$Apache::lonxml::depth;
  305:   }
  306:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
  307: #  &Apache::lonxml::debug("s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n");
  308: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
  309: }
  310: 
  311: sub decreasedepth {
  312:   my ($token) = @_;
  313:   $Apache::lonxml::depth--;
  314:   if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
  315:     $#Apache::lonxml::depthcounter--;
  316:     $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
  317:   }
  318:   if (  $Apache::lonxml::depth < -1) {
  319:     &Apache::lonxml::warning("Unbalanced tags in resource");   
  320:     $Apache::lonxml::depth='-1';
  321:   }
  322:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
  323: #  &Apache::lonxml::debug("e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n");
  324: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
  325: }
  326: 
  327: sub get_all_text {
  328: 
  329:  my($tag,$pars)= @_;
  330:  my $depth=0;
  331:  my $token;
  332:  my $result='';
  333:  if ( $tag =~ m:^/: ) { 
  334:    my $tag=substr($tag,1); 
  335: #   &Apache::lonxml::debug("have:$tag:");
  336:    while (($depth >=0) && ($token = $pars->get_token)) {
  337: #     &Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]");
  338:      if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
  339:        $result.=$token->[1];
  340:      } elsif ($token->[0] eq 'PI') {
  341:        $result.=$token->[2];
  342:      } elsif ($token->[0] eq 'S') {
  343:        if ($token->[1] eq $tag) { $depth++; }
  344:        $result.=$token->[4];
  345:      } elsif ($token->[0] eq 'E')  {
  346:        if ( $token->[1] eq $tag) { $depth--; }
  347:        #skip sending back the last end tag
  348:        if ($depth > -1) { $result.=$token->[2]; } else {
  349: 	 $pars->unget_token($token);
  350:        }
  351:      }
  352:    }
  353:  } else {
  354:    while ($token = $pars->get_token) {
  355: #     &Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
  356:      if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
  357:        $result.=$token->[1];
  358:      } elsif ($token->[0] eq 'PI') {
  359:        $result.=$token->[2];
  360:      } elsif ($token->[0] eq 'S') {
  361:        if ( $token->[1] eq $tag) { 
  362: 	 $pars->unget_token($token); last;
  363:        } else {
  364: 	 $result.=$token->[4];
  365:        }
  366:      } elsif ($token->[0] eq 'E')  {
  367:        $result.=$token->[2];
  368:      }
  369:    }
  370:  }
  371: # &Apache::lonxml::debug("Exit:$result:");
  372:  return $result
  373: }
  374: 
  375: sub newparser {
  376:   my ($parser,$contentref,$dir) = @_;
  377:   push (@$parser,HTML::TokeParser->new($contentref));
  378:   $$parser['-1']->xml_mode('1');
  379:   if ( $dir eq '' ) {
  380:     push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
  381:   } else {
  382:     push (@Apache::lonxml::pwd, $dir);
  383:   } 
  384: #  &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
  385: #  &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
  386: }
  387: 
  388: sub parstring {
  389:   my ($token) = @_;
  390:   my $temp='';
  391:   map {
  392:     unless ($_=~/\W/) {
  393:       my $val=$token->[2]->{$_};
  394:       $val =~ s/([\%\@\\])/\\$1/g;
  395:       #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
  396:       $temp .= "my \$$_=\"$val\";"
  397:     }
  398:   } @{$token->[3]};
  399:   return $temp;
  400: }
  401: 
  402: sub writeallows {
  403:     my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
  404:     my $thisdir=$thisurl;
  405:     $thisdir=~s/\/[^\/]+$//;
  406:     my %httpref=();
  407:     map {
  408:        $httpref{'httpref.'.
  409:  	        &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl;              } @extlinks;
  410:     &Apache::lonnet::appenv(%httpref);
  411: }
  412: 
  413: sub handler {
  414:   my $request=shift;
  415:   
  416:   my $target='tex';
  417:   $Apache::lonxml::debug=1;
  418:   if ($ENV{'browser.mathml'}) {
  419:     $request->content_type('text/xml');
  420:   } else {
  421:     $request->content_type('text/html');
  422:   }
  423: 
  424: #  $request->print(<<ENDHEADER);
  425: #<html>
  426: #<head>
  427: #<title>Just test</title>
  428: #</head>
  429: #<body bgcolor="#FFFFFF">
  430: #ENDHEADER
  431: #  &Apache::lonhomework::send_header($request);
  432:   $request->send_http_header;
  433: 
  434:   return OK if $request->header_only;
  435: 
  436: ##  $request->print(&Apache::lontexconvert::header());
  437: 
  438: ##  $request->print('<body bgcolor="#FFFFFF">'."\n");
  439: 
  440:   if ($target eq 'tex') {
  441: 
  442: 
  443: 
  444:   } else {
  445:          $request->print(&Apache::lontexconvert::header());
  446:          $request->print('<body bgcolor="#FFFFFF">'."\n");
  447:   }
  448: 
  449:   my $file=&Apache::lonnet::filelocation("",$request->uri);
  450:   my %mystyle;
  451:   my $result = ''; 
  452:   my $filecontents=&Apache::lonnet::getfile($file);
  453:   if ($filecontents == -1) {
  454:     &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
  455:     $filecontents='';
  456:   } else {
  457:     $result = &Apache::lonxml::xmlparse($target,$filecontents,'',%mystyle);
  458:   }
  459:   $request->print($result);
  460: 
  461: 
  462: ##  $request->print('</body>');
  463:   if ($target eq 'tex') {
  464: #    $request->print('\end{document}'."\n");
  465:   } else {
  466:     $request->print('</body>');
  467:     $request->print(&Apache::lontexconvert::footer());
  468:   }
  469: ##  $request->print(&Apache::lontexconvert::footer());
  470:   writeallows($request->uri);
  471:   return OK;
  472: }
  473:  
  474: $Apache::lonxml::debug=0;
  475: sub debug {
  476:   if ($Apache::lonxml::debug eq 1) {
  477:     print "DEBUG:".$_[0]."<br />\n";
  478:   }
  479: }
  480: 
  481: sub error {
  482:   if ($Apache::lonxml::debug eq 1) {
  483:     print "<b>ERROR:</b>".$_[0]."<br />\n";
  484:   } else {
  485:     print "<b>An Error occured while processing this resource. The instructor has been notified.</b> <br />";
  486:     #notify author
  487:     &Apache::lonmsg::author_res_msg($ENV{'request.filename'},$_[0]);
  488:     #notify course
  489:     if ( $ENV{'request.course.id'} ) {
  490:       my $users=$ENV{'course.'.$ENV{'request.course.id'}.'.comment.email'};
  491:       foreach my $user (split /\,/, $users) {
  492: 	($user,my $domain) = split /:/, $user;
  493: 	&Apache::lonmsg::user_normal_msg($user,$domain,"Error in $ENV{'request.filename'}",$_[0]);
  494:       }
  495:     }
  496:     
  497:     #FIXME probably shouldn't have me get everything forever.
  498:     &Apache::lonmsg::user_normal_msg('albertel','msu',"Error in $ENV{'request.filename'}",$_[0]);
  499:     #&Apache::lonmsg::user_normal_msg('albertel','103',"Error in $ENV{'request.filename'}",$_[0]);   
  500:   }
  501: }
  502: 
  503: sub warning {
  504:   if ($Apache::lonxml::debug eq 1) {
  505:     print "<b>W</b>ARNING<b>:</b>".$_[0]."<br />\n";
  506:   }
  507: }
  508: 
  509: 1;
  510: __END__

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