Annotation of loncom/xml/scripttag.pm, revision 1.147

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # <script> definiton
1.58      www         3: #
1.145     albertel    4: # $Id: scripttag.pm,v 1.144 2007/10/11 00:24:46 albertel Exp $
1.58      www         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: #
1.116     www        28: 
1.47      albertel   29: package Apache::scripttag;
1.1       albertel   30: 
                     31: use strict;
1.3       albertel   32: use Apache::lonnet;
1.145     albertel   33: use Apache::lonlocal;
                     34: use Apache::style();
1.1       albertel   35: 
1.47      albertel   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 = ();
1.61      harris41   41: BEGIN {
1.62      albertel   42:   &Apache::lonxml::register('Apache::scripttag',
                     43: 			    ('script','scriptlib','parserlib','import',
                     44: 			     'window','display','storetc','physnet',
1.123     albertel   45: 			     'standalone','comment','num','parse','algebra',
1.86      albertel   46: 			     'LONCAPA_INTERNAL_TURN_STYLE_ON',
1.100     albertel   47: 			     'LONCAPA_INTERNAL_TURN_STYLE_OFF',
1.86      albertel   48: 			     'LONCAPA_INTERNAL_LONHTTPD_PORT'));
1.81      albertel   49: }
                     50: 
                     51: sub start_LONCAPA_INTERNAL_TURN_STYLE_ON {
                     52:     $Apache::lonxml::usestyle=1;
1.89      albertel   53:     $Apache::lonxml::style_values='';
1.81      albertel   54:     return ('','no');
                     55: }
                     56: 
                     57: sub end_LONCAPA_INTERNAL_TURN_STYLE_ON {
1.91      albertel   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:     }
1.100     albertel   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 {
1.86      albertel   81:     return ('','no');
                     82: }
                     83: 
