File:  [LON-CAPA] / loncom / xml / scripttag.pm
Revision 1.168: download - view: text, annotated - select for diffs
Mon Sep 30 13:44:16 2013 UTC (10 years, 7 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Access to dependencies/links in HTML files uploaded directly to a course,
  where dependency or linked item was also uploaded directly to the same
  course.
  - Fix regexp used to identify items referenced in javascript object in text
   within <script></script>.
  - .js files in src attribute of <script> tag still retrieved, even if
   (type ="text/javascript" is missing) i.e., not valid xhtml.

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

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