File:  [LON-CAPA] / loncom / xml / scripttag.pm
Revision 1.146: download - view: text, annotated - select for diffs
Sat Nov 17 01:46:44 2007 UTC (16 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- BUG#5476 add a mime type option to the <window>

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

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