1.143     albertel   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: 
1.86      albertel   94: sub start_LONCAPA_INTERNAL_LONHTTPD_PORT {
                     95:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.137     albertel   96:     if ($target eq 'web' || $target eq 'webgrade') {
1.86      albertel   97: 	return '<script type="text/javascript">var lonhttpdport=\''.
1.143     albertel   98: 	    &Apache::loncommon::lonhttpd_port().'\';</script>';
1.86      albertel   99:     }
                    100:     return ('','no');
                    101: }
                    102: 
                    103: sub end_LONCAPA_INTERNAL_LONHTTPD_PORT {
1.81      albertel  104:     return ('','no');
1.1       albertel  105: }
                    106: 
                    107: sub start_script {
1.40      albertel  108:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.47      albertel  109:   @Apache::scripttag::parser_env = @_;
1.24      albertel  110:   my $result='';
1.39      albertel  111:   my $type= &Apache::lonxml::get_param('type',$parstack,$safeeval);
                    112:   &Apache::lonxml::debug("found type of $type");
1.17      albertel  113:   if ($type eq "loncapa/perl") {
                    114:     if ( $target eq "modified" ) {
1.103     albertel  115: 	$result=$token->[4].&Apache::edit::modifiedfield('/script',$parser);
1.54      albertel  116:     } elsif ( $target eq 'web' || $target eq 'tex' ||
1.130     albertel  117: 	      $target eq 'grade' || $target eq 'webgrade' ||
                    118: 	      $target eq 'answer' || $target eq 'analyze' ) {
1.107     albertel  119: 	my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/script",$parser);
1.79      albertel  120: 	if (!$Apache::lonxml::default_homework_loaded) {
                    121: 	    &Apache::lonxml::default_homework_load($safeeval);
                    122: 	}
                    123: 	&Apache::run::run($bodytext,$safeeval);
1.88      albertel  124: 	if (($target eq 'answer') &&
1.126     albertel  125: 	    ($env{'form.answer_output_mode'} ne 'tex') &&
1.88      albertel  126: 	    ($Apache::lonhomework::viewgrades == 'F')) {
1.79      albertel  127: 	    $Apache::lonxml::evaluate--;
1.138     albertel  128: 	    my (undef,undef,$udom,$uname)=&Apache::lonnet::whichuser();
1.118     www       129: 	    my $windowopen=&Apache::lonhtmlcommon::javascript_docopen();
1.135     albertel  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: 
1.140     albertel  138: 	    $uname =~s/\W//g;
                    139: 	    $udom  =~s/\W//g;
1.145     albertel  140: 	    my $function_name = 
                    141: 		join('_','LONCAPA_scriptvars',$uname,$udom,
                    142: 		     $env{'form.counter'},$Apache::lonxml::curdepth);
1.141     albertel  143: 	    my $script_var ="<script type=\"text/javascript\">
1.124     albertel  144: // <![CDATA[
1.145     albertel  145:     function $function_name() {newWindow=open('','new_W','width=500,height=500,scrollbars=1,resizable=yes');newWindow.$windowopen;newWindow.document.writeln('$start_page<pre>";
1.99      albertel  146: 	    my $listing=&Apache::run::dump($target,$safeeval);
1.128     albertel  147: 	    $listing=~s/\\/\\\\/g;
1.99      albertel  148: 	    $listing=~s/\'/\\\'/g;
1.141     albertel  149: 	    $script_var.=$listing;
                    150: 	    $script_var.= "<\\/pre>$end_page');newWindow.document.close();newWindow.focus()}
1.124     albertel  151: // ]]>
1.145     albertel  152: </script><a href=\"javascript:$function_name();void(0);\">".&mt('Script Vars')."</a><br />";
1.141     albertel  153: 	    &Apache::lonxml::add_script_result($script_var);
1.79      albertel  154: 	}
1.28      albertel  155:     } elsif ($target eq "edit" ) {
1.41      albertel  156:       #&Apache::run::run($bodytext,$safeeval);
                    157:       #$result="<br /> &lt;$token->[1]&gt; output: <br />$bodytext<br />Source:<br />";
1.107     albertel  158: 	my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/script",$parser);
1.103     albertel  159: 	$result=&Apache::edit::tag_start($target,$token,'Script');
                    160: 	$result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,4);
1.107     albertel  161:     } elsif ($target eq 'meta') {
                    162: 	my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/script",$parser);
1.17      albertel  163:     }
                    164:   } else {
1.108     matthew   165:       my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/script",$parser);
1.144     albertel  166:       if ($target ne "meta" && $target ne 'tex' && $target ne 'answer') {
1.112     albertel  167: 	  $result = $token->[4];
                    168: 	  $result.=$bodytext;
                    169:       }
1.13      albertel  170:   }
1.24      albertel  171:   return $result;
1.17      albertel  172: }
1.13      albertel  173: 
1.17      albertel  174: sub end_script {
1.40      albertel  175:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.28      albertel  176:   if ( $target eq "meta" ) { return ''; } 
1.39      albertel  177:   my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
1.29      albertel  178:   my $result='';
                    179:   #other script blocks need to survive
1.115     albertel  180:   if ($type ne "loncapa/perl" && $target ne 'tex') {
1.53      albertel  181:     return $token->[2];
                    182:   } elsif ($target eq 'edit' ) {
                    183:     return &Apache::edit::end_table();
1.78      albertel  184:   } elsif ($target eq 'answer') {
1.53      albertel  185:     $Apache::lonxml::evaluate++;
                    186:   }
1.28      albertel  187:   return '';
1.24      albertel  188: }
                    189: 
                    190: sub start_display {
1.40      albertel  191:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.119     albertel  192:   @Apache::scripttag::parser_env = @_;
1.48      albertel  193:   my $result;
1.39      albertel  194: 
1.24      albertel  195:   if ( $target eq "modified" ) {
1.103     albertel  196:       $result=$token->[4].&Apache::edit::modifiedfield("/display",$parser);
1.54      albertel  197:   } elsif ( $target eq 'web' || $target eq 'tex' ||
1.130     albertel  198: 	    $target eq 'grade' || $target eq 'webgrade' ||
                    199: 	    $target eq 'answer' || $target eq 'analyze') {
1.107     albertel  200:       my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/display",$parser);
1.80      albertel  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') {
1.114     albertel  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=''; }
1.80      albertel  211:       }
1.101     albertel  212:       $Apache::lonxml::post_evaluate=0;
1.48      albertel  213:   } elsif ($target eq "edit" ) {
1.110     albertel  214:     my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/display",$parser);
1.48      albertel  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');
1.77      albertel  219:     $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,1)
1.107     albertel  220:   } elsif ($target eq 'meta') {
                    221:       my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/display",$parser);
1.24      albertel  222:   }
                    223:   return $result;
                    224: }
                    225: 
                    226: sub end_display {
1.48      albertel  227:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    228:   if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
                    229:   return '';
1.1       albertel  230: }
1.3       albertel  231: 
                    232: sub start_scriptlib {
1.40      albertel  233:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.37      albertel  234:   my $bodytext;
                    235:   my $result ='';
                    236:   my $error='';
                    237: 
1.106     albertel  238:   if ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
                    239:       $target eq 'meta' || $target eq 'edit' || $target eq 'answer' ||
1.137     albertel  240:       $target eq 'analyze' || $target eq 'webgrade') {
1.37      albertel  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') {
1.145     albertel  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";
1.37      albertel  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 />");
1.18      albertel  257:   }
1.13      albertel  258:   if ($target eq "edit" ) {
1.37      albertel  259:     $result=
1.49      albertel  260:       &Apache::edit::tag_start($target,$token,'New Script Functions').
1.83      matthew   261: 	&Apache::edit::editline($token->[1],$bodytext,'scriptlib',40).
                    262:             &Apache::edit::browse(undef,'textnode').
1.49      albertel  263: 	  $error.'</td></tr>'.
                    264: 	    &Apache::edit::end_table();
1.37      albertel  265:   }
                    266:   if ($target eq "modified" ) {
1.103     albertel  267:       $result=$token->[4].&Apache::edit::modifiedfield("/scriptlib",$parser);
1.3       albertel  268:   }
                    269:   return $result;
                    270: }
                    271: 
