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

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # <script> definiton
                      3: 
                      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.30    ! sakharuk   13:                                                  'window','meta'));
1.24      albertel   14:                                                  'footnote','display'));
1.1       albertel   15: }
                     16: 
                     17: sub start_script {
1.2       albertel   18:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
1.17      albertel   19:   my $args ='';
1.29      albertel   20:   if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
1.17      albertel   21:   my $type = &Apache::run::run("{$args;".'return $type}',$safeeval);
1.24      albertel   22:   my $result='';
1.18      albertel   23:   #&Apache::lonxml::debug("found type of $type");
1.17      albertel   24:   if ($type eq "loncapa/perl") {
1.21      albertel   25:     my $bodytext=&Apache::lonxml::get_all_text("/script",$$parser[$#$parser]);
1.17      albertel   26:     
                     27:     if ( $target eq "modified" ) {
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.24      albertel   32:       $result="<br> &lt;$token->[1]&gt; output: <br>$bodytext<br>Source:<br>";
                     33:       $result.=&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 = 
                     70:       "<br> &lt;$token->[1]&gt; output: <br>$bodytext<br>Source:<br>";
                     71:     $result.=&editfield($token->[1],$bodytext);
                     72:   }
                     73:   return $result;
                     74: }
                     75: 
                     76: sub end_display {
1.1       albertel   77: }
1.3       albertel   78: 
                     79: sub start_scriptlib {
                     80:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
1.5       albertel   81:   my $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
1.3       albertel   82:   my $result ="";
1.13      albertel   83: 
1.18      albertel   84:   $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
                     85: 				   $$parstack[$#$parstack]);
                     86:   my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
                     87: 					     $bodytext);
                     88:   my $script=&Apache::lonnet::getfile($location);
                     89:   if ($script == -1) {
                     90:     &Apache::lonxml::error("<b> Unable to find <i>$location</i> for scriptlib</b>");
                     91:     return "";
                     92:   }
                     93:   &Apache::run::run($script,$safeeval);
                     94:   #&Apache::lonxml::debug("ran $bodytext:<br>".&Apache::lonnet::getfile($bodytext)."<br>");
1.13      albertel   95: 
                     96:   if ($target eq "edit" ) {
                     97:     $result.=&editfield($token->[1],$bodytext);
1.3       albertel   98:   }
                     99:   return $result;
                    100: }
                    101: 
                    102: sub end_scriptlib {}
1.4       albertel  103: 
                    104: sub start_parserlib {
                    105:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.6       albertel  106:   my $bodytext=$$parser[$#$parser]->get_text("/parserlib");
1.7       albertel  107:   my $result ="";
1.13      albertel  108: 
1.18      albertel  109:   $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
                    110: 				  $$parstack[$#$parstack]);
                    111:   my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
                    112: 					     $bodytext);
                    113:   my $styletext=&Apache::lonnet::getfile($location);
1.28      albertel  114:   #&Apache::lonxml::debug("found :$bodytext: in :$location: with :$styletext:");
1.18      albertel  115:   if ($styletext == -1) {
                    116:     &Apache::lonxml::error("<b> Unable to find <i>$location</i> for parserlib</b>");
                    117:     return "";
                    118:   }
                    119:   %$style = ( %$style , &Apache::style::styleparser($target,$styletext));
1.13      albertel  120: 
                    121:   if ($target eq "edit" ) {
1.7       albertel  122:     $result=&editfield($token->[1],$bodytext);
                    123:   }
                    124:   return $result;
1.4       albertel  125: }
                    126: 
                    127: sub end_parserlib {
1.6       albertel  128: }
                    129: 
1.30    ! sakharuk  130: sub start_window {
1.19      sakharuk  131:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.20      sakharuk  132:   my $result = '';
1.30    ! sakharuk  133:    if ($target eq 'web') {
        !           134:      $Apache::lonxml::redirection = 0;
        !           135:    } elsif ($target eq 'tex') {
        !           136:        $result = '\footnote{';
        !           137:    }
1.19      sakharuk  138:   return $result;  
                    139: }
                    140: 
1.30    ! sakharuk  141: sub end_window {
1.19      sakharuk  142:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.30    ! sakharuk  143:   my $result;
        !           144:    if ($target eq 'web') {
        !           145:      $Apache::lonxml::outputstack =~ s/\"/\&quot\;/g;
        !           146:      $result = "<a href=\"javascript:newWindow=open(\'\',\'new_W\',\'width=500,height=200\');newWindow.document.open(\'text/html\',\'replace\');newWindow.document.writeln(\'<html><head><title>newwindow</title></head><body bgcolor=&quot;#FFFFFF&quot;> $Apache::lonxml::outputstack </body></html>\');newWindow.document.close();void(0);\"><sup>*</sup></a>";
1.20      sakharuk  147:    $Apache::lonxml::outputstack = "";
                    148:    $Apache::lonxml::redirection = 1;
1.30    ! sakharuk  149:   } elsif ($target eq 'tex') {
        !           150:           $result = '}';
        !           151:   } else {
        !           152:         $result = '';
        !           153:   }
1.20      sakharuk  154:   return $result; 
1.19      sakharuk  155: }
                    156: 
1.6       albertel  157: sub start_import {
1.7       albertel  158:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.30    ! sakharuk  159:   my $bodytext=&Apache::lonxml::get_all_text("/import",$$parser[$#$parser]); 
        !           160: #  my $bodytext=$$parser[$#$parser]->get_text("/import");
1.7       albertel  161:   my $result ="";
1.13      albertel  162: 
                    163:   $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
1.18      albertel  164:   my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
                    165:   my $file=&Apache::lonnet::getfile($location);
                    166:   if ($file == -1) {
                    167:     &Apache::lonxml::error("<b> Unable to find <i>$bodytext $location</i> for import</b>");
                    168:     return "";
1.14      albertel  169:   }
                    170: 
1.18      albertel  171:   my $dir=$location;
1.30    ! sakharuk  172:   $dir=~s:/[^/]*$::;  
        !           173: #  &Apache::lonxml::debug("directory $dir $location file $file \n<b>END</b>\n");
1.18      albertel  174:   &Apache::lonxml::newparser($parser,\$file,$dir);
1.13      albertel  175: 
                    176:   if ($target eq "edit" ) {
                    177:     $result.=&editfield($token->[1],$bodytext);
1.8       albertel  178:     $result.="Click<a href=\"/res/$bodytext\">here</a> to edit<br></br>"
1.7       albertel  179:   }
1.27      albertel  180:   return '';
1.6       albertel  181: }
                    182: 
                    183: sub end_import {
1.7       albertel  184: }
1.30    ! sakharuk  185: 
        !           186: sub start_meta {
        !           187:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
        !           188:   my $result = '';
        !           189:     if ($target ne 'meta') {
        !           190:        $result = &Apache::lonxml::get_all_text("/meta",$$parser[$#$parser]); 
        !           191:        $result = '';
        !           192:       return $result; 
        !           193:     } else {
        !           194:        return $result; 
        !           195:     }
        !           196: }
        !           197: 
        !           198: sub end_meta {
        !           199: }
        !           200: 
        !           201: 
        !           202: 
        !           203: 
        !           204: 
        !           205: 
1.7       albertel  206: 
                    207: sub editfield {
                    208:   my ($tag,$data)=@_;
                    209:   
                    210:   my $count=0;
                    211:   my $maxlength=-1;
                    212:   map { $count++;
                    213: 	if (length($_) > $maxlength) { $maxlength = length ($_); }
                    214:       } split ("\n", $data);
                    215: 	  
                    216:   return "<br></br>\n&lt;$tag&gt;<br></br>\n&nbsp;&nbsp;&nbsp;<textarea rows=\"$count\" cols=\"$maxlength\" name=homework_edit_".$Apache::lonxml::curdepth.">$data</textarea><br></br>\n&lt;/$tag&gt;<br></br>\n";
1.4       albertel  217: }
1.3       albertel  218: 
1.18      albertel  219: sub getfilenothere {
1.3       albertel  220:   my ($filename) = @_;
                    221:   my $a="";
1.11      tsai      222:   
1.12      tsai      223:   $filename=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.10      albertel  224:   $filename="/home/httpd/html/res".$filename;
                    225:   if (! -e $filename ) {
                    226:     &Apache::lonnet::subscribe($filename);
                    227:     &Apache::lonnet::repcopy($filename);
                    228:   }
1.14      albertel  229:   if (! -e $filename ) { return -1; };
1.11      tsai      230:   my $fh=Apache::File->new($filename);
                    231:   while (<$fh>) {
1.3       albertel  232:       $a .=$_;
                    233:   }
                    234:   return $a
1.1       albertel  235: }
1.3       albertel  236: 
1.1       albertel  237: 1;
                    238: __END__

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