annotate parsedata.pl @ 8:a52a0bdb5ea1

Crop off empty hours (time slots) and days.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 11 Jan 2011 22:22:45 +0200
parents dd2bce7ec0c8
children a076d8d22422
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 #!/usr/bin/perl -w
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 #
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 # Fetch and parse HTML format class timetable into more sane formats
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 # (C) Copyright 2010-2010 Matti Hämäläinen <ccr@tnsp.org>
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 #
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 use strict;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 use Data::Dumper;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 use HTML::Entities;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 sub urlencode($)
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13 my $value = $_[0];
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14 $value =~ s/([^a-zA-Z_0-9 ])/"%" . uc(sprintf "%lx" , unpack("C", $1))/eg;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15 $value =~ tr/ /+/;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16 return $value;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 sub str_trim($)
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21 {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22 my $str = $_[0];
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23 if (defined($str)) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24 $str =~ s/^\s*//;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25 $str =~ s/\s*$//;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 return $str;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31 sub pop_token_a($)
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32 {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 my $tokens = $_[0];
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34 return shift(@$tokens);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38 sub pop_token($)
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39 {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40 return str_trim(pop_token_a($_[0]));
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44 sub parse_html_str($)
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 my $tokens = $_[0];
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47 my $token = pop_token($tokens);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48 my $str = "";
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 $token =~ s/^\s*//;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 return undef unless (substr($token, 0, 1) eq '"');
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52 $token = substr($token, 1);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54 while (defined($token)) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 if (substr($token, -1) eq '"') {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 $str .= substr($token, 0, -1);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 return $str;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 } else {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 $str .= $token;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61 $token = shift(@$tokens);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63 return undef;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 sub parse_html_tree($$);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 sub parse_html_tree($$)
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 my ($tokens, $tree) = @_;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73 while (my $token = pop_token($tokens)) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 if ($token =~ /^<[!\/]?[a-zA-Z]+/) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 $token = lc($token);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 if ($token =~ /^<\!.*>$/) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 # Ignore comments etc.
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78 } elsif ($token =~ /^<([a-z]+)(.*)>$/) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79 my ($name, $args) = ($1, $2);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 if ($name eq "meta" || $name eq "img") {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 my $tmp = {};
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 parse_html_tree($tokens, $tree);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 return $tree;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84 } else {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 my $tmp = { "name" => $name, "args" => str_trim($args) };
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 parse_html_tree($tokens, $tmp);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87 push(@{$$tree{"nodes"}}, $tmp);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 } elsif ($token =~ /^<\/([a-z]+)>$/) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 return $tree;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91 } else {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 die("HORROR TERROR ELITE: $token\n");
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 } else {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95 $token = str_trim(decode_entities($token));
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 push(@{$$tree{"nodes"}}, { "name" => "text", "args" => "", "text" => $token }) if length($token) > 0;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 return $tree;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 sub parse_html($)
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105 {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106 return undef unless defined($_[0]);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107 my $str = $_[0];
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 my $res = { "name" => "", "args" => "" };
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 $str =~ tr/\r/ /;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 $str =~ tr/\n/ /;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111 my @tokens = grep { !($_ =~ /^\s*$/) } split(/(<\/?[a-zA-Z]+.*?>)/, $str);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112 parse_html_tree(\@tokens, $res);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 return $res;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116 sub html_find_node($$$);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118 sub html_find_node($$$)
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119 {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120 my ($node, $name, $args) = @_;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122 if (defined($node)) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123 if (ref($node) eq "ARRAY") {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124 foreach my $n (@$node) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125 my $tmp = html_find_node($n, $name, $args);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126 # Must do it like this, in order not to break the loop
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 return $tmp if defined($tmp);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129 } elsif (ref($node) eq "HASH") {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130 if (defined($$node{"name"})) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 if ($$node{"name"} eq $name) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132 if ($args ne "") {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133 if (defined($$node{"args"}) && $$node{"args"} =~ /$args/) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134 } else {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135 return undef;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138 return $node;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
139 } else {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140 return html_find_node($$node{"nodes"}, $name, $args);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
141 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
142 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
144 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145 return undef;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
147
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
148
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
149 sub fnode($$)
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
150 {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151 return html_find_node($_[0], $_[1], "");
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
153
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
154
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
155 sub fnodea($$$)
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
156 {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
157 return html_find_node($_[0], $_[1], $_[2]);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
158 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
159
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
160
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
161 sub escape($)
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
162 {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
163 my $s = $_[0];
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
164 $s =~ s/(['"])/\\$1/g;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
165 return $s;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
166 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
167
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
168
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
169 sub html_collapse($$);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
170
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
171 sub html_collapse($$)
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
172 {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
173 my ($node, $strip) = @_;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
174 my $str = "";
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
175
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
176 if ($$node{"name"} eq "text") {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
177 $str .= $$node{"text"};
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
178 } else {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
179 $str .= "<".$$node{"name"}.">" unless ($strip);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
180 foreach my $n (@{$$node{"nodes"}}) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
181 $str .= html_collapse($n, $strip);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
182 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
183 $str .= "</".$$node{"name"}.">" unless ($strip);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
184 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
185
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
186 return $str;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
187 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
188
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
189
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
190 ###
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
191 ### Main program
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
192 ###
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
193 my $modes = "simple|php|xml";
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
194 my $opt_mode = "php";
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
195 my $opt_dump = 0;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
196 my $opt_filename;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
197 my $opt_outfile;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
198
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
199 while (defined(my $arg = shift)) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
200 if (substr($arg, 0, 1) eq "-") {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
201 if ($arg =~ /^-($modes)$/o) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
202 $opt_mode = $1;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
203 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
204 elsif ($arg eq "-dump") {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
205 $opt_dump = 1;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
206 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
207 elsif ($arg eq "-o") {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
208 $opt_outfile = shift or die("Output filename option -o requires an argument.\n");
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
209 } else {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
210 die("Invalid option '$arg'.\n");
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
211 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
212 } else {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
213 $opt_filename = $arg;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
214 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
215 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
216
4
dd2bce7ec0c8 Removed http fetching feature, it's not really useful here.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
217 die("Usage: $0 [options] <filename>
1
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
218
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
219 -php Output a PHP include file with data in arrays (default)
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
220 -simple Output simple tabled output for easy parsing.
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
221 -xml Output XML.
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
222
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
223 -o <filename> Set output filename. Default is to use stdout.
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
224
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
225 -dump Dump HTML tree to stdout and quit.
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
226
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
227 ") unless defined($opt_filename);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
228
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
229
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
230 my $data;
4
dd2bce7ec0c8 Removed http fetching feature, it's not really useful here.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
231 open(my $fh, '<', $opt_filename) or die("Error opening '$opt_filename': $!\n");
dd2bce7ec0c8 Removed http fetching feature, it's not really useful here.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
232 $data = do { local $/; <$fh> };
dd2bce7ec0c8 Removed http fetching feature, it's not really useful here.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
233 close($fh);
1
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
234
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
235 die("No data in input.\n") unless (defined($data) && $data ne "");
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
236
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
237
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
238 # Filter out certain unneeded elements
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
239 $data =~ s/<font[^>]*>//ig;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
240 $data =~ s/<\/font>//ig;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
241 $data =~ s/<\/?center>//ig;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
242 $data =~ s/<br>//ig;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
243 $data =~ s/&nbsp;/ /ig;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
244
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
245 ### Get some general information
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
246 my $otree = parse_html($data);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
247 if ($opt_dump) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
248 print Dumper(fnode($otree, "html"));
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
249 exit;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
250 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
251
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
252 my %class = ();
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
253 my $body = fnode($otree, "body");
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
254 if (defined($body) && defined($$body{"nodes"})) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
255 foreach my $n (@{$$body{"nodes"}}) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
256 if ($$n{"name"} eq "text") {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
257 push(@{$class{"info"}}, $$n{"text"});
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
258 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
259 elsif ($$n{"name"} eq "b") {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
260 push(@{$class{"data"}}, $n);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
261 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
262 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
263 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
264
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
265 # Filter out some more, for easier tree access during table parsing
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
266 $data =~ s/<\/?b>//ig;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
267 my $tree = parse_html($data);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
268 my $node = fnode(fnode($tree, "body"), "table");
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
269 die("No table element found in document. Perhaps the format has changed? :(\n") unless defined($node);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
270
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
271 ### Parse through the HTML document node tree to find the data we need
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
272 my $id = 0;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
273 my $q = $$node{"nodes"};
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
274 my $tunnit = {};
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
275 my $taulu = {};
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
276 my $maxdays = 6;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
277 my $maxhours = 0;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
278
8
a52a0bdb5ea1 Crop off empty hours (time slots) and days.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
279 # Skip zero position this way (can't use foreach here)
1
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
280 for (my $i = 1; $i < scalar(@{$q}); $i++) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
281 my $d = $$q[$i]{"nodes"};
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
282 if (defined($d)) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
283 foreach my $n (@{$d}) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
284 my $l = $$n{"nodes"}[0]{"nodes"};
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
285 if (defined($l) && $$n{"args"} =~ /colspan=6\s+rowspan=(\d+)/) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
286 my $tuntia = $1 / 2;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
287 my $data = [];
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
288 my $grouped = 0;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
289 foreach my $h (@{$l}) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
290 if (defined($$h{"nodes"})) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
291 foreach my $b (@{$$h{"nodes"}}) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
292 if (defined($$b{"nodes"})) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
293 my $text = $$b{"nodes"}[0]{"text"};
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
294 $text =~ s/\.$//;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
295
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
296 $grouped = 1 if ($text =~ /vuorov/);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
297
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
298 push(@$data, $text);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
299 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
300 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
301 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
302 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
303
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
304 my $tid;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
305 if (scalar(@$data) > 0) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
306 $id++;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
307 $tid = $id;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
308 } else {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
309 $tid = 0;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
310 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
311
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
312 my $tpd = 0;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
313 for (my $x = 0; $x < $maxdays; $x++) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
314 if (!defined($$taulu{$maxhours}{$x})) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
315 $tpd = $x;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
316 last;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
317 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
318 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
319 for (my $t = 0; $t < $tuntia; $t++) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
320 $$taulu{$maxhours + $t}{$tpd} = $tid;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
321 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
322
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
323 if (scalar(@$data) > 0) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
324 # Grouped, if there is another class ID in second slot
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
325 $grouped = 1 if ($$data[1] =~ /^[A-Z]\d{6}$/);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
326 $$tunnit{$id} = { "grouped" => $grouped, "day" => $tpd, "start" => $maxhours, "hours" => $tuntia, "data" => $data };
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
327 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
328 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
329 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
330 $maxhours++;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
331 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
332 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
333
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
334
8
a52a0bdb5ea1 Crop off empty hours (time slots) and days.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
335 ### 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
336 my $flag = 1;
a52a0bdb5ea1 Crop off empty hours (time slots) and days.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
337 for (my $y = $maxhours - 1; $y >= 0 && $flag; $y--) {
a52a0bdb5ea1 Crop off empty hours (time slots) and days.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
338 for (my $x = 0; $x < $maxdays && $flag; $x++) {
a52a0bdb5ea1 Crop off empty hours (time slots) and days.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
339 $flag = 0 if (defined($$taulu{$y}{$x}) && $$taulu{$y}{$x} != 0);
a52a0bdb5ea1 Crop off empty hours (time slots) and days.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
340 }
a52a0bdb5ea1 Crop off empty hours (time slots) and days.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
341 $maxhours-- if ($flag);
a52a0bdb5ea1 Crop off empty hours (time slots) and days.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
342 }
a52a0bdb5ea1 Crop off empty hours (time slots) and days.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
343
a52a0bdb5ea1 Crop off empty hours (time slots) and days.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
344 $flag = 1;
a52a0bdb5ea1 Crop off empty hours (time slots) and days.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
345 for (my $x = $maxdays - 1; $x >= 0 && $flag; $x--) {
a52a0bdb5ea1 Crop off empty hours (time slots) and days.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
346 for (my $y = 0; $y < $maxhours && $flag; $y++) {
a52a0bdb5ea1 Crop off empty hours (time slots) and days.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
347 $flag = 0 if (defined($$taulu{$y}{$x}) && $$taulu{$y}{$x} != 0);
a52a0bdb5ea1 Crop off empty hours (time slots) and days.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
348 }
a52a0bdb5ea1 Crop off empty hours (time slots) and days.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
349 $maxdays-- if ($flag);
a52a0bdb5ea1 Crop off empty hours (time slots) and days.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
350 }
a52a0bdb5ea1 Crop off empty hours (time slots) and days.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
351
a52a0bdb5ea1 Crop off empty hours (time slots) and days.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
352
1
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
353 ### Open output file, if specified
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
354 if (defined($opt_outfile)) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
355 open(STDOUT, '>', $opt_outfile) or die("Could not open output file '$opt_outfile'.\n");
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
356 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
357
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
358
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
359 ### Output data in desired format
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
360 if ($opt_mode eq "php") {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
361 print "<?\n".
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
362 "\$classInfo = array(\n".
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
363 " \"general\" => array(".join(", ", map { "\"".escape($_)."\""; } @{$class{"info"}})."),\n".
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
364 " \"info\" => array(".join(", ", map { "\"".escape(html_collapse($_, 1))."\""; } @{$class{"data"}})."),\n".
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
365 " \"info_tags\" => array(".join(", ", map { "\"".escape(html_collapse($_, 0))."\""; } @{$class{"data"}})."),\n".
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
366 " \"maxdays\" => $maxdays,\n".
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
367 " \"maxhours\" => $maxhours,\n".
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
368 ");\n\n";
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
369
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
370 print "\$classDefs = array(\n";
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
371 foreach my $id (sort { $a <=> $b } keys %{$tunnit}) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
372 print " $id => array(";
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
373 foreach my $key (keys %{$$tunnit{$id}}) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
374 my $a = $$tunnit{$id}{$key};
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
375 print "\"$key\" => ";
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
376 if (ref($a) eq "ARRAY") {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
377 print "array(".join(", ", map { "\"".escape($_)."\""; } @$a).")";
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
378 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
379 elsif ($a =~ /^\d+$/) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
380 print $a;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
381 } else {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
382 print "\"".escape($a)."\"";
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
383 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
384 print ", ";
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
385 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
386 print "),\n";
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
387 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
388
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
389 print ");\n".
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
390 "\n".
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
391 "\$classHourTable = array(\n";
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
392 for (my $y = 0; $y < $maxhours; $y++) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
393 my $str = "";
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
394 for (my $x = 0; $x < $maxdays; $x++) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
395 $str .= ", " unless ($str eq "");
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
396 $str .= sprintf "%3d", $$taulu{$y}{$x};
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
397 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
398 print " array(".$str."),\n";
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
399 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
400 print ");\n?>\n";
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
401 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
402
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
403 elsif ($opt_mode eq "xml") {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
404 print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
405 "<timetable>\n".
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
406 " <class>\n".
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
407 " <general>".join("", map { "<node>".encode_entities($_)."</node>"; } @{$class{"info"}})."</general>\n".
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
408 " <info>".join("", map { "<node>".encode_entities(html_collapse($_, 1))."</node>"; } @{$class{"data"}})."</info>\n".
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
409 " <maxdays>$maxdays</maxdays>\n".
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
410 " <maxhours>$maxhours</maxhours>\n".
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
411 " </class>\n";
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
412
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
413
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
414 print "</timetable>\n";
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
415 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
416
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
417 elsif ($opt_mode eq "simple") {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
418 for (my $y = 0; $y < $maxhours; $y++) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
419 for (my $x = 0; $x < $maxdays; $x++) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
420 printf "%3d ", $$taulu{$y}{$x};
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
421 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
422 print "\n";
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
423 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
424 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
425
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
426 close (STDOUT);