File:  [LON-CAPA] / loncom / xml / scripttag.pm
Revision 1.28: download - view: text, annotated - select for diffs
Thu Dec 21 18:12:12 2000 UTC (23 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- should properly work with meta target now

    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:     } elsif ( $target eq "web" || $target eq "grade" ) {
   31:       &Apache::run::run($bodytext,$safeeval);
   32:     } elsif ($target eq "edit" ) {
   33:       &Apache::run::run($bodytext,$safeeval);
   34:       $result="<br> &lt;$token->[1]&gt; output: <br>$bodytext<br>Source:<br>";
   35:       $result.=&editfield($token->[1],$bodytext);
   36:     }
   37:   } else {
   38:     if ($target ne "meta") { $result = $token->[4]; }
   39:   }
   40:   return $result;
   41: }
   42: 
   43: sub end_script {
   44:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   45:   if ( $target eq "meta" ) { return ''; } 
   46: #  return $token->[2]; 
   47:   return '';
   48: }
   49: 
   50: sub start_display {
   51:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   52:   my $args ='';
   53:   if ( $#$parstack > -1 ) {
   54:     $args=$$parstack[$#$parstack];
   55:   }
   56:   my $bodytext=&Apache::lonxml::get_all_text("/display",$$parser[$#$parser]);
   57:   
   58:   if ( $target eq "modified" ) {
   59:   }
   60:   my $result=&Apache::run::run($bodytext,$safeeval);
   61:   if ($target eq 'grade' ) {
   62:     # grade should produce no output
   63:     $result='';
   64:   }
   65:   if ($target eq "edit" ) {
   66:     $result = 
   67:       "<br> &lt;$token->[1]&gt; output: <br>$bodytext<br>Source:<br>";
   68:     $result.=&editfield($token->[1],$bodytext);
   69:   }
   70:   return $result;
   71: }
   72: 
   73: sub end_display {
   74: }
   75: 
   76: sub start_scriptlib {
   77:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   78:   my $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
   79:   my $result ="";
   80: 
   81:   $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
   82: 				   $$parstack[$#$parstack]);
   83:   my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
   84: 					     $bodytext);
   85:   my $script=&Apache::lonnet::getfile($location);
   86:   if ($script == -1) {
   87:     &Apache::lonxml::error("<b> Unable to find <i>$location</i> for scriptlib</b>");
   88:     return "";
   89:   }
   90:   &Apache::run::run($script,$safeeval);
   91:   #&Apache::lonxml::debug("ran $bodytext:<br>".&Apache::lonnet::getfile($bodytext)."<br>");
   92: 
   93:   if ($target eq "edit" ) {
   94:     $result.=&editfield($token->[1],$bodytext);
   95:   }
   96:   return $result;
   97: }
   98: 
   99: sub end_scriptlib {}
  100: 
  101: sub start_parserlib {
  102:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  103:   my $bodytext=$$parser[$#$parser]->get_text("/parserlib");
  104:   my $result ="";
  105: 
  106:   $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
  107: 				  $$parstack[$#$parstack]);
  108:   my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
  109: 					     $bodytext);
  110:   my $styletext=&Apache::lonnet::getfile($location);
  111:   #&Apache::lonxml::debug("found :$bodytext: in :$location: with :$styletext:");
  112:   if ($styletext == -1) {
  113:     &Apache::lonxml::error("<b> Unable to find <i>$location</i> for parserlib</b>");
  114:     return "";
  115:   }
  116:   %$style = ( %$style , &Apache::style::styleparser($target,$styletext));
  117: 
  118:   if ($target eq "edit" ) {
  119:     $result=&editfield($token->[1],$bodytext);
  120:   }
  121:   return $result;
  122: }
  123: 
  124: sub end_parserlib {
  125: }
  126: 
  127: sub start_footnote {
  128:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  129:   my $result = '';
  130:   $Apache::lonxml::redirection = 0;
  131:   return $result;  
  132: }
  133: 
  134: sub end_footnote {
  135:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  136:   $Apache::lonxml::outputstack =~ s/\"/\&quot\;/g;
  137:   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>";
  138:    $Apache::lonxml::outputstack = "";
  139:    $Apache::lonxml::redirection = 1;
  140:   return $result; 
  141: }
  142: 
  143: sub start_import {
  144:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  145:   my $bodytext=$$parser[$#$parser]->get_text("/import");
  146:   my $result ="";
  147: 
  148:   $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
  149:   my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
  150:   my $file=&Apache::lonnet::getfile($location);
  151:   if ($file == -1) {
  152:     &Apache::lonxml::error("<b> Unable to find <i>$bodytext $location</i> for import</b>");
  153:     return "";
  154:   }
  155: 
  156:   my $dir=$location;
  157:   $dir=~s:/[^/]*$::;
  158:   &Apache::lonxml::newparser($parser,\$file,$dir);
  159: 
  160:   if ($target eq "edit" ) {
  161:     $result.=&editfield($token->[1],$bodytext);
  162:     $result.="Click<a href=\"/res/$bodytext\">here</a> to edit<br></br>"
  163:   }
  164:   return '';
  165: }
  166: 
  167: sub end_import {
  168: }
  169: 
  170: sub editfield {
  171:   my ($tag,$data)=@_;
  172:   
  173:   my $count=0;
  174:   my $maxlength=-1;
  175:   map { $count++;
  176: 	if (length($_) > $maxlength) { $maxlength = length ($_); }
  177:       } split ("\n", $data);
  178: 	  
  179:   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";
  180: }
  181: 
  182: sub getfilenothere {
  183:   my ($filename) = @_;
  184:   my $a="";
  185:   
  186:   $filename=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
  187:   $filename="/home/httpd/html/res".$filename;
  188:   if (! -e $filename ) {
  189:     &Apache::lonnet::subscribe($filename);
  190:     &Apache::lonnet::repcopy($filename);
  191:   }
  192:   if (! -e $filename ) { return -1; };
  193:   my $fh=Apache::File->new($filename);
  194:   while (<$fh>) {
  195:       $a .=$_;
  196:   }
  197:   return $a
  198: }
  199: 
  200: 1;
  201: __END__

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