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

1.1       sakharuk    1: # The LearningOnline Network with CAPA
1.7       sakharuk    2: # Style Parser Module (new version)
                      3: #
1.22    ! jms         4: # $Id: style.pm,v 1.21 2004/01/21 13:03:29 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);
1.21      sakharuk   55:     $pstyle->xml_mode('1');
1.18      sakharuk   56:     while (my $stoken = $pstyle->get_token) {
                     57: 	if (($stoken->[0] eq 'S') && ($stoken->[1] eq 'definetag')) {
                     58: 	    push @keys,$stoken->[2]->{'name'};
                     59:             $current_value='';
                     60: 	    $allow=0;
                     61: 	} elsif (($stoken->[0] eq 'E') && ($stoken->[1] eq 'definetag')) {
                     62: 	    $current_value =~ s/(\s)+/$1/g;
                     63: 	    $current_value =~ s/\n//g;
                     64: 	    push(@values,$current_value);
                     65: 	} elsif (($target eq 'meta') && ($stoken->[0] eq 'S') && ($stoken->[1] eq 'meta')) {
                     66: 	    $allow=1;
                     67:         } elsif (($target eq 'meta') && ($stoken->[0] eq 'E') && ($stoken->[1] eq 'meta')) {
                     68: 	    $allow=0;
                     69: 	} elsif (($target ne 'meta') && ($stoken->[0] eq 'S') && ($stoken->[1] eq 'render')) {
                     70: 	    $allow=1;
                     71: 	} elsif (($target ne 'meta') && ($stoken->[0] eq 'E') && ($stoken->[1] eq 'render')) {
                     72: 	    $allow=0;
                     73: 	} elsif (($target ne 'meta') && ($target ne 'web') && ($stoken->[0] eq 'S') && ($stoken->[1] eq 'web')) {
                     74: 	    $allow=0;
                     75: 	} elsif (($target ne 'meta') && ($target ne 'web') && ($stoken->[0] eq 'E') && ($stoken->[1] eq 'web')) {
                     76: 	    $allow=1;
                     77: 	} elsif (($target ne 'meta') && ($target ne 'tex') && ($stoken->[0] eq 'S') && ($stoken->[1] eq 'tex')) {
                     78: 	    $allow=0;
                     79: 	} elsif (($target ne 'meta') && ($target ne 'tex') && ($stoken->[0] eq 'E') && ($stoken->[1] eq 'tex')) {
                     80: 	    $allow=1;
1.19      sakharuk   81: 	} 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   82: 	    $allow=0;
                     83: 	} elsif (($stoken->[0] eq 'E') && ($stoken->[1] eq 'target')) {
                     84: 	    $allow=1;
                     85:  	} elsif (($target ne 'meta') && ($stoken->[0] eq 'S') && ($stoken->[1] eq $target)) {
                     86: 	    $allow=1;
                     87: 	} elsif (($target ne 'meta') && ($stoken->[0] eq 'E') && ($stoken->[1] eq $target)) {
1.19      sakharuk   88: 	} 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   89: 	    $allow=1;
                     90: 	} elsif ($allow) { 
                     91: 	    if ($stoken->[0] eq 'T') {
                     92: 		$current_value .= $stoken->[1];
                     93: 	    } elsif ($stoken->[0] eq 'S') {
                     94: 		my $number=-1;
                     95: 		if ($stoken->[1] ne $keys[-1]) {
                     96: 		    $number = &testkey($stoken->[0],$stoken->[1],@keys);
                     97: 		}
                     98: 		if ($number != -1) {
                     99: 		    $current_value .= &testvalue($number,$stoken->[0],$stoken->[2],@values);
                    100: 		} else {
                    101: 		    $current_value .= $stoken->[4];
                    102: 		}
                    103: 	    } elsif ($stoken->[0] eq 'E') {
                    104: 		my $number=-1;
                    105: 		if (('/'.$stoken->[1]) ne $keys[-1]) {
                    106: 		    $number = &testkey($stoken->[0],$stoken->[1],@keys);
                    107: 		}
                    108: 		if ($number != -1) {
                    109: 		    $current_value .= &testvalue($number,$stoken->[0],$stoken->[2],@values);
                    110: 		} else {
                    111: 		    $current_value .= $stoken->[2];
                    112: 		}
1.14      albertel  113: 	    }
1.4       sakharuk  114: 	}
1.7       sakharuk  115:     }
1.20      albertel  116:     my %style_for_target;
1.7       sakharuk  117:     for (my $i=0; $i<=$#keys; $i++) {
1.20      albertel  118: 	if ($values[$i] !~ /^\s*$/) {
                    119: 	    $style_for_target{$keys[$i]}=$values[$i]; 
                    120: 	}
1.7       sakharuk  121:     }
1.18      sakharuk  122:     return %style_for_target; 
                    123: }
1.7       sakharuk  124: 
1.4       sakharuk  125: 
1.7       sakharuk  126: sub testkey {
1.4       sakharuk  127: 
1.7       sakharuk  128:     my ($zeroth,$first,@keys) = @_; 
                    129:     my $number = -1;
                    130:     if ($zeroth eq 'S') {
                    131: 	for (my $i=$#keys; $i>=0; $i=$i-1) {
                    132: 	    if ($first eq lc($keys[$i]))  { 
                    133: 		$number = $i;
                    134: 		last;
1.4       sakharuk  135: 	    }
1.7       sakharuk  136: 	}
                    137:     } elsif ($zeroth eq 'E') {
                    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:     }
                    145: 	return $number;
                    146: }
                    147: 
                    148: sub testvalue {
                    149: 
                    150:     my ($number,$zeroth,$second,@values) = @_;   
                    151:     my $current_content = $values[$number];
                    152:     if ($zeroth eq 'S') {
1.14      albertel  153: 	my %tempo_hash = %$second;
                    154: 	while ((my $current_k,my $current_v) = each %tempo_hash) {
                    155: 	    $current_content =~ s/\$$current_k/$current_v/g;
                    156: 	}
1.7       sakharuk  157:     } elsif ($zeroth eq 'E') {
1.14      albertel  158: 	$current_content = $values[$number];
1.7       sakharuk  159:     }
                    160:     return $current_content;
1.1       sakharuk  161: }
                    162: 
                    163: 1;
1.7       sakharuk  164: 
1.1       sakharuk  165: __END__
1.22    ! jms       166: 
        !           167: =pod
        !           168: 
        !           169: =head1 NAME
        !           170: 
        !           171: Apache::style.pm
        !           172: 
        !           173: =head1 SYNOPSIS
        !           174: 
        !           175: Style parsing module
        !           176: 
        !           177: This is part of the LearningOnline Network with CAPA project
        !           178: described at http://www.lon-capa.org.
        !           179: 
        !           180: 
        !           181: =head1 SUBROUTINES
        !           182: 
        !           183: =over
        !           184: 
        !           185: =item styleparser()
        !           186: 
        !           187: =item testkey()
        !           188: 
        !           189: =item testvalue()
        !           190: 
        !           191: =back
        !           192: 
        !           193: =cut
        !           194: 

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