1.37      albertel  272: sub end_scriptlib {
1.40      albertel  273:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.49      albertel  274:   my @result;
                    275:   if ($target eq "edit" ) { $result[1]='no'; }
                    276:   return @result;
1.37      albertel  277: }
1.4       albertel  278: 
                    279: sub start_parserlib {
1.40      albertel  280:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.37      albertel  281:   my $bodytext;
1.7       albertel  282:   my $result ="";
1.37      albertel  283:   my $error='';
1.106     albertel  284:   if ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
                    285:       $target eq 'meta' || $target eq 'edit' || $target eq 'answer' ||
1.137     albertel  286:       $target eq 'analyze' || $target eq 'webgrade') {
1.37      albertel  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));
1.18      albertel  303:   }
1.13      albertel  304:   if ($target eq "edit" ) {
1.37      albertel  305:     $result=
1.49      albertel  306:       &Apache::edit::tag_start($target,$token,'New Tag Definitions').
1.68      albertel  307: 	&Apache::edit::editline($token->[1],$bodytext,'',40).
1.49      albertel  308: 	  $error.'</td></tr>'.
                    309: 	    &Apache::edit::end_table();
1.37      albertel  310:   }
                    311:   if ($target eq "modified" ) {
1.103     albertel  312:       $result=$token->[4].&Apache::edit::modifiedfield("/parserlib",$parser);
1.7       albertel  313:   }
                    314:   return $result;
