File:  [LON-CAPA] / loncom / xml / scripttag.pm
Revision 1.48: download - view: text, annotated - select for diffs
Mon Jul 30 22:24:56 2001 UTC (22 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- improved edit interface for <display>

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

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