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

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

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