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