File:  [LON-CAPA] / loncom / xml / scripttag.pm
Revision 1.35: download - view: text, annotated - select for diffs
Tue Mar 27 16:56:56 2001 UTC (23 years, 2 months ago) by sakharuk
Branches: MAIN
CVS tags: HEAD

r changes for tex target

    1: # The LearningOnline Network with CAPA
    2: # <script> definiton
    3: # 2/21 Guy
    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:                                                  'window','display'));
   14: }
   15: 
   16: sub start_script {
   17:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   18:   my $args ='';
   19:   if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
   20:   my $type = &Apache::run::run("{$args;".'return $type}',$safeeval);
   21:   my $result='';
   22:   #&Apache::lonxml::debug("found type of $type");
   23:   if ($type eq "loncapa/perl") {
   24:     my $bodytext=&Apache::lonxml::get_all_text("/script",$$parser[$#$parser]);
   25:     
   26:     if ( $target eq "modified" ) {
   27:     } elsif ( $target eq "web" || $target eq "grade" ) {
   28:       &Apache::run::run($bodytext,$safeeval);
   29:     } elsif ($target eq "edit" ) {
   30:       &Apache::run::run($bodytext,$safeeval);
   31:       $result="<br /> &lt;$token->[1]&gt; output: <br />$bodytext<br />Source:<br />";
   32:       $result.=&editfield($token->[1],$bodytext);
   33:     }
   34:   } else {
   35:     if ($target ne "meta") { $result = $token->[4]; }
   36:   }
   37:   return $result;
   38: }
   39: 
   40: sub end_script {
   41:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   42:   if ( $target eq "meta" ) { return ''; } 
   43:   my $args ='';
   44:   if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
   45:   my $type = &Apache::run::run("{$args;".'return $type}',$safeeval);
   46:   my $result='';
   47:   #other script blocks need to survive
   48:   if ($type ne "loncapa/perl") { return $token->[2]; }
   49:   return '';
   50: }
   51: 
   52: sub start_display {
   53:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   54:   my $args ='';
   55:   if ( $#$parstack > -1 ) {
   56:     $args=$$parstack[$#$parstack];
   57:   }
   58:   my $bodytext=&Apache::lonxml::get_all_text("/display",$$parser[$#$parser]);
   59:   
   60:   if ( $target eq "modified" ) {
   61:   }
   62:   my $result=&Apache::run::run($bodytext,$safeeval);
   63:   if ($target eq 'grade' ) {
   64:     # grade should produce no output
   65:     $result='';
   66:   }
   67:   if ($target eq "edit" ) {
   68:     $result = 
   69:       "<br /> &lt;$token->[1]&gt; output: <br />$bodytext<br />Source:<br />";
   70:     $result.=&editfield($token->[1],$bodytext);
   71:   }
   72:   return $result;
   73: }
   74: 
   75: sub end_display {
   76: }
   77: 
   78: sub start_scriptlib {
   79:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   80:   my $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
   81:   my $result ="";
   82: 
   83:   $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
   84: 				   $$parstack[$#$parstack]);
   85:   my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
   86: 					     $bodytext);
   87:   my $script=&Apache::lonnet::getfile($location);
   88:   if ($script == -1) {
   89:     &Apache::lonxml::error("<b> Unable to find <i>$location</i> for scriptlib</b>");
   90:     return "";
   91:   }
   92:   &Apache::run::run($script,$safeeval);
   93:   #&Apache::lonxml::debug("ran $bodytext:<br />".&Apache::lonnet::getfile($bodytext)."<br />");
   94: 
   95:   if ($target eq "edit" ) {
   96:     $result.=&editfield($token->[1],$bodytext);
   97:   }
   98:   return $result;
   99: }
  100: 
  101: sub end_scriptlib {}
  102: 
  103: sub start_parserlib {
  104:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  105:   my $bodytext=$$parser[$#$parser]->get_text("/parserlib");
  106:   my $result ="";
  107: 
  108:   $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
  109: 				  $$parstack[$#$parstack]);
  110:   my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
  111: 					     $bodytext);
  112:   my $styletext=&Apache::lonnet::getfile($location);
  113:   #&Apache::lonxml::debug("found :$bodytext: in :$location: with :$styletext:");
  114:   if ($styletext == -1) {
  115:     &Apache::lonxml::error("<b> Unable to find <i>$location</i> for parserlib</b>");
  116:     return "";
  117:   }
  118:   %$style = ( %$style , &Apache::style::styleparser($target,$styletext));
  119: 
  120:   if ($target eq "edit" ) {
  121:     $result=&editfield($token->[1],$bodytext);
  122:   }
  123:   return $result;
  124: }
  125: 
  126: sub end_parserlib {
  127: }
  128: 
  129: sub start_window {
  130:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  131:   my $result = '';
  132:   if ($target eq 'web') {
  133:     &Apache::lonxml::startredirection;
  134:   }  elsif ($target eq 'tex') {
  135:        $result = '\footnote{';
  136:    }
  137:   return $result;  
  138: }
  139: 
  140: sub end_window {
  141:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  142:   my $result;
  143:   if ($target eq 'web') {
  144:     my $output=&Apache::lonxml::endredirection;
  145:     $output =~ s/\"/\&quot\;/g;
  146:     $result = "<a href=\"javascript:newWindow=open(\'\',\'new_W\',\'width=500,height=200,scrollbars=1\');newWindow.document.open(\'text/html\',\'replace\');newWindow.document.writeln(\'<html><head><title>newwindow</title></head><body bgcolor=&quot;#FFFFFF&quot;> $output </body></html>\');newWindow.document.close();void(0);\"><sup>*</sup></a>";
  147:   } elsif ($target eq 'tex') {
  148:       $result = '}';
  149:   } else {
  150:       $result = '';
  151:   }
  152:   return $result; 
  153: }
  154: 
  155: sub start_import {
  156:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  157:   my $bodytext=$$parser[$#$parser]->get_text("/import");
  158:   my $result ="";
  159: 
  160:   $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
  161:   my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
  162:   my $file=&Apache::lonnet::getfile($location);
  163:   if ($file == -1) {
  164:     &Apache::lonxml::error("<b> Unable to find <i>$bodytext $location</i> for import</b>");
  165:     return "";
  166:   }
  167: 
  168:   my $dir=$location;
  169:   $dir=~s:/[^/]*$::;  
  170: #  &Apache::lonxml::debug("directory $dir $location file $file \n<b>END</b>\n");
  171:   &Apache::lonxml::newparser($parser,\$file,$dir);
  172: 
  173:   if ($target eq "edit" ) {
  174:     $result.=&editfield($token->[1],$bodytext);
  175:     $result.="Click<a href=\"/res/$bodytext\">here</a> to edit<br />"
  176:   }
  177:   return '';
  178: }
  179: 
  180: sub end_import {
  181: }
  182: 
  183: sub start_meta {
  184:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  185:   my $result = '';
  186:     if ($target ne 'meta') {
  187:        $result = &Apache::lonxml::get_all_text("/meta",$$parser[$#$parser]); 
  188:        $result = '';
  189:       return $result; 
  190:     } else {
  191:        return $result; 
  192:     }
  193: }
  194: 
  195: sub end_meta {
  196: }
  197: 
  198: sub editfield {
  199:   my ($tag,$data)=@_;
  200:   
  201:   my $count=0;
  202:   my $maxlength=-1;
  203:   map { $count++;
  204: 	if (length($_) > $maxlength) { $maxlength = length ($_); }
  205:       } split ("\n", $data);
  206: 	  
  207:   return "<br />\n&lt;$tag&gt;<br />\n&nbsp;&nbsp;&nbsp;<textarea rows=\"$count\" cols=\"$maxlength\" name=homework_edit_".$Apache::lonxml::curdepth.">$data</textarea><br />\n&lt;/$tag&gt;<br />\n";
  208: }
  209: 
  210: sub getfilenothere {
  211:   my ($filename) = @_;
  212:   my $a="";
  213:   
  214:   $filename=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
  215:   $filename="/home/httpd/html/res".$filename;
  216:   if (! -e $filename ) {
  217:     &Apache::lonnet::subscribe($filename);
  218:     &Apache::lonnet::repcopy($filename);
  219:   }
  220:   if (! -e $filename ) { return -1; };
  221:   my $fh=Apache::File->new($filename);
  222:   while (<$fh>) {
  223:       $a .=$_;
  224:   }
  225:   return $a
  226: }
  227: 
  228: 1;
  229: __END__

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