Annotation of loncom/xml/scripttag.pm, revision 1.39

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

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