comparison parsedata.pl @ 54:0d574e3248b5

Cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 23 Oct 2012 19:00:19 +0300
parents b2f45dd616bc
children 656b2a22b945
comparison
equal deleted inserted replaced
52:b2f45dd616bc 54:0d574e3248b5
1 #!/usr/bin/perl -w 1 #!/usr/bin/perl -w
2 # 2 #
3 # Fetch and parse HTML format class timetable into more sane formats 3 # Fetch and parse HTML format class timetable into more sane formats
4 # (C) Copyright 2010-2011 Matti Hämäläinen <ccr@tnsp.org> 4 # (C) Copyright 2010-2012 Matti Hämäläinen <ccr@tnsp.org>
5 # 5 #
6 use strict; 6 use strict;
7 use Data::Dumper; 7 use Data::Dumper;
8 use HTML::Entities; 8 use HTML::Entities;
9 9
217 } 217 }
218 218
219 die("Usage: $0 [options] <filename> 219 die("Usage: $0 [options] <filename>
220 220
221 -php Output a PHP include file with data in arrays (default) 221 -php Output a PHP include file with data in arrays (default)
222 -xml Output XML. 222 -xml Output a simple XML file.
223 223
224 -o <filename> Set output filename. Default is to use stdout. 224 -o <filename> Set output filename. Default is to use stdout.
225 225
226 -dump Dump HTML tree to stdout and quit. 226 -dump Dump HTML tree to stdout and quit.
227 227
282 my $d = $$q[$i]{"nodes"}; 282 my $d = $$q[$i]{"nodes"};
283 if (defined($d)) { 283 if (defined($d)) {
284 foreach my $n (@{$d}) { 284 foreach my $n (@{$d}) {
285 my $l = $$n{"nodes"}[0]{"nodes"}; 285 my $l = $$n{"nodes"}[0]{"nodes"};
286 if (defined($l) && $$n{"args"} =~ /colspan=6\s+rowspan=(\d+)/) { 286 if (defined($l) && $$n{"args"} =~ /colspan=6\s+rowspan=(\d+)/) {
287 my $chours = $1 / 2; 287 my $chours = $1 / 2; # The table is actually in half cells
288 my $cdata = []; 288 my $cdata = [];
289 my $cgrouped = 0; 289 my $cgrouped = 0;
290
290 foreach my $h (@{$l}) { 291 foreach my $h (@{$l}) {
291 if (defined($$h{"nodes"})) { 292 if (defined($$h{"nodes"})) {
292 foreach my $b (@{$$h{"nodes"}}) { 293 foreach my $b (@{$$h{"nodes"}}) {
293 if (defined($$b{"nodes"})) { 294 if (defined($$b{"nodes"})) {
294 my $text = $$b{"nodes"}[0]{"text"}; 295 my $text = $$b{"nodes"}[0]{"text"};
362 if ($opt_mode eq "php") { 363 if ($opt_mode eq "php") {
363 print "<?\n". 364 print "<?\n".
364 "\$classInfo = array(\n". 365 "\$classInfo = array(\n".
365 " \"general\" => array(".join(", ", map { "\"".escape($_)."\""; } @{$class{"info"}})."),\n". 366 " \"general\" => array(".join(", ", map { "\"".escape($_)."\""; } @{$class{"info"}})."),\n".
366 " \"info\" => array(".join(", ", map { "\"".escape(html_collapse($_, 1))."\""; } @{$class{"data"}})."),\n". 367 " \"info\" => array(".join(", ", map { "\"".escape(html_collapse($_, 1))."\""; } @{$class{"data"}})."),\n".
367 " \"info_tags\" => array(".join(", ", map { "\"".escape(html_collapse($_, 0))."\""; } @{$class{"data"}})."),\n". 368 " \"tags\" => array(".join(", ", map { "\"".escape(html_collapse($_, 0))."\""; } @{$class{"data"}})."),\n".
368 " \"maxdays\" => $maxDays,\n". 369 " \"maxdays\" => $maxDays,\n".
369 " \"maxhours\" => $maxHours,\n". 370 " \"maxhours\" => $maxHours,\n".
370 ");\n\n"; 371 ");\n\n";
371 372
372 print "\$classDefs = array(\n"; 373 print "\$classHourDefs = array(\n";
373 foreach my $cid (sort { $a <=> $b } keys %{$hourDefs}) { 374 foreach my $cid (sort { $a <=> $b } keys %{$hourDefs}) {
374 print " $cid => array("; 375 print " $cid => array(";
375 foreach my $key (keys %{$$hourDefs{$cid}}) { 376 foreach my $key (keys %{$$hourDefs{$cid}}) {
376 my $a = $$hourDefs{$cid}{$key}; 377 my $a = $$hourDefs{$cid}{$key};
377 print "\"$key\" => "; 378 print "\"$key\" => ";