File:  [LON-CAPA] / loncom / xml / scripttag.pm
Revision 1.103: download - view: text, annotated - select for diffs
Thu Oct 30 20:52:54 2003 UTC (20 years, 7 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- BUG#1974, <textarea>s delete the initial linefeeds if they exist, now checks old value and preserves the \n from the old value if it had been there.

    1: # The LearningOnline Network with CAPA
    2: # <script> definiton
    3: #
    4: # $Id: scripttag.pm,v 1.103 2003/10/30 20:52:54 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:     if ( $target eq "modified" ) {
  108: 	$result=$token->[4].&Apache::edit::modifiedfield('/script',$parser);
  109:     } elsif ( $target eq 'web' || $target eq 'tex' ||
  110: 	      $target eq 'grade' || $target eq 'answer' ||
  111: 	      $target eq 'analyze' ) {
  112: 	my $bodytext=&Apache::lonxml::get_all_text("/script",$parser);
  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: 	my $bodytext=&Apache::lonxml::get_all_text("/script",$parser);
  133: 	$result=&Apache::edit::tag_start($target,$token,'Script');
  134: 	$result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,4);
  135:     }
  136:   } else {
  137:     if ($target ne "meta") {
  138:       $result = $token->[4];
  139:       my $bodytext=&Apache::lonxml::get_all_text("/script",$parser);
  140:       $result.=$bodytext;
  141:     }
  142:   }
  143:   return $result;
  144: }
  145: 
  146: sub end_script {
  147:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  148:   if ( $target eq "meta" ) { return ''; } 
  149:   my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
  150:   my $result='';
  151:   #other script blocks need to survive
  152:   if ($type ne "loncapa/perl") {
  153:     return $token->[2];
  154:   } elsif ($target eq 'edit' ) {
  155:     return &Apache::edit::end_table();
  156:   } elsif ($target eq 'answer') {
  157:     $Apache::lonxml::evaluate++;
  158:   }
  159:   return '';
  160: }
  161: 
  162: sub start_display {
  163:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  164: 
  165:   my $result;
  166: 
  167:   if ( $target eq "modified" ) {
  168:       $result=$token->[4].&Apache::edit::modifiedfield("/display",$parser);
  169:   } elsif ( $target eq 'web' || $target eq 'tex' ||
  170: 	    $target eq 'grade' || $target eq 'answer' ||
  171: 	      $target eq 'analyze') {
  172:       my $bodytext=&Apache::lonxml::get_all_text("/display",$parser);
  173:       if (!$Apache::lonxml::default_homework_loaded) {
  174: 	  &Apache::lonxml::default_homework_load($safeeval);
  175:       }
  176:       $result=&Apache::run::run($bodytext,$safeeval);
  177:       if ($target eq 'grade' || $target eq 'answer' ||
  178: 	  $target eq 'analyze') {
  179: 	  $result=''; # grade should produce no output
  180:       }
  181:       $Apache::lonxml::post_evaluate=0;
  182:   } elsif ($target eq "edit" ) {
  183:     my $bodytext=&Apache::lonxml::get_all_text("/display",$parser);
  184:     #$result = 
  185:     #  "<br /> &lt;$token->[1]&gt; output: <br />$bodytext<br />Source:<br />";
  186:     #$result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1);
  187:     $result=&Apache::edit::tag_start($target,$token,'Script With Display');
  188:     $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,1)
  189:   }
  190:   return $result;
  191: }
  192: 
  193: sub end_display {
  194:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  195:   if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
  196:   return '';
  197: }
  198: 
  199: sub start_scriptlib {
  200:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  201:   my $bodytext;
  202:   my $result ='';
  203:   my $error='';
  204: 
  205:   if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' || 
  206:       $target eq 'edit' || $target eq 'answer' || $target eq 'analyze') {
  207:     $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
  208:     $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
  209: 				     $$parstack[$#$parstack]);
  210:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
  211: 					       $bodytext);
  212:     my $script=&Apache::lonnet::getfile($location);
  213:     if ($script == -1) {
  214:       if ($target eq 'edit') {
  215:         $error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
  216:       } else {
  217: 	&Apache::lonxml::error("<b> Unable to find <i>$location</i> for scriptlib</b>");
  218: 	return "";
  219:       }
  220:     }
  221:     &Apache::run::run($script,$safeeval);
  222:     #&Apache::lonxml::debug("ran $bodytext:<br />".&Apache::lonnet::getfile($bodytext)."<br />");
  223:   }
  224:   if ($target eq "edit" ) {
  225:     $result=
  226:       &Apache::edit::tag_start($target,$token,'New Script Functions').
  227: 	&Apache::edit::editline($token->[1],$bodytext,'scriptlib',40).
  228:             &Apache::edit::browse(undef,'textnode').
  229: 	  $error.'</td></tr>'.
  230: 	    &Apache::edit::end_table();
  231:   }
  232:   if ($target eq "modified" ) {
  233:       $result=$token->[4].&Apache::edit::modifiedfield("/scriptlib",$parser);
  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:       $result=$token->[4].&Apache::edit::modifiedfield("/parserlib",$parser);
  278:   }
  279:   return $result;
  280: }
  281: 
  282: sub end_parserlib {
  283:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  284:   my @result;
  285:   if ($target eq "edit" ) { $result[1]='no'; }
  286:   return @result;
  287: }
  288: 
  289: sub start_window {
  290:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  291:   my $result = '';
  292:   if ($target eq 'web') {
  293:     &Apache::lonxml::startredirection;
  294:   }  elsif ($target eq 'tex') {
  295:        $result = '\unskip\footnote{';
  296:    }
  297:   return $result;  
  298: }
  299: 
  300: sub end_window {
  301:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  302:   my $result;
  303:   if ($target eq 'web') {
  304:     my $output=&Apache::lonxml::endredirection;
  305:     $output =~ s/[\n\r]/ /g;
  306: #    $output = &HTML::Entities::encode($output,'<>&"\'');
  307:     $output =~ s/\'/\\\'/g;
  308:     my $linktext= &Apache::lonxml::get_param('linktext',$parstack,$safeeval);
  309:     if (!$linktext) { $linktext='<sup>*</sup>'; }
  310:     my $width= &Apache::lonxml::get_param('width',$parstack,$safeeval);
  311:     if (!$width) { $width='500'; }
  312:     my $height= &Apache::lonxml::get_param('height',$parstack,$safeeval);
  313:     if (!$height) { $height='200'; }
  314:     $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>";
  315:   } elsif ($target eq 'tex') {
  316:       $result = '}';
  317:   } else {
  318:       $result = '';
  319:   }
  320:   return $result; 
  321: }
  322: 
  323: sub start_import {
  324:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  325:   my $bodytext=$$parser[$#$parser]->get_text("/import");
  326:   my $result ="";
  327: 
  328:   $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
  329: 
  330:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  331:       $target eq 'tex' || $target eq 'analyze' ) {
  332:     # FIXME this probably needs to be smart about construction vs.
  333:     # non construction space.
  334:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
  335:     my $file=&Apache::lonnet::getfile($location);
  336:     if ($file == -1) {
  337:       &Apache::lonxml::error("<b> Unable to find <i>$bodytext as $location</i> for import</b>");
  338:       return "";
  339:     }
  340: 
  341:     my $dir=$location;
  342:     $dir=~s:/[^/]*$::;
  343:     #  &Apache::lonxml::debug("directory $dir $location file $file \n<b>END</b>\n");
  344:     my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
  345:     if (!$id) { $id=$Apache::lonxml::curdepth; }
  346:     push(@Apache::inputtags::import,$id);
  347:     push(@Apache::inputtags::importlist,$id);
  348: 
  349: 	&Apache::lonxml::newparser($parser,\$file,$dir);
  350: 
  351:   } elsif ($target eq "edit" ) {
  352:     $result.=&Apache::edit::tag_start($target,$token);
  353:     $result.=&Apache::edit::editline($token->[1],$bodytext,'',40);
  354:     $result.=&Apache::edit::browse(undef,'textnode');
  355:     #FIXME this need to convert $bodytext to be a contruction space reference
  356:     #my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
  357:     #$result.="Click<a href=\"$location\">here</a> to edit<br />"
  358:   } elsif ($target eq 'modified') {
  359:       $result=$token->[4].&Apache::edit::modifiedfield("/import",$parser);
  360:   } elsif ($target eq 'meta') {
  361:     my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
  362:     $result.='<import part="'.$Apache::inputtags::part;
  363:     if ($id) {
  364:       $result.='" id="'.$id;
  365:     }
  366:     $result.='">';
  367:     $result.=$bodytext;
  368:     $result.='</import>';
  369:   }
  370:   return $result;
  371: }
  372: 
  373: sub end_import {
  374:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  375:   pop(@Apache::inputtags::import);
  376:   my $result;
  377:   if ($target eq 'edit' ) { $result=&Apache::edit::end_table(); }
  378:   return $result;
  379: }
  380: 
  381: sub start_storetc {
  382:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  383:   my $result = '';
  384:   &Apache::lonxml::startredirection;
  385:   return $result; 
  386: }
  387: 
  388: sub end_storetc {
  389:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  390:     my $result;
  391:     my $output=&Apache::lonxml::endredirection;
  392:     $output =~ s/\"/\&quot\;/g;
  393:     $result = '{\bf '.$output.'.}}\write\tcfile{\protect\tcpc{ '.$output.'.}{\the\value{relpage}}}';
  394:     return $result;
  395: }
  396: 
  397: 
  398: sub start_physnet {
  399:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  400:     my $bodytext = '/adm/includes/physnet.sty';
  401:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
  402:     my $cbistyletext=&Apache::lonnet::getfile($location);
  403: 
  404:     %$style = (%$style,&Apache::style::styleparser($target,$cbistyletext));
  405:     $$parser['-1']->unget_token($token);
  406: #    if ( defined($$style{'physnet'}) ) {
  407: #        &Apache::lonxml::newparser($parser,\$$style{'physnet'});
  408: #    }
  409:     return "";
  410: }
  411: 
  412: sub end_physnet {
  413:   return '';
  414: }
  415: 
  416: sub start_standalone {
  417:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  418:   my $result='';
  419:   if ($target eq 'web' ) {
  420:     if ( $ENV{'request.course.id'} ) {
  421:       my $inside = &Apache::lonxml::get_all_text("/standalone",$parser);
  422:     } else {
  423:       $result='<table bgcolor="#E1E1E1" border="2"><tr><td>';
  424:     }
  425:   }
  426:   return $result;
  427: }
  428: 
  429: sub end_standalone {
  430:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  431:   my $result='';
  432:   if ($target eq 'web' ) {
  433:     if ( $ENV{'request.course.id'} ) {
  434:     } else {
  435:       $result='</td></tr></table>';
  436:     }
  437:   }
  438:   return $result;
  439: }
  440: 
  441: sub start_comment {
  442:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  443:   my $result='';
  444:   if ($target eq 'edit') {
  445:     $result=&Apache::edit::tag_start($target,$token);
  446:     my $bodytext=&Apache::lonxml::get_all_text("/comment",$parser);
  447:     $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,4)
  448:   } elsif ( $target eq 'modified') {
  449:     $result=$token->[4].&Apache::edit::modifiedfield("/comment",$parser);
  450:   } elsif ( $target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
  451: 	    $target eq 'answer' || $target eq 'meta' || $target eq 'analyze') {
  452:     #normally throw away comments
  453:     my $bodytext=&Apache::lonxml::get_all_text("/comment",$parser);
  454:   }
  455:   return $result;
  456: }
  457: 
  458: sub end_comment {
  459:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  460:   if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
  461:   return '';
  462: }
  463: 
  464: 
  465: sub xmlparse {
  466:   my ($string) = @_;
  467: #  &Apache::lonxml::debug("Got $string");
  468:   my ($target,$token,$tagstack,$parstack,$oldparser,$safeeval,$style)=
  469:     @Apache::scripttag::parser_env;
  470:   my @parser;
  471:   &Apache::lonxml::newparser(\@parser,\$string);
  472:   my $result=&Apache::lonxml::inner_xmlparse($target,$tagstack,
  473: 					     $parstack,\@parser,
  474: 					     $safeeval,$style);
  475:   return $result;
  476: }
  477: 
  478: 1;
  479: __END__

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