File:  [LON-CAPA] / loncom / xml / scripttag.pm
Revision 1.52: download - view: text, annotated - select for diffs
Wed Oct 3 13:34:00 2001 UTC (22 years, 8 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- quoting bug

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

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