File:  [LON-CAPA] / loncom / xml / scripttag.pm
Revision 1.38: download - view: text, annotated - select for diffs
Sun May 13 20:01:11 2001 UTC (23 years ago) by albertel
Branches: MAIN
CVS tags: HEAD
- changed the args to tag_start
- added args to editfield

    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: 
   10: sub BEGIN {
   11:   &Apache::lonxml::register('Apache::scripttag',('script','scriptlib',
   12: 						 'parserlib','import',
   13:                                                  'window','display'));
   14: }
   15: 
   16: sub start_script {
   17:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   18:   my $args ='';
   19:   if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
   20:   my $type = &Apache::run::run("{$args;".'return $type}',$safeeval);
   21:   my $result='';
   22:   #&Apache::lonxml::debug("found type of $type");
   23:   if ($type eq "loncapa/perl") {
   24:     my $bodytext=&Apache::lonxml::get_all_text("/script",$$parser[$#$parser]);
   25:     
   26:     if ( $target eq "modified" ) {
   27:       $result=$token->[4].$bodytext;
   28:     } elsif ( $target eq "web" || $target eq "grade" ) {
   29:       &Apache::run::run($bodytext,$safeeval);
   30:     } elsif ($target eq "edit" ) {
   31:       &Apache::run::run($bodytext,$safeeval);
   32:       $result="<br /> &lt;$token->[1]&gt; output: <br />$bodytext<br />Source:<br />";
   33:       $result.=&Apache::edit::editfield($token->[1],$bodytext,'',50,5);
   34:     }
   35:   } else {
   36:     if ($target ne "meta") { $result = $token->[4]; }
   37:   }
   38:   return $result;
   39: }
   40: 
   41: sub end_script {
   42:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   43:   if ( $target eq "meta" ) { return ''; } 
   44:   my $args ='';
   45:   if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
   46:   my $type = &Apache::run::run("{$args;".'return $type}',$safeeval);
   47:   my $result='';
   48:   #other script blocks need to survive
   49:   if ($type ne "loncapa/perl") { return $token->[2]; }
   50:   return '';
   51: }
   52: 
   53: sub start_display {
   54:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   55:   my $args ='';
   56:   if ( $#$parstack > -1 ) {
   57:     $args=$$parstack[$#$parstack];
   58:   }
   59:   my $bodytext=&Apache::lonxml::get_all_text("/display",$$parser[$#$parser]);
   60:   
   61:   if ( $target eq "modified" ) {
   62:   }
   63:   my $result=&Apache::run::run($bodytext,$safeeval);
   64:   if ($target eq 'grade' ) {
   65:     # grade should produce no output
   66:     $result='';
   67:   }
   68:   if ($target eq "edit" ) {
   69:     $result = 
   70:       "<br /> &lt;$token->[1]&gt; output: <br />$bodytext<br />Source:<br />";
   71:     $result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1);
   72:   }
   73:   return $result;
   74: }
   75: 
   76: sub end_display {
   77:   return ''
   78: }
   79: 
   80: sub start_scriptlib {
   81:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
   82:   my $bodytext;
   83:   my $result ='';
   84:   my $error='';
   85: 
   86:   if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' || $target eq 'edit') {
   87:     $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
   88:     $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
   89: 				     $$parstack[$#$parstack]);
   90:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
   91: 					       $bodytext);
   92:     my $script=&Apache::lonnet::getfile($location);
   93:     if ($script == -1) {
   94:       if ($target eq 'edit') {
   95:         $error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
   96:       } else {
   97: 	&Apache::lonxml::error("<b> Unable to find <i>$location</i> for scriptlib</b>");
   98: 	return "";
   99:       }
  100:     }
  101:     &Apache::run::run($script,$safeeval);
  102:     #&Apache::lonxml::debug("ran $bodytext:<br />".&Apache::lonnet::getfile($bodytext)."<br />");
  103:   }
  104:   if ($target eq "edit" ) {
  105:     $result=
  106:       &Apache::edit::tag_start($target,$token).
  107: 	&Apache::edit::editfield($token->[1],$bodytext,'New Script Functions',40,1).
  108: 	  $error;
  109:   }
  110:   if ($target eq "modified" ) {
  111:     $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
  112:     $result=&Apache::edit::modifiedfield($token);
  113:     &Apache::lonxml::debug($result);
  114:   }
  115:   return $result;
  116: }
  117: 
  118: sub end_scriptlib {
  119:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  120:   my $result='';
  121: #  if ($target eq "edit" ) { $result=" "; }
  122:   return $result;
  123: }
  124: 
  125: sub start_parserlib {
  126:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  127:   my $bodytext;
  128:   my $result ="";
  129:   my $error='';
  130: 
  131:   if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' || $target eq 'edit') {
  132:     $bodytext=$$parser[$#$parser]->get_text("/parserlib");
  133:     $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
  134: 				     $$parstack[$#$parstack]);
  135:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
  136: 					       $bodytext);
  137:     my $styletext=&Apache::lonnet::getfile($location);
  138:     #&Apache::lonxml::debug("found :$bodytext: in :$location: with :$styletext:");
  139:     if ($styletext == -1) {
  140:       if ($target eq 'edit') {
  141: 	$error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
  142:       } else {
  143: 	&Apache::lonxml::error("<b> Unable to find <i>$location</i> for parserlib</b>");
  144: 	return "";
  145:       }
  146:     }
  147:     %$style = ( %$style , &Apache::style::styleparser($target,$styletext));
  148:   }
  149:   if ($target eq "edit" ) {
  150:     $result=
  151:       &Apache::edit::tag_start($target,$token).
  152: 	&Apache::edit::editfield($token->[1],$bodytext,"New Tag Definitions",40,1).
  153: 	  $error;
  154:   }
  155:   if ($target eq "modified" ) {
  156:     $bodytext=$$parser[$#$parser]->get_text("/parserlib");
  157:     $result=&Apache::edit::modifiedfield($token);
  158:     &Apache::lonxml::debug($result);
  159:   }
  160:   return $result;
  161: }
  162: 
  163: sub end_parserlib {
  164:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  165:   my $result='';
  166: #  if ($target eq "edit" ) { 
  167: #    $result=&Apache::edit::tag_end($target,$token);
  168: #  }
  169:   return $result;
  170: }
  171: 
  172: sub start_window {
  173:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  174:   my $result = '';
  175:   if ($target eq 'web') {
  176:     &Apache::lonxml::startredirection;
  177:   }  elsif ($target eq 'tex') {
  178:        $result = '\footnote{';
  179:    }
  180:   return $result;  
  181: }
  182: 
  183: sub end_window {
  184:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  185:   my $result;
  186:   if ($target eq 'web') {
  187:     my $output=&Apache::lonxml::endredirection;
  188:     $output =~ s/\"/\&quot\;/g;
  189:     $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>";
  190:   } elsif ($target eq 'tex') {
  191:       $result = '}';
  192:   } else {
  193:       $result = '';
  194:   }
  195:   return $result; 
  196: }
  197: 
  198: sub start_import {
  199:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  200:   my $bodytext=$$parser[$#$parser]->get_text("/import");
  201:   my $result ="";
  202: 
  203:   $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
  204:   my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
  205:   my $file=&Apache::lonnet::getfile($location);
  206:   if ($file == -1) {
  207:     &Apache::lonxml::error("<b> Unable to find <i>$bodytext $location</i> for import</b>");
  208:     return "";
  209:   }
  210: 
  211:   my $dir=$location;
  212:   $dir=~s:/[^/]*$::;  
  213: #  &Apache::lonxml::debug("directory $dir $location file $file \n<b>END</b>\n");
  214:   &Apache::lonxml::newparser($parser,\$file,$dir);
  215: 
  216:   if ($target eq "edit" ) {
  217:     $result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1);
  218:     $result.="Click<a href=\"/res/$bodytext\">here</a> to edit<br />"
  219:   }
  220:   return '';
  221: }
  222: 
  223: sub end_import {
  224: }
  225: 
  226: sub start_meta {
  227:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  228:   my $result = '';
  229:     if ($target ne 'meta') {
  230:        $result = &Apache::lonxml::get_all_text("/meta",$$parser[$#$parser]); 
  231:        $result = '';
  232:       return $result; 
  233:     } else {
  234:        return $result; 
  235:     }
  236: }
  237: 
  238: sub end_meta {
  239: }
  240: 
  241: sub getfilenothere {
  242:   my ($filename) = @_;
  243:   my $a="";
  244:   
  245:   $filename=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
  246:   $filename="/home/httpd/html/res".$filename;
  247:   if (! -e $filename ) {
  248:     &Apache::lonnet::subscribe($filename);
  249:     &Apache::lonnet::repcopy($filename);
  250:   }
  251:   if (! -e $filename ) { return -1; };
  252:   my $fh=Apache::File->new($filename);
  253:   while (<$fh>) {
  254:       $a .=$_;
  255:   }
  256:   return $a
  257: }
  258: 
  259: 1;
  260: __END__

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