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

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

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