1.4       albertel  315: }
                    316: 
                    317: sub end_parserlib {
1.40      albertel  318:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.49      albertel  319:   my @result;
                    320:   if ($target eq "edit" ) { $result[1]='no'; }
                    321:   return @result;
1.6       albertel  322: }
                    323: 
1.30      sakharuk  324: sub start_window {
1.105     albertel  325:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    326:     my $result = '';
1.137     albertel  327:     if ($target eq 'web' || $target eq 'webgrade') {
1.105     albertel  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);
1.131     albertel  334: 	$result.=&Apache::edit::text_arg('Height:','height',$token,5);
                    335: 	$result.=&Apache::edit::text_arg('Width:','width',$token,5);
1.146     albertel  336: 	$result.=&Apache::edit::text_arg('Mime Type:','mimetype',$token,5);
1.105     albertel  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,
1.129     raeburn   340: 						     $safeeval,'linktext',
1.146     albertel  341: 						     'width','height',
                    342: 						     'mimetype');
1.105     albertel  343: 	if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
                    344:     }
                    345:     return $result;  
1.19      sakharuk  346: }
                    347: 
1.30      sakharuk  348: sub end_window {
1.40      albertel  349:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.30      sakharuk  350:   my $result;
1.137     albertel  351:   if ($target eq 'web' || $target eq 'webgrade') {
1.34      albertel  352:     my $output=&Apache::lonxml::endredirection;
1.97      albertel  353:     $output =~ s/[\n\r]/ /g;
1.98      albertel  354: #    $output = &HTML::Entities::encode($output,'<>&"\'');
                    355:     $output =~ s/\'/\\\'/g;
1.92      albertel  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'; }
1.146     albertel  362:     my $mimetype= &Apache::lonxml::get_param('mimetype',$parstack,$safeeval)
                    363: 	          || 'text/html';
1.135     albertel  364: 
1.146     albertel  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:     }
1.136     albertel  375:     $result = "<script type=\"text/javascript\">
                    376: //<!--
                    377:  function LONCAPA_newwindow_$Apache::lonxml::curdepth() {
1.147   ! albertel  378: newWindow=open(".&Apache::lonhtmlcommon::javascript_nothing().",'new_W');
        !           379: newWindow.close();
1.136     albertel  380: newWindow=open(".&Apache::lonhtmlcommon::javascript_nothing().",'new_W','width=$width,height=$height,scrollbars=1');
1.146     albertel  381: newWindow.".&Apache::lonhtmlcommon::javascript_docopen($mimetype).";
1.136     albertel  382: newWindow.document.writeln('$start_page $output $end_page');
                    383: newWindow.document.close();}
                    384: //-->
                    385: </script><a href=\"javascript:LONCAPA_newwindow_$Apache::lonxml::curdepth();void(0);\">$linktext</a>";
1.35      sakharuk  386:   } elsif ($target eq 'tex') {
                    387:       $result = '}';
1.30      sakharuk  388:   } else {
1.35      sakharuk  389:       $result = '';
1.30      sakharuk  390:   }
1.20      sakharuk  391:   return $result; 
1.19      sakharuk  392: }
                    393: 
