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

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

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