File:  [LON-CAPA] / loncom / xml / scripttag.pm
Revision 1.69: download - view: text, annotated - select for diffs
Thu Mar 28 22:02:27 2002 UTC (22 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- editing <import> looks better

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

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