Mercurial > hg > lukkari
annotate parsedata.pl @ 142:0e8b8fac920d
Check request instead of $_GET.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 31 Aug 2014 01:10:09 +0300 |
parents | 4faf9956741b |
children | c028506aa9cc |
rev | line source |
---|---|
1 | 1 #!/usr/bin/perl -w |
2 # | |
3 # Fetch and parse HTML format class timetable into more sane formats | |
54 | 4 # (C) Copyright 2010-2012 Matti Hämäläinen <ccr@tnsp.org> |
1 | 5 # |
6 use strict; | |
7 use Data::Dumper; | |
8 use HTML::Entities; | |
9 | |
10 | |
11 sub urlencode($) | |
12 { | |
13 my $value = $_[0]; | |
14 $value =~ s/([^a-zA-Z_0-9 ])/"%" . uc(sprintf "%lx" , unpack("C", $1))/eg; | |
15 $value =~ tr/ /+/; | |
16 return $value; | |
17 } | |
18 | |
19 | |
20 sub str_trim($) | |
21 { | |
22 my $str = $_[0]; | |
23 if (defined($str)) { | |
24 $str =~ s/^\s*//; | |
25 $str =~ s/\s*$//; | |
26 } | |
27 return $str; | |
28 } | |
29 | |
30 | |
31 sub pop_token_a($) | |
32 { | |
33 my $tokens = $_[0]; | |
34 return shift(@$tokens); | |
35 } | |
36 | |
37 | |
38 sub pop_token($) | |
39 { | |
40 return str_trim(pop_token_a($_[0])); | |
41 } | |
42 | |
43 | |
44 sub parse_html_str($) | |
45 { | |
46 my $tokens = $_[0]; | |
47 my $token = pop_token($tokens); | |
48 my $str = ""; | |
49 $token =~ s/^\s*//; | |
50 | |
51 return undef unless (substr($token, 0, 1) eq '"'); | |
52 $token = substr($token, 1); | |
53 | |
54 while (defined($token)) { | |
26 | 55 my $tmp = $token; |
25
a076d8d22422
Fix string parsing in the HTML parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
56 $tmp =~ s/\s*$//; |
a076d8d22422
Fix string parsing in the HTML parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
57 if (substr($tmp, -1) eq '"') { |
a076d8d22422
Fix string parsing in the HTML parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
58 $str .= substr($tmp, 0, -1); |
1 | 59 return $str; |
60 } else { | |
61 $str .= $token; | |
62 } | |
63 $token = shift(@$tokens); | |
64 } | |
65 return undef; | |
66 } | |
67 | |
68 | |
69 sub parse_html_tree($$); | |
70 | |
71 sub parse_html_tree($$) | |
72 { | |
73 my ($tokens, $tree) = @_; | |
74 | |
75 while (my $token = pop_token($tokens)) { | |
76 if ($token =~ /^<[!\/]?[a-zA-Z]+/) { | |
77 $token = lc($token); | |
78 if ($token =~ /^<\!.*>$/) { | |
79 # Ignore comments etc. | |
80 } elsif ($token =~ /^<([a-z]+)(.*)>$/) { | |
81 my ($name, $args) = ($1, $2); | |
82 if ($name eq "meta" || $name eq "img") { | |
83 my $tmp = {}; | |
84 parse_html_tree($tokens, $tree); | |
85 return $tree; | |
86 } else { | |
87 my $tmp = { "name" => $name, "args" => str_trim($args) }; | |
88 parse_html_tree($tokens, $tmp); | |
89 push(@{$$tree{"nodes"}}, $tmp); | |
90 } | |
91 } elsif ($token =~ /^<\/([a-z]+)>$/) { | |
92 return $tree; | |
93 } else { | |
94 die("HORROR TERROR ELITE: $token\n"); | |
95 } | |
96 } else { | |
97 $token = str_trim(decode_entities($token)); | |
98 push(@{$$tree{"nodes"}}, { "name" => "text", "args" => "", "text" => $token }) if length($token) > 0; | |
99 } | |
100 } | |
101 | |
102 return $tree; | |
103 } | |
104 | |
105 | |
106 sub parse_html($) | |
107 { | |
108 return undef unless defined($_[0]); | |
109 my $str = $_[0]; | |
110 my $res = { "name" => "", "args" => "" }; | |
111 $str =~ tr/\r/ /; | |
112 $str =~ tr/\n/ /; | |
113 my @tokens = grep { !($_ =~ /^\s*$/) } split(/(<\/?[a-zA-Z]+.*?>)/, $str); | |
114 parse_html_tree(\@tokens, $res); | |
115 return $res; | |
116 } | |
117 | |
118 sub html_find_node($$$); | |
119 | |
120 sub html_find_node($$$) | |
121 { | |
122 my ($node, $name, $args) = @_; | |
123 | |
124 if (defined($node)) { | |
125 if (ref($node) eq "ARRAY") { | |
126 foreach my $n (@$node) { | |
127 my $tmp = html_find_node($n, $name, $args); | |
128 # Must do it like this, in order not to break the loop | |
129 return $tmp if defined($tmp); | |
130 } | |
131 } elsif (ref($node) eq "HASH") { | |
132 if (defined($$node{"name"})) { | |
133 if ($$node{"name"} eq $name) { | |
134 if ($args ne "") { | |
135 if (defined($$node{"args"}) && $$node{"args"} =~ /$args/) { | |
136 } else { | |
33
ad96ed91de92
Fix recursive parsing with attributes.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
137 return html_find_node($$node{"nodes"}, $name, $args); |
1 | 138 } |
139 } | |
140 return $node; | |
141 } else { | |
142 return html_find_node($$node{"nodes"}, $name, $args); | |
143 } | |
144 } | |
145 } | |
146 } | |
147 return undef; | |
148 } | |
149 | |
150 | |
151 sub fnode($$) | |
152 { | |
153 return html_find_node($_[0], $_[1], ""); | |
154 } | |
155 | |
156 | |
157 sub fnodea($$$) | |
158 { | |
159 return html_find_node($_[0], $_[1], $_[2]); | |
160 } | |
161 | |
162 | |
163 sub escape($) | |
164 { | |
165 my $s = $_[0]; | |
166 $s =~ s/(['"])/\\$1/g; | |
167 return $s; | |
168 } | |
169 | |
170 | |
171 sub html_collapse($$); | |
172 | |
173 sub html_collapse($$) | |
174 { | |
175 my ($node, $strip) = @_; | |
176 my $str = ""; | |
177 | |
178 if ($$node{"name"} eq "text") { | |
179 $str .= $$node{"text"}; | |
180 } else { | |
181 $str .= "<".$$node{"name"}.">" unless ($strip); | |
182 foreach my $n (@{$$node{"nodes"}}) { | |
183 $str .= html_collapse($n, $strip); | |
184 } | |
185 $str .= "</".$$node{"name"}.">" unless ($strip); | |
186 } | |
187 | |
188 return $str; | |
189 } | |
190 | |
191 | |
192 ### | |
193 ### Main program | |
194 ### | |
51
0db0d485eb59
Rename some variables, remove "simple" mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
195 my $modes = "php|xml"; |
1 | 196 my $opt_mode = "php"; |
197 my $opt_dump = 0; | |
198 my $opt_filename; | |
199 my $opt_outfile; | |
200 | |
201 while (defined(my $arg = shift)) { | |
202 if (substr($arg, 0, 1) eq "-") { | |
203 if ($arg =~ /^-($modes)$/o) { | |
204 $opt_mode = $1; | |
205 } | |
206 elsif ($arg eq "-dump") { | |
207 $opt_dump = 1; | |
208 } | |
209 elsif ($arg eq "-o") { | |
210 $opt_outfile = shift or die("Output filename option -o requires an argument.\n"); | |
211 } else { | |
212 die("Invalid option '$arg'.\n"); | |
213 } | |
214 } else { | |
215 $opt_filename = $arg; | |
216 } | |
217 } | |
218 | |
4
dd2bce7ec0c8
Removed http fetching feature, it's not really useful here.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
219 die("Usage: $0 [options] <filename> |
1 | 220 |
221 -php Output a PHP include file with data in arrays (default) | |
54 | 222 -xml Output a simple XML file. |
1 | 223 |
224 -o <filename> Set output filename. Default is to use stdout. | |
225 | |
226 -dump Dump HTML tree to stdout and quit. | |
227 | |
228 ") unless defined($opt_filename); | |
229 | |
230 | |
231 my $data; | |
36
bb00a40252e2
Input in ISO-8859-1 and output in UTF-8.
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
232 open(my $fh, '<:encoding(iso-8859-1)', $opt_filename) or die("Error opening '$opt_filename': $!\n"); |
4
dd2bce7ec0c8
Removed http fetching feature, it's not really useful here.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
233 $data = do { local $/; <$fh> }; |
dd2bce7ec0c8
Removed http fetching feature, it's not really useful here.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
234 close($fh); |
1 | 235 |
236 die("No data in input.\n") unless (defined($data) && $data ne ""); | |
237 | |
238 | |
239 # Filter out certain unneeded elements | |
240 $data =~ s/<font[^>]*>//ig; | |
241 $data =~ s/<\/font>//ig; | |
242 $data =~ s/<\/?center>//ig; | |
243 $data =~ s/<br>//ig; | |
244 $data =~ s/ / /ig; | |
245 | |
246 ### Get some general information | |
247 my $otree = parse_html($data); | |
248 if ($opt_dump) { | |
249 print Dumper(fnode($otree, "html")); | |
250 exit; | |
251 } | |
252 | |
253 my %class = (); | |
254 my $body = fnode($otree, "body"); | |
255 if (defined($body) && defined($$body{"nodes"})) { | |
256 foreach my $n (@{$$body{"nodes"}}) { | |
257 if ($$n{"name"} eq "text") { | |
258 push(@{$class{"info"}}, $$n{"text"}); | |
259 } | |
260 elsif ($$n{"name"} eq "b") { | |
261 push(@{$class{"data"}}, $n); | |
262 } | |
263 } | |
264 } | |
265 | |
266 # Filter out some more, for easier tree access during table parsing | |
267 $data =~ s/<\/?b>//ig; | |
268 my $tree = parse_html($data); | |
269 my $node = fnode(fnode($tree, "body"), "table"); | |
270 die("No table element found in document. Perhaps the format has changed? :(\n") unless defined($node); | |
271 | |
272 ### Parse through the HTML document node tree to find the data we need | |
51
0db0d485eb59
Rename some variables, remove "simple" mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
273 my $cid = 0; |
1 | 274 my $q = $$node{"nodes"}; |
57
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
275 my $hourTimes = []; |
51
0db0d485eb59
Rename some variables, remove "simple" mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
276 my $hourDefs = {}; |
0db0d485eb59
Rename some variables, remove "simple" mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
277 my $hourTable = {}; |
57
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
278 my $hourFillTable = {}; |
53
1c5174dcd92a
Clean up maxDays computation.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
279 my $maxDays = 0; |
57
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
280 my $firstHour = 0; |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
281 my $lastHour = 0; |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
282 my $totalHours = 0; |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
283 |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
284 |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
285 sub parseHourData($$) |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
286 { |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
287 my ($l, $rowspan) = @_; |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
288 my $chours = $rowspan / 2; # The table is actually in half cells |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
289 my $cdata = []; |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
290 my $cgrouped = 0; |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
291 |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
292 # Pull in data for the class/hour cell |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
293 foreach my $h (@{$l}) { |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
294 if (defined($$h{"nodes"})) { |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
295 foreach my $b (@{$$h{"nodes"}}) { |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
296 if (defined($$b{"nodes"})) { |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
297 my $text = $$b{"nodes"}[0]{"text"}; |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
298 $text =~ s/\.$//; |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
299 |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
300 $cgrouped = 1 if ($text =~ /vuorov/); |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
301 |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
302 push(@$cdata, $text); |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
303 } |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
304 } |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
305 } |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
306 } |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
307 |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
308 # Increased ID if there is data in this class/hour cell |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
309 my $tid; |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
310 if (scalar(@$cdata) > 0) { |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
311 $cid++; |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
312 $tid = $cid; |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
313 } else { |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
314 $tid = 0; |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
315 } |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
316 |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
317 # Determine current day |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
318 my $cday = 0; |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
319 for (my $x = 0; $x < 7; $x++) { |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
320 if (!defined($$hourFillTable{$lastHour}{$x})) { |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
321 $cday = $x; |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
322 last; |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
323 } |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
324 } |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
325 for (my $t = 0; $t < $chours; $t++) { |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
326 $$hourFillTable{$lastHour + $t}{$cday} = $tid; |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
327 } |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
328 |
72
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
329 if ($tid) |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
330 { |
57
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
331 $maxDays = $cday + 1 if ($cday + 1 > $maxDays); |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
332 |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
333 # Grouped, if there is another class ID in second slot |
80
4faf9956741b
Fix parsing of some class IDs.
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
334 $cgrouped = 1 if ($$cdata[1] =~ /^[A-Z]\d{6}[A-Z]*$/); |
72
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
335 if ($cgrouped) |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
336 { |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
337 my $cdata1 = []; |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
338 my $cdata2 = []; |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
339 for (my $i = 0; $i < length($cdata); $i += 2) |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
340 { |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
341 push(@$cdata1, $$cdata[$i]) if defined($$cdata[$i]); |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
342 push(@$cdata2, $$cdata[$i+1]) if defined($$cdata[$i+1]); |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
343 } |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
344 $$hourDefs{$cid} = { "grouped" => $cgrouped, "start" => $lastHour, "hours" => $chours, "data" => [ $cdata1, $cdata2 ] }; |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
345 } |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
346 else |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
347 { |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
348 $$hourDefs{$cid} = { "grouped" => $cgrouped, "start" => $lastHour, "hours" => $chours, "data" => [ $cdata ] }; |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
349 } |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
350 |
57
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
351 push(@{$$hourTable{$cday}}, $tid); |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
352 $totalHours += $chours; |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
353 } |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
354 } |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
355 |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
356 sub parseHour($) |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
357 { |
72
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
358 if ($_[0] =~ /(\d+):(\d+)/) |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
359 { |
57
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
360 return ((int($1) * 60 + int($2)) * 60); |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
361 } |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
362 return undef; |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
363 } |
1 | 364 |
72
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
365 |
74
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
366 sub getDataStruct($$); |
72
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
367 |
74
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
368 sub getDataStruct($$) |
72
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
369 { |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
370 my @out = (); |
74
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
371 my ($tmp, $first) = @_; |
72
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
372 |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
373 if (ref($tmp) eq "ARRAY") |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
374 { |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
375 my @str = (); |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
376 foreach my $item (@{$tmp}) |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
377 { |
74
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
378 push(@str, getDataStruct($item, 0)); |
72
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
379 } |
74
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
380 if (scalar(@str) > 0) |
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
381 { |
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
382 push(@out, "array(".join(", ", @str).")") if ($opt_mode eq "php"); |
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
383 push(@out, "<group>".join("", @str)."</group>") if ($opt_mode eq "xml"); |
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
384 } |
72
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
385 } |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
386 elsif (ref($tmp) eq "HASH") |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
387 { |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
388 my @str = (); |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
389 foreach my $key (keys %{$tmp}) |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
390 { |
74
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
391 push(@out, "\"".$key."\" => ".getDataStruct($$tmp{$key}, 1)) if ($opt_mode eq "php"); |
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
392 push(@out, "<".$key.">".getDataStruct($$tmp{$key}, 1)."</".$key.">") if ($opt_mode eq "xml"); |
72
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
393 } |
74
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
394 if (scalar(@str) > 0) |
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
395 { |
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
396 push(@out, "array(".join(", ", @str).")") if ($opt_mode eq "php"); |
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
397 push(@out, join("", @str)) if ($opt_mode eq "xml"); |
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
398 } |
72
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
399 } |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
400 elsif ($tmp =~ /^\d+$/) |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
401 { |
74
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
402 if ($opt_mode eq "php") |
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
403 { |
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
404 push(@out, $tmp); |
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
405 } |
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
406 else |
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
407 { |
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
408 push(@out, $first ? $tmp : "<item>".$tmp."</item>"); |
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
409 } |
72
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
410 } |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
411 else |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
412 { |
74
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
413 if ($opt_mode eq "php") |
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
414 { |
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
415 push(@out, "\"".$tmp."\""); |
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
416 } |
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
417 else |
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
418 { |
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
419 push(@out, $first ? $tmp : "<item>".$tmp."</item>"); |
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
420 } |
72
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
421 } |
74
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
422 |
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
423 return join(", ", @out) if ($opt_mode eq "php"); |
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
424 return join("", @out); |
72
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
425 } |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
426 |
8
a52a0bdb5ea1
Crop off empty hours (time slots) and days.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
427 # Skip zero position this way (can't use foreach here) |
1 | 428 for (my $i = 1; $i < scalar(@{$q}); $i++) { |
429 my $d = $$q[$i]{"nodes"}; | |
72
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
430 if (defined($d)) |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
431 { |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
432 foreach my $n (@{$d}) |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
433 { |
1 | 434 my $l = $$n{"nodes"}[0]{"nodes"}; |
57
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
435 if (defined($l)) |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
436 { |
72
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
437 if ($$n{"args"} =~ /colspan=6\s+rowspan=(\d+)/) |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
438 { |
57
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
439 parseHourData($l, $1); |
1 | 440 } |
72
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
441 elsif ($$n{"args"} =~ /rowspan=2\s+align/) |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
442 { |
57
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
443 my $qstart = parseHour($$l[0]{"nodes"}[0]{"nodes"}[0]{"text"}); |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
444 my $qend = parseHour($$l[1]{"nodes"}[0]{"nodes"}[0]{"text"}); |
72
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
445 if (defined($qstart) && defined($qend)) |
6fd715063abc
Clean up some parsing operations.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
446 { |
57
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
447 push(@$hourTimes, {"start" => $qstart, "end" => $qend}); |
1 | 448 } |
449 } | |
450 } | |
451 } | |
57
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
452 $lastHour++; |
1 | 453 } |
454 } | |
455 | |
456 | |
8
a52a0bdb5ea1
Crop off empty hours (time slots) and days.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
457 ### Go through hour table, find last day and hour of the week, crop |
a52a0bdb5ea1
Crop off empty hours (time slots) and days.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
458 my $flag = 1; |
57
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
459 for (my $y = 0; $y < $lastHour && $flag; $y++) { |
51
0db0d485eb59
Rename some variables, remove "simple" mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
460 for (my $x = 0; $x < $maxDays && $flag; $x++) { |
57
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
461 $flag = 0 if (defined($$hourFillTable{$y}{$x}) && $$hourFillTable{$y}{$x} != 0); |
8
a52a0bdb5ea1
Crop off empty hours (time slots) and days.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
462 } |
57
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
463 $firstHour++ if ($flag); |
8
a52a0bdb5ea1
Crop off empty hours (time slots) and days.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
464 } |
a52a0bdb5ea1
Crop off empty hours (time slots) and days.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
465 |
57
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
466 $flag = 1; |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
467 for (my $y = $lastHour - 1; $y >= 0 && $flag; $y--) { |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
468 for (my $x = 0; $x < $maxDays && $flag; $x++) { |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
469 $flag = 0 if (defined($$hourFillTable{$y}{$x}) && $$hourFillTable{$y}{$x} != 0); |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
470 } |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
471 $lastHour-- if ($flag); |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
472 } |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
473 |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
474 |
1 | 475 ### Open output file, if specified |
476 if (defined($opt_outfile)) { | |
477 open(STDOUT, '>', $opt_outfile) or die("Could not open output file '$opt_outfile'.\n"); | |
478 } | |
479 | |
36
bb00a40252e2
Input in ISO-8859-1 and output in UTF-8.
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
480 binmode STDOUT, ':encoding(utf-8)'; |
1 | 481 |
482 ### Output data in desired format | |
483 if ($opt_mode eq "php") { | |
484 print "<?\n". | |
485 "\$classInfo = array(\n". | |
486 " \"general\" => array(".join(", ", map { "\"".escape($_)."\""; } @{$class{"info"}})."),\n". | |
487 " \"info\" => array(".join(", ", map { "\"".escape(html_collapse($_, 1))."\""; } @{$class{"data"}})."),\n". | |
54 | 488 " \"tags\" => array(".join(", ", map { "\"".escape(html_collapse($_, 0))."\""; } @{$class{"data"}})."),\n". |
57
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
489 " \"maxDays\" => $maxDays,\n". |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
490 " \"firstHour\" => $firstHour,\n". |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
491 " \"lastHour\" => $lastHour,\n". |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
492 " \"totalHours\" => $totalHours\n". |
1 | 493 ");\n\n"; |
494 | |
57
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
495 print "\$classHourTimes = array(\n"; |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
496 foreach my $chour (@$hourTimes) { |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
497 print " array(\"start\" => ".$$chour{"start"}.", \"end\" => ".$$chour{"end"}."),\n"; |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
498 } |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
499 print ");\n\n"; |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
500 |
54 | 501 print "\$classHourDefs = array(\n"; |
51
0db0d485eb59
Rename some variables, remove "simple" mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
502 foreach my $cid (sort { $a <=> $b } keys %{$hourDefs}) { |
74
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
503 print " $cid => array(".getDataStruct($$hourDefs{$cid}, 0)."),\n"; |
1 | 504 } |
57
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
505 print ");\n\n"; |
1 | 506 |
57
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
507 print |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
508 "\$classDayTable = array(\n"; |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
509 for (my $y = 0; $y < $maxDays; $y++) |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
510 { |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
511 if (defined($$hourTable{$y})) |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
512 { |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
513 print " $y => array(".join(", ", @{$$hourTable{$y}})."),\n"; |
1 | 514 } |
515 } | |
516 print ");\n?>\n"; | |
517 } | |
518 | |
519 elsif ($opt_mode eq "xml") { | |
520 print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n". | |
521 "<timetable>\n". | |
522 " <class>\n". | |
77
49a329e87367
Adjust some tag names to be more sane in the XML output.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
523 " <general>".join("", map { "<item>".$_."</item>"; } @{$class{"info"}})."</general>\n". |
49a329e87367
Adjust some tag names to be more sane in the XML output.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
524 " <info>".join("", map { "<item>".html_collapse($_, 1)."</item>"; } @{$class{"data"}})."</info>\n". |
52
b2f45dd616bc
Oops, the output variable names should not have been changed.
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
525 " <maxdays>$maxDays</maxdays>\n". |
57
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
526 " <firsthour>$firstHour</firsthour>\n". |
93c87f42c803
New parser output format, parser logic cleaned up a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
527 " <lasthour>$lastHour</lasthour>\n". |
58
119f0cef6498
Work on XML output support (not finished yet.)
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
528 " <totalhours>$totalHours</totalhours>\n". |
1 | 529 " </class>\n"; |
530 | |
58
119f0cef6498
Work on XML output support (not finished yet.)
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
531 print " <hours>\n"; |
75
3d9e42477367
More improvements in the parsing and XML output.
Matti Hamalainen <ccr@tnsp.org>
parents:
74
diff
changeset
|
532 my $cid = 0; |
58
119f0cef6498
Work on XML output support (not finished yet.)
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
533 foreach my $chour (@$hourTimes) { |
75
3d9e42477367
More improvements in the parsing and XML output.
Matti Hamalainen <ccr@tnsp.org>
parents:
74
diff
changeset
|
534 print " <hour id=\"".$cid."\"><start>".$$chour{"start"}."</start><end>".$$chour{"end"}."</end></hour>\n"; |
3d9e42477367
More improvements in the parsing and XML output.
Matti Hamalainen <ccr@tnsp.org>
parents:
74
diff
changeset
|
535 $cid++; |
58
119f0cef6498
Work on XML output support (not finished yet.)
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
536 } |
119f0cef6498
Work on XML output support (not finished yet.)
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
537 print " </hours>\n\n"; |
119f0cef6498
Work on XML output support (not finished yet.)
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
538 |
119f0cef6498
Work on XML output support (not finished yet.)
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
539 print " <classes>\n"; |
75
3d9e42477367
More improvements in the parsing and XML output.
Matti Hamalainen <ccr@tnsp.org>
parents:
74
diff
changeset
|
540 foreach $cid (sort { $a <=> $b } keys %{$hourDefs}) { |
74
b51ad733b624
Improvements in the parser, now also produces "XML" output.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
541 print " <class id=\"$cid\">".getDataStruct($$hourDefs{$cid}, 0)."</class>\n"; |
58
119f0cef6498
Work on XML output support (not finished yet.)
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
542 } |
119f0cef6498
Work on XML output support (not finished yet.)
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
543 print " </classes>\n\n"; |
119f0cef6498
Work on XML output support (not finished yet.)
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
544 |
119f0cef6498
Work on XML output support (not finished yet.)
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
545 print " <days>\n"; |
119f0cef6498
Work on XML output support (not finished yet.)
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
546 for (my $y = 0; $y < $maxDays; $y++) |
119f0cef6498
Work on XML output support (not finished yet.)
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
547 { |
119f0cef6498
Work on XML output support (not finished yet.)
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
548 if (defined($$hourTable{$y})) |
119f0cef6498
Work on XML output support (not finished yet.)
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
549 { |
119f0cef6498
Work on XML output support (not finished yet.)
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
550 print " <day id=\"$y\">".join("", map { "<class>".$_."</class>" } @{$$hourTable{$y}})."</day>\n"; |
119f0cef6498
Work on XML output support (not finished yet.)
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
551 } |
119f0cef6498
Work on XML output support (not finished yet.)
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
552 } |
119f0cef6498
Work on XML output support (not finished yet.)
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
553 print " </days>\n"; |
1 | 554 |
555 print "</timetable>\n"; | |
556 } | |
557 | |
558 close (STDOUT); |