File:  [LON-CAPA] / loncom / xml / scripttag.pm
Revision 1.24: download - view: text, annotated - select for diffs
Wed Nov 22 00:00:54 2000 UTC (23 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
-added <display> like <script> but returns the result of the script
-removed SCRIPT_RESULT, <display> takes care of this.

    1: # The LearningOnline Network with CAPA
    2: # <script> definiton
    3: 
    4: 
    5: package Apache::scripttag; 
    6: 
    7: use strict;
    8: use Apache::lonnet;
    9: 
   10: sub BEGIN {
   11:   &Apache::lonxml::register('Apache::scripttag',('script','scriptlib',
   12: 						 'parserlib','import',
   13:                                                  'footnote','display'));
   14: }
   15: 
   16: sub start_script {
   17:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   18:   my $args ='';
   19:   if ( $#$parstack > -1 ) {
   20:     $args=$$parstack[$#$parstack];
   21:   }
   22:   my $type = &Apache::run::run("{$args;".'return $type}',$safeeval);
   23:   my $result='';
   24:   #&Apache::lonxml::debug("found type of $type");
   25:   if ($type eq "loncapa/perl") {
   26:     my $bodytext=&Apache::lonxml::get_all_text("/script",$$parser[$#$parser]);
   27:     
   28:     if ( $target eq "modified" ) {
   29:     }
   30:     &Apache::run::run($bodytext,$safeeval);
   31:     
   32:     if ($target eq "edit" ) {
   33:       $result="<br> &lt;$token->[1]&gt; output: <br>$bodytext<br>Source:<br>";
   34:       $result.=&editfield($token->[1],$bodytext);
   35:     }
   36:   } else {
   37:     $result = $token->[4];
   38:   }
   39:   return $result;
   40: }
   41: 
   42: sub end_script {
   43:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   44:   return '';
   45:   #return $token->[2]; 
   46: }
   47: 
   48: sub start_display {
   49:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   50:   my $args ='';
   51:   if ( $#$parstack > -1 ) {
   52:     $args=$$parstack[$#$parstack];
   53:   }
   54:   my $bodytext=&Apache::lonxml::get_all_text("/display",$$parser[$#$parser]);
   55:   
   56:   if ( $target eq "modified" ) {
   57:   }
   58:   my $result=&Apache::run::run($bodytext,$safeeval);
   59:   
   60:   if ($target eq "edit" ) {
   61:     $result = 
   62:       "<br> &lt;$token->[1]&gt; output: <br>$bodytext<br>Source:<br>";
   63:     $result.=&editfield($token->[1],$bodytext);
   64:   }
   65:   return $result;
   66: }
   67: 
   68: sub end_display {
   69: }
   70: 
   71: sub start_scriptlib {
   72:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   73:   my $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
   74:   my $result ="";
   75: 
   76:   $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
   77: 				   $$parstack[$#$parstack]);
   78:   my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
   79: 					     $bodytext);
   80:   my $script=&Apache::lonnet::getfile($location);
   81:   if ($script == -1) {
   82:     &Apache::lonxml::error("<b> Unable to find <i>$location</i> for scriptlib</b>");
   83:     return "";
   84:   }
   85:   &Apache::run::run($script,$safeeval);
   86:   #&Apache::lonxml::debug("ran $bodytext:<br>".&Apache::lonnet::getfile($bodytext)."<br>");
   87: 
   88:   if ($target eq "edit" ) {
   89:     $result.=&editfield($token->[1],$bodytext);
   90:   }
   91:   return $result;
   92: }
   93: 
   94: sub end_scriptlib {}
   95: 
   96: sub start_parserlib {
   97:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
   98:   my $bodytext=$$parser[$#$parser]->get_text("/parserlib");
   99:   my $result ="";
  100: 
  101:   $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
  102: 				  $$parstack[$#$parstack]);
  103:   my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
  104: 					     $bodytext);
  105:   my $styletext=&Apache::lonnet::getfile($location);
  106:   if ($styletext == -1) {
  107:     &Apache::lonxml::error("<b> Unable to find <i>$location</i> for parserlib</b>");
  108:     return "";
  109:   }
  110:   %$style = ( %$style , &Apache::style::styleparser($target,$styletext));
  111: 
  112:   if ($target eq "edit" ) {
  113:     $result=&editfield($token->[1],$bodytext);
  114:   }
  115:   return $result;
  116: }
  117: 
  118: sub end_parserlib {
  119: }
  120: 
  121: sub start_footnote {
  122:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  123:   my $result = '';
  124:   $Apache::lonxml::redirection = 0;
  125:   return $result;  
  126: }
  127: 
  128: sub end_footnote {
  129:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  130:   $Apache::lonxml::outputstack =~ s/\"/\&quot\;/g;
  131:   my $result = "<a href=\"javascript:newWindow=open(\'\',\'new_W\',\'width=500,height=200\');newWindow.document.open(\'text/html\',\'replace\');newWindow.document.writeln(\'<html><head><title>newwindow</title></head><body bgcolor=&quot;#FFFFFF&quot;> $Apache::lonxml::outputstack </body></html>\');newWindow.document.close();void(0);\"><sup>*</sup></a>";
  132:    $Apache::lonxml::outputstack = "";
  133:    $Apache::lonxml::redirection = 1;
  134:   return $result; 
  135: }
  136: 
  137: sub start_import {
  138:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  139:   my $bodytext=$$parser[$#$parser]->get_text("/import");
  140:   my $result ="";
  141: 
  142:   $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
  143:   my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
  144:   my $file=&Apache::lonnet::getfile($location);
  145:   if ($file == -1) {
  146:     &Apache::lonxml::error("<b> Unable to find <i>$bodytext $location</i> for import</b>");
  147:     return "";
  148:   }
  149: 
  150:   my $dir=$location;
  151:   $dir=~s:/[^/]*$::;
  152:   &Apache::lonxml::newparser($parser,\$file,$dir);
  153: 
  154:   if ($target eq "edit" ) {
  155:     $result.=&editfield($token->[1],$bodytext);
  156:     $result.="Click<a href=\"/res/$bodytext\">here</a> to edit<br></br>"
  157:   }
  158: }
  159: 
  160: sub end_import {
  161: }
  162: 
  163: sub editfield {
  164:   my ($tag,$data)=@_;
  165:   
  166:   my $count=0;
  167:   my $maxlength=-1;
  168:   map { $count++;
  169: 	if (length($_) > $maxlength) { $maxlength = length ($_); }
  170:       } split ("\n", $data);
  171: 	  
  172:   return "<br></br>\n&lt;$tag&gt;<br></br>\n&nbsp;&nbsp;&nbsp;<textarea rows=\"$count\" cols=\"$maxlength\" name=homework_edit_".$Apache::lonxml::curdepth.">$data</textarea><br></br>\n&lt;/$tag&gt;<br></br>\n";
  173: }
  174: 
  175: sub getfilenothere {
  176:   my ($filename) = @_;
  177:   my $a="";
  178:   
  179:   $filename=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
  180:   $filename="/home/httpd/html/res".$filename;
  181:   if (! -e $filename ) {
  182:     &Apache::lonnet::subscribe($filename);
  183:     &Apache::lonnet::repcopy($filename);
  184:   }
  185:   if (! -e $filename ) { return -1; };
  186:   my $fh=Apache::File->new($filename);
  187:   while (<$fh>) {
  188:       $a .=$_;
  189:   }
  190:   return $a
  191: }
  192: 
  193: 1;
  194: __END__

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