File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.6: download - view: text, annotated - select for diffs
Thu Sep 14 15:41:32 2000 UTC (23 years, 8 months ago) by tsai
Branches: MAIN
CVS tags: HEAD
*** empty log message ***

    1: # The LearningOnline Network with CAPA # <script> definiton
    2: 
    3: package Apache::structuretags; 
    4: 
    5: use strict;
    6: use Apache::lonnet;
    7: 
    8: sub BEGIN {
    9:   &Apache::lonxml::register('Apache::structuretags',('block','while','randomlist'));
   10: }
   11: 
   12: sub start_block {
   13:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   14: 
   15:   my $code = @$parstack[$#$parstack];
   16:   $code =~ s/\"//g;
   17:   $code .=';return $condition;';
   18: #  print "<br>$code<br>";
   19:   my $result = &Apache::run::run($code,$safeeval);
   20:   if ( ! $result ) { 
   21: #    my $skip=$$parser[$#$parser]->get_text("/block");
   22:     my $skip=&Apache::lonxml::get_all_text("block",$$parser[$#$parser]);
   23: #    print "skipped $skip";
   24:   }
   25:   return "";
   26: }
   27: 
   28: sub end_block {
   29: }
   30: 
   31: sub start_while {
   32:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   33: 
   34:   my $code = @$parstack[$#$parstack];
   35:   $code =~ s/\"//g;
   36:   $code .=';return $condition;';
   37: 
   38:   push( @Apache::structuretags::whileconds, $code); 
   39:   my $result = &Apache::run::run($code,$safeeval);
   40:   my $bodytext=$$parser[$#$parser]->get_text("/while");
   41:   push( @Apache::structuretags::whilebody, $bodytext);
   42:   if ( $result ) { 
   43:      my $tempparser=HTML::TokeParser->new(\$bodytext);
   44:      push (@$parser,$tempparser);
   45:   }
   46:   return "";
   47: }
   48: 
   49: sub end_while {
   50:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   51:   my $code = pop @Apache::structuretags::whileconds;
   52:   my $bodytext = pop @Apache::structuretags::whilebody;
   53:   my $result = &Apache::run::run($code,$safeeval);
   54:   if ( $result ) { 
   55:      my $tempparser=HTML::TokeParser->new(\$bodytext);
   56:      push (@$parser,$tempparser);
   57:   } 
   58:   return "";
   59: }
   60: 
   61: # <randomlist> 
   62: #  <tag1>..</tag1>
   63: #  <tag2>..</tag2>
   64: #  <tag3>..</tag3>
   65: #  ... 
   66: # </randomlist>
   67: sub start_randomlist {
   68:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   69:   my $body= &Apache::lonxml::get_all_text("randomlist",$$parser[$#$parser]);
   70:   my $b_parser= HTML::TokeParser->new(\$body);
   71:   my $b_tok;
   72:   my @randomlist;
   73:   my $list_item;
   74:   my $param;
   75: 
   76:   while($b_tok = $b_parser->get_token() ) {
   77:     if($b_tok->[0] eq 'S') { # start tag
   78:     # get content of the tag until matching end tag
   79:     # get all text upto the matching tag
   80:     # and push the content into @randomlist
   81:       $list_item = &Apache::lonxml::get_all_text($b_tok->[1],$b_parser);
   82:       $param='';
   83:       map {$param .= "$_=\"$b_tok->[2]->{$_}\" "} @{$b_tok->[3]};
   84:       $param=~ s/^\s*(\S+)\s*$/$1/;
   85:       $param= " $param" if $param ne "";
   86:       $list_item = "<$b_tok->[1]"."$param>"."$list_item"."</$b_tok->[1]>";
   87:       print "<B>START-TAG $b_tok->[1], $list_item</B><BR>";
   88:       push(@randomlist,$list_item);
   89:     }
   90:     if($b_tok->[0] eq 'T') { # text
   91:     # what to do with text in between tags?
   92:       #  print "<B>TEXT $b_tok->[1]</B><BR>";
   93:     }
   94:     # if($b_tok->[0] eq 'E') { # end tag, should not happen
   95:       #  print "<B>END-TAG $b_tok->[1]</B><BR>";
   96:     # }
   97:   }
   98:   # randomly select one item from the array @randomlist 
   99:   # and created a new parser for it and push it onto the
  100:   # parser stack
  101:   my $bodytext = $randomlist[int(rand($#randomlist - 1)) + 1];
  102:   my $tempparser=HTML::TokeParser->new(\$bodytext);
  103:   print "SELECT-BODY<B>$bodytext</B><BR>";
  104:   push (@$parser,$tempparser);
  105:   return "";
  106: }
  107: 
  108: sub end_randomlist {
  109: }
  110: 
  111: 
  112: 1;
  113: __END__

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