Annotation of loncom/xml/style.pl, revision 1.2
1.1 sakharuk 1: #!/usr/local/bin/perl
2:
3: use strict;
4: use HTML::TokeParser;
5: use IO::File;
6:
7: {
8: my @content_of_style;
9: my $content_style_string;
10:
11: my $target = 'web';
12:
13: # read style file and put the content in string
14: {
15: my $filesty=IO::File->new('/home/sasha/final/attempt2.sty')
16: || die "Couldn't open style file for reading: $!\n";
17: @content_of_style = <$filesty>;
18: }
19: $content_style_string = join(' ',@content_of_style);
20:
21: # Call styleparser to parser syle string and create hash
22:
23: my %style_for_target = &styleparser($target,$content_style_string);
24:
25: }
26:
27: # style subroutine
28: # create a table for defined target
29: # from the information from Style File
30:
31: sub styleparser {
32:
33: my ($target,$content_style_string) = @_;
34: my @target_list = ('target','web','tex','edit','modified','rat','answer','metadis');
35: my @value_style = ();
36: my $current_key = '';
37: my $current_value = '';
38: my $stoken;
39: my $flag;
40: my $iele;
41: my $flag_target;
42:
43: my $pstyle = HTML::TokeParser->new(\$content_style_string);
44:
45: while ($stoken = $pstyle->get_token) {
46: # start for tag definition
47: if ($stoken->[0] eq 'S' and $stoken->[1] eq 'definetag') {
48: # new key in hash
49: $current_key = $stoken->[2]{name};
50: $flag = 0;
51: # metadata output
52: if ($target eq 'meta') {
53: while ($stoken = $pstyle->get_token and $stoken->[1] ne 'definetag') {
54: if ($stoken->[0] eq 'S' and $stoken->[1] eq 'meta') {
55: while ($stoken = $pstyle->get_token and $stoken->[1] ne 'meta') {
56: $current_value .= $stoken->[1];
57: }
58: }
59: }
60: } else {
61: # render output
62: while ($stoken = $pstyle->get_token and $stoken->[1] ne 'render') {
63: if ($stoken->[1] eq 'definetag') {
64: $flag = 1;
65: last;
66: }
67: }
68: if ($flag == 0) {
69: while ($stoken = $pstyle->get_token and $stoken->[1] ne 'definetag'
70: and $stoken->[1] ne 'render') {
71: # if token not equal to target $flag_target=0
72: $flag_target = 0;
73: for (my $i=0; $i<$#target_list; $i++) {
74: if ($stoken->[1] eq $target_list[$i]) {
75: $flag_target = 1;
76: }
77: }
78: if ($flag_target == 0) {
79: # target not found
80: if ($stoken->[0] eq 'T') {
81: $current_value .= $stoken->[1];
82: } elsif ($stoken->[0] eq 'S') {
83: my $flagelem = 0;
84: for (my $i=$#value_style-1;$i>=0;$i=$i-2) {
85: if ($stoken->[1] eq $value_style[$i]) {
86: $flagelem = 1;
87: $iele = $i + 1;
88: last;
89: }
90: }
91: if ($flagelem == 0) {
92: $current_value .= $stoken->[4];
93: } else {
94: $current_value .= $value_style[$iele];
95: }
96: } elsif ($stoken->[0] eq 'E') {
97: my $flagelem = 0;
98: for (my $i=$#value_style-1;$i>=0;$i=$i-2) {
99: if ($stoken->[1] eq $value_style[$i]) {
100: $flagelem = 1;
101: $iele = $i + 1;
102: last;
103: }
104: }
105: if ($flagelem == 0) {
106: $current_value .= $stoken->[2];
107: } else {
108: $current_value .= $value_style[$iele];
109: }
110: }
111: } else {
112: # target found
113: if ($stoken->[0] eq 'S' and $stoken->[1] eq 'target') {
114: # target defined via <target> tag
1.2 ! sakharuk 115: if (defined $stoken->[2]{dest}) {
! 116: if (index($stoken->[2]{dest},$target) == -1) {
1.1 sakharuk 117: while ($stoken = $pstyle->get_token and $stoken->[1] ne 'target') {
118: }
1.2 ! sakharuk 119: } elsif (index($stoken->[2]{dest},$target) != -1) {
1.1 sakharuk 120: while ($stoken = $pstyle->get_token and $stoken->[1] ne 'target') {
121: if ($stoken->[0] eq 'T') {
122: $current_value .= $stoken->[1];
123: } elsif ($stoken->[0] eq 'S') {
124: my $flagelem = 0;
125: for (my $i=$#value_style-1;$i>=0;$i=$i-2) {
126: if ($stoken->[1] eq $value_style[$i]) {
127: $flagelem = 1;
128: $iele = $i + 1;
129: last;
130: }
131: }
132: if ($flagelem == 0) {
133: $current_value .= $stoken->[4];
134: } else {
135: $current_value .= $value_style[$iele];
136: }
137: } elsif ($stoken->[0] eq 'E') {
138: my $flagelem = 0;
139: for (my $i=$#value_style-1;$i>=0;$i=$i-2) {
140: if ($stoken->[1] eq $value_style[$i]) {
141: $flagelem = 1;
142: $iele = $i + 1;
143: last;
144: }
145: }
146: if ($flagelem == 0) {
147: $current_value .= $stoken->[2];
148: } else {
149: $current_value .= $value_style[$iele];
150: }
151: }
152: }
1.2 ! sakharuk 153: }
! 154: } else {
! 155: if (index($stoken->[2]{excl},$target) != -1) {
! 156: while ($stoken = $pstyle->get_token and $stoken->[1] ne 'target') {
! 157: }
! 158: } elsif (index($stoken->[2]{excl},$target) == -1) {
! 159: while ($stoken = $pstyle->get_token and $stoken->[1] ne 'target') {
! 160: if ($stoken->[0] eq 'T') {
! 161: $current_value .= $stoken->[1];
! 162: } elsif ($stoken->[0] eq 'S') {
! 163: my $flagelem = 0;
! 164: for (my $i=$#value_style-1;$i>=0;$i=$i-2) {
! 165: if ($stoken->[1] eq $value_style[$i]) {
! 166: $flagelem = 1;
! 167: $iele = $i + 1;
! 168: last;
! 169: }
! 170: }
! 171: if ($flagelem == 0) {
! 172: $current_value .= $stoken->[4];
! 173: } else {
! 174: $current_value .= $value_style[$iele];
! 175: }
! 176: } elsif ($stoken->[0] eq 'E') {
! 177: my $flagelem = 0;
! 178: for (my $i=$#value_style-1;$i>=0;$i=$i-2) {
! 179: if ($stoken->[1] eq $value_style[$i]) {
! 180: $flagelem = 1;
! 181: $iele = $i + 1;
! 182: last;
! 183: }
! 184: }
! 185: if ($flagelem == 0) {
! 186: $current_value .= $stoken->[2];
! 187: } else {
! 188: $current_value .= $value_style[$iele];
! 189: }
! 190: }
! 191: }
! 192: }
! 193:
! 194:
! 195: }
1.1 sakharuk 196: } elsif ($stoken->[1] ne $target) {
197: #target defined via short-form tag
198: my $tempo_token = $stoken->[1];
199: while ($stoken = $pstyle->get_token and $stoken->[1] ne $tempo_token) {
200: }
201: } else {
202: my $tempo_token = $stoken->[1];
203: while ($stoken = $pstyle->get_token and $stoken->[1] ne $tempo_token) {
204: if ($stoken->[0] eq 'T') {
205: $current_value .= $stoken->[1];
206: } elsif ($stoken->[0] eq 'S') {
207: my $flagelem = 0;
208: for (my $i=$#value_style-1;$i>=0;$i=$i-2) {
209: if ($stoken->[1] eq $value_style[$i]) {
210: $flagelem = 1;
211: $iele = $i + 1;
212: last;
213: }
214: }
215: if ($flagelem == 0) {
216: $current_value .= $stoken->[4];
217: } else {
218: $current_value .= $value_style[$iele];
219: }
220: } elsif ($stoken->[0] eq 'E') {
221: my $flagelem = 0;
222: for (my $i=$#value_style-1;$i>=0;$i=$i-2) {
223: if ($stoken->[1] eq $value_style[$i]) {
224: $flagelem = 1;
225: $iele = $i + 1;
226: last;
227: }
228: }
229: if ($flagelem == 0) {
230: $current_value .= $stoken->[2];
231: } else {
232: $current_value .= $value_style[$iele];
233: }
234: }
235: }
236: }
237: }
238: }
239:
240: }
241: }
242: }
243: $current_value =~ s/(\s)+/$1/g;
244: if ($current_value ne ' ' and $current_value ne '' ) {
245: push (@value_style,lc $current_key,$current_value);
246: }
247: $current_key = '';
248: $current_value = '';
249: }
250: my %style_for_target = @value_style;
251: # check printing
252: while (($current_key,$current_value) = each %style_for_target) {
253: print "$current_key => $current_value\n";
254: }
255: # return result
256: return %style_for_target;
257: }
258:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>