File:  [LON-CAPA] / loncom / xml / scripttag.pm
Revision 1.58: download - view: text, annotated - select for diffs
Thu Nov 29 19:03:58 2001 UTC (22 years, 6 months ago) by www
Branches: MAIN
CVS tags: HEAD
GPL

    1: # The LearningOnline Network with CAPA
    2: # <script> definiton
    3: #
    4: # $Id: scripttag.pm,v 1.58 2001/11/29 19:03:58 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: #
   28: # 2/21 Guy
   29: # 8/20 Gerd Kortemeyer
   30: 
   31: package Apache::scripttag;
   32: 
   33: use strict;
   34: use Apache::lonnet;
   35: use Apache::style;
   36: 
   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 = ();
   42: sub BEGIN {
   43:   &Apache::lonxml::register('Apache::scripttag',('script','scriptlib',
   44: 						 'parserlib','import',
   45:                                                  'window','display',
   46:                                                  'storetc','physnet'));
   47: }
   48: 
   49: sub start_script {
   50:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   51:   @Apache::scripttag::parser_env = @_;
   52:   my $result='';
   53:   my $type= &Apache::lonxml::get_param('type',$parstack,$safeeval);
   54:   &Apache::lonxml::debug("found type of $type");
   55:   if ($type eq "loncapa/perl") {
   56:     my $bodytext=&Apache::lonxml::get_all_text("/script",$$parser[$#$parser]);
   57:     if ( $target eq "modified" ) {
   58:       $result=$token->[4].&Apache::edit::modifiedfield();
   59:     } elsif ( $target eq 'web' || $target eq 'tex' ||
   60: 	      $target eq 'grade' || $target eq 'answer') {
   61:       &Apache::run::run($bodytext,$safeeval);
   62:       if (($ENV{'request.state'} eq 'construct') && ($target eq 'answer')) {
   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;
   68: 	$result.= "</pre></body></html>\');newWindow.document.close();void(0);\">Script Vars</a><br />";
   69:       }
   70:     } elsif ($target eq "edit" ) {
   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);
   75:     }
   76:   } else {
   77:     if ($target ne "meta") { $result = $token->[4]; }
   78:   }
   79:   return $result;
   80: }
   81: 
   82: sub end_script {
   83:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   84:   if ( $target eq "meta" ) { return ''; } 
   85:   my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
   86:   my $result='';
   87:   #other script blocks need to survive
   88:   if ($type ne "loncapa/perl") {
   89:     return $token->[2];
   90:   } elsif ($target eq 'edit' ) {
   91:     return &Apache::edit::end_table();
   92:   } elsif (($ENV{'request.state'} eq 'construct') && ($target eq 'answer')) {
   93:     $Apache::lonxml::evaluate++;
   94:   }
   95:   return '';
   96: }
   97: 
   98: sub start_display {
   99:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  100: 
  101:   my $result;
  102:   my $bodytext=&Apache::lonxml::get_all_text("/display",$$parser[$#$parser]);
  103: 
  104:   if ( $target eq "modified" ) {
  105:     $result=$token->[4].&Apache::edit::modifiedfield();
  106:   } elsif ( $target eq 'web' || $target eq 'tex' ||
  107: 	    $target eq 'grade' || $target eq 'answer') {
  108:     $result=&Apache::run::run($bodytext,$safeeval);
  109:     if ($target eq 'grade' || $target eq 'answer' ) {
  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)
  118:   }
  119:   return $result;
  120: }
  121: 
  122: sub end_display {
  123:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  124:   if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
  125:   return '';
  126: }
  127: 
  128: sub start_scriptlib {
  129:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  130:   my $bodytext;
  131:   my $result ='';
  132:   my $error='';
  133: 
  134:   if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' || $target eq 'edit' || $target eq 'answer') {
  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 />");
  151:   }
  152:   if ($target eq "edit" ) {
  153:     $result=
  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();
  158:   }
  159:   if ($target eq "modified" ) {
  160:     $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
  161:     $result=&Apache::edit::modifiedfield($token);
  162:     &Apache::lonxml::debug($result);
  163:   }
  164:   return $result;
  165: }
  166: 
  167: sub end_scriptlib {
  168:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  169:   my @result;
  170:   if ($target eq "edit" ) { $result[1]='no'; }
  171:   return @result;
  172: }
  173: 
  174: sub start_parserlib {
  175:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  176:   my $bodytext;
  177:   my $result ="";
  178:   my $error='';
  179:   if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' || $target eq 'edit' || $target eq 'answer') {
  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));
  196:   }
  197:   if ($target eq "edit" ) {
  198:     $result=
  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();
  203:   }
  204:   if ($target eq "modified" ) {
  205:     $bodytext=$$parser[$#$parser]->get_text("/parserlib");
  206:     $result=&Apache::edit::modifiedfield($token);
  207:     &Apache::lonxml::debug($result);
  208:   }
  209:   return $result;
  210: }
  211: 
  212: sub end_parserlib {
  213:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  214:   my @result;
  215:   if ($target eq "edit" ) { $result[1]='no'; }
  216:   return @result;
  217: }
  218: 
  219: sub start_window {
  220:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  221:   my $result = '';
  222:   if ($target eq 'web') {
  223:     &Apache::lonxml::startredirection;
  224:   }  elsif ($target eq 'tex') {
  225:        $result = '\unskip\footnote{';
  226:    }
  227:   return $result;  
  228: }
  229: 
  230: sub end_window {
  231:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  232:   my $result;
  233:   if ($target eq 'web') {
  234:     my $output=&Apache::lonxml::endredirection;
  235:     $output =~ s/\"/\&quot\;/g;
  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 = '}';
  239:   } else {
  240:       $result = '';
  241:   }
  242:   return $result; 
  243: }
  244: 
  245: sub start_import {
  246:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  247:   my $bodytext=$$parser[$#$parser]->get_text("/import");
  248:   my $result ="";
  249: 
  250:   $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
  251: 
  252:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
  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:     }
  261: 
  262:     my $dir=$location;
  263:     $dir=~s:/[^/]*$::;
  264:     #  &Apache::lonxml::debug("directory $dir $location file $file \n<b>END</b>\n");
  265:     my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
  266:     if (!$id) { $id=$Apache::lonxml::curdepth; }
  267:     push(@Apache::inputtags::import,$id);
  268:     &Apache::lonxml::newparser($parser,\$file,$dir);
  269:   } elsif ($target eq "edit" ) {
  270:     $result.=&Apache::edit::tag_start($target,$token);
  271:     $result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1);
  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 />"
  275:   } elsif ($target eq 'modified') {
  276:     $bodytext=$$parser[$#$parser]->get_text("/import");
  277:     $result=&Apache::edit::modifiedfield($token);
  278:     &Apache::lonxml::debug($result);
  279:   } elsif ($target eq 'meta') {
  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.='">';
  286:     $result.=$bodytext;
  287:     $result.='</import>';
  288:   }
  289:   return $result;
  290: }
  291: 
  292: sub end_import {
  293:   pop(@Apache::inputtags::import);
  294:   return '';
  295: }
  296: 
  297: sub start_storetc {
  298:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  299:   my $result = '';
  300:   &Apache::lonxml::startredirection;
  301:   return $result; 
  302: }
  303: 
  304: sub end_storetc {
  305:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  306:     my $result;
  307:     my $output=&Apache::lonxml::endredirection;
  308:     $output =~ s/\"/\&quot\;/g;
  309:     $result = '{\bf '.$output.'.}}\write\tcfile{\protect\tcpc{ '.$output.'.}{\the\value{relpage}}}';
  310:     return $result;
  311: }
  312: 
  313: 
  314: sub start_physnet {
  315:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  316:     my $bodytext = '/adm/includes/physnet.sty';
  317:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
  318:     my $cbistyletext=&Apache::lonnet::getfile($location);
  319: 
  320:     %$style = (%$style,&Apache::style::styleparser($target,$cbistyletext));
  321:     if ( defined($$style{'physnet'}) ) {
  322:         &Apache::lonxml::newparser($parser,\$$style{'physnet'});
  323:     }
  324:     return "";
  325: }
  326: 
  327: sub end_physnet {
  328:   return '';
  329: }
  330: 
  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: }
  343: 
  344: 1;
  345: __END__

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