# HG changeset patch # User Matti Hamalainen # Date 1351004556 -10800 # Node ID 0db0d485eb59d8acec80daa9e08be7167014b391 # Parent 2be3eec4c7a5d445451d09f32972f774fe81a808 Rename some variables, remove "simple" mode. diff -r 2be3eec4c7a5 -r 0db0d485eb59 parsedata.pl --- a/parsedata.pl Wed Oct 17 15:48:30 2012 +0300 +++ b/parsedata.pl Tue Oct 23 18:02:36 2012 +0300 @@ -192,7 +192,7 @@ ### ### Main program ### -my $modes = "simple|php|xml"; +my $modes = "php|xml"; my $opt_mode = "php"; my $opt_dump = 0; my $opt_filename; @@ -219,7 +219,6 @@ die("Usage: $0 [options] -php Output a PHP include file with data in arrays (default) - -simple Output simple tabled output for easy parsing. -xml Output XML. -o Set output filename. Default is to use stdout. @@ -271,12 +270,12 @@ die("No table element found in document. Perhaps the format has changed? :(\n") unless defined($node); ### Parse through the HTML document node tree to find the data we need -my $id = 0; +my $cid = 0; my $q = $$node{"nodes"}; -my $tunnit = {}; -my $taulu = {}; -my $maxdays = 6; -my $maxhours = 0; +my $hourDefs = {}; +my $hourTable = {}; +my $maxDays = 6; +my $maxHours = 0; # Skip zero position this way (can't use foreach here) for (my $i = 1; $i < scalar(@{$q}); $i++) { @@ -285,9 +284,9 @@ foreach my $n (@{$d}) { my $l = $$n{"nodes"}[0]{"nodes"}; if (defined($l) && $$n{"args"} =~ /colspan=6\s+rowspan=(\d+)/) { - my $tuntia = $1 / 2; - my $data = []; - my $grouped = 0; + my $chours = $1 / 2; + my $cdata = []; + my $cgrouped = 0; foreach my $h (@{$l}) { if (defined($$h{"nodes"})) { foreach my $b (@{$$h{"nodes"}}) { @@ -295,60 +294,60 @@ my $text = $$b{"nodes"}[0]{"text"}; $text =~ s/\.$//; - $grouped = 1 if ($text =~ /vuorov/); + $cgrouped = 1 if ($text =~ /vuorov/); - push(@$data, $text); + push(@$cdata, $text); } } } } my $tid; - if (scalar(@$data) > 0) { - $id++; - $tid = $id; + if (scalar(@$cdata) > 0) { + $cid++; + $tid = $cid; } else { $tid = 0; } - my $tpd = 0; - for (my $x = 0; $x < $maxdays; $x++) { - if (!defined($$taulu{$maxhours}{$x})) { - $tpd = $x; + my $cday = 0; + for (my $x = 0; $x < $maxDays; $x++) { + if (!defined($$hourTable{$maxHours}{$x})) { + $cday = $x; last; } } - for (my $t = 0; $t < $tuntia; $t++) { - $$taulu{$maxhours + $t}{$tpd} = $tid; + for (my $t = 0; $t < $chours; $t++) { + $$hourTable{$maxHours + $t}{$cday} = $tid; } - if (scalar(@$data) > 0) { + if (scalar(@$cdata) > 0) { # Grouped, if there is another class ID in second slot - $grouped = 1 if ($$data[1] =~ /^[A-Z]\d{6}$/); - $$tunnit{$id} = { "grouped" => $grouped, "day" => $tpd, "start" => $maxhours, "hours" => $tuntia, "data" => $data }; + $cgrouped = 1 if ($$cdata[1] =~ /^[A-Z]\d{6}$/); + $$hourDefs{$cid} = { "grouped" => $cgrouped, "day" => $cday, "start" => $maxHours, "hours" => $chours, "data" => $cdata }; } } } - $maxhours++; + $maxHours++; } } ### Go through hour table, find last day and hour of the week, crop my $flag = 1; -for (my $y = $maxhours - 1; $y >= 0 && $flag; $y--) { - for (my $x = 0; $x < $maxdays && $flag; $x++) { - $flag = 0 if (defined($$taulu{$y}{$x}) && $$taulu{$y}{$x} != 0); +for (my $y = $maxHours - 1; $y >= 0 && $flag; $y--) { + for (my $x = 0; $x < $maxDays && $flag; $x++) { + $flag = 0 if (defined($$hourTable{$y}{$x}) && $$hourTable{$y}{$x} != 0); } - $maxhours-- if ($flag); + $maxHours-- if ($flag); } $flag = 1; -for (my $x = $maxdays - 1; $x >= 0 && $flag; $x--) { - for (my $y = 0; $y < $maxhours && $flag; $y++) { - $flag = 0 if (defined($$taulu{$y}{$x}) && $$taulu{$y}{$x} != 0); +for (my $x = $maxDays - 1; $x >= 0 && $flag; $x--) { + for (my $y = 0; $y < $maxHours && $flag; $y++) { + $flag = 0 if (defined($$hourTable{$y}{$x}) && $$hourTable{$y}{$x} != 0); } - $maxdays-- if ($flag); + $maxDays-- if ($flag); } @@ -366,15 +365,15 @@ " \"general\" => array(".join(", ", map { "\"".escape($_)."\""; } @{$class{"info"}})."),\n". " \"info\" => array(".join(", ", map { "\"".escape(html_collapse($_, 1))."\""; } @{$class{"data"}})."),\n". " \"info_tags\" => array(".join(", ", map { "\"".escape(html_collapse($_, 0))."\""; } @{$class{"data"}})."),\n". - " \"maxdays\" => $maxdays,\n". - " \"maxhours\" => $maxhours,\n". + " \"maxDays\" => $maxDays,\n". + " \"maxHours\" => $maxHours,\n". ");\n\n"; print "\$classDefs = array(\n"; - foreach my $id (sort { $a <=> $b } keys %{$tunnit}) { - print " $id => array("; - foreach my $key (keys %{$$tunnit{$id}}) { - my $a = $$tunnit{$id}{$key}; + foreach my $cid (sort { $a <=> $b } keys %{$hourDefs}) { + print " $cid => array("; + foreach my $key (keys %{$$hourDefs{$cid}}) { + my $a = $$hourDefs{$cid}{$key}; print "\"$key\" => "; if (ref($a) eq "ARRAY") { print "array(".join(", ", map { "\"".escape($_)."\""; } @$a).")"; @@ -392,11 +391,11 @@ print ");\n". "\n". "\$classHourTable = array(\n"; - for (my $y = 0; $y < $maxhours; $y++) { + for (my $y = 0; $y < $maxHours; $y++) { my $str = ""; - for (my $x = 0; $x < $maxdays; $x++) { + for (my $x = 0; $x < $maxDays; $x++) { $str .= ", " unless ($str eq ""); - $str .= sprintf "%3d", $$taulu{$y}{$x}; + $str .= sprintf "%3d", $$hourTable{$y}{$x}; } print " array(".$str."),\n"; } @@ -409,21 +408,12 @@ " \n". " ".join("", map { "".encode_entities($_).""; } @{$class{"info"}})."\n". " ".join("", map { "".encode_entities(html_collapse($_, 1)).""; } @{$class{"data"}})."\n". - " $maxdays\n". - " $maxhours\n". + " $maxDays\n". + " $maxHours\n". " \n"; print "\n"; } -elsif ($opt_mode eq "simple") { - for (my $y = 0; $y < $maxhours; $y++) { - for (my $x = 0; $x < $maxdays; $x++) { - printf "%3d ", $$taulu{$y}{$x}; - } - print "\n"; - } -} - close (STDOUT);