File:  [LON-CAPA] / loncom / xml / style.pm
Revision 1.20: download - view: text, annotated - select for diffs
Sun Nov 9 01:14:30 2003 UTC (20 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: version_1_1_0, version_1_0_99_3, version_1_0_99_2, version_1_0_99_1, version_1_0_99, HEAD
- if the style is nothing or just ws throw it away

    1: # The LearningOnline Network with CAPA
    2: # Style Parser Module (new version)
    3: #
    4: # $Id: style.pm,v 1.20 2003/11/09 01:14:30 albertel Exp $
    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: #
   39: # written 01/08/01 by Alexander Sakharuk
   40: #
   41: 
   42: package Apache::style;
   43: 
   44: use strict;
   45: use HTML::TokeParser;
   46: 
   47: sub styleparser {
   48: 
   49:     my ($target,$content_style_string)=@_;
   50:     my @keys = ();
   51:     my @values = ();
   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;
   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*$)/)) {
   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)) {
   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*$)/)) {
   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: 		}
  112: 	    }
  113: 	}
  114:     }
  115:     my %style_for_target;
  116:     for (my $i=0; $i<=$#keys; $i++) {
  117: 	if ($values[$i] !~ /^\s*$/) {
  118: 	    $style_for_target{$keys[$i]}=$values[$i]; 
  119: 	}
  120:     }
  121:     return %style_for_target; 
  122: }
  123: 
  124: 
  125: sub testkey {
  126: 
  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;
  134: 	    }
  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;
  141: 	    }
  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') {
  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: 	}
  156:     } elsif ($zeroth eq 'E') {
  157: 	$current_content = $values[$number];
  158:     }
  159:     return $current_content;
  160: }
  161: 
  162: 1;
  163: 
  164: __END__

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