File:  [LON-CAPA] / loncom / xml / scripttag.pm
Revision 1.101: download - view: text, annotated - select for diffs
Fri Oct 3 17:15:01 2003 UTC (20 years, 7 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- don't interpolate the result from <display>

    1: # The LearningOnline Network with CAPA
    2: # <script> definiton
    3: #
    4: # $Id: scripttag.pm,v 1.101 2003/10/03 17:15:01 albertel 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: BEGIN {
   43:   &Apache::lonxml::register('Apache::scripttag',
   44: 			    ('script','scriptlib','parserlib','import',
   45: 			     'window','display','storetc','physnet',
   46: 			     'standalone','comment',
   47: 			     'LONCAPA_INTERNAL_TURN_STYLE_ON',
   48: 			     'LONCAPA_INTERNAL_TURN_STYLE_OFF',
   49: 			     'LONCAPA_INTERNAL_LONHTTPD_PORT'));
   50: }
   51: 
   52: sub start_LONCAPA_INTERNAL_TURN_STYLE_ON {
   53:     $Apache::lonxml::usestyle=1;
   54:     $Apache::lonxml::style_values='';
   55:     return ('','no');
   56: }
   57: 
   58: sub end_LONCAPA_INTERNAL_TURN_STYLE_ON {
   59:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   60:     my $end=&Apache::lonxml::get_param('end',$parstack,$safeeval);
   61:     if (defined($end)) {
   62: 	&Apache::lonxml::end_tag($tagstack,$parstack,$token);
   63:     }
   64:     return ('','no');
   65: }
   66: 
   67: sub start_LONCAPA_INTERNAL_TURN_STYLE_OFF {
   68:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   69:     $Apache::lonxml::usestyle=0;
   70:     my $end=&Apache::lonxml::get_param('end',$parstack,$safeeval);
   71:     if (!$end) {
   72: 	$Apache::lonxml::style_values=$$parstack[-1];
   73: 	$Apache::lonxml::style_end_values=$$parstack[-1];
   74:     } else {
   75: 	$Apache::lonxml::style_values=$Apache::lonxml::style_end_values;
   76: 	$Apache::lonxml::style_end_values='';
   77:     }
   78:     return ('','no');
   79: }
   80: 
   81: sub end_LONCAPA_INTERNAL_TURN_STYLE_OFF {
   82:     return ('','no');
   83: }
   84: 
   85: sub start_LONCAPA_INTERNAL_LONHTTPD_PORT {
   86:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   87:     if ($target eq 'web') {
   88: 	my $lonhttpdPort=$Apache::lonnet::perlvar{'lonhttpdPort'};
   89: 	if (!defined($lonhttpdPort)) { $lonhttpdPort='8080'; }
   90: 	return '<script type="text/javascript">var lonhttpdport=\''.
   91: 	    $lonhttpdPort.'\';</script>';
   92:     }
   93:     return ('','no');
   94: }
   95: 
   96: sub end_LONCAPA_INTERNAL_LONHTTPD_PORT {
   97:     return ('','no');
   98: }
   99: 
  100: sub start_script {
  101:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  102:   @Apache::scripttag::parser_env = @_;
  103:   my $result='';
  104:   my $type= &Apache::lonxml::get_param('type',$parstack,$safeeval);
  105:   &Apache::lonxml::debug("found type of $type");
  106:   if ($type eq "loncapa/perl") {
  107:     my $bodytext=&Apache::lonxml::get_all_text("/script",$parser);
  108:     if ( $target eq "modified" ) {
  109:       $result=$token->[4].&Apache::edit::modifiedfield();
  110:     } elsif ( $target eq 'web' || $target eq 'tex' ||
  111: 	      $target eq 'grade' || $target eq 'answer' ||
  112: 	      $target eq 'analyze' ) {
  113: 	if (!$Apache::lonxml::default_homework_loaded) {
  114: 	    &Apache::lonxml::default_homework_load($safeeval);
  115: 	}
  116: 	&Apache::run::run($bodytext,$safeeval);
  117: 	if (($target eq 'answer') &&
  118: 	    ($ENV{'form.answer_output_mode'} ne 'tex') &&
  119: 	    ($Apache::lonhomework::viewgrades == 'F')) {
  120: 	    $Apache::lonxml::evaluate--;
  121: 	    $result.="<script type=\"text/javascript\"> function LONCAPA_scriptvars_$Apache::lonxml::curdepth() {newWindow=open('','new_W','width=500,height=200,scrollbars=1');newWindow.document.open('text/html','replace');newWindow.document.writeln('<html><head><title>Script Vars</title></head><body bgcolor=\"#FFFFFF\"><pre>";
  122: 	    my $listing=&Apache::run::dump($target,$safeeval);
  123: 	    $listing=~s/\'/\\\'/g;
  124: 	    $listing=~s/\n/\\n/g;
  125: 	    $listing=~s/\r/\\r/g;
  126: 	    $result.=$listing;
  127: 	    $result.= "</pre></body></html>');newWindow.document.close();}</script><a href=\"javascript:LONCAPA_scriptvars_$Apache::lonxml::curdepth();void(0);\">Script Vars</a><br />";
  128: 	}
  129:     } elsif ($target eq "edit" ) {
  130:       #&Apache::run::run($bodytext,$safeeval);
  131:       #$result="<br /> &lt;$token->[1]&gt; output: <br />$bodytext<br />Source:<br />";
  132:       $result=&Apache::edit::tag_start($target,$token,'Script');
  133:       $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,4);
  134:     }
  135:   } else {
  136:     if ($target ne "meta") {
  137:       $result = $token->[4];
  138:       my $bodytext=&Apache::lonxml::get_all_text("/script",$parser);
  139:       $result.=$bodytext;
  140:     }
  141:   }
  142:   return $result;
  143: }
  144: 
  145: sub end_script {
  146:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  147:   if ( $target eq "meta" ) { return ''; } 
  148:   my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
  149:   my $result='';
  150:   #other script blocks need to survive
  151:   if ($type ne "loncapa/perl") {
  152:     return $token->[2];
  153:   } elsif ($target eq 'edit' ) {
  154:     return &Apache::edit::end_table();
  155:   } elsif ($target eq 'answer') {
  156:     $Apache::lonxml::evaluate++;
  157:   }
  158:   return '';
  159: }
  160: 
  161: sub start_display {
  162:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  163: 
  164:   my $result;
  165:   my $bodytext=&Apache::lonxml::get_all_text("/display",$parser);
  166: 
  167:   if ( $target eq "modified" ) {
  168:     $result=$token->[4].&Apache::edit::modifiedfield();
  169:   } elsif ( $target eq 'web' || $target eq 'tex' ||
  170: 	    $target eq 'grade' || $target eq 'answer' ||
  171: 	      $target eq 'analyze') {
  172:       if (!$Apache::lonxml::default_homework_loaded) {
  173: 	  &Apache::lonxml::default_homework_load($safeeval);
  174:       }
  175:       $result=&Apache::run::run($bodytext,$safeeval);
  176:       if ($target eq 'grade' || $target eq 'answer' ||
  177: 	  $target eq 'analyze') {
  178: 	  $result=''; # grade should produce no output
  179:       }
  180:       $Apache::lonxml::post_evaluate=0;
  181:   } elsif ($target eq "edit" ) {
  182:     #$result = 
  183:     #  "<br /> &lt;$token->[1]&gt; output: <br />$bodytext<br />Source:<br />";
  184:     #$result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1);
  185:     $result=&Apache::edit::tag_start($target,$token,'Script With Display');
  186:     $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,1)
  187:   }
  188:   return $result;
  189: }
  190: 
  191: sub end_display {
  192:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  193:   if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
  194:   return '';
  195: }
  196: 
  197: sub start_scriptlib {
  198:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  199:   my $bodytext;
  200:   my $result ='';
  201:   my $error='';
  202: 
  203:   if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' || 
  204:       $target eq 'edit' || $target eq 'answer' || $target eq 'analyze') {
  205:     $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
  206:     $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
  207: 				     $$parstack[$#$parstack]);
  208:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
  209: 					       $bodytext);
  210:     my $script=&Apache::lonnet::getfile($location);
  211:     if ($script == -1) {
  212:       if ($target eq 'edit') {
  213:         $error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
  214:       } else {
  215: 	&Apache::lonxml::error("<b> Unable to find <i>$location</i> for scriptlib</b>");
  216: 	return "";
  217:       }
  218:     }
  219:     &Apache::run::run($script,$safeeval);
  220:     #&Apache::lonxml::debug("ran $bodytext:<br />".&Apache::lonnet::getfile($bodytext)."<br />");
  221:   }
  222:   if ($target eq "edit" ) {
  223:     $result=
  224:       &Apache::edit::tag_start($target,$token,'New Script Functions').
  225: 	&Apache::edit::editline($token->[1],$bodytext,'scriptlib',40).
  226:             &Apache::edit::browse(undef,'textnode').
  227: 	  $error.'</td></tr>'.
  228: 	    &Apache::edit::end_table();
  229:   }
  230:   if ($target eq "modified" ) {
  231:     $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
  232:     $result=$token->[4].&Apache::edit::modifiedfield($token);
  233:     &Apache::lonxml::debug($result);
  234:   }
  235:   return $result;
  236: }
  237: 
  238: sub end_scriptlib {
  239:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  240:   my @result;
  241:   if ($target eq "edit" ) { $result[1]='no'; }
  242:   return @result;
  243: }
  244: 
  245: sub start_parserlib {
  246:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  247:   my $bodytext;
  248:   my $result ="";
  249:   my $error='';
  250:   if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' ||
  251:       $target eq 'edit' || $target eq 'answer' || $target eq 'analyze') {
  252:     $bodytext=$$parser[$#$parser]->get_text("/parserlib");
  253:     $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
  254: 				     $$parstack[$#$parstack]);
  255:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
  256: 					       $bodytext);
  257:     my $styletext=&Apache::lonnet::getfile($location);
  258:     #&Apache::lonxml::debug("found :$bodytext: in :$location: with :$styletext:");
  259:     if ($styletext == -1) {
  260:       if ($target eq 'edit') {
  261: 	$error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
  262:       } else {
  263: 	&Apache::lonxml::error("<b> Unable to find <i>$location</i> for parserlib</b>");
  264: 	return "";
  265:       }
  266:     }
  267:     %$style = ( %$style , &Apache::style::styleparser($target,$styletext));
  268:   }
  269:   if ($target eq "edit" ) {
  270:     $result=
  271:       &Apache::edit::tag_start($target,$token,'New Tag Definitions').
  272: 	&Apache::edit::editline($token->[1],$bodytext,'',40).
  273: 	  $error.'</td></tr>'.
  274: 	    &Apache::edit::end_table();
  275:   }
  276:   if ($target eq "modified" ) {
  277:     $bodytext=$$parser[$#$parser]->get_text("/parserlib");
  278:     $result=$token->[4].&Apache::edit::modifiedfield($token);
  279:     &Apache::lonxml::debug($result);
  280:   }
  281:   return $result;
  282: }
  283: 
  284: sub end_parserlib {
  285:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  286:   my @result;
  287:   if ($target eq "edit" ) { $result[1]='no'; }
  288:   return @result;
  289: }
  290: 
  291: sub start_window {
  292:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  293:   my $result = '';
  294:   if ($target eq 'web') {
  295:     &Apache::lonxml::startredirection;
  296:   }  elsif ($target eq 'tex') {
  297:        $result = '\unskip\footnote{';
  298:    }
  299:   return $result;  
  300: }
  301: 
  302: sub end_window {
  303:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  304:   my $result;
  305:   if ($target eq 'web') {
  306:     my $output=&Apache::lonxml::endredirection;
  307:     $output =~ s/[\n\r]/ /g;
  308: #    $output = &HTML::Entities::encode($output,'<>&"\'');
  309:     $output =~ s/\'/\\\'/g;
  310:     my $linktext= &Apache::lonxml::get_param('linktext',$parstack,$safeeval);
  311:     if (!$linktext) { $linktext='<sup>*</sup>'; }
  312:     my $width= &Apache::lonxml::get_param('width',$parstack,$safeeval);
  313:     if (!$width) { $width='500'; }
  314:     my $height= &Apache::lonxml::get_param('height',$parstack,$safeeval);
  315:     if (!$height) { $height='200'; }
  316:     $result = "<script type=\"text/javascript\"> function LONCAPA_newwindow_$Apache::lonxml::curdepth() {newWindow=open('','new_W','width=$width,height=$height,scrollbars=1');newWindow.document.open('text/html','replace');newWindow.document.writeln('<html><head><title>newwindow</title></head><body bgcolor=\"#FFFFFF\"> $output </body></html>');newWindow.document.close();}</script><a href=\"javascript:LONCAPA_newwindow_$Apache::lonxml::curdepth();void(0);\">$linktext</a>";
  317:   } elsif ($target eq 'tex') {
  318:       $result = '}';
  319:   } else {
  320:       $result = '';
  321:   }
  322:   return $result; 
  323: }
  324: 
  325: sub start_import {
  326:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  327:   my $bodytext=$$parser[$#$parser]->get_text("/import");
  328:   my $result ="";
  329: 
  330:   $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
  331: 
  332:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  333:       $target eq 'tex' || $target eq 'analyze' ) {
  334:     # FIXME this probably needs to be smart about construction vs.
  335:     # non construction space.
  336:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
  337:     my $file=&Apache::lonnet::getfile($location);
  338:     if ($file == -1) {
  339:       &Apache::lonxml::error("<b> Unable to find <i>$bodytext as $location</i> for import</b>");
  340:       return "";
  341:     }
  342: 
  343:     my $dir=$location;
  344:     $dir=~s:/[^/]*$::;
  345:     #  &Apache::lonxml::debug("directory $dir $location file $file \n<b>END</b>\n");
  346:     my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
  347:     if (!$id) { $id=$Apache::lonxml::curdepth; }
  348:     push(@Apache::inputtags::import,$id);
  349:     push(@Apache::inputtags::importlist,$id);
  350: 
  351: 	&Apache::lonxml::newparser($parser,\$file,$dir);
  352: 
  353:   } elsif ($target eq "edit" ) {
  354:     $result.=&Apache::edit::tag_start($target,$token);
  355:     $result.=&Apache::edit::editline($token->[1],$bodytext,'',40);
  356:     $result.=&Apache::edit::browse(undef,'textnode');
  357:     #FIXME this need to convert $bodytext to be a contruction space reference
  358:     #my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
  359:     #$result.="Click<a href=\"$location\">here</a> to edit<br />"
  360:   } elsif ($target eq 'modified') {
  361:     $bodytext=$$parser[$#$parser]->get_text("/import");
  362:     $result=$token->[4].&Apache::edit::modifiedfield($token);
  363:     &Apache::lonxml::debug($result);
  364:   } elsif ($target eq 'meta') {
  365:     my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
  366:     $result.='<import part="'.$Apache::inputtags::part;
  367:     if ($id) {
  368:       $result.='" id="'.$id;
  369:     }
  370:     $result.='">';
  371:     $result.=$bodytext;
  372:     $result.='</import>';
  373:   }
  374:   return $result;
  375: }
  376: 
  377: sub end_import {
  378:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  379:   pop(@Apache::inputtags::import);
  380:   my $result;
  381:   if ($target eq 'edit' ) { $result=&Apache::edit::end_table(); }
  382:   return $result;
  383: }
  384: 
  385: sub start_storetc {
  386:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  387:   my $result = '';
  388:   &Apache::lonxml::startredirection;
  389:   return $result; 
  390: }
  391: 
  392: sub end_storetc {
  393:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  394:     my $result;
  395:     my $output=&Apache::lonxml::endredirection;
  396:     $output =~ s/\"/\&quot\;/g;
  397:     $result = '{\bf '.$output.'.}}\write\tcfile{\protect\tcpc{ '.$output.'.}{\the\value{relpage}}}';
  398:     return $result;
  399: }
  400: 
  401: 
  402: sub start_physnet {
  403:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  404:     my $bodytext = '/adm/includes/physnet.sty';
  405:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
  406:     my $cbistyletext=&Apache::lonnet::getfile($location);
  407: 
  408:     %$style = (%$style,&Apache::style::styleparser($target,$cbistyletext));
  409:     $$parser['-1']->unget_token($token);
  410: #    if ( defined($$style{'physnet'}) ) {
  411: #        &Apache::lonxml::newparser($parser,\$$style{'physnet'});
  412: #    }
  413:     return "";
  414: }
  415: 
  416: sub end_physnet {
  417:   return '';
  418: }
  419: 
  420: sub start_standalone {
  421:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  422:   my $result='';
  423:   if ($target eq 'web' ) {
  424:     if ( $ENV{'request.course.id'} ) {
  425:       my $inside = &Apache::lonxml::get_all_text("/standalone",$parser);
  426:     } else {
  427:       $result='<table bgcolor="#E1E1E1" border="2"><tr><td>';
  428:     }
  429:   }
  430:   return $result;
  431: }
  432: 
  433: sub end_standalone {
  434:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  435:   my $result='';
  436:   if ($target eq 'web' ) {
  437:     if ( $ENV{'request.course.id'} ) {
  438:     } else {
  439:       $result='</td></tr></table>';
  440:     }
  441:   }
  442:   return $result;
  443: }
  444: 
  445: sub start_comment {
  446:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  447:   my $result='';
  448:   if ($target eq 'edit') {
  449:     $result=&Apache::edit::tag_start($target,$token);
  450:     my $bodytext=&Apache::lonxml::get_all_text("/comment",$parser);
  451:     $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,4)
  452:   } elsif ( $target eq 'modified') {
  453:     $result=$token->[4].&Apache::edit::modifiedfield($token);
  454:   } elsif ( $target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
  455: 	    $target eq 'answer' || $target eq 'meta' || $target eq 'analyze') {
  456:     #normally throw away comments
  457:     my $bodytext=&Apache::lonxml::get_all_text("/comment",$parser);
  458:   }
  459:   return $result;
  460: }
  461: 
  462: sub end_comment {
  463:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  464:   if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
  465:   return '';
  466: }
  467: 
  468: 
  469: sub xmlparse {
  470:   my ($string) = @_;
  471: #  &Apache::lonxml::debug("Got $string");
  472:   my ($target,$token,$tagstack,$parstack,$oldparser,$safeeval,$style)=
  473:     @Apache::scripttag::parser_env;
  474:   my @parser;
  475:   &Apache::lonxml::newparser(\@parser,\$string);
  476:   my $result=&Apache::lonxml::inner_xmlparse($target,$tagstack,
  477: 					     $parstack,\@parser,
  478: 					     $safeeval,$style);
  479:   return $result;
  480: }
  481: 
  482: 1;
  483: __END__

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