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

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # <script> definiton
1.58      www         3: #
1.72    ! matthew     4: # $Id: scripttag.pm,v 1.71 2002/04/25 17:25:36 matthew Exp $
1.58      www         5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.34      albertel   28: # 2/21 Guy
1.50      www        29: # 8/20 Gerd Kortemeyer
1.1       albertel   30: 
1.47      albertel   31: package Apache::scripttag;
1.1       albertel   32: 
                     33: use strict;
1.3       albertel   34: use Apache::lonnet;
1.42      sakharuk   35: use Apache::style;
1.1       albertel   36: 
1.47      albertel   37: #Globals
                     38: # this used to pass around the standard callsub arguments to a tag func
                     39: # so xmlparse can reenter the inner_xmlparse loop.
                     40: 
                     41: @Apache::scripttag::parser_env = ();
1.61      harris41   42: BEGIN {
1.62      albertel   43:   &Apache::lonxml::register('Apache::scripttag',
                     44: 			    ('script','scriptlib','parserlib','import',
                     45: 			     'window','display','storetc','physnet',
1.63      albertel   46: 			     'standalone'));
1.1       albertel   47: }
                     48: 
                     49: sub start_script {
1.40      albertel   50:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.47      albertel   51:   @Apache::scripttag::parser_env = @_;
1.24      albertel   52:   my $result='';
1.39      albertel   53:   my $type= &Apache::lonxml::get_param('type',$parstack,$safeeval);
                     54:   &Apache::lonxml::debug("found type of $type");
1.17      albertel   55:   if ($type eq "loncapa/perl") {
1.21      albertel   56:     my $bodytext=&Apache::lonxml::get_all_text("/script",$$parser[$#$parser]);
1.17      albertel   57:     if ( $target eq "modified" ) {
1.41      albertel   58:       $result=$token->[4].&Apache::edit::modifiedfield();
1.54      albertel   59:     } elsif ( $target eq 'web' || $target eq 'tex' ||
                     60: 	      $target eq 'grade' || $target eq 'answer') {
1.28      albertel   61:       &Apache::run::run($bodytext,$safeeval);
1.60      albertel   62:       if (($target eq 'answer') && ($Apache::lonhomework::viewgrades == 'F')) {
1.53      albertel   63: 	$Apache::lonxml::evaluate--;
                     64: 	$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>";
1.70      albertel   65: 	my $listing= &HTML::Entities::encode(&Apache::run::dump($target,$safeeval));
1.53      albertel   66: 	$result.=$listing;
1.55      albertel   67: 	$result.= "</pre></body></html>\');newWindow.document.close();void(0);\">Script Vars</a><br />";
1.53      albertel   68:       }
1.28      albertel   69:     } elsif ($target eq "edit" ) {
1.41      albertel   70:       #&Apache::run::run($bodytext,$safeeval);
                     71:       #$result="<br /> &lt;$token->[1]&gt; output: <br />$bodytext<br />Source:<br />";
                     72:       $result=&Apache::edit::tag_start($target,$token,'Script');
                     73:       $result.=&Apache::edit::editfield($token->[1],$bodytext,'',50,4);
1.17      albertel   74:     }
                     75:   } else {
1.66      albertel   76:     if ($target ne "meta") {
                     77:       $result = $token->[4];
                     78:       my $bodytext=&Apache::lonxml::get_all_text("/script",$$parser[$#$parser]);
                     79:       $result.=$bodytext;
                     80:     }
1.13      albertel   81:   }
1.24      albertel   82:   return $result;
1.17      albertel   83: }
1.13      albertel   84: 
1.17      albertel   85: sub end_script {
1.40      albertel   86:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.28      albertel   87:   if ( $target eq "meta" ) { return ''; } 
1.39      albertel   88:   my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
1.29      albertel   89:   my $result='';
                     90:   #other script blocks need to survive
1.53      albertel   91:   if ($type ne "loncapa/perl") {
                     92:     return $token->[2];
                     93:   } elsif ($target eq 'edit' ) {
                     94:     return &Apache::edit::end_table();
1.55      albertel   95:   } elsif (($ENV{'request.state'} eq 'construct') && ($target eq 'answer')) {
1.53      albertel   96:     $Apache::lonxml::evaluate++;
                     97:   }
1.28      albertel   98:   return '';
1.24      albertel   99: }
                    100: 
                    101: sub start_display {
1.40      albertel  102:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.39      albertel  103: 
1.48      albertel  104:   my $result;
1.24      albertel  105:   my $bodytext=&Apache::lonxml::get_all_text("/display",$$parser[$#$parser]);
1.39      albertel  106: 
1.24      albertel  107:   if ( $target eq "modified" ) {
1.48      albertel  108:     $result=$token->[4].&Apache::edit::modifiedfield();
1.54      albertel  109:   } elsif ( $target eq 'web' || $target eq 'tex' ||
                    110: 	    $target eq 'grade' || $target eq 'answer') {
1.48      albertel  111:     $result=&Apache::run::run($bodytext,$safeeval);
1.55      albertel  112:     if ($target eq 'grade' || $target eq 'answer' ) {
1.48      albertel  113:       $result=''; # grade should produce no output
                    114:     }
                    115:   } elsif ($target eq "edit" ) {
                    116:     #$result = 
                    117:     #  "<br /> &lt;$token->[1]&gt; output: <br />$bodytext<br />Source:<br />";
                    118:     #$result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1);
                    119:     $result=&Apache::edit::tag_start($target,$token,'Script With Display');
                    120:     $result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1)
1.24      albertel  121:   }
                    122:   return $result;
                    123: }
                    124: 
                    125: sub end_display {
1.48      albertel  126:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    127:   if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
                    128:   return '';
1.1       albertel  129: }
1.3       albertel  130: 
                    131: sub start_scriptlib {
1.40      albertel  132:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.37      albertel  133:   my $bodytext;
                    134:   my $result ='';
                    135:   my $error='';
                    136: 
1.51      albertel  137:   if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' || $target eq 'edit' || $target eq 'answer') {
1.37      albertel  138:     $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
                    139:     $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
                    140: 				     $$parstack[$#$parstack]);
                    141:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
                    142: 					       $bodytext);
                    143:     my $script=&Apache::lonnet::getfile($location);
                    144:     if ($script == -1) {
                    145:       if ($target eq 'edit') {
                    146:         $error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
                    147:       } else {
                    148: 	&Apache::lonxml::error("<b> Unable to find <i>$location</i> for scriptlib</b>");
                    149: 	return "";
                    150:       }
                    151:     }
                    152:     &Apache::run::run($script,$safeeval);
                    153:     #&Apache::lonxml::debug("ran $bodytext:<br />".&Apache::lonnet::getfile($bodytext)."<br />");
1.18      albertel  154:   }
1.13      albertel  155:   if ($target eq "edit" ) {
1.37      albertel  156:     $result=
1.49      albertel  157:       &Apache::edit::tag_start($target,$token,'New Script Functions').
1.68      albertel  158: 	&Apache::edit::editline($token->[1],$bodytext,'',40).
1.49      albertel  159: 	  $error.'</td></tr>'.
                    160: 	    &Apache::edit::end_table();
1.37      albertel  161:   }
                    162:   if ($target eq "modified" ) {
                    163:     $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
1.65      matthew   164:     $result=$token->[4].&Apache::edit::modifiedfield($token);
1.37      albertel  165:     &Apache::lonxml::debug($result);
1.3       albertel  166:   }
                    167:   return $result;
                    168: }
                    169: 
1.37      albertel  170: sub end_scriptlib {
1.40      albertel  171:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.49      albertel  172:   my @result;
                    173:   if ($target eq "edit" ) { $result[1]='no'; }
                    174:   return @result;
1.37      albertel  175: }
1.4       albertel  176: 
                    177: sub start_parserlib {
1.40      albertel  178:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.37      albertel  179:   my $bodytext;
1.7       albertel  180:   my $result ="";
1.37      albertel  181:   my $error='';
1.51      albertel  182:   if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' || $target eq 'edit' || $target eq 'answer') {
1.37      albertel  183:     $bodytext=$$parser[$#$parser]->get_text("/parserlib");
                    184:     $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
                    185: 				     $$parstack[$#$parstack]);
                    186:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
                    187: 					       $bodytext);
                    188:     my $styletext=&Apache::lonnet::getfile($location);
                    189:     #&Apache::lonxml::debug("found :$bodytext: in :$location: with :$styletext:");
                    190:     if ($styletext == -1) {
                    191:       if ($target eq 'edit') {
                    192: 	$error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
                    193:       } else {
                    194: 	&Apache::lonxml::error("<b> Unable to find <i>$location</i> for parserlib</b>");
                    195: 	return "";
                    196:       }
                    197:     }
                    198:     %$style = ( %$style , &Apache::style::styleparser($target,$styletext));
1.18      albertel  199:   }
1.13      albertel  200:   if ($target eq "edit" ) {
1.37      albertel  201:     $result=
1.49      albertel  202:       &Apache::edit::tag_start($target,$token,'New Tag Definitions').
1.68      albertel  203: 	&Apache::edit::editline($token->[1],$bodytext,'',40).
1.49      albertel  204: 	  $error.'</td></tr>'.
                    205: 	    &Apache::edit::end_table();
1.37      albertel  206:   }
                    207:   if ($target eq "modified" ) {
                    208:     $bodytext=$$parser[$#$parser]->get_text("/parserlib");
1.65      matthew   209:     $result=$token->[4].&Apache::edit::modifiedfield($token);
1.37      albertel  210:     &Apache::lonxml::debug($result);
1.7       albertel  211:   }
                    212:   return $result;
1.4       albertel  213: }
                    214: 
                    215: sub end_parserlib {
1.40      albertel  216:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.49      albertel  217:   my @result;
                    218:   if ($target eq "edit" ) { $result[1]='no'; }
                    219:   return @result;
1.6       albertel  220: }
                    221: 
1.30      sakharuk  222: sub start_window {
1.40      albertel  223:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.20      sakharuk  224:   my $result = '';
1.31      albertel  225:   if ($target eq 'web') {
1.34      albertel  226:     &Apache::lonxml::startredirection;
1.35      sakharuk  227:   }  elsif ($target eq 'tex') {
1.42      sakharuk  228:        $result = '\unskip\footnote{';
1.35      sakharuk  229:    }
1.19      sakharuk  230:   return $result;  
                    231: }
                    232: 
1.30      sakharuk  233: sub end_window {
1.40      albertel  234:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.30      sakharuk  235:   my $result;
1.31      albertel  236:   if ($target eq 'web') {
1.34      albertel  237:     my $output=&Apache::lonxml::endredirection;
                    238:     $output =~ s/\"/\&quot\;/g;
1.35      sakharuk  239:     $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>";
                    240:   } elsif ($target eq 'tex') {
                    241:       $result = '}';
1.30      sakharuk  242:   } else {
1.35      sakharuk  243:       $result = '';
1.30      sakharuk  244:   }
1.20      sakharuk  245:   return $result; 
1.19      sakharuk  246: }
                    247: 
1.6       albertel  248: sub start_import {
1.40      albertel  249:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.31      albertel  250:   my $bodytext=$$parser[$#$parser]->get_text("/import");
1.7       albertel  251:   my $result ="";
1.13      albertel  252: 
                    253:   $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
1.14      albertel  254: 
1.51      albertel  255:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
1.46      albertel  256:     # FIXME this probably needs to be smart about construction vs.
                    257:     # non construction space.
                    258:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
                    259:     my $file=&Apache::lonnet::getfile($location);
                    260:     if ($file == -1) {
                    261:       &Apache::lonxml::error("<b> Unable to find <i>$bodytext as $location</i> for import</b>");
                    262:       return "";
                    263:     }
1.13      albertel  264: 
1.46      albertel  265:     my $dir=$location;
                    266:     $dir=~s:/[^/]*$::;
                    267:     #  &Apache::lonxml::debug("directory $dir $location file $file \n<b>END</b>\n");
1.57      albertel  268:     my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
                    269:     if (!$id) { $id=$Apache::lonxml::curdepth; }
                    270:     push(@Apache::inputtags::import,$id);
1.46      albertel  271:     &Apache::lonxml::newparser($parser,\$file,$dir);
1.56      albertel  272:   } elsif ($target eq "edit" ) {
1.46      albertel  273:     $result.=&Apache::edit::tag_start($target,$token);
1.68      albertel  274:     $result.=&Apache::edit::editline($token->[1],$bodytext,'',40);
1.46      albertel  275:     #FIXME this need to convert $bodytext to be a contruction space reference
                    276:     #my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
                    277:     #$result.="Click<a href=\"$location\">here</a> to edit<br />"
1.56      albertel  278:   } elsif ($target eq 'modified') {
1.46      albertel  279:     $bodytext=$$parser[$#$parser]->get_text("/import");
1.65      matthew   280:     $result=$token->[4].&Apache::edit::modifiedfield($token);
1.46      albertel  281:     &Apache::lonxml::debug($result);
1.56      albertel  282:   } elsif ($target eq 'meta') {
1.57      albertel  283:     my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
                    284:     $result.='<import part="'.$Apache::inputtags::part;
                    285:     if ($id) {
                    286:       $result.='" id="'.$id;
                    287:     }
                    288:     $result.='">';
1.56      albertel  289:     $result.=$bodytext;
                    290:     $result.='</import>';
1.46      albertel  291:   }
                    292:   return $result;
1.6       albertel  293: }
                    294: 
                    295: sub end_import {
1.69      albertel  296:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.57      albertel  297:   pop(@Apache::inputtags::import);
1.69      albertel  298:   my $result;
                    299:   if ($target eq 'edit' ) { $result=&Apache::edit::end_table(); }
                    300:   return $result;
1.1       albertel  301: }
1.42      sakharuk  302: 
                    303: sub start_storetc {
1.43      albertel  304:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.42      sakharuk  305:   my $result = '';
                    306:   &Apache::lonxml::startredirection;
                    307:   return $result; 
                    308: }
                    309: 
                    310: sub end_storetc {
1.43      albertel  311:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.42      sakharuk  312:     my $result;
                    313:     my $output=&Apache::lonxml::endredirection;
                    314:     $output =~ s/\"/\&quot\;/g;
1.52      albertel  315:     $result = '{\bf '.$output.'.}}\write\tcfile{\protect\tcpc{ '.$output.'.}{\the\value{relpage}}}';
                    316:     return $result;
1.42      sakharuk  317: }
                    318: 
                    319: 
                    320: sub start_physnet {
1.45      sakharuk  321:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.50      www       322:     my $bodytext = '/adm/includes/physnet.sty';
1.45      sakharuk  323:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
                    324:     my $cbistyletext=&Apache::lonnet::getfile($location);
1.42      sakharuk  325: 
1.45      sakharuk  326:     %$style = (%$style,&Apache::style::styleparser($target,$cbistyletext));
1.59      albertel  327:     $$parser['-1']->unget_token($token);
                    328: #    if ( defined($$style{'physnet'}) ) {
                    329: #        &Apache::lonxml::newparser($parser,\$$style{'physnet'});
                    330: #    }
1.45      sakharuk  331:     return "";
                    332: }
1.42      sakharuk  333: 
1.45      sakharuk  334: sub end_physnet {
1.47      albertel  335:   return '';
1.42      sakharuk  336: }
1.62      albertel  337: 
                    338: sub start_standalone {
                    339:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.64      albertel  340:   if ($target eq 'web'  && $ENV{'request.course.id'} ) {
1.62      albertel  341:     my $inside = &Apache::lonxml::get_all_text("/standalone",$$parser[-1]);
                    342:   }
                    343:   return '';
                    344: }
                    345: 
                    346: sub end_standalone {
                    347:   return '';
                    348: }
                    349: 
1.42      sakharuk  350: 
1.47      albertel  351: sub xmlparse {
                    352:   my ($string) = @_;
                    353:   &Apache::lonxml::debug("Got $string");
                    354:   my ($target,$token,$tagstack,$parstack,$oldparser,$safeeval,$style)=
                    355:     @Apache::scripttag::parser_env;
                    356:   my @parser;
                    357:   &Apache::lonxml::newparser(\@parser,\$string);
                    358:   my $result=&Apache::lonxml::inner_xmlparse($target,$tagstack,
                    359: 					     $parstack,\@parser,
                    360: 					     $safeeval,$style);
                    361:   return $result;
                    362: }
1.3       albertel  363: 
1.1       albertel  364: 1;
                    365: __END__

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