1.6       albertel  394: sub start_import {
1.40      albertel  395:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.31      albertel  396:   my $bodytext=$$parser[$#$parser]->get_text("/import");
1.7       albertel  397:   my $result ="";
1.13      albertel  398: 
                    399:   $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
1.14      albertel  400: 
1.135     albertel  401:   if ($target eq 'web' ||  $target eq 'webgrade' || $target eq 'grade' 
                    402:       || $target eq 'answer' || $target eq 'tex' || $target eq 'analyze' ) {
1.46      albertel  403:     # FIXME this probably needs to be smart about construction vs.
                    404:     # non construction space.
                    405:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
                    406:     my $file=&Apache::lonnet::getfile($location);
                    407:     if ($file == -1) {
                    408:       &Apache::lonxml::error("<b> Unable to find <i>$bodytext as $location</i> for import</b>");
                    409:       return "";
                    410:     }
1.13      albertel  411: 
1.46      albertel  412:     my $dir=$location;
                    413:     $dir=~s:/[^/]*$::;
                    414:     #  &Apache::lonxml::debug("directory $dir $location file $file \n<b>END</b>\n");
1.142     albertel  415:     my $id= &Apache::lonxml::get_id($parstack,$safeeval);
1.57      albertel  416:     if (!$id) { $id=$Apache::lonxml::curdepth; }
                    417:     push(@Apache::inputtags::import,$id);
1.90      albertel  418:     push(@Apache::inputtags::importlist,$id);
1.87      sakharuk  419: 
1.144     albertel  420:     &Apache::lonxml::newparser($parser,\$file,$dir);
1.87      sakharuk  421: 
1.56      albertel  422:   } elsif ($target eq "edit" ) {
1.46      albertel  423:     $result.=&Apache::edit::tag_start($target,$token);
1.68      albertel  424:     $result.=&Apache::edit::editline($token->[1],$bodytext,'',40);
1.83      matthew   425:     $result.=&Apache::edit::browse(undef,'textnode');
1.46      albertel  426:     #FIXME this need to convert $bodytext to be a contruction space reference
                    427:     #my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
                    428:     #$result.="Click<a href=\"$location\">here</a> to edit<br />"
1.56      albertel  429:   } elsif ($target eq 'modified') {
1.103     albertel  430:       $result=$token->[4].&Apache::edit::modifiedfield("/import",$parser);
1.56      albertel  431:   } elsif ($target eq 'meta') {
1.142     albertel  432:     my $id= &Apache::lonxml::get_id($parstack,$safeeval);
1.57      albertel  433:     $result.='<import part="'.$Apache::inputtags::part;
                    434:     if ($id) {
                    435:       $result.='" id="'.$id;
                    436:     }
                    437:     $result.='">';
1.56      albertel  438:     $result.=$bodytext;
                    439:     $result.='</import>';
1.46      albertel  440:   }
                    441:   return $result;
1.6       albertel  442: }
                    443: 
                    444: sub end_import {
1.69      albertel  445:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.57      albertel  446:   pop(@Apache::inputtags::import);
1.69      albertel  447:   my $result;
1.120     albertel  448:   if ($target eq 'edit' ) { $result=&Apache::edit::end_row.
                    449: 				&Apache::edit::end_table(); }
1.69      albertel  450:   return $result;
1.1       albertel  451: }
1.42      sakharuk  452: 
                    453: sub start_storetc {
1.43      albertel  454:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.42      sakharuk  455:   my $result = '';
                    456:   &Apache::lonxml::startredirection;
                    457:   return $result; 
                    458: }
                    459: 
                    460: sub end_storetc {
1.43      albertel  461:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.42      sakharuk  462:     my $result;
                    463:     my $output=&Apache::lonxml::endredirection;
                    464:     $output =~ s/\"/\&quot\;/g;
1.52      albertel  465:     $result = '{\bf '.$output.'.}}\write\tcfile{\protect\tcpc{ '.$output.'.}{\the\value{relpage}}}';
                    466:     return $result;
1.42      sakharuk  467: }
                    468: 
                    469: 
                    470: sub start_physnet {
1.45      sakharuk  471:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.50      www       472:     my $bodytext = '/adm/includes/physnet.sty';
1.45      sakharuk  473:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
                    474:     my $cbistyletext=&Apache::lonnet::getfile($location);
1.42      sakharuk  475: 
1.45      sakharuk  476:     %$style = (%$style,&Apache::style::styleparser($target,$cbistyletext));
1.59      albertel  477:     $$parser['-1']->unget_token($token);
                    478: #    if ( defined($$style{'physnet'}) ) {
                    479: #        &Apache::lonxml::newparser($parser,\$$style{'physnet'});
                    480: #    }
1.45      sakharuk  481:     return "";
                    482: }
1.42      sakharuk  483: 
1.45      sakharuk  484: sub end_physnet {
1.47      albertel  485:   return '';
1.42      sakharuk  486: }
1.62      albertel  487: 
                    488: sub start_standalone {
                    489:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.75      albertel  490:   my $result='';
1.137     albertel  491:   if ($target eq 'web' || $target eq 'webgrade') {
1.126     albertel  492:     if ( $env{'request.course.id'} ) {
1.133     albertel  493:       my $inside = &Apache::lonxml::get_all_text("/standalone",$parser,$style);
1.75      albertel  494:     } else {
                    495:       $result='<table bgcolor="#E1E1E1" border="2"><tr><td>';
                    496:     }
1.62      albertel  497:   }
1.75      albertel  498:   return $result;
1.62      albertel  499: }
                    500: 
                    501: sub end_standalone {
1.75      albertel  502:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    503:   my $result='';
1.137     albertel  504:   if ($target eq 'web' || $target eq 'webgrade' ) {
1.126     albertel  505:     if ( $env{'request.course.id'} ) {
1.75      albertel  506:     } else {
                    507:       $result='</td></tr></table>';
                    508:     }
                    509:   }
                    510:   return $result;
1.74      albertel  511: }
                    512: 
                    513: sub start_comment {
                    514:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    515:   my $result='';
                    516:   if ($target eq 'edit') {
                    517:     $result=&Apache::edit::tag_start($target,$token);
1.133     albertel  518:     my $bodytext=&Apache::lonxml::get_all_text("/comment",$parser,$style);
1.77      albertel  519:     $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,4)
1.74      albertel  520:   } elsif ( $target eq 'modified') {
1.103     albertel  521:     $result=$token->[4].&Apache::edit::modifiedfield("/comment",$parser);
1.137     albertel  522:   } elsif ( $target eq 'web'    || $target eq 'tex'  || $target eq 'grade'   ||
                    523: 	    $target eq 'answer' || $target eq 'meta' || $target eq 'analyze' ||
                    524: 	    $target eq 'webgrade') {
1.74      albertel  525:     #normally throw away comments
1.133     albertel  526:     my $bodytext=&Apache::lonxml::get_all_text("/comment",$parser,$style);
1.74      albertel  527:   }
                    528:   return $result;
                    529: }
                    530: 
                    531: sub end_comment {
                    532:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    533:   if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
1.62      albertel  534:   return '';
                    535: }
                    536: 
