browser: apply transparency based on usage
[sheet.git] / browser.plp
index 5afabaf08331ccb5046094d04aa3ec2f5db52aa4..95dd4a98b645ea3331448be6a16441209aa536ea 100644 (file)
@@ -1,5 +1,6 @@
 <(common.inc.plp)><:
 use 5.010;
+use utf8;
 use List::Util qw(sum max);
 
 Html({
@@ -9,7 +10,7 @@ Html({
                "caniuse.",
        keywords => [qw'html css browser feature'],
        stylesheet => [qw'light dark circus mono red'],
-       data => ['caniuse.js'],
+       data => ['browser-support.inc.pl'],
 });
 
 :>
@@ -20,29 +21,7 @@ with <a href="http://stats.wikimedia.org/archive/squid_reports/">Wikimedia</a>
 browser usage statistics.</p>
 
 <:
-use JSON;
-use File::Slurp 'read_file';
-my $source = read_file('caniuse.js');
-for ($source) { # cleanup
-       # convert seperate variables to hash keys
-       s/\A/{/;
-       s/^caniuse\.(\w+) = /"$1":/gm;
-       s/;$/,/gm;
-       s/,\s*\Z/\n}/;
-       # fractions not supported by barekey
-       s/(?<=[,{]) (\d*\.\d) (?=:')/"$1"/gx;
-       # escapes not supported in singlequote
-       s{'((?:[^\\']+|\\.)*)'}{
-               my $_ = $1;
-               s/"/\\"/g;
-               s/\\'/'/g;
-               qq("$_");
-       }ge;
-}
-my $caniuse = from_json($source, {
-#      allow_singlequote => 1,
-       allow_barekey => 1,
-});
+my $caniuse = do 'browser-support.inc.pl' or die $! || $@;
 
 my %CSTATS = (
        n => 'di-b',
@@ -81,15 +60,31 @@ print '<col>' x 3;
 printf '<colgroup span="%d">', scalar @{ $versions{$_} } for @browsers;
 print "\n";
 
-print '<thead><tr>';
-print '<th colspan="3">feature';
-printf '<th colspan="%d">%s',
-       scalar @{ $versions{$_} }, $caniuse->{agents}->{$_}->{browser}
-               for @browsers;
-print '<th>%';
+my $header = join('',
+       '<tr>',
+       '<th colspan="3">feature',
+       (map {
+               sprintf('<th colspan="%d" title="%.1f%%">%s',
+                       scalar @{ $versions{$_} },
+                       sum(values %{ $canihas->{$_} }),
+                       do {
+                               my $name = $caniuse->{agents}->{$_}->{browser};
+                               length $name < 16 ? $name : $caniuse->{agents}->{$_}->{abbr};
+                       },
+               )
+       } @browsers),
+       '<th>%',
+);
+print '<thead>', $header;
 # preceding row without any colspan to work around gecko bug
-print '<tr>';
-print '<td>' x $_ for 3, (map { scalar @{ $versions{$_} } } @browsers), 1;
+print "\n<tr>";
+print '<td>' x 3;
+for my $browser (@browsers) {
+       printf('<td title="%.1f%%">%s',
+               $canihas->{$browser}->{$_}, showversions($_),
+       ) for @{ $versions{$browser} };
+}
+print '<td>' x 1;
 print "</thead>\n";
 
 sub featurescore {
@@ -155,7 +150,7 @@ for my $id (sort {
                                        ),
                                        scalar @span,
                                        $usage,
-                                       showversions(\@span),
+                                       showversions(@span),
                                undef $prev;
                                @span = ();
                        }
@@ -168,6 +163,7 @@ for my $id (sort {
        });
        print '<td>', int featurescore($caniuse->{data}->{$id}->{stats}) / $maxscore * 100;
 }
+print '<tfoot>', $header;
 print '</table>';
 
 sub paddedver {
@@ -177,16 +173,28 @@ sub paddedver {
 }
 
 sub showversions {
-       my ($span) = @_;
-       splice @$span, 1, -1;
-       for (@$span) {
+       my @span = ($_[0], @_>1 ? $_[-1] : ());
+       for (@span) {
                s/^\./0./;
                s/x$/.*/;
        }
-       return join('‒', @$span);
+       return join('‒', @span);
 }
 
 :>
+<script>
+var classmatch = /\bopacity(\d+)/;
+Array.forEach(document.getElementsByTagName('TD'), function(val) {
+       var opacity;
+       if (opacity = classmatch.exec(val.className)) {
+               var c = document.defaultView.getComputedStyle(val, null).getPropertyValue('background-color');
+               var o = opacity[1] / 100;
+               val.style.backgroundColor = c.replace(/rgb\((.*)\)/, 'rgba($1, '+o+')');
+               val.style.textShadow = c+' 1px 1px 2px, ' + c+' -1px -1px 2px';
+       }
+});
+alert(t);
+</script>
 <hr>
 
 <div class="legend">