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

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

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