1.42      sakharuk  537: 
1.47      albertel  538: sub xmlparse {
                    539:   my ($string) = @_;
1.111     albertel  540:   &Apache::lonxml::debug("xmlparse recursion starting with $string");
1.113     albertel  541:   # Apache::run::evaluate does an 'eval' on the name of the subroutine
                    542:   # if it detects something that looks like a subroutine, this ends up calling
                    543:   # things without any arguments and since perl is nice enough to pass
                    544:   # along the default arguments when you don't explicitly say no arguments
                    545:   # if you call &xmlparse, it gets &xmlparse passed as it argument.
                    546:   # Same thing soccurs with &chemparse.
1.111     albertel  547:   if ($string eq '&xmlparse') { return '&xmlparse'; }
1.113     albertel  548:   if ($string eq '&chemparse') { return '&chemparse'; }
1.47      albertel  549:   my ($target,$token,$tagstack,$parstack,$oldparser,$safeeval,$style)=
                    550:     @Apache::scripttag::parser_env;
                    551:   my @parser;
                    552:   &Apache::lonxml::newparser(\@parser,\$string);
1.132     albertel  553:   &Apache::lonxml::startredirection();
1.47      albertel  554:   my $result=&Apache::lonxml::inner_xmlparse($target,$tagstack,
                    555: 					     $parstack,\@parser,
                    556: 					     $safeeval,$style);
1.132     albertel  557:   $result.=&Apache::lonxml::endredirection();
1.122     albertel  558:   &Apache::lonxml::debug("target is $target xmlparse recursion ending with $result");
1.47      albertel  559:   return $result;
                    560: }
