File:  [LON-CAPA] / loncom / xml / scripttag.pm
Revision 1.53: download - view: text, annotated - select for diffs
Fri Oct 5 22:29:10 2001 UTC (22 years, 7 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- added a link to see the script variables when in construction space

    1: # The LearningOnline Network with CAPA
    2: # <script> definiton
    3: # 2/21 Guy
    4: # 8/20 Gerd Kortemeyer
    5: 
    6: package Apache::scripttag;
    7: 
    8: use strict;
    9: use Apache::lonnet;
   10: use Apache::style;
   11: 
   12: #Globals
   13: # this used to pass around the standard callsub arguments to a tag func
   14: # so xmlparse can reenter the inner_xmlparse loop.
   15: 
   16: @Apache::scripttag::parser_env = ();
   17: sub BEGIN {
   18:   &Apache::lonxml::register('Apache::scripttag',('script','scriptlib',
   19: 						 'parserlib','import',
   20:                                                  'window','display',
   21:                                                  'storetc','physnet'));
   22: }
   23: 
   24: sub start_script {
   25:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   26:   @Apache::scripttag::parser_env = @_;
   27:   my $result='';
   28:   my $type= &Apache::lonxml::get_param('type',$parstack,$safeeval);
   29:   &Apache::lonxml::debug("found type of $type");
   30:   if ($type eq "loncapa/perl") {
   31:     my $bodytext=&Apache::lonxml::get_all_text("/script",$$parser[$#$parser]);
   32:     if ( $target eq "modified" ) {
   33:       $result=$token->[4].&Apache::edit::modifiedfield();
   34:     } elsif ( $target eq "web" || $target eq "grade" || $target eq 'answer') {
   35:       &Apache::run::run($bodytext,$safeeval);
   36:       if (($ENV{'request.state'} eq 'construct') && ($target eq 'web')) {
   37: 	$Apache::lonxml::evaluate--;
   38: 	$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>";
   39: 	my $listing= &Apache::run::dump($target,$safeeval);
   40: 	$listing =~ s/\n/\\n/g;
   41: 	$result.=$listing;
   42: 	$result.= "</pre></body></html>\');newWindow.document.close();void(0);\">Script Vars</a>";
   43:       }
   44:     } elsif ($target eq "edit" ) {
   45:       #&Apache::run::run($bodytext,$safeeval);
   46:       #$result="<br /> &lt;$token->[1]&gt; output: <br />$bodytext<br />Source:<br />";
   47:       $result=&Apache::edit::tag_start($target,$token,'Script');
   48:       $result.=&Apache::edit::editfield($token->[1],$bodytext,'',50,4);
   49:     }
   50:   } else {
   51:     if ($target ne "meta") { $result = $token->[4]; }
   52:   }
   53:   return $result;
   54: }
   55: 
   56: sub end_script {
   57:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   58:   if ( $target eq "meta" ) { return ''; } 
   59:   my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
   60:   my $result='';
   61:   #other script blocks need to survive
   62:   if ($type ne "loncapa/perl") {
   63:     return $token->[2];
   64:   } elsif ($target eq 'edit' ) {
   65:     return &Apache::edit::end_table();
   66:   } elsif (($ENV{'request.state'} eq 'construct') && ($target eq 'web')) {
   67:     $Apache::lonxml::evaluate++;
   68:   }
   69:   return '';
   70: }
   71: 
   72: sub start_display {
   73:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   74: 
   75:   my $result;
   76:   my $bodytext=&Apache::lonxml::get_all_text("/display",$$parser[$#$parser]);
   77: 
   78:   if ( $target eq "modified" ) {
   79:     $result=$token->[4].&Apache::edit::modifiedfield();
   80:   } elsif ( $target eq "web" || $target eq "grade" || $target eq 'answer') {
   81:     $result=&Apache::run::run($bodytext,$safeeval);
   82:     if ($target eq 'grade' ) {
   83:       $result=''; # grade should produce no output
   84:     }
   85:   } elsif ($target eq "edit" ) {
   86:     #$result = 
   87:     #  "<br /> &lt;$token->[1]&gt; output: <br />$bodytext<br />Source:<br />";
   88:     #$result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1);
   89:     $result=&Apache::edit::tag_start($target,$token,'Script With Display');
   90:     $result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1)
   91:   }
   92:   return $result;
   93: }
   94: 
   95: sub end_display {
   96:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   97:   if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
   98:   return '';
   99: }
  100: 
  101: sub start_scriptlib {
  102:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  103:   my $bodytext;
  104:   my $result ='';
  105:   my $error='';
  106: 
  107:   if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' || $target eq 'edit' || $target eq 'answer') {
  108:     $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
  109:     $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
  110: 				     $$parstack[$#$parstack]);
  111:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
  112: 					       $bodytext);
  113:     my $script=&Apache::lonnet::getfile($location);
  114:     if ($script == -1) {
  115:       if ($target eq 'edit') {
  116:         $error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
  117:       } else {
  118: 	&Apache::lonxml::error("<b> Unable to find <i>$location</i> for scriptlib</b>");
  119: 	return "";
  120:       }
  121:     }
  122:     &Apache::run::run($script,$safeeval);
  123:     #&Apache::lonxml::debug("ran $bodytext:<br />".&Apache::lonnet::getfile($bodytext)."<br />");
  124:   }
  125:   if ($target eq "edit" ) {
  126:     $result=
  127:       &Apache::edit::tag_start($target,$token,'New Script Functions').
  128: 	&Apache::edit::editfield($token->[1],$bodytext,'',40,1).
  129: 	  $error.'</td></tr>'.
  130: 	    &Apache::edit::end_table();
  131:   }
  132:   if ($target eq "modified" ) {
  133:     $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
  134:     $result=&Apache::edit::modifiedfield($token);
  135:     &Apache::lonxml::debug($result);
  136:   }
  137:   return $result;
  138: }
  139: 
  140: sub end_scriptlib {
  141:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  142:   my @result;
  143:   if ($target eq "edit" ) { $result[1]='no'; }
  144:   return @result;
  145: }
  146: 
  147: sub start_parserlib {
  148:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  149:   my $bodytext;
  150:   my $result ="";
  151:   my $error='';
  152:   if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' || $target eq 'edit' || $target eq 'answer') {
  153:     $bodytext=$$parser[$#$parser]->get_text("/parserlib");
  154:     $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
  155: 				     $$parstack[$#$parstack]);
  156:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
  157: 					       $bodytext);
  158:     my $styletext=&Apache::lonnet::getfile($location);
  159:     #&Apache::lonxml::debug("found :$bodytext: in :$location: with :$styletext:");
  160:     if ($styletext == -1) {
  161:       if ($target eq 'edit') {
  162: 	$error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
  163:       } else {
  164: 	&Apache::lonxml::error("<b> Unable to find <i>$location</i> for parserlib</b>");
  165: 	return "";
  166:       }
  167:     }
  168:     %$style = ( %$style , &Apache::style::styleparser($target,$styletext));
  169:   }
  170:   if ($target eq "edit" ) {
  171:     $result=
  172:       &Apache::edit::tag_start($target,$token,'New Tag Definitions').
  173: 	&Apache::edit::editfield($token->[1],$bodytext,'',40,1).
  174: 	  $error.'</td></tr>'.
  175: 	    &Apache::edit::end_table();
  176:   }
  177:   if ($target eq "modified" ) {
  178:     $bodytext=$$parser[$#$parser]->get_text("/parserlib");
  179:     $result=&Apache::edit::modifiedfield($token);
  180:     &Apache::lonxml::debug($result);
  181:   }
  182:   return $result;
  183: }
  184: 
  185: sub end_parserlib {
  186:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  187:   my @result;
  188:   if ($target eq "edit" ) { $result[1]='no'; }
  189:   return @result;
  190: }
  191: 
  192: sub start_window {
  193:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  194:   my $result = '';
  195:   if ($target eq 'web') {
  196:     &Apache::lonxml::startredirection;
  197:   }  elsif ($target eq 'tex') {
  198:        $result = '\unskip\footnote{';
  199:    }
  200:   return $result;  
  201: }
  202: 
  203: sub end_window {
  204:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  205:   my $result;
  206:   if ($target eq 'web') {
  207:     my $output=&Apache::lonxml::endredirection;
  208:     $output =~ s/\"/\&quot\;/g;
  209:     $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>";
  210:   } elsif ($target eq 'tex') {
  211:       $result = '}';
  212:   } else {
  213:       $result = '';
  214:   }
  215:   return $result; 
  216: }
  217: 
  218: sub start_import {
  219:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  220:   my $bodytext=$$parser[$#$parser]->get_text("/import");
  221:   my $result ="";
  222: 
  223:   $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
  224: 
  225:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
  226:     # FIXME this probably needs to be smart about construction vs.
  227:     # non construction space.
  228:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
  229:     my $file=&Apache::lonnet::getfile($location);
  230:     if ($file == -1) {
  231:       &Apache::lonxml::error("<b> Unable to find <i>$bodytext as $location</i> for import</b>");
  232:       return "";
  233:     }
  234: 
  235:     my $dir=$location;
  236:     $dir=~s:/[^/]*$::;
  237:     #  &Apache::lonxml::debug("directory $dir $location file $file \n<b>END</b>\n");
  238:     &Apache::lonxml::newparser($parser,\$file,$dir);
  239:   }
  240:   if ($target eq "edit" ) {
  241:     $result.=&Apache::edit::tag_start($target,$token);
  242:     $result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1);
  243:     #FIXME this need to convert $bodytext to be a contruction space reference
  244:     #my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
  245:     #$result.="Click<a href=\"$location\">here</a> to edit<br />"
  246:   }
  247:   if ($target eq 'modified') {
  248:     $bodytext=$$parser[$#$parser]->get_text("/import");
  249:     $result=&Apache::edit::modifiedfield($token);
  250:     &Apache::lonxml::debug($result);
  251:   }
  252:   return $result;
  253: }
  254: 
  255: sub end_import {
  256:   return '';
  257: }
  258: 
  259: sub start_storetc {
  260:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  261:   my $result = '';
  262:   &Apache::lonxml::startredirection;
  263:   return $result; 
  264: }
  265: 
  266: sub end_storetc {
  267:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  268:     my $result;
  269:     my $output=&Apache::lonxml::endredirection;
  270:     $output =~ s/\"/\&quot\;/g;
  271:     $result = '{\bf '.$output.'.}}\write\tcfile{\protect\tcpc{ '.$output.'.}{\the\value{relpage}}}';
  272:     return $result;
  273: }
  274: 
  275: 
  276: sub start_physnet {
  277:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  278:     my $bodytext = '/adm/includes/physnet.sty';
  279:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
  280:     my $cbistyletext=&Apache::lonnet::getfile($location);
  281: 
  282:     %$style = (%$style,&Apache::style::styleparser($target,$cbistyletext));
  283:     if ( defined($$style{'physnet'}) ) {
  284:         &Apache::lonxml::newparser($parser,\$$style{'physnet'});
  285:     }
  286:     return "";
  287: }
  288: 
  289: sub end_physnet {
  290:   return '';
  291: }
  292: 
  293: sub xmlparse {
  294:   my ($string) = @_;
  295:   &Apache::lonxml::debug("Got $string");
  296:   my ($target,$token,$tagstack,$parstack,$oldparser,$safeeval,$style)=
  297:     @Apache::scripttag::parser_env;
  298:   my @parser;
  299:   &Apache::lonxml::newparser(\@parser,\$string);
  300:   my $result=&Apache::lonxml::inner_xmlparse($target,$tagstack,
  301: 					     $parstack,\@parser,
  302: 					     $safeeval,$style);
  303:   return $result;
  304: }
  305: 
  306: 1;
  307: __END__

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