browser: transfer usage of unknown version to next row
[sheet.git] / browser.plp
index 2f75abd8b5cee9464a9cca102b9e9683be7c005e..eee60960506b1c6f549c5203d81356a7ba7dd0d4 100644 (file)
@@ -1,45 +1,24 @@
 <(common.inc.plp)><:
 use 5.010;
 use utf8;
-use List::Util qw(sum max);
+use List::Util qw(sum max first);
 
 Html({
        title => 'browser compatibility cheat sheet',
        version => 'v1.0',
        description =>
                "caniuse.",
-       keywords => [qw'html css browser feature'],
+       keywords => [qw'
+               web browser support compatibility usage available feature
+               html html5 css css3 svg javascript js dom mobile
+               ie internet explorer firefox chrome safari webkit opera
+       '],
        stylesheet => [qw'circus dark mono red light'],
        data => ['browser-support.inc.pl'],
 });
 
-:>
-<h1>Browser compatibility</h1>
-
-<p id="intro">Alternate view of Fyrd's <a href="http://caniuse.com/">when can I use...</a> page<:
-my ($canihas, $usage);
-given ($get{usage} // 'wm') {
-       when (!$_) {
-               # none
-       }
-       when (!/^\w+$/) {
-               printf "<p>Invalid browser usage data request: <em>%s</em>",
-                       'identifier must be alphanumeric name or <q>0</q>';
-       }
-       $canihas = do "browser-usage-$_.inc.pl" or do {
-               printf "<p>Browser usage data not found: <em>%s</em>", $! || $@;
-               break;
-       };
-       $usage = $_;
-       my $ref = $canihas->{-source} || 'unknown';
-       $ref = sprintf '<a href="%s">%s</a>', $_, $ref for $canihas->{-url} || ();
-       $ref .= " $_" for $canihas->{-date} || ();
-       print "\nwith $ref browser usage statistics";
-}
-:>.
-</p>
+say "<h1>Browser compatibility</h1>\n";
 
-<:
 my $caniuse = do 'browser-support.inc.pl' or die $! || $@;
 $_->{verrelease} = {
        # mark last two (future) versions as unreleased, ensure current isn't
@@ -90,6 +69,46 @@ if (my ($somerow) = values %{ $caniuse->{data} }) {
 my @browsers = grep { $versions{$_} }
        qw(trident gecko webkit_saf ios_saf webkit_chr android presto op_mob op_mini);
 
+print <<'';
+<p id="intro">Alternate rendition of Fyrd's <a href="http://caniuse.com/">when can I use...</a> page
+
+my ($canihas, $usage);
+given ($get{usage} // 'wm') {
+       when (!$_) {
+               # none
+       }
+       when (!/^\w+$/) {
+               printf "<p>Invalid browser usage data request: <em>%s</em>",
+                       'identifier must be alphanumeric name or <q>0</q>';
+       }
+       $canihas = do "browser-usage-$_.inc.pl" or do {
+               printf "<p>Browser usage data not found: <em>%s</em>", $! || $@;
+               break;
+       };
+       $usage = $_;
+       my $ref = $canihas->{-source} || 'unknown';
+       $ref = sprintf '<a href="%s">%s</a>', $_, $ref for $canihas->{-url} || ();
+       $ref .= " $_" for $canihas->{-date} || ();
+       print "\nwith $ref browser usage statistics";
+}
+if ($usage) {
+       # first() does not work inside given >:(
+       while (my ($browser, $row) = each %$canihas) {
+               my $verlist = $versions{$browser} or next;
+               my %supported = map { $_ => 1 } @$verlist;
+               $row->{$_} //= undef for @$verlist;  # ensure stable keys during iteration
+               while (my ($version, $usage) = each %$row) {
+                       next if defined $supported{$version};
+                       $_ = paddedver($_) for $version;
+                       my $next = first { paddedver($_) ge $version } @$verlist; # or next
+                       $row->{$next} += $usage;
+               }
+       }
+}
+:>.
+</p>
+
+<:
 $canihas ||= {
        map {
                $_ => +{