File:  [LON-CAPA] / loncom / xml / scripttag.pm
Revision 1.95: download - view: text, annotated - select for diffs
Sun Jun 8 21:03:19 2003 UTC (20 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- I like using encode

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

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