File:  [LON-CAPA] / loncom / xml / scripttag.pm
Revision 1.16: download - view: text, annotated - select for diffs
Tue Sep 19 19:10:01 2000 UTC (23 years, 8 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- moved $external::target to always be defined
- fixed scripttag to allow it be able to report results from scripts
- fixed caparesponse to not print out anything

    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: }
   14: 
   15: sub start_script {
   16:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   17:   my $result="";
   18:   $safeeval->share('$Apache::scripttag::start_script::result');
   19: #  my $bodytext=$$parser[$#$parser]->get_text("/script");
   20:   my $bodytext=&Apache::lonxml::get_all_text("script",$$parser[$#$parser]);
   21: 
   22:   if ( $target eq "modified" ) {
   23:     
   24:   }
   25: 
   26: #  print "<br> script runs $bodytext</br>";
   27:   &Apache::run::run($bodytext,$safeeval);
   28: 
   29:   if ($target eq "edit" ) {
   30:     $result= "<br> &lt;$token->[1]&gt; output: <br>$bodytext<br>Source:<br>";
   31:     $result.=&editfield($token->[1],$bodytext);
   32:   }
   33:   return $result;
   34: }
   35: 
   36: sub end_script {}
   37: 
   38: sub start_scriptlib {
   39:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   40:   my $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
   41:   my $result ="";
   42: 
   43:   $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
   44:   &Apache::run::run(&getfile($bodytext),$safeeval);
   45:   #print "ran $bodytext:<br>".&getfile($bodytext)."<br>";
   46: 
   47:   if ($target eq "edit" ) {
   48:     $result.=&editfield($token->[1],$bodytext);
   49:   }
   50:   return $result;
   51: }
   52: 
   53: sub end_scriptlib {}
   54: 
   55: sub start_parserlib {
   56:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
   57:   my $bodytext=$$parser[$#$parser]->get_text("/parserlib");
   58:   my $result ="";
   59: 
   60:   $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
   61:   %$style = ( %$style , &Apache::style::styleparser($target, 
   62: 		&getfile($bodytext)));
   63: 
   64:   if ($target eq "edit" ) {
   65:     $result=&editfield($token->[1],$bodytext);
   66:   }
   67:   return $result;
   68: }
   69: 
   70: sub end_parserlib {
   71: }
   72: 
   73: sub start_import {
   74:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
   75:   my $bodytext=$$parser[$#$parser]->get_text("/import");
   76:   my $result ="";
   77: 
   78:   $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
   79:   my $file=&getfile($bodytext);
   80:   if ($file eq -1) {
   81:     return "<b> Unable to find <i>$bodytext</i> for import</b>";
   82:   }
   83: 
   84:   my $tempparser=HTML::TokeParser->new(\$file);
   85:   push (@$parser,$tempparser);
   86: 
   87:   if ($target eq "edit" ) {
   88:     $result.=&editfield($token->[1],$bodytext);
   89:     $result.="Click<a href=\"/res/$bodytext\">here</a> to edit<br></br>"
   90:   }
   91: }
   92: 
   93: sub end_import {
   94: }
   95: 
   96: sub editfield {
   97:   my ($tag,$data)=@_;
   98:   
   99:   my $count=0;
  100:   my $maxlength=-1;
  101:   map { $count++;
  102: 	if (length($_) > $maxlength) { $maxlength = length ($_); }
  103:       } split ("\n", $data);
  104: 	  
  105:   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";
  106: }
  107: 
  108: sub getfile {
  109:   my ($filename) = @_;
  110:   my $a="";
  111:   
  112:   $filename=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
  113:   $filename="/home/httpd/html/res".$filename;
  114:   if (! -e $filename ) {
  115:     &Apache::lonnet::subscribe($filename);
  116:     &Apache::lonnet::repcopy($filename);
  117:   }
  118:   if (! -e $filename ) { return -1; };
  119:   my $fh=Apache::File->new($filename);
  120:   while (<$fh>) {
  121:       $a .=$_;
  122:   }
  123:   return $a
  124: }
  125: 
  126: 1;
  127: __END__

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