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

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

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