File:  [LON-CAPA] / loncom / xml / scripttag.pm
Revision 1.171: download - view: text, annotated - select for diffs
Sun Aug 3 14:20:43 2014 UTC (9 years, 9 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Support style of packaging .js dependency used in Camtasia Studio 8.4.2
  *_player.html files.
- Dependency on spritesheet.min.css no longer included as <link> tag in
  Camtasia's top-levl *.html file, so explicitly include in %allow.

    1: # The LearningOnline Network with CAPA
    2: # <script> definiton
    3: #
    4: # $Id: scripttag.pm,v 1.171 2014/08/03 14:20:43 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{\.addMediaSrc\((["'])((?!\1).)+\1\);}) {
  146:                       my $quote = $1;
  147:                       if ($bodytext =~ m{\Q.addMediaSrc($quote\E([^$quote]+)\Q$quote)\E}) {
  148:                           my $fname = $1;
  149:                           my $cleanhref =
  150:                               &Apache::londefdef::clean_docs_httpref($fname,$url,$cdom,$cnum);
  151:                           if ($cleanhref) {
  152:                               &Apache::lonxml::extlink($cleanhref);
  153:                           }
  154:                       }
  155:                   }
  156:                   if ($bodytext =~ m{\.set\w+(Src|Swf)\(["']}i) {
  157:                       my @srcs = split(/\.set/,$bodytext);
  158:                       if (scalar(@srcs) > 1) {
  159:                           foreach my $item (@srcs) {
  160:                               if ($item =~ m{^(FlashPlayerSwf|MediaSrc|XMPSrc|ConfigurationSrc|PosterImageSrc)\((['"])(?:(?!\2).)+\2\)}is) {
  161:                                   my $srctype = $1;
  162:                                   my $quote = $2;
  163:                                   my ($fname) = ($item =~ m{^\Q$srctype($quote\E([^$quote]+)\Q$quote)\E}); 
  164:                                   my $cleanhref =
  165:                                       &Apache::londefdef::clean_docs_httpref($fname,$url,$cdom,$cnum);
  166:                                   if ($cleanhref) {
  167:                                       &Apache::lonxml::extlink($cleanhref);
  168:                                       if ($srctype eq 'ConfigurationSrc') {
  169:                                           if ($cleanhref =~ m{^(.+/)configuration_express\.xml$}) {
  170: #
  171: # Camtasia 8.1: express_show/spritesheet.png needed, and included in zip archive.
  172: # Not referenced directly in <main>.html or <main>_player.html files,
  173: # so call lonxml::extlink() here to include httpref for the uploaded file.
  174: # (where <main> is name user gave to file/archive).
  175: #
  176: 
  177:                                               my $spritesheet = $1.'express_show/spritesheet.png';
  178:                                               if (&Apache::lonnet::repcopy_userfile($spritesheet) eq 'ok') {
  179:                                                   &Apache::lonxml::extlink($spritesheet);
  180:                                               }
  181:                                           }
  182: #
  183: # Camtasia 8.4: express_show/spritesheet.min.css needed, and included in zip archive.
  184: # Not referenced directly in <main>.html or <main>_player.html files,
  185: # so call lonxml::extlink() here to include httpref for the uploaded file.
  186: # (where <main> is name user gave to file/archive).
  187: #
  188:                                           my $spritesheet_css = $1.'express_show/spritesheet.min.css';
  189:                                           if (&Apache::lonnet::repcopy_userfile($spritesheet_css) eq 'ok') {
  190:                                               &Apache::lonxml::extlink($spritesheet_css);
  191:                                           }
  192:                                       } elsif ($srctype eq 'PosterImageSrc') {
  193:                                           if ($fname =~ m{^(.+)_First_Frame\.png$}) {
  194:                                               my $prefix = $1;
  195:                                               my ($path) = ($cleanhref =~ m{^(.+/)\Q$fname\E});
  196: #
  197: # Camtasia 8.1: <main>_Thumbnails.png needed, and included in zip archive.
  198: # Not referenced directly in <main>.html or <main>_player.html files,
  199: # so call lonxml::extlink() here to include httpref for the uploaded file
  200: # (where <main> is name user gave to file/archive).
  201: #
  202:                                               my $thumbnail = $path.$prefix.'_Thumbnails.png';
  203:                                               if (&Apache::lonnet::repcopy_userfile($thumbnail) eq 'ok') {
  204:                                                   &Apache::lonxml::extlink($thumbnail);
  205:                                               }
  206:                                           }
  207:                                       }
  208:                                   }
  209:                               }
  210:                           }
  211:                       }
  212:                   }
  213:                   if ($bodytext =~ /\(document,\s*(['"])script\1,\s*\[([^\]]+)\]\);/s) {
  214:                       my $scriptslist = $2;
  215:                       my @srcs = split(/\s*,\s*/,$scriptslist);
  216:                       foreach my $src (@srcs) {
  217:                           if ($src =~ /(["'])(?:(?!\1).)+\.js\1/) {
  218:                               my $quote = $1;
  219:                               my ($fname) = ($src =~ m/\Q$quote\E([^$quote]+)\Q$quote\E/);
  220:                               my $cleanhref =
  221:                                   &Apache::londefdef::clean_docs_httpref($fname,$url,$cdom,$cnum);
  222:                               if ($cleanhref) {
  223:                                   &Apache::lonxml::extlink($cleanhref);
  224:                               }
  225:                           }
  226:                       }
  227:                   }
  228:                   if ($bodytext =~ m{loadScript\(\s*(['"])((?:(?!\1).)+\.js)\1,\s*function}is) {
  229:                       my $fname = $2;
  230:                       if ($fname) {
  231:                           my $cleanhref =
  232:                               &Apache::londefdef::clean_docs_httpref($fname,$url,$cdom,$cnum);
  233:                           if ($cleanhref) {
  234:                               &Apache::lonxml::extlink($cleanhref);
  235:                           }
  236:                       }
  237:                   }
  238:               }
  239:           }
  240:       }
  241:   }
  242:   return $result;
  243: }
  244: 
  245: sub end_script {
  246:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  247:   if ( $target eq "meta" ) { return ''; } 
  248:   my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
  249:   my $result='';
  250:   #other script blocks need to survive
  251:   if ($type ne "loncapa/perl" && $target ne 'tex') {
  252:     return $token->[2];
  253:   } elsif ($target eq 'edit' ) {
  254:     return &Apache::edit::end_table();
  255:   } elsif ($target eq 'answer') {
  256:     $Apache::lonxml::evaluate++;
  257:   }
  258:   return '';
  259: }
  260: 
  261: sub start_display {
  262:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  263:   @Apache::scripttag::parser_env = @_;
  264:   my $result;
  265: 
  266:   if ( $target eq "modified" ) {
  267:       $result=$token->[4].&Apache::edit::modifiedfield("/display",$parser);
  268:   } elsif ( $target eq 'web' || $target eq 'tex' ||
  269: 	    $target eq 'grade' || $target eq 'webgrade' ||
  270: 	    $target eq 'answer' || $target eq 'analyze') {
  271:       my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/display",$parser);
  272:       if (!$Apache::lonxml::default_homework_loaded) {
  273: 	  &Apache::lonxml::default_homework_load($safeeval);
  274:       }
  275:       $result=&Apache::run::run($bodytext,$safeeval);
  276:       if ($target eq 'grade' || $target eq 'answer' ||
  277: 	  $target eq 'analyze') {
  278: 	  # grade/answer/analyxe should produce no output but if we
  279: 	  # are redirecting, the redirecter should know what to do
  280: 	  # with the output
  281: 	  if (!$Apache::lonxml::redirection) { $result=''; }
  282:       }
  283:       $Apache::lonxml::post_evaluate=0;
  284:   } elsif ($target eq "edit" ) {
  285:     my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/display",$parser);
  286:     #$result = 
  287:     #  "<br /> &lt;$token->[1]&gt; output: <br />$bodytext<br />Source:<br />";
  288:     #$result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1);
  289:     $result=&Apache::edit::tag_start($target,$token,'Script With Display');
  290:     $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,1)
  291:   } elsif ($target eq 'meta') {
  292:       my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/display",$parser);
  293:   }
  294:   return $result;
  295: }
  296: 
  297: sub end_display {
  298:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  299:   if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
  300:   return '';
  301: }
  302: 
  303: sub start_scriptlib {
  304:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  305:   my $bodytext;
  306:   my $result ='';
  307:   my $error='';
  308: 
  309:   if ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
  310:       $target eq 'meta' || $target eq 'edit' || $target eq 'answer' ||
  311:       $target eq 'analyze' || $target eq 'webgrade') {
  312:     $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
  313:     $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
  314: 				     $$parstack[$#$parstack]);
  315:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
  316: 					       $bodytext);
  317:     my $script=&Apache::lonnet::getfile($location);
  318:     if ($script == -1) {
  319:       if ($target eq 'edit') {
  320:         $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";
  321:       } else {
  322: 	&Apache::lonxml::error("<b> Unable to find <i>$location</i> for scriptlib</b>");
  323: 	return "";
  324:       }
  325:     }
  326:     &Apache::run::run($script,$safeeval);
  327:     #&Apache::lonxml::debug("ran $bodytext:<br />".&Apache::lonnet::getfile($bodytext)."<br />");
  328:   }
  329:   if ($target eq "edit" ) {
  330:     $result=
  331:       &Apache::edit::tag_start($target,$token,'New Script Functions').
  332: 	&Apache::edit::editline($token->[1],$bodytext,'scriptlib',40).
  333:             &Apache::edit::browse(undef,'textnode').
  334: 	  $error.'</td></tr>'.
  335: 	    &Apache::edit::end_table();
  336:   }
  337:   if ($target eq "modified" ) {
  338:       $result=$token->[4].&Apache::edit::modifiedfield("/scriptlib",$parser);
  339:   }
  340:   return $result;
  341: }
  342: 
  343: sub end_scriptlib {
  344:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  345:   my @result;
  346:   if ($target eq "edit" ) { $result[1]='no'; }
  347:   return @result;
  348: }
  349: 
  350: sub start_parserlib {
  351:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  352:   my $bodytext;
  353:   my $result ="";
  354:   my $error='';
  355:   if ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
  356:       $target eq 'meta' || $target eq 'edit' || $target eq 'answer' ||
  357:       $target eq 'analyze' || $target eq 'webgrade') {
  358:     $bodytext=$$parser[$#$parser]->get_text("/parserlib");
  359:     $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
  360: 				     $$parstack[$#$parstack]);
  361:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
  362: 					       $bodytext);
  363:     my $styletext=&Apache::lonnet::getfile($location);
  364:     #&Apache::lonxml::debug("found :$bodytext: in :$location: with :$styletext:");
  365:     if ($styletext == -1) {
  366:       if ($target eq 'edit') {
  367: 	$error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
  368:       } else {
  369: 	&Apache::lonxml::error("<b> Unable to find <i>$location</i> for parserlib</b>");
  370: 	return "";
  371:       }
  372:     }
  373:     %$style = ( %$style , &Apache::style::styleparser($target,$styletext));
  374:   }
  375:   if ($target eq "edit" ) {
  376:     $result=
  377:       &Apache::edit::tag_start($target,$token,'New Tag Definitions').
  378: 	&Apache::edit::editline($token->[1],$bodytext,'',40).
  379: 	  $error.'</td></tr>'.
  380: 	    &Apache::edit::end_table();
  381:   }
  382:   if ($target eq "modified" ) {
  383:       $result=$token->[4].&Apache::edit::modifiedfield("/parserlib",$parser);
  384:   }
  385:   return $result;
  386: }
  387: 
  388: sub end_parserlib {
  389:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  390:   my @result;
  391:   if ($target eq "edit" ) { $result[1]='no'; }
  392:   return @result;
  393: }
  394: 
  395: sub start_window {
  396:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  397:     my $result = '';
  398:     if ($target eq 'web' || $target eq 'webgrade') {
  399: 	&Apache::lonxml::startredirection;
  400:     } elsif ($target eq 'tex') {
  401:         my $printtext=&Apache::lonxml::get_param('printtext',$parstack,$safeeval);
  402:         if ($printtext=~/\w/) {
  403: # If printtext is given, do not output any intervening information
  404:            &Apache::lonxml::startredirection;
  405:         } else {
  406:            $result = '\unskip\footnote{';
  407:         }
  408:     } elsif ($target eq 'edit') {
  409: 	$result.=&Apache::edit::tag_start($target,$token);
  410: 	$result.=&Apache::edit::text_arg('Text of Link:','linktext',$token,70);
  411: 	$result.=&Apache::edit::text_arg('Height:','height',$token,5);
  412: 	$result.=&Apache::edit::text_arg('Width:','width',$token,5);
  413:         $result.=&Apache::edit::text_arg('Printed text (optional):','printtext',$token,20);
  414: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  415:     } elsif ($target eq 'modified') {
  416: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  417: 						     $safeeval,'linktext',
  418: 						     'width','height');
  419: 	if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
  420:     }
  421:     return $result;  
  422: }
  423: 
  424: sub end_window {
  425:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  426:   my $result;
  427:   if ($target eq 'web' || $target eq 'webgrade') {
  428:     my $output=&Apache::lonxml::endredirection;
  429:     my $linktext= &Apache::lonxml::get_param('linktext',$parstack,$safeeval);
  430:     if (!$linktext) { $linktext='<sup>*</sup>'; }
  431:     my $width= &Apache::lonxml::get_param('width',$parstack,$safeeval);
  432:     if (!$width) { $width='500'; }
  433:     my $height= &Apache::lonxml::get_param('height',$parstack,$safeeval);
  434:     if (!$height) { $height='200'; }
  435:     $result=&Apache::loncommon::modal_adhoc_window
  436:            ("LONCAPA_newwindow_$Apache::lonxml::curdepth",$width,$height,$output,$linktext);
  437:   } elsif ($target eq 'tex') {
  438:       my $printtext=&Apache::lonxml::get_param('printtext',$parstack,$safeeval);
  439:       if ($printtext=~/\w/) {
  440: # If a "printtext" is given, proceed to retrieve all intervening information and trash it
  441:          my $output=&Apache::lonxml::endredirection;
  442: # Use printtext instead
  443:          $result=$printtext;
  444:       } else {
  445:          $result='}';
  446:       }
  447:   } else {
  448:       $result = '';
  449:   }
  450:   return $result; 
  451: }
  452: 
  453: 
  454: sub start_windowlink {
  455:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  456:     my $result = '';
  457:     if ($target eq 'web' || $target eq 'webgrade') {
  458:         &Apache::lonxml::startredirection;
  459:     } elsif ($target eq 'edit') {
  460:         $result.=&Apache::edit::tag_start($target,$token);
  461:         $result.=&Apache::edit::text_arg('Link:','href',$token,70);
  462:         $result.=&Apache::edit::text_arg('Height:','height',$token,5);
  463:         $result.=&Apache::edit::text_arg('Width:','width',$token,5);
  464:         $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  465:     } elsif ($target eq 'modified') {
  466:         my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  467:                                                      $safeeval,'href',
  468:                                                      'width','height');
  469:         if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
  470:     }
  471:     return $result;
  472: }
  473: 
  474: sub end_windowlink {
  475:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  476:   my $result;
  477:   if ($target eq 'web' || $target eq 'webgrade') {
  478:     my $output=&Apache::lonxml::endredirection;
  479:     my $href= &Apache::lonxml::get_param('href',$parstack,$safeeval);
  480:     if (!$href) { $href='/adm/rat/empty.html'; }
  481:     my $width= &Apache::lonxml::get_param('width',$parstack,$safeeval);
  482:     if (!$width) { $width='500'; }
  483:     my $height= &Apache::lonxml::get_param('height',$parstack,$safeeval);
  484:     if (!$height) { $height='200'; }
  485:     $result=&Apache::loncommon::modal_link($href,$output,$width,$height);
  486:   } else {
  487:       $result = '';
  488:   }
  489:   return $result;
  490: }
  491: 
  492: 
  493: sub start_togglebox {
  494:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  495:     my $result = '';
  496:     if ($target eq 'web' || $target eq 'webgrade') {
  497:         my $id="LONCAPA_togglebox_$Apache::lonxml::curdepth";
  498:         my $heading=&Apache::lonxml::get_param('heading',$parstack,$safeeval);
  499:         unless ($heading) { $heading=''; } else { $heading.=' '; }
  500:         my $showtext=&Apache::lonxml::get_param('showtext',$parstack,$safeeval);
  501:         my $hidetext=&Apache::lonxml::get_param('hidetext',$parstack,$safeeval);
  502:         my $headerbg=&Apache::lonxml::get_param('headerbg',$parstack,$safeeval);
  503:         $result=&Apache::loncommon::start_togglebox($id,$heading,$headerbg,$hidetext,$showtext);
  504:     } elsif ($target eq 'tex') {
  505:         my $heading=&Apache::lonxml::get_param('heading',$parstack,$safeeval);
  506:         unless ($heading) { $heading=''; } else { $heading.=' '; }
  507:         $result = "\n\n".'\fbox{{\bf '.$heading.'} \qquad '."\n";
  508:     } elsif ($target eq 'edit') {
  509:         $result.=&Apache::edit::tag_start($target,$token);
  510:         $result.=&Apache::edit::text_arg('Heading:','heading',$token,70);
  511:         $result.=&Apache::edit::text_arg('Header Background:','headerbg',$token,7);
  512:         $result.=&Apache::edit::text_arg('Show text:','showtext',$token,10);
  513:         $result.=&Apache::edit::text_arg('Hide text:','hidetext',$token,10);
  514:         $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  515:     } elsif ($target eq 'modified') {
  516:         my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  517:                                                      $safeeval,'heading',
  518:                                                      'showtext','hidetext',
  519:                                                      'headerbg','textbg');
  520:         if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
  521:     }
  522:     return $result;
  523: }
  524: 
  525: sub end_togglebox {
  526:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  527:   my $result;
  528:   if ($target eq 'web' || $target eq 'webgrade') {
  529:     $result=&Apache::loncommon::end_togglebox();
  530:   } elsif ($target eq 'tex') {
  531:       $result = "}\n\n";
  532:   } else {
  533:       $result = '';
  534:   }
  535:   return $result;
  536: }
  537: 
  538: 
  539: 
  540: sub start_import {
  541:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  542:   my $bodytext=$$parser[$#$parser]->get_text("/import");
  543:   my $result ="";
  544: 
  545:   $bodytext=&Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
  546: 
  547:   if ($target eq 'web' ||  $target eq 'webgrade' || $target eq 'grade' 
  548:       || $target eq 'answer' || $target eq 'tex' || $target eq 'analyze' ) {
  549:     # FIXME this probably needs to be smart about construction vs.
  550:     # non construction space.
  551:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
  552:     my $file=&Apache::lonnet::getfile($location);
  553:     if ($file == -1) {
  554:       &Apache::lonxml::error("<b> Unable to find <i>$bodytext as $location</i> for import</b>");
  555:       return "";
  556:     }
  557:     my $importmode=&Apache::lonxml::get_param('importmode',$parstack,$safeeval);
  558:     if (($importmode eq 'problem') || ($importmode eq 'part')) {
  559: # We are using import to import published problems
  560:        if (($importmode eq 'problem') || ($file=~/<part[^<]*>/s)) {
  561: # We explicitly don't want this to be a separate part or the problem already has parts
  562:           $file=~s/^\s*<problem>/<library>/s;
  563: 	  $file=~s/<\/problem>\s*$/<\/library>/s;
  564:        } else {
  565: # We want this to be a separate part, but it currently is not
  566:           $file=~s/^\s*<problem>/<library><part>/s;
  567: 	  $file=~s/<\/problem>\s*$/<\/part><\/library>/s;
  568:        }
  569:     }
  570:     my $dir=$location;
  571:     $dir=~s:/[^/]*$::;
  572:     #  &Apache::lonxml::debug("directory $dir $location file $file \n<b>END</b>\n");
  573:     my $id= &Apache::lonxml::get_id($parstack,$safeeval);
  574:     if (!$id) { $id=$Apache::lonxml::curdepth; }
  575:     push(@Apache::inputtags::import,$id);
  576:     push(@Apache::inputtags::importlist,$id);
  577: 
  578:     &Apache::lonxml::newparser($parser,\$file,$dir);
  579: 
  580:   } elsif ($target eq "edit" ) {
  581:     $result.=&Apache::edit::tag_start($target,$token);
  582:     my $location=$token->[1];
  583:     $location=~s/^\s*//s;
  584:     $location=~s/\s*$//s;
  585:     $result.=&Apache::edit::editline($location,$bodytext,'',40);
  586:     $result.=&Apache::edit::browse(undef,'textnode');
  587:     $result.= '&nbsp;<label>'.&mt('Import as:').
  588:               '<select name="importmode_'.$Apache::lonxml::curdepth.'">';
  589:     my %options=&Apache::lonlocal::texthash(''        => 'as standard library',
  590:                                             'problem' => 'as problem',
  591:                                             'part'    => 'as problem part(s)');
  592:     foreach my $option (sort(keys(%options))) {
  593:        $result.='<option value="'.$option.'"';
  594:        if ($option eq &Apache::lonxml::get_param('importmode',$parstack,$safeeval)) {
  595:           $result.=' selected="selected"';
  596:        }
  597:        $result.='>'.$options{$option}.'</option>';
  598:     }
  599:     $result.='</select></label>';
  600:     #FIXME this need to convert $bodytext to be a contruction space reference
  601:     #my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
  602:     #$result.="Click<a href=\"$location\">here</a> to edit<br />"
  603:   } elsif ($target eq 'modified') {
  604:       &Apache::edit::get_new_args($token,$parstack,$safeeval,'importmode');
  605:       $result='<import id="'.$token->[2]{'id'}.'" importmode="'.$token->[2]{'importmode'}.'">';
  606:       $result.=&Apache::edit::modifiedfield("/import",$parser);
  607:   } elsif ($target eq 'meta') {
  608:     my $id= &Apache::lonxml::get_id($parstack,$safeeval);
  609:     $result.='<import part="'.$Apache::inputtags::part;
  610:     if ($id) {
  611:       $result.='" id="'.$id;
  612:     }
  613:     $result.='" importmode="'.$token->[2]{'importmode'}.'">';
  614:     $result.=$bodytext;
  615:     $result.='</import>';
  616:   }
  617:   return $result;
  618: }
  619: 
  620: sub end_import {
  621:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  622:   pop(@Apache::inputtags::import);
  623:   my $result;
  624:   if ($target eq 'edit' ) { $result=&Apache::edit::end_row.
  625: 				&Apache::edit::end_table(); }
  626:   return $result;
  627: }
  628: 
  629: sub start_storetc {
  630:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  631:   my $result = '';
  632:   &Apache::lonxml::startredirection;
  633:   return $result; 
  634: }
  635: 
  636: sub end_storetc {
  637:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  638:     my $result;
  639:     my $output=&Apache::lonxml::endredirection;
  640:     $output =~ s/\"/\&quot\;/g;
  641:     $result = '{\bf '.$output.'.}}\write\tcfile{\protect\tcpc{ '.$output.'.}{\the\value{relpage}}}';
  642:     return $result;
  643: }
  644: 
  645: 
  646: sub start_physnet {
  647:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  648:     my $bodytext = '/adm/includes/physnet.sty';
  649:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
  650:     my $cbistyletext=&Apache::lonnet::getfile($location);
  651: 
  652:     %$style = (%$style,&Apache::style::styleparser($target,$cbistyletext));
  653:     $$parser['-1']->unget_token($token);
  654: #    if ( defined($$style{'physnet'}) ) {
  655: #        &Apache::lonxml::newparser($parser,\$$style{'physnet'});
  656: #    }
  657:     return "";
  658: }
  659: 
  660: sub end_physnet {
  661:   return '';
  662: }
  663: 
  664: sub start_standalone {
  665:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  666:   my $result='';
  667:   if ($target eq 'web' || $target eq 'webgrade') {
  668:     if ( $env{'request.course.id'} ) {
  669:       my $inside = &Apache::lonxml::get_all_text("/standalone",$parser,$style);
  670:     } else {
  671:       $result='<table bgcolor="#E1E1E1" border="2"><tr><td>';
  672:     }
  673:   }
  674:   return $result;
  675: }
  676: 
  677: sub end_standalone {
  678:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  679:   my $result='';
  680:   if ($target eq 'web' || $target eq 'webgrade' ) {
  681:     if ( $env{'request.course.id'} ) {
  682:     } else {
  683:       $result='</td></tr></table>';
  684:     }
  685:   }
  686:   return $result;
  687: }
  688: 
  689: sub start_comment {
  690:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  691:   my $result='';
  692:   if ($target eq 'edit') {
  693:     $result=&Apache::edit::tag_start($target,$token);
  694:     my $bodytext=&Apache::lonxml::get_all_text("/comment",$parser,$style);
  695:     $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,4)
  696:   } elsif ( $target eq 'modified') {
  697:     $result=$token->[4].&Apache::edit::modifiedfield("/comment",$parser);
  698:   } elsif ( $target eq 'web'    || $target eq 'tex'  || $target eq 'grade'   ||
  699: 	    $target eq 'answer' || $target eq 'meta' || $target eq 'analyze' ||
  700: 	    $target eq 'webgrade') {
  701:     #normally throw away comments
  702:     my $bodytext=&Apache::lonxml::get_all_text("/comment",$parser,$style);
  703:   }
  704:   return $result;
  705: }
  706: 
  707: sub end_comment {
  708:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  709:   if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
  710:   return '';
  711: }
  712: 
  713: 
  714: sub xmlparse {
  715:   my ($string) = @_;
  716:   &Apache::lonxml::debug("xmlparse recursion starting with $string");
  717:   # Apache::run::evaluate does an 'eval' on the name of the subroutine
  718:   # if it detects something that looks like a subroutine, this ends up calling
  719:   # things without any arguments and since perl is nice enough to pass
  720:   # along the default arguments when you don't explicitly say no arguments
  721:   # if you call &xmlparse, it gets &xmlparse passed as it argument.
  722:   # Same thing soccurs with &chemparse.
  723:   if ($string eq '&xmlparse') { return '&xmlparse'; }
  724:   if ($string eq '&chemparse') { return '&chemparse'; }
  725:   my ($target,$token,$tagstack,$parstack,$oldparser,$safeeval,$style)=
  726:     @Apache::scripttag::parser_env;
  727:   my @parser;
  728:   &Apache::lonxml::newparser(\@parser,\$string);
  729:   &Apache::lonxml::startredirection();
  730:   my $result=&Apache::lonxml::inner_xmlparse($target,$tagstack,
  731: 					     $parstack,\@parser,
  732: 					     $safeeval,$style);
  733:   $result.=&Apache::lonxml::endredirection();
  734:   &Apache::lonxml::debug("target is $target xmlparse recursion ending with $result");
  735:   return $result;
  736: }
  737: 
  738: sub start_num {
  739:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  740:     my $result = '';
  741:     my $inside = &Apache::lonxml::get_all_text_unbalanced("/num",$parser);
  742:     if ($target eq 'tex' || $target eq 'web' || $target eq 'webgrade') {
  743: 	$inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
  744: 	if (!$Apache::lonxml::default_homework_loaded) {
  745: 	    &Apache::lonxml::default_homework_load($safeeval);
  746: 	}
  747: 	@Apache::scripttag::parser_env = @_;
  748: 	my $format=&Apache::lonxml::get_param('format',$parstack,$safeeval);
  749: 	$result=&Apache::run::run("return &prettyprint(q\0$inside\0,q\0$format\0);",$safeeval);
  750:     }    
  751:     return $result;
  752: }
  753: 
  754: sub end_num {
  755:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  756:     my $result = '';
  757:     return $result;
  758: }
  759: 
  760: sub start_parse {
  761:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  762:     my $result = '';
  763:     if ( $target eq 'web'    || $target eq 'tex'    ||
  764: 	 $target eq 'grade'  || $target eq 'answer' ||
  765: 	 $target eq 'analyze'|| $target eq 'webgrade') {
  766: 	my $inside = &Apache::lonxml::get_all_text_unbalanced("/parse",$parser);
  767: 	$inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
  768: 	if (!$Apache::lonxml::default_homework_loaded) {
  769: 	    &Apache::lonxml::default_homework_load($safeeval);
  770: 	}
  771: 	@Apache::scripttag::parser_env = @_;
  772: 	$result=&Apache::run::run("return &xmlparse(q\0$inside\0);",$safeeval);
  773: 	if ($target eq 'grade' || $target eq 'answer' ||
  774: 	    $target eq 'analyze') {
  775: 	    # grade/answer/analyxe should produce no output but if we
  776: 	    # are redirecting, the redirecter should know what to do
  777: 	    # with the output
  778: 	    if (!$Apache::lonxml::redirection) { $result=''; }
  779: 	}
  780:     }
  781:     return $result;
  782: }
  783: 
  784: sub end_parse {
  785:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  786:     my $result = '';
  787:     return $result;
  788: }
  789: 
  790: sub start_algebra {
  791:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  792:     my $result = '';
  793:     if ( $target eq 'web'     || $target eq 'tex'    ||
  794: 	 $target eq 'grade'   || $target eq 'answer' ||
  795: 	 $target eq 'analyze' || $target eq 'webgrade') {
  796: 	my $inside = &Apache::lonxml::get_all_text_unbalanced("/algebra",$parser);
  797: 	$inside = &Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
  798: 	if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
  799: 	    my $style=&Apache::lonxml::get_param('style',$parstack,$safeeval);
  800: 	    $result=&Apache::lontexconvert::algebra($inside,$target,$style,$parstack,$safeeval);
  801: 	}
  802: 	$Apache::lonxml::post_evaluate=0;
  803:     }
  804:     return $result;
  805: }
  806: 
  807: sub end_algebra {
  808:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  809:     my $result = '';
  810:     return $result;
  811: }
  812: 
  813: 1;
  814: __END__
  815: 
  816: =pod
  817: 
  818: =head1 NAME
  819: 
  820: Apache::scripttag.pm
  821: 
  822: =head1 SYNOPSIS
  823: 
  824: implements <script>, <scriptlib>, <parserlib>,
  825: and <import>
  826: 
  827: This is part of the LearningOnline Network with CAPA project
  828: described at http://www.lon-capa.org.
  829: 
  830: =cut
  831: 

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