changeset 72:6fd715063abc

Clean up some parsing operations.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 24 Oct 2012 06:31:32 +0300
parents 0329105ddedc
children 241839ebb09f
files index.php parsedata.pl
diffstat 2 files changed, 95 insertions(+), 57 deletions(-) [+]
line wrap: on
line diff
--- 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 = "<table>".
-    "<tr><td>".lukMatchCourse($data[0])."</td><td>".lukMatchCourse($data[1])."</td></tr>";
+    $out = "<table><tr>";
 
-    for ($i = 2; $i < count($data); $i += 2)
+    $maxCount = 0;
+    foreach ($data as $col)
+    {
+      $out .= "<td>".lukMatchCourse($col[0])."</td>";
+      if (count($col) > $maxCount)
+        $maxCount = count($col);
+    }
+    $out .= "</tr>";
+
+    for ($i = 1; $i < $maxCount; $i++)
     {
-      $out .= "<tr><td>".lukChEntities($data[$i])."</td><td>".
-        (isset($data[$i+1]) ? lukChEntities($data[$i+1]) : "")."</td></tr>";
+      $out .= "<tr>";
+      foreach ($data as $col)
+      {
+        $out .= "<td>";
+        if (isset($col[$i]))
+          $out .= lukChEntities($col[$i]);
+        $out .= "</td>";
+      }
+      $out .= "</tr>";
     }
-    
+
     return $out."</table>";
   }
   else
   {
-    $out = lukMatchCourse($data[0])."<br />";
+    $out = lukMatchCourse($data[0][0])."<br />";
 
-    for ($i = 1; $i < count($data); $i++)
+    for ($i = 1; $i < count($data[0]); $i++)
     {
-      $out .= lukChEntities($data[$i])."<br />";
+      $out .= lukChEntities($data[0][$i])."<br />";
     }
 
     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 "<p>Ei tunteja.</p>";
-  }
-  else
-    return "<p>Ei mitään.</p>";
-}
-
-
 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 .= "  <td rowspan=\"".$class["hours"].
           "\" class=\"".($isActive ? "clactive " : "").($class["grouped"] ? "clgrouped" : "clnormal")."\">".
-          lukGetClassInfo($isSplit, $class["data"]).
+          lukGetClassInfo($class["grouped"], $class["data"]).
           "<div class=\"nhours\"><span>".$class["hours"]."h</span></div></td>\n";
         }
       }
--- 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";