[LON-CAPA-cvs] cvs: loncom /xml style.pm
sakharuk
lon-capa-cvs@mail.lon-capa.org
Fri, 19 Sep 2003 17:46:59 -0000
sakharuk Fri Sep 19 13:46:59 2003 EDT
Modified files:
/loncom/xml style.pm
Log:
New (almost completely) version of style parser which allows
1. use <target name="..."> tag with new and old targets;
2. use general for <web>, <tex> target (all in general) info - see particular example in physnet.sty (tag - LearnerModuleId);
3. use a few outputs for the same target - see particular example in physnet.sty (tag - LibraryBook);
Index: loncom/xml/style.pm
diff -u loncom/xml/style.pm:1.17 loncom/xml/style.pm:1.18
--- loncom/xml/style.pm:1.17 Mon Jun 9 17:46:11 2003
+++ loncom/xml/style.pm Fri Sep 19 13:46:59 2003
@@ -1,7 +1,7 @@
# The LearningOnline Network with CAPA
# Style Parser Module (new version)
#
-# $Id: style.pm,v 1.17 2003/06/09 21:46:11 albertel Exp $
+# $Id: style.pm,v 1.18 2003/09/19 17:46:59 sakharuk Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -46,88 +46,80 @@
sub styleparser {
- my ($target,$content_style_string) = @_;
- my $current_key = '';
- my $current_value = '';
+ my ($target,$content_style_string)=@_;
my @keys = ();
my @values = ();
my @style_array = ();
- my $stoken;
-
- my $b_pos;
- my $e_pos;
- my $entry;
-
- $b_pos = index($content_style_string,'<definetag',0);
- while ($b_pos != -1) {
- $e_pos = index($content_style_string,'</definetag',$b_pos);
- $entry = substr($content_style_string,$b_pos,$e_pos-$b_pos+12);
- $_ = $entry;
- m/<definetag\s+name\s*=\s*\"([^\"]*)\"/;
- $current_key = $1;
- push @keys,$current_key;
-# &Apache::lonxml::debug("$current_key\n");
- my $b_position = index($entry,'<'.$target.'>',0);
- my $e_position = index($entry,'</'.$target.'>',$b_position);
- my $target_length = length($target) + 2;
- if ($b_position > -1) {
- my $entry_target = substr($entry,$b_position+$target_length,$e_position-$b_position-$target_length);
- my $pstyle = HTML::TokeParser->new(\$entry_target);
- while ($stoken = $pstyle->get_token) {
- if ($stoken->[0] eq 'T') {
- $current_value .= $stoken->[1];
- } elsif ($stoken->[0] eq 'S') {
- my $number=-1;
- if ($stoken->[1] ne "$current_key") {
- $number = &testkey($stoken->[0],$stoken->[1],@keys);
- }
- if ($number != -1) {
- $current_value .= &testvalue($number,$stoken->[0],$stoken->[2],@values);
- } else {
- $current_value .= $stoken->[4];
- }
- } else {
- my $number=-1;
- if (('/'.$stoken->[1]) ne "$current_key") {
- $number = &testkey($stoken->[0],$stoken->[1],@keys);
- }
- if ($number != -1) {
- $current_value .= &testvalue($number,$stoken->[0],$stoken->[2],@values);
- } else {
- $current_value .= $stoken->[2];
- }
- }
- }
- }
- $current_value =~ s/\n//g;
-###########
-# $current_value =~ s/^\s*//g;
-# $current_value =~ s/\s*$//g;
-# $current_value =~ s/\( (\w)/($1/g;
-###########
- if ($current_value) {
+ my $current_value;
+ my $allow=0;
+ my $pstyle = HTML::TokeParser->new(\$content_style_string);
+ while (my $stoken = $pstyle->get_token) {
+ if (($stoken->[0] eq 'S') && ($stoken->[1] eq 'definetag')) {
+ push @keys,$stoken->[2]->{'name'};
+ $current_value='';
+ $allow=0;
+ } elsif (($stoken->[0] eq 'E') && ($stoken->[1] eq 'definetag')) {
+ $current_value =~ s/(\s)+/$1/g;
+ $current_value =~ s/\n//g;
push(@values,$current_value);
- #&Apache::lonxml::debug("a:$current_value: $#values \n");
- } else {
- pop(@keys);
+ } elsif (($target eq 'meta') && ($stoken->[0] eq 'S') && ($stoken->[1] eq 'meta')) {
+ $allow=1;
+ } elsif (($target eq 'meta') && ($stoken->[0] eq 'E') && ($stoken->[1] eq 'meta')) {
+ $allow=0;
+ } elsif (($target ne 'meta') && ($stoken->[0] eq 'S') && ($stoken->[1] eq 'render')) {
+ $allow=1;
+ } elsif (($target ne 'meta') && ($stoken->[0] eq 'E') && ($stoken->[1] eq 'render')) {
+ $allow=0;
+ } elsif (($target ne 'meta') && ($target ne 'web') && ($stoken->[0] eq 'S') && ($stoken->[1] eq 'web')) {
+ $allow=0;
+ } elsif (($target ne 'meta') && ($target ne 'web') && ($stoken->[0] eq 'E') && ($stoken->[1] eq 'web')) {
+ $allow=1;
+ } elsif (($target ne 'meta') && ($target ne 'tex') && ($stoken->[0] eq 'S') && ($stoken->[1] eq 'tex')) {
+ $allow=0;
+ } elsif (($target ne 'meta') && ($target ne 'tex') && ($stoken->[0] eq 'E') && ($stoken->[1] eq 'tex')) {
+ $allow=1;
+ } elsif (($stoken->[0] eq 'S') && ($stoken->[1] eq 'target') && ($stoken->[2]->{'name'} ne $target)) {
+ $allow=0;
+ } elsif (($stoken->[0] eq 'E') && ($stoken->[1] eq 'target')) {
+ $allow=1;
+ } elsif (($target ne 'meta') && ($stoken->[0] eq 'S') && ($stoken->[1] eq $target)) {
+ $allow=1;
+ } elsif (($target ne 'meta') && ($stoken->[0] eq 'E') && ($stoken->[1] eq $target)) {
+ } elsif (($stoken->[0] eq 'S') && ($stoken->[1] eq 'target') && ($stoken->[2]->{'name'} eq $target)) {
+ $allow=1;
+ } elsif ($allow) {
+ if ($stoken->[0] eq 'T') {
+ $current_value .= $stoken->[1];
+ } elsif ($stoken->[0] eq 'S') {
+ my $number=-1;
+ if ($stoken->[1] ne $keys[-1]) {
+ $number = &testkey($stoken->[0],$stoken->[1],@keys);
+ }
+ if ($number != -1) {
+ $current_value .= &testvalue($number,$stoken->[0],$stoken->[2],@values);
+ } else {
+ $current_value .= $stoken->[4];
+ }
+ } elsif ($stoken->[0] eq 'E') {
+ my $number=-1;
+ if (('/'.$stoken->[1]) ne $keys[-1]) {
+ $number = &testkey($stoken->[0],$stoken->[1],@keys);
+ }
+ if ($number != -1) {
+ $current_value .= &testvalue($number,$stoken->[0],$stoken->[2],@values);
+ } else {
+ $current_value .= $stoken->[2];
+ }
+ }
}
- $current_key = '';
- $current_value ='';
- $b_pos = index($content_style_string,'<definetag',$b_pos+1);
}
-
for (my $i=0; $i<=$#keys; $i++) {
push @style_array,$keys[$i],$values[$i];
}
my %style_for_target = @style_array;
-# check printing
-# foreach $current_key (sort keys %style_for_target) {
-# &Apache::lonxml::debug("$current_key => $style_for_target{$current_key}\n");
-# }
-# return result
- return %style_for_target;
-
+ return %style_for_target;
}
+
sub testkey {