File:  [LON-CAPA] / loncom / xml / scripttag.pm
Revision 1.56: download - view: text, annotated - select for diffs
Mon Nov 5 22:59:33 2001 UTC (22 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- <import> tag makes a reference in the .meta file

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

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