Annotation of loncom/xml/scripttag.pm, revision 1.15

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # <script> definiton
                      3: 
                      4: 
                      5: package Apache::scripttag; 
                      6: 
                      7: use strict;
1.3       albertel    8: use Apache::lonnet;
1.1       albertel    9: 
                     10: sub BEGIN {
1.6       albertel   11:   &Apache::lonxml::register('Apache::scripttag',('script','scriptlib',
                     12: 						 'parserlib','import'));
1.1       albertel   13: }
                     14: 
                     15: sub start_script {
1.2       albertel   16:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
                     17:   my $result="";
1.15    ! albertel   18:   $safeeval->share($Apache::scripttag::start_script::result);
1.13      albertel   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>";
1.15    ! albertel   27:   &Apache::run::run($bodytext,$safeeval);
1.13      albertel   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);
1.2       albertel   32:   }
                     33:   return $result;
1.1       albertel   34: }
                     35: 
1.3       albertel   36: sub end_script {}
                     37: 
                     38: sub start_scriptlib {
                     39:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
1.5       albertel   40:   my $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
1.3       albertel   41:   my $result ="";
1.13      albertel   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);
1.3       albertel   49:   }
                     50:   return $result;
                     51: }
                     52: 
                     53: sub end_scriptlib {}
1.4       albertel   54: 
                     55: sub start_parserlib {
                     56:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.6       albertel   57:   my $bodytext=$$parser[$#$parser]->get_text("/parserlib");
1.7       albertel   58:   my $result ="";
1.13      albertel   59: 
                     60:   $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
                     61:   %$style = ( %$style , &Apache::style::styleparser($target, 
1.9       albertel   62: 		&getfile($bodytext)));
1.13      albertel   63: 
                     64:   if ($target eq "edit" ) {
1.7       albertel   65:     $result=&editfield($token->[1],$bodytext);
                     66:   }
                     67:   return $result;
1.4       albertel   68: }
                     69: 
                     70: sub end_parserlib {
1.6       albertel   71: }
                     72: 
                     73: sub start_import {
1.7       albertel   74:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.6       albertel   75:   my $bodytext=$$parser[$#$parser]->get_text("/import");
1.7       albertel   76:   my $result ="";
1.13      albertel   77: 
                     78:   $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
                     79:   my $file=&getfile($bodytext);
1.14      albertel   80:   if ($file eq -1) {
                     81:     return "<b> Unable to find <i>$bodytext</i> for import</b>";
                     82:   }
                     83: 
1.13      albertel   84:   my $tempparser=HTML::TokeParser->new(\$file);
                     85:   push (@$parser,$tempparser);
                     86: 
                     87:   if ($target eq "edit" ) {
                     88:     $result.=&editfield($token->[1],$bodytext);
1.8       albertel   89:     $result.="Click<a href=\"/res/$bodytext\">here</a> to edit<br></br>"
1.7       albertel   90:   }
1.6       albertel   91: }
                     92: 
                     93: sub end_import {
1.7       albertel   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";
1.4       albertel  106: }
1.3       albertel  107: 
                    108: sub getfile {
                    109:   my ($filename) = @_;
                    110:   my $a="";
1.11      tsai      111:   
1.12      tsai      112:   $filename=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.10      albertel  113:   $filename="/home/httpd/html/res".$filename;
                    114:   if (! -e $filename ) {
                    115:     &Apache::lonnet::subscribe($filename);
                    116:     &Apache::lonnet::repcopy($filename);
                    117:   }
1.14      albertel  118:   if (! -e $filename ) { return -1; };
1.11      tsai      119:   my $fh=Apache::File->new($filename);
                    120:   while (<$fh>) {
1.3       albertel  121:       $a .=$_;
                    122:   }
                    123:   return $a
1.1       albertel  124: }
1.3       albertel  125: 
1.1       albertel  126: 1;
                    127: __END__

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