Annotation of loncom/xml/style.pm, revision 1.15

1.1       sakharuk    1: # The LearningOnline Network with CAPA
1.7       sakharuk    2: # Style Parser Module (new version)
                      3: #
1.15    ! albertel    4: # $Id: style.pm,v 1.14 2002/10/16 20:37:46 albertel Exp $
1.13      www         5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
                     28: # Copyright for TtHfunc and TtMfunc by Ian Hutchinson. 
                     29: # TtHfunc and TtMfunc (the "Code") may be compiled and linked into 
                     30: # binary executable programs or libraries distributed by the 
                     31: # Michigan State University (the "Licensee"), but any binaries so 
                     32: # distributed are hereby licensed only for use in the context
                     33: # of a program or computational system for which the Licensee is the 
                     34: # primary author or distributor, and which performs substantial 
                     35: # additional tasks beyond the translation of (La)TeX into HTML.
                     36: # The C source of the Code may not be distributed by the Licensee
                     37: # to any other parties under any circumstances.
                     38: #
1.7       sakharuk   39: # written 01/08/01 by Alexander Sakharuk
1.1       sakharuk   40: #
                     41: 
1.7       sakharuk   42: package Apache::style;
1.1       sakharuk   43: 
                     44: use strict;
                     45: use HTML::TokeParser;
                     46: 
                     47: sub styleparser {
                     48: 
1.7       sakharuk   49:     my ($target,$content_style_string) = @_;
                     50:     my $current_key = '';
                     51:     my $current_value = '';
                     52:     my @keys = ();
                     53:     my @values = ();
                     54:     my @style_array = ();
                     55:     my $stoken;
                     56: 
                     57:     my $b_pos;
                     58:     my $e_pos;
                     59:     my $entry;
                     60: 
                     61:     $b_pos = index($content_style_string,'<definetag',0);
                     62:     while ($b_pos != -1) {
                     63: 	$e_pos = index($content_style_string,'</definetag',$b_pos);
                     64: 	$entry = substr($content_style_string,$b_pos,$e_pos-$b_pos+12);       
                     65: 	$_ = $entry;
                     66: 	m/<definetag\s+name\s*=\s*\"([^\"]*)\"/;
                     67: 	$current_key = $1;
                     68: 	push @keys,$current_key;
1.8       albertel   69: #	&Apache::lonxml::debug("$current_key\n");
1.7       sakharuk   70: 	my $b_position =  index($entry,'<'.$target.'>',0);
                     71: 	my $e_position =  index($entry,'</'.$target.'>',$b_position);
                     72: 	my $target_length = length($target) + 2;
                     73: 	if ($b_position > -1) {
                     74: 	    my $entry_target = substr($entry,$b_position+$target_length,$e_position-$b_position-$target_length);
                     75: 	    my $pstyle = HTML::TokeParser->new(\$entry_target);
                     76: 	    while ($stoken = $pstyle->get_token) {
                     77: 		if ($stoken->[0] eq 'T') {
                     78: 		    $current_value .= $stoken->[1];
                     79: 		} elsif ($stoken->[0] eq 'S') {
1.14      albertel   80: 		    my $number=-1;
                     81: 		    if ($stoken->[1] ne "$current_key") {
                     82: 			$number = &testkey($stoken->[0],$stoken->[1],@keys);
                     83: 		    }
1.7       sakharuk   84: 		    if ($number != -1) {
                     85: 			$current_value .= &testvalue($number,$stoken->[0],$stoken->[2],@values);
                     86: 		    } else {
                     87: 			$current_value .= $stoken->[4];
1.14      albertel   88: 		    }
1.7       sakharuk   89: 	        } else {
1.14      albertel   90: 		    my $number=-1;
                     91: 		    if (('/'.$stoken->[1]) ne "$current_key") {
                     92: 			$number = &testkey($stoken->[0],$stoken->[1],@keys);
                     93: 		    }
1.7       sakharuk   94: 		    if ($number != -1) {
                     95: 			$current_value .= &testvalue($number,$stoken->[0],$stoken->[2],@values);
1.4       sakharuk   96: 		    } else {
1.7       sakharuk   97: 			$current_value .= $stoken->[2];
1.14      albertel   98: 		    }
1.7       sakharuk   99: 	        }
1.14      albertel  100: 	    }
1.4       sakharuk  101: 	}
1.9       sakharuk  102:         $current_value =~ s/\n//g;
1.12      sakharuk  103: ###########
                    104: #	$current_value =~ s/^\s*//g;
                    105: #	$current_value =~ s/\s*$//g;
                    106: #	$current_value =~ s/\( (\w)/($1/g;
                    107: ###########
1.15    ! albertel  108: 	if ($current_value) {
        !           109: 	    push(@values,$current_value);
        !           110: 	    &Apache::lonxml::debug("a:$current_value: $#values \n");
        !           111: 	} else {
        !           112: 	    pop(@keys);
        !           113: 	}
1.7       sakharuk  114: 	$current_key = '';
                    115: 	$current_value ='';
                    116: 	$b_pos = index($content_style_string,'<definetag',$b_pos+1);
                    117:     }
                    118: 
                    119:     for (my $i=0; $i<=$#keys; $i++) {
1.11      sakharuk  120: 	push @style_array,$keys[$i],$values[$i]; 
1.7       sakharuk  121:     }
1.9       sakharuk  122:     my %style_for_target =  @style_array;
1.1       sakharuk  123: 
1.4       sakharuk  124: # check printing
1.11      sakharuk  125: #    foreach $current_key (sort keys %style_for_target) {
                    126: #	&Apache::lonxml::debug("$current_key => $style_for_target{$current_key}\n");
                    127: #    }
1.4       sakharuk  128: # return result
1.1       sakharuk  129:   return %style_for_target; 
1.7       sakharuk  130: 
1.4       sakharuk  131: }
                    132: 
1.7       sakharuk  133: sub testkey {
1.4       sakharuk  134: 
1.7       sakharuk  135:     my ($zeroth,$first,@keys) = @_; 
                    136:     my $number = -1;
                    137:     if ($zeroth eq 'S') {
                    138: 	for (my $i=$#keys; $i>=0; $i=$i-1) {
                    139: 	    if ($first eq lc($keys[$i]))  { 
                    140: 		$number = $i;
                    141: 		last;
1.4       sakharuk  142: 	    }
1.7       sakharuk  143: 	}
                    144:     } elsif ($zeroth eq 'E') {
                    145: 	for (my $i=$#keys; $i>=0; $i=$i-1) {
                    146: 	    if ('/'.$first eq lc($keys[$i]))  { 
                    147: 		$number = $i;
                    148: 	 	last;
1.4       sakharuk  149: 	    }
1.7       sakharuk  150: 	}
                    151:     }
                    152: 	return $number;
                    153: }
                    154: 
                    155: sub testvalue {
                    156: 
                    157:     my ($number,$zeroth,$second,@values) = @_;   
                    158:     my $current_content = $values[$number];
                    159:     if ($zeroth eq 'S') {
1.14      albertel  160: 	my %tempo_hash = %$second;
                    161: 	while ((my $current_k,my $current_v) = each %tempo_hash) {
                    162: 	    $current_content =~ s/\$$current_k/$current_v/g;
                    163: 	}
1.7       sakharuk  164:     } elsif ($zeroth eq 'E') {
1.14      albertel  165: 	$current_content = $values[$number];
1.7       sakharuk  166:     }
                    167:     return $current_content;
1.1       sakharuk  168: }
                    169: 
                    170: 1;
1.7       sakharuk  171: 
1.1       sakharuk  172: __END__

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