File:  [LON-CAPA] / loncom / xml / scripttag.pm
Revision 1.176: download - view: text, annotated - select for diffs
Tue Nov 7 12:26:01 2023 UTC (5 months, 3 weeks ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, HEAD
- Support domain default or user override for activation or deactivation
  of CodeMirror.

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

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