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

1.1       sakharuk    1: # The LearningOnline Network with CAPA
1.7       sakharuk    2: # Style Parser Module (new version)
                      3: #
1.20    ! albertel    4: # $Id: style.pm,v 1.19 2003/09/22 13:01:57 sakharuk 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.18      sakharuk   49:     my ($target,$content_style_string)=@_;
1.7       sakharuk   50:     my @keys = ();
                     51:     my @values = ();
1.18      sakharuk   52:     my $current_value;
                     53:     my $allow=0;
                     54:     my $pstyle = HTML::TokeParser->new(\$content_style_string);
                     55:     while (my $stoken = $pstyle->get_token) {
                     56: 	if (($stoken->[0] eq 'S') && ($stoken->[1] eq 'definetag')) {
                     57: 	    push @keys,$stoken->[2]->{'name'};
                     58:             $current_value='';
                     59: 	    $allow=0;
                     60: 	} elsif (($stoken->[0] eq 'E') && ($stoken->[1] eq 'definetag')) {
                     61: 	    $current_value =~ s/(\s)+/$1/g;
                     62: 	    $current_value =~ s/\n//g;
                     63: 	    push(@values,$current_value);
                     64: 	} elsif (($target eq 'meta') && ($stoken->[0] eq 'S') && ($stoken->[1] eq 'meta')) {
                     65: 	    $allow=1;
                     66:         } elsif (($target eq 'meta') && ($stoken->[0] eq 'E') && ($stoken->[1] eq 'meta')) {
                     67: 	    $allow=0;
                     68: 	} elsif (($target ne 'meta') && ($stoken->[0] eq 'S') && ($stoken->[1] eq 'render')) {
                     69: 	    $allow=1;
                     70: 	} elsif (($target ne 'meta') && ($stoken->[0] eq 'E') && ($stoken->[1] eq 'render')) {
                     71: 	    $allow=0;
                     72: 	} elsif (($target ne 'meta') && ($target ne 'web') && ($stoken->[0] eq 'S') && ($stoken->[1] eq 'web')) {
                     73: 	    $allow=0;
                     74: 	} elsif (($target ne 'meta') && ($target ne 'web') && ($stoken->[0] eq 'E') && ($stoken->[1] eq 'web')) {
                     75: 	    $allow=1;
                     76: 	} elsif (($target ne 'meta') && ($target ne 'tex') && ($stoken->[0] eq 'S') && ($stoken->[1] eq 'tex')) {
                     77: 	    $allow=0;
                     78: 	} elsif (($target ne 'meta') && ($target ne 'tex') && ($stoken->[0] eq 'E') && ($stoken->[1] eq 'tex')) {
                     79: 	    $allow=1;
1.19      sakharuk   80: 	} elsif (($stoken->[0] eq 'S') && ($stoken->[1] eq 'target') && (not $stoken->[2]->{'name'}=~/(^\s*$target\s*,|,\s*$target\s*,|,\s*$target\s*$|^\s*$target\s*$)/)) {
1.18      sakharuk   81: 	    $allow=0;
                     82: 	} elsif (($stoken->[0] eq 'E') && ($stoken->[1] eq 'target')) {
                     83: 	    $allow=1;
                     84:  	} elsif (($target ne 'meta') && ($stoken->[0] eq 'S') && ($stoken->[1] eq $target)) {
                     85: 	    $allow=1;
                     86: 	} elsif (($target ne 'meta') && ($stoken->[0] eq 'E') && ($stoken->[1] eq $target)) {
1.19      sakharuk   87: 	} elsif (($stoken->[0] eq 'S') && ($stoken->[1] eq 'target') && ($stoken->[2]->{'name'}=~/(^\s*$target\s*,|,\s*$target\s*,|,\s*$target\s*$|^\s*$target\s*$)/)) {
1.18      sakharuk   88: 	    $allow=1;
                     89: 	} elsif ($allow) { 
                     90: 	    if ($stoken->[0] eq 'T') {
                     91: 		$current_value .= $stoken->[1];
                     92: 	    } elsif ($stoken->[0] eq 'S') {
                     93: 		my $number=-1;
                     94: 		if ($stoken->[1] ne $keys[-1]) {
                     95: 		    $number = &testkey($stoken->[0],$stoken->[1],@keys);
                     96: 		}
                     97: 		if ($number != -1) {
                     98: 		    $current_value .= &testvalue($number,$stoken->[0],$stoken->[2],@values);
                     99: 		} else {
                    100: 		    $current_value .= $stoken->[4];
                    101: 		}
                    102: 	    } elsif ($stoken->[0] eq 'E') {
                    103: 		my $number=-1;
                    104: 		if (('/'.$stoken->[1]) ne $keys[-1]) {
                    105: 		    $number = &testkey($stoken->[0],$stoken->[1],@keys);
                    106: 		}
                    107: 		if ($number != -1) {
                    108: 		    $current_value .= &testvalue($number,$stoken->[0],$stoken->[2],@values);
                    109: 		} else {
                    110: 		    $current_value .= $stoken->[2];
                    111: 		}
1.14      albertel  112: 	    }
1.4       sakharuk  113: 	}
1.7       sakharuk  114:     }
1.20    ! albertel  115:     my %style_for_target;
1.7       sakharuk  116:     for (my $i=0; $i<=$#keys; $i++) {
1.20    ! albertel  117: 	if ($values[$i] !~ /^\s*$/) {
        !           118: 	    $style_for_target{$keys[$i]}=$values[$i]; 
        !           119: 	}
1.7       sakharuk  120:     }
1.18      sakharuk  121:     return %style_for_target; 
                    122: }
1.7       sakharuk  123: 
1.4       sakharuk  124: 
1.7       sakharuk  125: sub testkey {
1.4       sakharuk  126: 
1.7       sakharuk  127:     my ($zeroth,$first,@keys) = @_; 
                    128:     my $number = -1;
                    129:     if ($zeroth eq 'S') {
                    130: 	for (my $i=$#keys; $i>=0; $i=$i-1) {
                    131: 	    if ($first eq lc($keys[$i]))  { 
                    132: 		$number = $i;
                    133: 		last;
1.4       sakharuk  134: 	    }
1.7       sakharuk  135: 	}
                    136:     } elsif ($zeroth eq 'E') {
                    137: 	for (my $i=$#keys; $i>=0; $i=$i-1) {
                    138: 	    if ('/'.$first eq lc($keys[$i]))  { 
                    139: 		$number = $i;
                    140: 	 	last;
1.4       sakharuk  141: 	    }
1.7       sakharuk  142: 	}
                    143:     }
                    144: 	return $number;
                    145: }
                    146: 
                    147: sub testvalue {
                    148: 
                    149:     my ($number,$zeroth,$second,@values) = @_;   
                    150:     my $current_content = $values[$number];
                    151:     if ($zeroth eq 'S') {
1.14      albertel  152: 	my %tempo_hash = %$second;
                    153: 	while ((my $current_k,my $current_v) = each %tempo_hash) {
                    154: 	    $current_content =~ s/\$$current_k/$current_v/g;
                    155: 	}
1.7       sakharuk  156:     } elsif ($zeroth eq 'E') {
1.14      albertel  157: 	$current_content = $values[$number];
1.7       sakharuk  158:     }
                    159:     return $current_content;
1.1       sakharuk  160: }
                    161: 
                    162: 1;
1.7       sakharuk  163: 
1.1       sakharuk  164: __END__

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