1.3       albertel  561: 
1.122     albertel  562: sub start_num {
                    563:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
                    564:     my $result = '';
                    565:     my $inside = &Apache::lonxml::get_all_text_unbalanced("/num",$parser);
1.137     albertel  566:     if ($target eq 'tex' || $target eq 'web' || $target eq 'webgrade') {
1.122     albertel  567: 	$inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
                    568: 	if (!$Apache::lonxml::default_homework_loaded) {
                    569: 	    &Apache::lonxml::default_homework_load($safeeval);
                    570: 	}
                    571: 	@Apache::scripttag::parser_env = @_;
                    572: 	my $format=&Apache::lonxml::get_param('format',$parstack,$safeeval);
                    573: 	$result=&Apache::run::run("return &prettyprint(q\0$inside\0,q\0$format\0);",$safeeval);
                    574:     }    
                    575:     return $result;
                    576: }
                    577: 
                    578: sub end_num {
                    579:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
                    580:     my $result = '';
                    581:     return $result;
                    582: }
                    583: 
                    584: sub start_parse {
                    585:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
                    586:     my $result = '';
1.137     albertel  587:     if ( $target eq 'web'    || $target eq 'tex'    ||
                    588: 	 $target eq 'grade'  || $target eq 'answer' ||
                    589: 	 $target eq 'analyze'|| $target eq 'webgrade') {
1.122     albertel  590: 	my $inside = &Apache::lonxml::get_all_text_unbalanced("/parse",$parser);
                    591: 	$inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
                    592: 	if (!$Apache::lonxml::default_homework_loaded) {
                    593: 	    &Apache::lonxml::default_homework_load($safeeval);
                    594: 	}
                    595: 	@Apache::scripttag::parser_env = @_;
                    596: 	$result=&Apache::run::run("return &xmlparse(q\0$inside\0);",$safeeval);
1.134     albertel  597: 	if ($target eq 'grade' || $target eq 'answer' ||
                    598: 	    $target eq 'analyze') {
                    599: 	    # grade/answer/analyxe should produce no output but if we
                    600: 	    # are redirecting, the redirecter should know what to do
                    601: 	    # with the output
                    602: 	    if (!$Apache::lonxml::redirection) { $result=''; }
                    603: 	}
1.122     albertel  604:     }
                    605:     return $result;
                    606: }
                    607: 
                    608: sub end_parse {
                    609:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
                    610:     my $result = '';
                    611:     return $result;
                    612: }
1.123     albertel  613: 
                    614: sub start_algebra {
                    615:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
                    616:     my $result = '';
1.137     albertel  617:     if ( $target eq 'web'     || $target eq 'tex'    ||
                    618: 	 $target eq 'grade'   || $target eq 'answer' ||
                    619: 	 $target eq 'analyze' || $target eq 'webgrade') {
1.123     albertel  620: 	my $inside = &Apache::lonxml::get_all_text_unbalanced("/algebra",$parser);
                    621: 	$inside = &Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
                    622: 	if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.125     albertel  623: 	    my $style=&Apache::lonxml::get_param('style',$parstack,$safeeval);
                    624: 	    $result=&Apache::lontexconvert::algebra($inside,$target,$style);
1.123     albertel  625: 	}
                    626: 	$Apache::lonxml::post_evaluate=0;
                    627:     }
                    628:     return $result;
                    629: }
                    630: 
                    631: sub end_algebra {
                    632:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
                    633:     my $result = '';
                    634:     return $result;
                    635: }
                    636: 
1.1       albertel  637: 1;
                    638: __END__

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