File:  [LON-CAPA] / loncom / xml / scripttag.pm
Revision 1.81: download - view: text, annotated - select for diffs
Wed Nov 6 23:05:02 2002 UTC (21 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- Fixes most of BUG#793
- added new global var that controls wheter syle files defs are used
- use new tag <LONCAPA_INTERNAL_TURN_STYLE_ON /> to turn style def use back on
- removed recurse sub completely
- get_all_text now understands the Parser stack

Bugs:
- get_all_unbalanced_text needs to use parser stack
- get_all_text uses need to pass entrie parser stack
- style defs that have a tag that ends in a different style def can't use get_all_text


Need To Test:
- pars stack pops in end tags looking for start tags in get_all_text

    1: # The LearningOnline Network with CAPA
    2: # <script> definiton
    3: #
    4: # $Id: scripttag.pm,v 1.81 2002/11/06 23:05:02 albertel Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: # 2/21 Guy
   29: # 8/20 Gerd Kortemeyer
   30: 
   31: package Apache::scripttag;
   32: 
   33: use strict;
   34: use Apache::lonnet;
   35: use Apache::style;
   36: 
   37: #Globals
   38: # this used to pass around the standard callsub arguments to a tag func
   39: # so xmlparse can reenter the inner_xmlparse loop.
   40: 
   41: @Apache::scripttag::parser_env = ();
   42: BEGIN {
   43:   &Apache::lonxml::register('Apache::scripttag',
   44: 			    ('script','scriptlib','parserlib','import',
   45: 			     'window','display','storetc','physnet',
   46: 			     'standalone','comment',
   47: 			     'LONCAPA_INTERNAL_TURN_STYLE_ON'));
   48: }
   49: 
   50: sub start_LONCAPA_INTERNAL_TURN_STYLE_ON {
   51:     $Apache::lonxml::usestyle=1;
   52:     return ('','no');
   53: }
   54: 
   55: sub end_LONCAPA_INTERNAL_TURN_STYLE_ON {
   56:     return ('','no');
   57: }
   58: 
   59: sub start_script {
   60:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   61:   @Apache::scripttag::parser_env = @_;
   62:   my $result='';
   63:   my $type= &Apache::lonxml::get_param('type',$parstack,$safeeval);
   64:   &Apache::lonxml::debug("found type of $type");
   65:   if ($type eq "loncapa/perl") {
   66:     my $bodytext=&Apache::lonxml::get_all_text("/script",$$parser[$#$parser]);
   67:     if ( $target eq "modified" ) {
   68:       $result=$token->[4].&Apache::edit::modifiedfield();
   69:     } elsif ( $target eq 'web' || $target eq 'tex' ||
   70: 	      $target eq 'grade' || $target eq 'answer' ||
   71: 	      $target eq 'analyze' ) {
   72: 	if (!$Apache::lonxml::default_homework_loaded) {
   73: 	    &Apache::lonxml::default_homework_load($safeeval);
   74: 	}
   75: 	&Apache::run::run($bodytext,$safeeval);
   76: 	if (($target eq 'answer') && ($Apache::lonhomework::viewgrades == 'F')) {
   77: 	    $Apache::lonxml::evaluate--;
   78: 	    $result.="<a href=\"javascript:newWindow=open(\'\',\'new_W\',\'width=500,height=200,scrollbars=1\');newWindow.document.open(\'text/html\',\'replace\');newWindow.document.writeln(\'<html><head><title>newwindow</title></head><body bgcolor=&quot;#FFFFFF&quot;><pre>";
   79: 	    my $listing= &HTML::Entities::encode(&Apache::run::dump($target,$safeeval));
   80: 
   81: 	    $result.=$listing;
   82: 	    $result.= "</pre></body></html>\');newWindow.document.close();void(0);\">Script Vars</a><br />";
   83: 	}
   84:     } elsif ($target eq "edit" ) {
   85:       #&Apache::run::run($bodytext,$safeeval);
   86:       #$result="<br /> &lt;$token->[1]&gt; output: <br />$bodytext<br />Source:<br />";
   87:       $result=&Apache::edit::tag_start($target,$token,'Script');
   88:       $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,4);
   89:     }
   90:   } else {
   91:     if ($target ne "meta") {
   92:       $result = $token->[4];
   93:       my $bodytext=&Apache::lonxml::get_all_text("/script",$$parser[$#$parser]);
   94:       $result.=$bodytext;
   95:     }
   96:   }
   97:   return $result;
   98: }
   99: 
  100: sub end_script {
  101:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  102:   if ( $target eq "meta" ) { return ''; } 
  103:   my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
  104:   my $result='';
  105:   #other script blocks need to survive
  106:   if ($type ne "loncapa/perl") {
  107:     return $token->[2];
  108:   } elsif ($target eq 'edit' ) {
  109:     return &Apache::edit::end_table();
  110:   } elsif ($target eq 'answer') {
  111:     $Apache::lonxml::evaluate++;
  112:   }
  113:   return '';
  114: }
  115: 
  116: sub start_display {
  117:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  118: 
  119:   my $result;
  120:   my $bodytext=&Apache::lonxml::get_all_text("/display",$$parser[$#$parser]);
  121: 
  122:   if ( $target eq "modified" ) {
  123:     $result=$token->[4].&Apache::edit::modifiedfield();
  124:   } elsif ( $target eq 'web' || $target eq 'tex' ||
  125: 	    $target eq 'grade' || $target eq 'answer' ||
  126: 	      $target eq 'analyze') {
  127:       if (!$Apache::lonxml::default_homework_loaded) {
  128: 	  &Apache::lonxml::default_homework_load($safeeval);
  129:       }
  130:       $result=&Apache::run::run($bodytext,$safeeval);
  131:       if ($target eq 'grade' || $target eq 'answer' ||
  132: 	  $target eq 'analyze') {
  133: 	  $result=''; # grade should produce no output
  134:       }
  135:   } elsif ($target eq "edit" ) {
  136:     #$result = 
  137:     #  "<br /> &lt;$token->[1]&gt; output: <br />$bodytext<br />Source:<br />";
  138:     #$result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1);
  139:     $result=&Apache::edit::tag_start($target,$token,'Script With Display');
  140:     $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,1)
  141:   }
  142:   return $result;
  143: }
  144: 
  145: sub end_display {
  146:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  147:   if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
  148:   return '';
  149: }
  150: 
  151: sub start_scriptlib {
  152:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  153:   my $bodytext;
  154:   my $result ='';
  155:   my $error='';
  156: 
  157:   if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' || 
  158:       $target eq 'edit' || $target eq 'answer' || $target eq 'analyze') {
  159:     $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
  160:     $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
  161: 				     $$parstack[$#$parstack]);
  162:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
  163: 					       $bodytext);
  164:     my $script=&Apache::lonnet::getfile($location);
  165:     if ($script == -1) {
  166:       if ($target eq 'edit') {
  167:         $error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
  168:       } else {
  169: 	&Apache::lonxml::error("<b> Unable to find <i>$location</i> for scriptlib</b>");
  170: 	return "";
  171:       }
  172:     }
  173:     &Apache::run::run($script,$safeeval);
  174:     #&Apache::lonxml::debug("ran $bodytext:<br />".&Apache::lonnet::getfile($bodytext)."<br />");
  175:   }
  176:   if ($target eq "edit" ) {
  177:     $result=
  178:       &Apache::edit::tag_start($target,$token,'New Script Functions').
  179: 	&Apache::edit::editline($token->[1],$bodytext,'',40).
  180: 	  $error.'</td></tr>'.
  181: 	    &Apache::edit::end_table();
  182:   }
  183:   if ($target eq "modified" ) {
  184:     $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
  185:     $result=$token->[4].&Apache::edit::modifiedfield($token);
  186:     &Apache::lonxml::debug($result);
  187:   }
  188:   return $result;
  189: }
  190: 
  191: sub end_scriptlib {
  192:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  193:   my @result;
  194:   if ($target eq "edit" ) { $result[1]='no'; }
  195:   return @result;
  196: }
  197: 
  198: sub start_parserlib {
  199:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  200:   my $bodytext;
  201:   my $result ="";
  202:   my $error='';
  203:   if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' ||
  204:       $target eq 'edit' || $target eq 'answer' || $target eq 'analyze') {
  205:     $bodytext=$$parser[$#$parser]->get_text("/parserlib");
  206:     $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
  207: 				     $$parstack[$#$parstack]);
  208:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
  209: 					       $bodytext);
  210:     my $styletext=&Apache::lonnet::getfile($location);
  211:     #&Apache::lonxml::debug("found :$bodytext: in :$location: with :$styletext:");
  212:     if ($styletext == -1) {
  213:       if ($target eq 'edit') {
  214: 	$error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
  215:       } else {
  216: 	&Apache::lonxml::error("<b> Unable to find <i>$location</i> for parserlib</b>");
  217: 	return "";
  218:       }
  219:     }
  220:     %$style = ( %$style , &Apache::style::styleparser($target,$styletext));
  221:   }
  222:   if ($target eq "edit" ) {
  223:     $result=
  224:       &Apache::edit::tag_start($target,$token,'New Tag Definitions').
  225: 	&Apache::edit::editline($token->[1],$bodytext,'',40).
  226: 	  $error.'</td></tr>'.
  227: 	    &Apache::edit::end_table();
  228:   }
  229:   if ($target eq "modified" ) {
  230:     $bodytext=$$parser[$#$parser]->get_text("/parserlib");
  231:     $result=$token->[4].&Apache::edit::modifiedfield($token);
  232:     &Apache::lonxml::debug($result);
  233:   }
  234:   return $result;
  235: }
  236: 
  237: sub end_parserlib {
  238:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  239:   my @result;
  240:   if ($target eq "edit" ) { $result[1]='no'; }
  241:   return @result;
  242: }
  243: 
  244: sub start_window {
  245:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  246:   my $result = '';
  247:   if ($target eq 'web') {
  248:     &Apache::lonxml::startredirection;
  249:   }  elsif ($target eq 'tex') {
  250:        $result = '\unskip\footnote{';
  251:    }
  252:   return $result;  
  253: }
  254: 
  255: sub end_window {
  256:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  257:   my $result;
  258:   if ($target eq 'web') {
  259:     my $output=&Apache::lonxml::endredirection;
  260:     $output =~ s/\"/\&quot\;/g;
  261:     $result = "<a href=\"javascript:newWindow=open(\'\',\'new_W\',\'width=500,height=200,scrollbars=1\');newWindow.document.open(\'text/html\',\'replace\');newWindow.document.writeln(\'<html><head><title>newwindow</title></head><body bgcolor=&quot;#FFFFFF&quot;> $output </body></html>\');newWindow.document.close();void(0);\"><sup>*</sup></a>";
  262:   } elsif ($target eq 'tex') {
  263:       $result = '}';
  264:   } else {
  265:       $result = '';
  266:   }
  267:   return $result; 
  268: }
  269: 
  270: sub start_import {
  271:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  272:   my $bodytext=$$parser[$#$parser]->get_text("/import");
  273:   my $result ="";
  274: 
  275:   $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
  276: 
  277:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  278:       $target eq 'tex' || $target eq 'analyze' ) {
  279:     # FIXME this probably needs to be smart about construction vs.
  280:     # non construction space.
  281:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
  282:     my $file=&Apache::lonnet::getfile($location);
  283:     if ($file == -1) {
  284:       &Apache::lonxml::error("<b> Unable to find <i>$bodytext as $location</i> for import</b>");
  285:       return "";
  286:     }
  287: 
  288:     my $dir=$location;
  289:     $dir=~s:/[^/]*$::;
  290:     #  &Apache::lonxml::debug("directory $dir $location file $file \n<b>END</b>\n");
  291:     my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
  292:     if (!$id) { $id=$Apache::lonxml::curdepth; }
  293:     push(@Apache::inputtags::import,$id);
  294:     &Apache::lonxml::newparser($parser,\$file,$dir);
  295:   } elsif ($target eq "edit" ) {
  296:     $result.=&Apache::edit::tag_start($target,$token);
  297:     $result.=&Apache::edit::editline($token->[1],$bodytext,'',40);
  298:     #FIXME this need to convert $bodytext to be a contruction space reference
  299:     #my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
  300:     #$result.="Click<a href=\"$location\">here</a> to edit<br />"
  301:   } elsif ($target eq 'modified') {
  302:     $bodytext=$$parser[$#$parser]->get_text("/import");
  303:     $result=$token->[4].&Apache::edit::modifiedfield($token);
  304:     &Apache::lonxml::debug($result);
  305:   } elsif ($target eq 'meta') {
  306:     my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
  307:     $result.='<import part="'.$Apache::inputtags::part;
  308:     if ($id) {
  309:       $result.='" id="'.$id;
  310:     }
  311:     $result.='">';
  312:     $result.=$bodytext;
  313:     $result.='</import>';
  314:   }
  315:   return $result;
  316: }
  317: 
  318: sub end_import {
  319:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  320:   pop(@Apache::inputtags::import);
  321:   my $result;
  322:   if ($target eq 'edit' ) { $result=&Apache::edit::end_table(); }
  323:   return $result;
  324: }
  325: 
  326: sub start_storetc {
  327:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  328:   my $result = '';
  329:   &Apache::lonxml::startredirection;
  330:   return $result; 
  331: }
  332: 
  333: sub end_storetc {
  334:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  335:     my $result;
  336:     my $output=&Apache::lonxml::endredirection;
  337:     $output =~ s/\"/\&quot\;/g;
  338:     $result = '{\bf '.$output.'.}}\write\tcfile{\protect\tcpc{ '.$output.'.}{\the\value{relpage}}}';
  339:     return $result;
  340: }
  341: 
  342: 
  343: sub start_physnet {
  344:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  345:     my $bodytext = '/adm/includes/physnet.sty';
  346:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
  347:     my $cbistyletext=&Apache::lonnet::getfile($location);
  348: 
  349:     %$style = (%$style,&Apache::style::styleparser($target,$cbistyletext));
  350:     $$parser['-1']->unget_token($token);
  351: #    if ( defined($$style{'physnet'}) ) {
  352: #        &Apache::lonxml::newparser($parser,\$$style{'physnet'});
  353: #    }
  354:     return "";
  355: }
  356: 
  357: sub end_physnet {
  358:   return '';
  359: }
  360: 
  361: sub start_standalone {
  362:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  363:   my $result='';
  364:   if ($target eq 'web' ) {
  365:     if ( $ENV{'request.course.id'} ) {
  366:       my $inside = &Apache::lonxml::get_all_text("/standalone",$$parser[-1]);
  367:     } else {
  368:       $result='<table bgcolor="#E1E1E1" border="2"><tr><td>';
  369:     }
  370:   }
  371:   return $result;
  372: }
  373: 
  374: sub end_standalone {
  375:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  376:   my $result='';
  377:   if ($target eq 'web' ) {
  378:     if ( $ENV{'request.course.id'} ) {
  379:     } else {
  380:       $result='</td></tr></table>';
  381:     }
  382:   }
  383:   return $result;
  384: }
  385: 
  386: sub start_comment {
  387:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  388:   my $result='';
  389:   if ($target eq 'edit') {
  390:     $result=&Apache::edit::tag_start($target,$token);
  391:     my $bodytext=&Apache::lonxml::get_all_text("/comment",$$parser[$#$parser]);
  392:     $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,4)
  393:   } elsif ( $target eq 'modified') {
  394:     $result=$token->[4].&Apache::edit::modifiedfield($token);
  395:   } elsif ( $target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
  396: 	    $target eq 'answer' || $target eq 'meta' || $target eq 'analyze') {
  397:     #normally throw away comments
  398:     my $bodytext=&Apache::lonxml::get_all_text("/comment",$$parser[$#$parser]);
  399:   }
  400:   return $result;
  401: }
  402: 
  403: sub end_comment {
  404:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  405:   if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
  406:   return '';
  407: }
  408: 
  409: 
  410: sub xmlparse {
  411:   my ($string) = @_;
  412:   &Apache::lonxml::debug("Got $string");
  413:   my ($target,$token,$tagstack,$parstack,$oldparser,$safeeval,$style)=
  414:     @Apache::scripttag::parser_env;
  415:   my @parser;
  416:   &Apache::lonxml::newparser(\@parser,\$string);
  417:   my $result=&Apache::lonxml::inner_xmlparse($target,$tagstack,
  418: 					     $parstack,\@parser,
  419: 					     $safeeval,$style);
  420:   return $result;
  421: }
  422: 
  423: 1;
  424: __END__

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