# HG changeset patch # User Matti Hamalainen # Date 1351049492 -10800 # Node ID 6fd715063abc026256e8c03620d99dfbaf4ff13c # Parent 0329105ddedc3ef67c053e7660de4ccfb984adac Clean up some parsing operations. diff -r 0329105ddedc -r 6fd715063abc index.php --- a/index.php Wed Oct 24 04:30:36 2012 +0300 +++ b/index.php Wed Oct 24 06:31:32 2012 +0300 @@ -176,28 +176,43 @@ } -function lukGetClassInfo($isSplit, $data) +function lukGetClassInfo($grouped, $data) { - if ($isSplit) + if ($grouped) { - $out = "". - ""; + $out = "
".lukMatchCourse($data[0])."".lukMatchCourse($data[1])."
"; - for ($i = 2; $i < count($data); $i += 2) + $maxCount = 0; + foreach ($data as $col) + { + $out .= ""; + if (count($col) > $maxCount) + $maxCount = count($col); + } + $out .= ""; + + for ($i = 1; $i < $maxCount; $i++) { - $out .= ""; + $out .= ""; + foreach ($data as $col) + { + $out .= ""; + } + $out .= ""; } - + return $out."
".lukMatchCourse($col[0])."
".lukChEntities($data[$i])."". - (isset($data[$i+1]) ? lukChEntities($data[$i+1]) : "")."
"; + if (isset($col[$i])) + $out .= lukChEntities($col[$i]); + $out .= "
"; } else { - $out = lukMatchCourse($data[0])."
"; + $out = lukMatchCourse($data[0][0])."
"; - for ($i = 1; $i < count($data); $i++) + for ($i = 1; $i < count($data[0]); $i++) { - $out .= lukChEntities($data[$i])."
"; + $out .= lukChEntities($data[0][$i])."
"; } return $out; @@ -205,26 +220,6 @@ } -function lukGetClassInfoData($id) -{ - global $classInfo; - - if (isset($id)) - { - if ($id >= 0 && isset($classInfo[$id])) - { - $i = $classInfo[$id]; - $isSplit = preg_match("/^[A-Z]\d{6}$/", $i["data"][1]); - return lukGetClassInfo($isSplit, $i["data"]); - } - else - return "

Ei tunteja.

"; - } - else - return "

Ei mitään.

"; -} - - function lukFindClass($day, $hour) { global $classHourDefs, $classDayTable; @@ -385,15 +380,13 @@ { $class["set"] = TRUE; - $isSplit = preg_match("/^[A-Z]\d{6}$/", $class["data"][1]); - $isActive = $day == $currDay && $currTime >= $classHourTimes[$class["start"]]["start"] && $currTime < $classHourTimes[$class["start"] + $class["hours"] - 1]["end"]; $out .= " ". - lukGetClassInfo($isSplit, $class["data"]). + lukGetClassInfo($class["grouped"], $class["data"]). "
".$class["hours"]."h
\n"; } } diff -r 0329105ddedc -r 6fd715063abc parsedata.pl --- a/parsedata.pl Wed Oct 24 04:30:36 2012 +0300 +++ b/parsedata.pl Wed Oct 24 06:31:32 2012 +0300 @@ -326,12 +326,28 @@ $$hourFillTable{$lastHour + $t}{$cday} = $tid; } - if ($tid) { + if ($tid) + { $maxDays = $cday + 1 if ($cday + 1 > $maxDays); # Grouped, if there is another class ID in second slot $cgrouped = 1 if ($$cdata[1] =~ /^[A-Z]\d{6}$/); - $$hourDefs{$cid} = { "grouped" => $cgrouped, "start" => $lastHour, "hours" => $chours, "data" => $cdata }; + if ($cgrouped) + { + my $cdata1 = []; + my $cdata2 = []; + for (my $i = 0; $i < length($cdata); $i += 2) + { + push(@$cdata1, $$cdata[$i]) if defined($$cdata[$i]); + push(@$cdata2, $$cdata[$i+1]) if defined($$cdata[$i+1]); + } + $$hourDefs{$cid} = { "grouped" => $cgrouped, "start" => $lastHour, "hours" => $chours, "data" => [ $cdata1, $cdata2 ] }; + } + else + { + $$hourDefs{$cid} = { "grouped" => $cgrouped, "start" => $lastHour, "hours" => $chours, "data" => [ $cdata ] }; + } + push(@{$$hourTable{$cday}}, $tid); $totalHours += $chours; } @@ -339,27 +355,70 @@ sub parseHour($) { - if ($_[0] =~ /(\d+):(\d+)/) { + if ($_[0] =~ /(\d+):(\d+)/) + { return ((int($1) * 60 + int($2)) * 60); } return undef; } + +sub getDataStruct($); + +sub getDataStruct($) +{ + my @out = (); + my $tmp = $_[0]; + + if (ref($tmp) eq "ARRAY") + { + my @str = (); + foreach my $item (@{$tmp}) + { + push(@str, getDataStruct($item)); + } + push(@out, "array(".join(", ", @str).")"); + } + elsif (ref($tmp) eq "HASH") + { + my @str = (); + foreach my $key (keys %{$tmp}) + { + push(@out, "\"".$key."\" => ".getDataStruct($$tmp{$key})); + } + push(@out, "array(".join(", ", @str).")"); + } + elsif ($tmp =~ /^\d+$/) + { + push(@out, $tmp); + } + else + { + push(@out, "\"".$tmp."\""); + } + return join(", ", @out); +} + # Skip zero position this way (can't use foreach here) for (my $i = 1; $i < scalar(@{$q}); $i++) { my $d = $$q[$i]{"nodes"}; - if (defined($d)) { - foreach my $n (@{$d}) { + if (defined($d)) + { + foreach my $n (@{$d}) + { my $l = $$n{"nodes"}[0]{"nodes"}; if (defined($l)) { - if ($$n{"args"} =~ /colspan=6\s+rowspan=(\d+)/) { + if ($$n{"args"} =~ /colspan=6\s+rowspan=(\d+)/) + { parseHourData($l, $1); } - elsif ($$n{"args"} =~ /rowspan=2\s+align/) { + elsif ($$n{"args"} =~ /rowspan=2\s+align/) + { my $qstart = parseHour($$l[0]{"nodes"}[0]{"nodes"}[0]{"text"}); my $qend = parseHour($$l[1]{"nodes"}[0]{"nodes"}[0]{"text"}); - if (defined($qstart) && defined($qend)) { + if (defined($qstart) && defined($qend)) + { push(@$hourTimes, {"start" => $qstart, "end" => $qend}); } } @@ -416,21 +475,7 @@ print "\$classHourDefs = array(\n"; 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).")"; - } - elsif ($a =~ /^\d+$/) { - print $a; - } else { - print "\"".escape($a)."\""; - } - print ", "; - } - print "),\n"; + print " $cid => array(".getDataStruct($$hourDefs{$cid})."),\n"; } print ");\n\n";