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

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

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