File:  [LON-CAPA] / loncom / xml / scripttag.pm
Revision 1.157: download - view: text, annotated - select for diffs
Sun Aug 8 03:00:56 2010 UTC (13 years, 8 months ago) by raeburn
Branches: MAIN
CVS tags: language_hyphenation_merge, language_hyphenation, HEAD
- bug 6290.
- revert args passed to Apache::lontexconvert::algebra() changed in 1.152.
  (the new $tth arg in lontexconvert::algebra is appended as a sixth arg, not
  added as a second arg shifting existing args).

    1: # The LearningOnline Network with CAPA
    2: # <script> definiton
    3: #
    4: # $Id: scripttag.pm,v 1.157 2010/08/08 03:00:56 raeburn 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: 
   29: package Apache::scripttag;
   30: 
   31: use strict;
   32: use Apache::lonnet;
   33: use Apache::lonlocal;
   34: use Apache::style();
   35: 
   36: #Globals
   37: # this used to pass around the standard callsub arguments to a tag func
   38: # so xmlparse can reenter the inner_xmlparse loop.
   39: 
   40: @Apache::scripttag::parser_env = ();
   41: BEGIN {
   42:   &Apache::lonxml::register('Apache::scripttag',
   43: 			    ('script','scriptlib','parserlib','import',
   44: 			     'window','display','storetc','physnet',
   45: 			     'standalone','comment','num','parse','algebra',
   46: 			     'LONCAPA_INTERNAL_TURN_STYLE_ON',
   47: 			     'LONCAPA_INTERNAL_TURN_STYLE_OFF'));
   48: }
   49: 
   50: sub start_LONCAPA_INTERNAL_TURN_STYLE_ON {
   51:     $Apache::lonxml::usestyle=1;
   52:     $Apache::lonxml::style_values='';
   53:     return ('','no');
   54: }
   55: 
   56: sub end_LONCAPA_INTERNAL_TURN_STYLE_ON {
   57:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   58:     my $end=&Apache::lonxml::get_param('end',$parstack,$safeeval);
   59:     if (defined($end)) {
   60: 	&Apache::lonxml::end_tag($tagstack,$parstack,$token);
   61:     }
   62:     return ('','no');
   63: }
   64: 
   65: sub start_LONCAPA_INTERNAL_TURN_STYLE_OFF {
   66:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   67:     $Apache::lonxml::usestyle=0;
   68:     my $end=&Apache::lonxml::get_param('end',$parstack,$safeeval);
   69:     if (!$end) {
   70: 	$Apache::lonxml::style_values=$$parstack[-1];
   71: 	$Apache::lonxml::style_end_values=$$parstack[-1];
   72:     } else {
   73: 	$Apache::lonxml::style_values=$Apache::lonxml::style_end_values;
   74: 	$Apache::lonxml::style_end_values='';
   75:     }
   76:     return ('','no');
   77: }
   78: 
   79: sub end_LONCAPA_INTERNAL_TURN_STYLE_OFF {
   80:     return ('','no');
   81: }
   82: 
   83: sub start_script {
   84:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   85:   @Apache::scripttag::parser_env = @_;
   86:   my $result='';
   87:   my $type= &Apache::lonxml::get_param('type',$parstack,$safeeval);
   88:   &Apache::lonxml::debug("found type of $type");
   89:   if ($type eq "loncapa/perl") {
   90:     if ( $target eq "modified" ) {
   91: 	$result=$token->[4].&Apache::edit::modifiedfield('/script',$parser);
   92:     } elsif ( $target eq 'web' || $target eq 'tex' ||
   93: 	      $target eq 'grade' || $target eq 'webgrade' ||
   94: 	      $target eq 'answer' || $target eq 'analyze' ) {
   95: 	my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/script",$parser);
   96: 	if (!$Apache::lonxml::default_homework_loaded) {
   97: 	    &Apache::lonxml::default_homework_load($safeeval);
   98: 	}
   99: 	&Apache::run::run($bodytext,$safeeval);
  100: 	if (($target eq 'answer') &&
  101: 	    ($env{'form.answer_output_mode'} ne 'tex') &&
  102: 	    ($Apache::lonhomework::viewgrades == 'F')) {
  103: 	    $Apache::lonxml::evaluate--;
  104: 	    my (undef,undef,$udom,$uname)=&Apache::lonnet::whichuser();
  105: 	    my $windowopen=&Apache::lonhtmlcommon::javascript_docopen();
  106: 	    my $start_page =
  107: 		&Apache::loncommon::start_page('Script Vars', undef,
  108: 					       {'only_body' => 1,
  109: 						'bgcolor'   => '#FFFFFF',
  110: 						'js_ready'  => 1,});
  111: 	    my $end_page =
  112: 		&Apache::loncommon::end_page({'js_ready' => 1,});
  113: 
  114: 	    $uname =~s/\W//g;
  115: 	    $udom  =~s/\W//g;
  116: 	    my $function_name = 
  117: 		join('_','LONCAPA_scriptvars',$uname,$udom,
  118: 		     $env{'form.counter'},$Apache::lonxml::curdepth);
  119: 	    my $script_var ="<script type=\"text/javascript\">
  120: // <![CDATA[
  121:     function $function_name() {newWindow=open('','new_W','width=500,height=500,scrollbars=1,resizable=yes');newWindow.$windowopen;newWindow.document.writeln('$start_page<pre>";
  122: 	    my $listing=&Apache::run::dump($target,$safeeval);
  123: 	    $listing=~s/\\/\\\\/g;
  124: 	    $listing=~s/\'/\\\'/g;
  125: 	    $script_var.=$listing;
  126: 	    $script_var.= "<\\/pre>$end_page');newWindow.document.close();newWindow.focus()}
  127: // ]]>
  128: </script><a href=\"javascript:$function_name();void(0);\">".&mt('Script Vars')."</a><br />";
  129: 	    &Apache::lonxml::add_script_result($script_var);
  130: 	}
  131:     } elsif ($target eq "edit" ) {
  132:       #&Apache::run::run($bodytext,$safeeval);
  133:       #$result="<br /> &lt;$token->[1]&gt; output: <br />$bodytext<br />Source:<br />";
  134: 	my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/script",$parser);
  135: 	$result=&Apache::edit::tag_start($target,$token,'Script');
  136: 	$result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,4);
  137:     } elsif ($target eq 'meta') {
  138: 	my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/script",$parser);
  139:     }
  140:   } else {
  141:       my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/script",$parser);
  142:       if ($target ne "meta" && $target ne 'tex' && $target ne 'answer') {
  143: 	  $result = $token->[4];
  144: 	  $result.=$bodytext;
  145:       }
  146:   }
  147:   return $result;
  148: }
  149: 
  150: sub end_script {
  151:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  152:   if ( $target eq "meta" ) { return ''; } 
  153:   my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
  154:   my $result='';
  155:   #other script blocks need to survive
  156:   if ($type ne "loncapa/perl" && $target ne 'tex') {
  157:     return $token->[2];
  158:   } elsif ($target eq 'edit' ) {
  159:     return &Apache::edit::end_table();
  160:   } elsif ($target eq 'answer') {
  161:     $Apache::lonxml::evaluate++;
  162:   }
  163:   return '';
  164: }
  165: 
  166: sub start_display {
  167:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  168:   @Apache::scripttag::parser_env = @_;
  169:   my $result;
  170: 
  171:   if ( $target eq "modified" ) {
  172:       $result=$token->[4].&Apache::edit::modifiedfield("/display",$parser);
  173:   } elsif ( $target eq 'web' || $target eq 'tex' ||
  174: 	    $target eq 'grade' || $target eq 'webgrade' ||
  175: 	    $target eq 'answer' || $target eq 'analyze') {
  176:       my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/display",$parser);
  177:       if (!$Apache::lonxml::default_homework_loaded) {
  178: 	  &Apache::lonxml::default_homework_load($safeeval);
  179:       }
  180:       $result=&Apache::run::run($bodytext,$safeeval);
  181:       if ($target eq 'grade' || $target eq 'answer' ||
  182: 	  $target eq 'analyze') {
  183: 	  # grade/answer/analyxe should produce no output but if we
  184: 	  # are redirecting, the redirecter should know what to do
  185: 	  # with the output
  186: 	  if (!$Apache::lonxml::redirection) { $result=''; }
  187:       }
  188:       $Apache::lonxml::post_evaluate=0;
  189:   } elsif ($target eq "edit" ) {
  190:     my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/display",$parser);
  191:     #$result = 
  192:     #  "<br /> &lt;$token->[1]&gt; output: <br />$bodytext<br />Source:<br />";
  193:     #$result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1);
  194:     $result=&Apache::edit::tag_start($target,$token,'Script With Display');
  195:     $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,1)
  196:   } elsif ($target eq 'meta') {
  197:       my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/display",$parser);
  198:   }
  199:   return $result;
  200: }
  201: 
  202: sub end_display {
  203:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  204:   if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
  205:   return '';
  206: }
  207: 
  208: sub start_scriptlib {
  209:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  210:   my $bodytext;
  211:   my $result ='';
  212:   my $error='';
  213: 
  214:   if ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
  215:       $target eq 'meta' || $target eq 'edit' || $target eq 'answer' ||
  216:       $target eq 'analyze' || $target eq 'webgrade') {
  217:     $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
  218:     $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
  219: 				     $$parstack[$#$parstack]);
  220:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
  221: 					       $bodytext);
  222:     my $script=&Apache::lonnet::getfile($location);
  223:     if ($script == -1) {
  224:       if ($target eq 'edit') {
  225:         $error='</tr><tr><td>'.&mt('Errors').'</td><td colspan="2"><b>'.&mt(' Unable to find [_1]','<span class="LC_filename">'.$location.'</span>').'</b></td>'."\n";
  226:       } else {
  227: 	&Apache::lonxml::error("<b> Unable to find <i>$location</i> for scriptlib</b>");
  228: 	return "";
  229:       }
  230:     }
  231:     &Apache::run::run($script,$safeeval);
  232:     #&Apache::lonxml::debug("ran $bodytext:<br />".&Apache::lonnet::getfile($bodytext)."<br />");
  233:   }
  234:   if ($target eq "edit" ) {
  235:     $result=
  236:       &Apache::edit::tag_start($target,$token,'New Script Functions').
  237: 	&Apache::edit::editline($token->[1],$bodytext,'scriptlib',40).
  238:             &Apache::edit::browse(undef,'textnode').
  239: 	  $error.'</td></tr>'.
  240: 	    &Apache::edit::end_table();
  241:   }
  242:   if ($target eq "modified" ) {
  243:       $result=$token->[4].&Apache::edit::modifiedfield("/scriptlib",$parser);
  244:   }
  245:   return $result;
  246: }
  247: 
  248: sub end_scriptlib {
  249:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  250:   my @result;
  251:   if ($target eq "edit" ) { $result[1]='no'; }
  252:   return @result;
  253: }
  254: 
  255: sub start_parserlib {
  256:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  257:   my $bodytext;
  258:   my $result ="";
  259:   my $error='';
  260:   if ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
  261:       $target eq 'meta' || $target eq 'edit' || $target eq 'answer' ||
  262:       $target eq 'analyze' || $target eq 'webgrade') {
  263:     $bodytext=$$parser[$#$parser]->get_text("/parserlib");
  264:     $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
  265: 				     $$parstack[$#$parstack]);
  266:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
  267: 					       $bodytext);
  268:     my $styletext=&Apache::lonnet::getfile($location);
  269:     #&Apache::lonxml::debug("found :$bodytext: in :$location: with :$styletext:");
  270:     if ($styletext == -1) {
  271:       if ($target eq 'edit') {
  272: 	$error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
  273:       } else {
  274: 	&Apache::lonxml::error("<b> Unable to find <i>$location</i> for parserlib</b>");
  275: 	return "";
  276:       }
  277:     }
  278:     %$style = ( %$style , &Apache::style::styleparser($target,$styletext));
  279:   }
  280:   if ($target eq "edit" ) {
  281:     $result=
  282:       &Apache::edit::tag_start($target,$token,'New Tag Definitions').
  283: 	&Apache::edit::editline($token->[1],$bodytext,'',40).
  284: 	  $error.'</td></tr>'.
  285: 	    &Apache::edit::end_table();
  286:   }
  287:   if ($target eq "modified" ) {
  288:       $result=$token->[4].&Apache::edit::modifiedfield("/parserlib",$parser);
  289:   }
  290:   return $result;
  291: }
  292: 
  293: sub end_parserlib {
  294:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  295:   my @result;
  296:   if ($target eq "edit" ) { $result[1]='no'; }
  297:   return @result;
  298: }
  299: 
  300: sub start_window {
  301:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  302:     my $result = '';
  303:     if ($target eq 'web' || $target eq 'webgrade') {
  304: 	&Apache::lonxml::startredirection;
  305:     } elsif ($target eq 'tex') {
  306: 	$result = '\unskip\footnote{';
  307:     } elsif ($target eq 'edit') {
  308: 	$result.=&Apache::edit::tag_start($target,$token);
  309: 	$result.=&Apache::edit::text_arg('Text of Link:','linktext',$token,70);
  310: 	$result.=&Apache::edit::text_arg('Height:','height',$token,5);
  311: 	$result.=&Apache::edit::text_arg('Width:','width',$token,5);
  312: 	$result.=&Apache::edit::text_arg('Mime Type:','mimetype',$token,5);
  313: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  314:     } elsif ($target eq 'modified') {
  315: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  316: 						     $safeeval,'linktext',
  317: 						     'width','height',
  318: 						     'mimetype');
  319: 	if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
  320:     }
  321:     return $result;  
  322: }
  323: 
  324: sub end_window {
  325:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  326:   my $result;
  327:   if ($target eq 'web' || $target eq 'webgrade') {
  328:     my $output=&Apache::lonxml::endredirection;
  329:     $output =~ s/[\n\r]/ /g;
  330: #    $output = &HTML::Entities::encode($output,'<>&"\'');
  331:     $output =~ s/\'/\\\'/g;
  332:     my $linktext= &Apache::lonxml::get_param('linktext',$parstack,$safeeval);
  333:     if (!$linktext) { $linktext='<sup>*</sup>'; }
  334:     my $width= &Apache::lonxml::get_param('width',$parstack,$safeeval);
  335:     if (!$width) { $width='500'; }
  336:     my $height= &Apache::lonxml::get_param('height',$parstack,$safeeval);
  337:     if (!$height) { $height='200'; }
  338:     my $mimetype= &Apache::lonxml::get_param('mimetype',$parstack,$safeeval)
  339: 	          || 'text/html';
  340: 
  341:     my ($start_page,$end_page);
  342:     if ($mimetype eq 'text/html') {
  343: 	$start_page =
  344: 	    &Apache::loncommon::start_page($linktext, undef,
  345: 					   {'only_body' => 1,
  346: 					    'bgcolor'   => '#FFFFFF',
  347: 					    'js_ready'  => 1,});
  348: 	$end_page =
  349: 	    &Apache::loncommon::end_page({'js_ready' => 1,});
  350:     }
  351:     $result = "<script type=\"text/javascript\">
  352: //<!--
  353:  function LONCAPA_newwindow_$Apache::lonxml::curdepth() {
  354: newWindow=open(".&Apache::lonhtmlcommon::javascript_nothing().",'new_W');
  355: newWindow.close();
  356: newWindow=open(".&Apache::lonhtmlcommon::javascript_nothing().",'new_W','width=$width,height=$height,scrollbars=1');
  357: newWindow.".&Apache::lonhtmlcommon::javascript_docopen($mimetype).";
  358: newWindow.document.writeln('$start_page $output $end_page');
  359: newWindow.document.close();}
  360: //-->
  361: </script><a href=\"javascript:LONCAPA_newwindow_$Apache::lonxml::curdepth();void(0);\">$linktext</a>";
  362:   } elsif ($target eq 'tex') {
  363:       $result = '}';
  364:   } else {
  365:       $result = '';
  366:   }
  367:   return $result; 
  368: }
  369: 
  370: sub start_import {
  371:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  372:   my $bodytext=$$parser[$#$parser]->get_text("/import");
  373:   my $result ="";
  374: 
  375:   $bodytext=&Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
  376: 
  377:   if ($target eq 'web' ||  $target eq 'webgrade' || $target eq 'grade' 
  378:       || $target eq 'answer' || $target eq 'tex' || $target eq 'analyze' ) {
  379:     # FIXME this probably needs to be smart about construction vs.
  380:     # non construction space.
  381:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
  382:     my $file=&Apache::lonnet::getfile($location);
  383:     if ($file == -1) {
  384:       &Apache::lonxml::error("<b> Unable to find <i>$bodytext as $location</i> for import</b>");
  385:       return "";
  386:     }
  387:     my $importmode=&Apache::lonxml::get_param('importmode',$parstack,$safeeval);
  388:     if (($importmode eq 'problem') || ($importmode eq 'part')) {
  389: # We are using import to import published problems
  390:        if (($importmode eq 'problem') || ($file=~/<part[^<]*>/s)) {
  391: # We explicitly don't want this to be a separate part or the problem already has parts
  392:           $file=~s/^\s*<problem>/<library>/s;
  393: 	  $file=~s/<\/problem>\s*$/<\/library>/s;
  394:        } else {
  395: # We want this to be a separate part, but it currently is not
  396:           $file=~s/^\s*<problem>/<library><part>/s;
  397: 	  $file=~s/<\/problem>\s*$/<\/part><\/library>/s;
  398:        }
  399:     }
  400:     my $dir=$location;
  401:     $dir=~s:/[^/]*$::;
  402:     #  &Apache::lonxml::debug("directory $dir $location file $file \n<b>END</b>\n");
  403:     my $id= &Apache::lonxml::get_id($parstack,$safeeval);
  404:     if (!$id) { $id=$Apache::lonxml::curdepth; }
  405:     push(@Apache::inputtags::import,$id);
  406:     push(@Apache::inputtags::importlist,$id);
  407: 
  408:     &Apache::lonxml::newparser($parser,\$file,$dir);
  409: 
  410:   } elsif ($target eq "edit" ) {
  411:     $result.=&Apache::edit::tag_start($target,$token);
  412:     my $location=$token->[1];
  413:     $location=~s/^\s*//s;
  414:     $location=~s/\s*$//s;
  415:     $result.=&Apache::edit::editline($location,$bodytext,'',40);
  416:     $result.=&Apache::edit::browse(undef,'textnode');
  417:     $result.= '&nbsp;<label>'.&mt('Import as:').
  418:               '<select name="importmode_'.$Apache::lonxml::curdepth.'">';
  419:     my %options=&Apache::lonlocal::texthash(''        => 'as standard library',
  420:                                             'problem' => 'as problem',
  421:                                             'part'    => 'as problem part(s)');
  422:     foreach my $option (sort(keys(%options))) {
  423:        $result.='<option value="'.$option.'"';
  424:        if ($option eq &Apache::lonxml::get_param('importmode',$parstack,$safeeval)) {
  425:           $result.=' selected="selected"';
  426:        }
  427:        $result.='>'.$options{$option}.'</option>';
  428:     }
  429:     $result.='</select></label>';
  430:     #FIXME this need to convert $bodytext to be a contruction space reference
  431:     #my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
  432:     #$result.="Click<a href=\"$location\">here</a> to edit<br />"
  433:   } elsif ($target eq 'modified') {
  434:       &Apache::edit::get_new_args($token,$parstack,$safeeval,'importmode');
  435:       $result='<import id="'.$token->[2]{'id'}.'" importmode="'.$token->[2]{'importmode'}.'">';
  436:       $result.=&Apache::edit::modifiedfield("/import",$parser);
  437:   } elsif ($target eq 'meta') {
  438:     my $id= &Apache::lonxml::get_id($parstack,$safeeval);
  439:     $result.='<import part="'.$Apache::inputtags::part;
  440:     if ($id) {
  441:       $result.='" id="'.$id;
  442:     }
  443:     $result.='" importmode="'.$token->[2]{'importmode'}.'">';
  444:     $result.=$bodytext;
  445:     $result.='</import>';
  446:   }
  447:   return $result;
  448: }
  449: 
  450: sub end_import {
  451:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  452:   pop(@Apache::inputtags::import);
  453:   my $result;
  454:   if ($target eq 'edit' ) { $result=&Apache::edit::end_row.
  455: 				&Apache::edit::end_table(); }
  456:   return $result;
  457: }
  458: 
  459: sub start_storetc {
  460:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  461:   my $result = '';
  462:   &Apache::lonxml::startredirection;
  463:   return $result; 
  464: }
  465: 
  466: sub end_storetc {
  467:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  468:     my $result;
  469:     my $output=&Apache::lonxml::endredirection;
  470:     $output =~ s/\"/\&quot\;/g;
  471:     $result = '{\bf '.$output.'.}}\write\tcfile{\protect\tcpc{ '.$output.'.}{\the\value{relpage}}}';
  472:     return $result;
  473: }
  474: 
  475: 
  476: sub start_physnet {
  477:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  478:     my $bodytext = '/adm/includes/physnet.sty';
  479:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
  480:     my $cbistyletext=&Apache::lonnet::getfile($location);
  481: 
  482:     %$style = (%$style,&Apache::style::styleparser($target,$cbistyletext));
  483:     $$parser['-1']->unget_token($token);
  484: #    if ( defined($$style{'physnet'}) ) {
  485: #        &Apache::lonxml::newparser($parser,\$$style{'physnet'});
  486: #    }
  487:     return "";
  488: }
  489: 
  490: sub end_physnet {
  491:   return '';
  492: }
  493: 
  494: sub start_standalone {
  495:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  496:   my $result='';
  497:   if ($target eq 'web' || $target eq 'webgrade') {
  498:     if ( $env{'request.course.id'} ) {
  499:       my $inside = &Apache::lonxml::get_all_text("/standalone",$parser,$style);
  500:     } else {
  501:       $result='<table bgcolor="#E1E1E1" border="2"><tr><td>';
  502:     }
  503:   }
  504:   return $result;
  505: }
  506: 
  507: sub end_standalone {
  508:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  509:   my $result='';
  510:   if ($target eq 'web' || $target eq 'webgrade' ) {
  511:     if ( $env{'request.course.id'} ) {
  512:     } else {
  513:       $result='</td></tr></table>';
  514:     }
  515:   }
  516:   return $result;
  517: }
  518: 
  519: sub start_comment {
  520:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  521:   my $result='';
  522:   if ($target eq 'edit') {
  523:     $result=&Apache::edit::tag_start($target,$token);
  524:     my $bodytext=&Apache::lonxml::get_all_text("/comment",$parser,$style);
  525:     $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,4)
  526:   } elsif ( $target eq 'modified') {
  527:     $result=$token->[4].&Apache::edit::modifiedfield("/comment",$parser);
  528:   } elsif ( $target eq 'web'    || $target eq 'tex'  || $target eq 'grade'   ||
  529: 	    $target eq 'answer' || $target eq 'meta' || $target eq 'analyze' ||
  530: 	    $target eq 'webgrade') {
  531:     #normally throw away comments
  532:     my $bodytext=&Apache::lonxml::get_all_text("/comment",$parser,$style);
  533:   }
  534:   return $result;
  535: }
  536: 
  537: sub end_comment {
  538:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  539:   if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
  540:   return '';
  541: }
  542: 
  543: 
  544: sub xmlparse {
  545:   my ($string) = @_;
  546:   &Apache::lonxml::debug("xmlparse recursion starting with $string");
  547:   # Apache::run::evaluate does an 'eval' on the name of the subroutine
  548:   # if it detects something that looks like a subroutine, this ends up calling
  549:   # things without any arguments and since perl is nice enough to pass
  550:   # along the default arguments when you don't explicitly say no arguments
  551:   # if you call &xmlparse, it gets &xmlparse passed as it argument.
  552:   # Same thing soccurs with &chemparse.
  553:   if ($string eq '&xmlparse') { return '&xmlparse'; }
  554:   if ($string eq '&chemparse') { return '&chemparse'; }
  555:   my ($target,$token,$tagstack,$parstack,$oldparser,$safeeval,$style)=
  556:     @Apache::scripttag::parser_env;
  557:   my @parser;
  558:   &Apache::lonxml::newparser(\@parser,\$string);
  559:   &Apache::lonxml::startredirection();
  560:   my $result=&Apache::lonxml::inner_xmlparse($target,$tagstack,
  561: 					     $parstack,\@parser,
  562: 					     $safeeval,$style);
  563:   $result.=&Apache::lonxml::endredirection();
  564:   &Apache::lonxml::debug("target is $target xmlparse recursion ending with $result");
  565:   return $result;
  566: }
  567: 
  568: sub start_num {
  569:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  570:     my $result = '';
  571:     my $inside = &Apache::lonxml::get_all_text_unbalanced("/num",$parser);
  572:     if ($target eq 'tex' || $target eq 'web' || $target eq 'webgrade') {
  573: 	$inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
  574: 	if (!$Apache::lonxml::default_homework_loaded) {
  575: 	    &Apache::lonxml::default_homework_load($safeeval);
  576: 	}
  577: 	@Apache::scripttag::parser_env = @_;
  578: 	my $format=&Apache::lonxml::get_param('format',$parstack,$safeeval);
  579: 	$result=&Apache::run::run("return &prettyprint(q\0$inside\0,q\0$format\0);",$safeeval);
  580:     }    
  581:     return $result;
  582: }
  583: 
  584: sub end_num {
  585:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  586:     my $result = '';
  587:     return $result;
  588: }
  589: 
  590: sub start_parse {
  591:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  592:     my $result = '';
  593:     if ( $target eq 'web'    || $target eq 'tex'    ||
  594: 	 $target eq 'grade'  || $target eq 'answer' ||
  595: 	 $target eq 'analyze'|| $target eq 'webgrade') {
  596: 	my $inside = &Apache::lonxml::get_all_text_unbalanced("/parse",$parser);
  597: 	$inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
  598: 	if (!$Apache::lonxml::default_homework_loaded) {
  599: 	    &Apache::lonxml::default_homework_load($safeeval);
  600: 	}
  601: 	@Apache::scripttag::parser_env = @_;
  602: 	$result=&Apache::run::run("return &xmlparse(q\0$inside\0);",$safeeval);
  603: 	if ($target eq 'grade' || $target eq 'answer' ||
  604: 	    $target eq 'analyze') {
  605: 	    # grade/answer/analyxe should produce no output but if we
  606: 	    # are redirecting, the redirecter should know what to do
  607: 	    # with the output
  608: 	    if (!$Apache::lonxml::redirection) { $result=''; }
  609: 	}
  610:     }
  611:     return $result;
  612: }
  613: 
  614: sub end_parse {
  615:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  616:     my $result = '';
  617:     return $result;
  618: }
  619: 
  620: sub start_algebra {
  621:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  622:     my $result = '';
  623:     if ( $target eq 'web'     || $target eq 'tex'    ||
  624: 	 $target eq 'grade'   || $target eq 'answer' ||
  625: 	 $target eq 'analyze' || $target eq 'webgrade') {
  626: 	my $inside = &Apache::lonxml::get_all_text_unbalanced("/algebra",$parser);
  627: 	$inside = &Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
  628: 	if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
  629: 	    my $style=&Apache::lonxml::get_param('style',$parstack,$safeeval);
  630: 	    $result=&Apache::lontexconvert::algebra($inside,$target,$style,$parstack,$safeeval);
  631: 	}
  632: 	$Apache::lonxml::post_evaluate=0;
  633:     }
  634:     return $result;
  635: }
  636: 
  637: sub end_algebra {
  638:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  639:     my $result = '';
  640:     return $result;
  641: }
  642: 
  643: 1;
  644: __END__
  645: 
  646: =pod
  647: 
  648: =head1 NAME
  649: 
  650: Apache::scripttag.pm
  651: 
  652: =head1 SYNOPSIS
  653: 
  654: implements <script>, <scriptlib>, <parserlib>,
  655: and <import>
  656: 
  657: This is part of the LearningOnline Network with CAPA project
  658: described at http://www.lon-capa.org.
  659: 
  660: =cut
  661: 

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