# HG changeset patch # User Matti Hamalainen # Date 1440403213 -10800 # Node ID 34ce8339b95658627fb707b7cdbf93427bef6e2c # Parent 04c4f1a95dbd5a4cbef3233410d701990cd87b18 Cosmetic reformatting. diff -r 04c4f1a95dbd -r 34ce8339b956 parsedata.pl --- a/parsedata.pl Mon Aug 24 10:59:20 2015 +0300 +++ b/parsedata.pl Mon Aug 24 11:00:13 2015 +0300 @@ -1,7 +1,7 @@ #!/usr/bin/perl -w # # Fetch and parse HTML format class timetable into more sane formats -# (C) Copyright 2010-2012 Matti Hämäläinen +# (C) Copyright 2010-2015 Matti Hämäläinen # use strict; use Data::Dumper; @@ -30,7 +30,8 @@ sub str_trim($) { my $str = $_[0]; - if (defined($str)) { + if (defined($str)) + { $str =~ s/^\s*//; $str =~ s/\s*$//; } @@ -61,13 +62,17 @@ return undef unless (substr($token, 0, 1) eq '"'); $token = substr($token, 1); - while (defined($token)) { + while (defined($token)) + { my $tmp = $token; $tmp =~ s/\s*$//; - if (substr($tmp, -1) eq '"') { + if (substr($tmp, -1) eq '"') + { $str .= substr($tmp, 0, -1); return $str; - } else { + } + else + { $str .= $token; } $token = shift(@$tokens); @@ -82,28 +87,42 @@ { my ($tokens, $tree) = @_; - while (my $token = pop_token($tokens)) { - if ($token =~ /^<[!\/]?[a-zA-Z]+/) { + while (my $token = pop_token($tokens)) + { + if ($token =~ /^<[!\/]?[a-zA-Z]+/) + { $token = lc($token); - if ($token =~ /^<\!.*>$/) { + if ($token =~ /^<\!.*>$/) + { # Ignore comments etc. - } elsif ($token =~ /^<([a-z]+)(.*)>$/) { + } + elsif ($token =~ /^<([a-z]+)(.*)>$/) + { my ($name, $args) = ($1, $2); - if ($name eq "meta" || $name eq "img") { + if ($name eq "meta" || $name eq "img") + { my $tmp = {}; parse_html_tree($tokens, $tree); return $tree; - } else { + } + else + { my $tmp = { "name" => $name, "args" => str_trim($args) }; parse_html_tree($tokens, $tmp); push(@{$$tree{"nodes"}}, $tmp); } - } elsif ($token =~ /^<\/([a-z]+)>$/) { + } + elsif ($token =~ /^<\/([a-z]+)>$/) + { return $tree; - } else { + } + else + { die("HORROR TERROR ELITE: $token\n"); } - } else { + } + else + { $token = str_trim(decode_entities($token)); push(@{$$tree{"nodes"}}, { "name" => "text", "args" => "", "text" => $token }) if length($token) > 0; } @@ -131,24 +150,36 @@ { my ($node, $name, $args) = @_; - if (defined($node)) { - if (ref($node) eq "ARRAY") { - foreach my $n (@$node) { + if (defined($node)) + { + if (ref($node) eq "ARRAY") + { + foreach my $n (@$node) + { my $tmp = html_find_node($n, $name, $args); # Must do it like this, in order not to break the loop return $tmp if defined($tmp); } - } elsif (ref($node) eq "HASH") { - if (defined($$node{"name"})) { - if ($$node{"name"} eq $name) { + } + elsif (ref($node) eq "HASH") + { + if (defined($$node{"name"})) + { + if ($$node{"name"} eq $name) + { if ($args ne "") { - if (defined($$node{"args"}) && $$node{"args"} =~ /$args/) { - } else { + if (defined($$node{"args"}) && $$node{"args"} =~ /$args/) + { + } + else + { return html_find_node($$node{"nodes"}, $name, $args); } } return $node; - } else { + } + else + { return html_find_node($$node{"nodes"}, $name, $args); } } @@ -185,11 +216,15 @@ my ($node, $strip) = @_; my $str = ""; - if ($$node{"name"} eq "text") { + if ($$node{"name"} eq "text") + { $str .= $$node{"text"}; - } else { + } + else + { $str .= "<".$$node{"name"}.">" unless ($strip); - foreach my $n (@{$$node{"nodes"}}) { + foreach my $n (@{$$node{"nodes"}}) + { $str .= html_collapse($n, $strip); } $str .= "" unless ($strip); @@ -208,10 +243,14 @@ my $cgrouped = 0; # Pull in data for the class/hour cell - foreach my $h (@{$l}) { - if (defined($$h{"nodes"})) { - foreach my $b (@{$$h{"nodes"}}) { - if (defined($$b{"nodes"})) { + foreach my $h (@{$l}) + { + if (defined($$h{"nodes"})) + { + foreach my $b (@{$$h{"nodes"}}) + { + if (defined($$b{"nodes"})) + { my $text = $$b{"nodes"}[0]{"text"}; $text =~ s/\.$//; @@ -239,13 +278,16 @@ # Determine current day my $cday = 0; - for (my $x = 0; $x < 7; $x++) { - if (!defined($$hourFillTable{$lastHour}{$x})) { + for (my $x = 0; $x < 7; $x++) + { + if (!defined($$hourFillTable{$lastHour}{$x})) + { $cday = $x; last; } } - for (my $t = 0; $t < $chours; $t++) { + for (my $t = 0; $t < $chours; $t++) + { $$hourFillTable{$lastHour + $t}{$cday} = $tid; }