File:  [LON-CAPA] / loncom / xml / scripttag.pm
Revision 1.14: download - view: text, annotated - select for diffs
Thu Sep 14 20:57:49 2000 UTC (23 years, 8 months ago) by albertel
Branches: MAIN
CVS tags: HEAD

-outputs error message for missing <import> files

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

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