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

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

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