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
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
54
0d574e3248b5 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 52
diff changeset
4 # (C) Copyright 2010-2012 Matti Hämäläinen <ccr@tnsp.org>
1
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
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
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 sub str_trim($)
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31 {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32 my $str = $_[0];
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 if (defined($str)) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34 $str =~ s/^\s*//;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35 $str =~ s/\s*$//;
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 return $str;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38 }
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
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41 sub pop_token_a($)
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 my $tokens = $_[0];
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44 return shift(@$tokens);
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
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48 sub pop_token($)
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 return str_trim(pop_token_a($_[0]));
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52
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 sub parse_html_str($)
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 my $tokens = $_[0];
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 my $token = pop_token($tokens);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 my $str = "";
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 $token =~ s/^\s*//;
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 return undef unless (substr($token, 0, 1) eq '"');
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 $token = substr($token, 1);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 while (defined($token)) {
26
eb82a7ddf5bd Oops, 10l.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
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
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 return $str;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 } else {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 $str .= $token;
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 $token = shift(@$tokens);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 return undef;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79 sub parse_html_tree($$);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 sub parse_html_tree($$)
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 my ($tokens, $tree) = @_;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 while (my $token = pop_token($tokens)) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 if ($token =~ /^<[!\/]?[a-zA-Z]+/) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87 $token = lc($token);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 if ($token =~ /^<\!.*>$/) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 # Ignore comments etc.
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 } elsif ($token =~ /^<([a-z]+)(.*)>$/) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91 my ($name, $args) = ($1, $2);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 if ($name eq "meta" || $name eq "img") {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 my $tmp = {};
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 parse_html_tree($tokens, $tree);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95 return $tree;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 } else {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 my $tmp = { "name" => $name, "args" => str_trim($args) };
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98 parse_html_tree($tokens, $tmp);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 push(@{$$tree{"nodes"}}, $tmp);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 } elsif ($token =~ /^<\/([a-z]+)>$/) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102 return $tree;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103 } else {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 die("HORROR TERROR ELITE: $token\n");
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 } else {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107 $token = str_trim(decode_entities($token));
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 push(@{$$tree{"nodes"}}, { "name" => "text", "args" => "", "text" => $token }) if length($token) > 0;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112 return $tree;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 }
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 parse_html($)
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 return undef unless defined($_[0]);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119 my $str = $_[0];
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120 my $res = { "name" => "", "args" => "" };
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121 $str =~ tr/\r/ /;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122 $str =~ tr/\n/ /;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123 my @tokens = grep { !($_ =~ /^\s*$/) } split(/(<\/?[a-zA-Z]+.*?>)/, $str);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124 parse_html_tree(\@tokens, $res);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125 return $res;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128 sub html_find_node($$$);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130 sub html_find_node($$$)
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132 my ($node, $name, $args) = @_;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134 if (defined($node)) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135 if (ref($node) eq "ARRAY") {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136 foreach my $n (@$node) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137 my $tmp = html_find_node($n, $name, $args);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138 # Must do it like this, in order not to break the loop
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
139 return $tmp if defined($tmp);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
141 } elsif (ref($node) eq "HASH") {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
142 if (defined($$node{"name"})) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143 if ($$node{"name"} eq $name) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
144 if ($args ne "") {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145 if (defined($$node{"args"}) && $$node{"args"} =~ /$args/) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
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 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
150 return $node;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151 } else {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152 return html_find_node($$node{"nodes"}, $name, $args);
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 }
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 undef;
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 fnode($$)
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 return html_find_node($_[0], $_[1], "");
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
164 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
165
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 sub fnodea($$$)
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 return html_find_node($_[0], $_[1], $_[2]);
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
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 sub escape($)
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
174 {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
175 my $s = $_[0];
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
176 $s =~ s/(['"])/\\$1/g;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
177 return $s;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
178 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
179
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
180
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
181 sub html_collapse($$);
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 sub html_collapse($$)
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 my ($node, $strip) = @_;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
186 my $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 if ($$node{"name"} eq "text") {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
189 $str .= $$node{"text"};
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
190 } else {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
191 $str .= "<".$$node{"name"}.">" unless ($strip);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
192 foreach my $n (@{$$node{"nodes"}}) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
193 $str .= html_collapse($n, $strip);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
194 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
195 $str .= "</".$$node{"name"}.">" unless ($strip);
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
196 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
197
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
198 return $str;
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
199 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
200
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
1a5994671dcf Oops. Fix a silly 10L.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
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
1a5994671dcf Oops. Fix a silly 10L.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
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
1a5994671dcf Oops. Fix a silly 10L.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
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
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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/&nbsp;/ /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
3790db4eb29b Cosmetic.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
402 if ($opt_dump)
3790db4eb29b Cosmetic.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
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
2654466d2655 Oops, 10L.
Matti Hamalainen <ccr@tnsp.org>
parents: 163
diff changeset
431 my $q = $$node{"nodes"};
161
62687fee6f1c Cosmetic.
Matti Hamalainen <ccr@tnsp.org>
parents: 156
diff changeset
432 for (my $i = 1; $i < scalar(@{$q}); $i++)
62687fee6f1c Cosmetic.
Matti Hamalainen <ccr@tnsp.org>
parents: 156
diff changeset
433 {
1
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
453 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
454 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
455 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
458 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
459 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
460
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
480 ### Open output file, if specified
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
481 if (defined($opt_outfile)) {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
482 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
483 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
486
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
487 ### Output data in desired format
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
488 if ($opt_mode eq "php") {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
489 print "<?\n".
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
490 "\$classInfo = array(\n".
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
491 " \"general\" => array(".join(", ", map { "\"".escape($_)."\""; } @{$class{"info"}})."),\n".
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
492 " \"info\" => array(".join(", ", map { "\"".escape(html_collapse($_, 1))."\""; } @{$class{"data"}})."),\n".
54
0d574e3248b5 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 52
diff changeset
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
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
498 ");\n\n";
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
0d574e3248b5 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 52
diff changeset
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
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
519 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
520 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
521 print ");\n?>\n";
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
522 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
523
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
524 elsif ($opt_mode eq "xml") {
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
525 print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
526 "<timetable>\n".
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
534 " </class>\n";
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
559
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
560 print "</timetable>\n";
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
561 }
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
562
21fde93375e9 Add beta code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
563 close (STDOUT);