8c0bd6a70a76e17e9ca7786365f028d774c7b06e
[sheet.git] / browser.plp
1 <(common.inc.plp)><:
2 use 5.010;
3 use utf8;
4 use List::Util qw(sum max);
5
6 Html({
7         title => 'browser compatibility cheat sheet',
8         version => 'v1.0',
9         description =>
10                 "caniuse.",
11         keywords => [qw'html css browser feature'],
12         stylesheet => [qw'circus dark mono red'],
13         data => ['browser-support.inc.pl'],
14 });
15
16 :>
17 <h1>Browser compatibility</h1>
18
19 <p>Alternate view of Fyrd's <a href="http://caniuse.com/">when can I use...</a> page
20 with <a href="http://stats.wikimedia.org/archive/squid_reports/">Wikimedia</a>
21 browser usage statistics.</p>
22
23 <:
24 my $caniuse = do 'browser-support.inc.pl' or die $! || $@;
25
26 my %CSTATS = (
27         'n'   => 'l0',
28         'y'   => 'l6',
29         'y x' => 'l5',
30         'a'   => 'l4',
31         'a x' => 'l4',
32         'p j' => 'l2',
33         'j'   => 'l2',
34         'p'   => 'l2',
35         'u'   => 'ex',
36 );
37 my %CSTATUS = (
38         unoff => 'ex', # unofficial
39         wd    => 'l0', # draft
40         pr    => 'l2', # proposed
41         cr    => 'l4', # candidate
42         rec   => 'l5', # recommendation
43         ietf  => 'l6', # standard
44 );
45 my @browsers = qw(trident gecko webkit_saf webkit_chr presto);
46 my %versions;
47 if (my ($somerow) = values %{ $caniuse->{data} }) {
48         while (my ($browser, $row) = each %{ $somerow->{stats} }) {
49                 $versions{$browser} = [ sort { paddedver($a) cmp paddedver($b) } keys %$row ];
50         }
51 }
52
53 my $canihas = do 'browser-usage.inc.pl' or do {
54         printf "<p>Browser usage data not found: <em>%s</em>.</p>\n", $_
55                 for $! || $@;
56 };
57 my $scorediv = (max(map { sum(values %$_) } values %$canihas) // 1) / 100;
58
59 print '<table class="mapped">';
60 print '<col>' x 3;
61 printf '<colgroup span="%d">', scalar @{ $versions{$_} } for @browsers;
62 print "\n";
63
64 my $header = join('',
65         '<tr>',
66         '<th colspan="3">feature',
67         (map {
68                 sprintf('<th colspan="%d" title="%.1f%%">%s',
69                         scalar @{ $versions{$_} },
70                         sum(values %{ $canihas->{$_} }),
71                         do {
72                                 my $name = $caniuse->{agents}->{$_}->{browser};
73                                 length $name < 16 ? $name : $caniuse->{agents}->{$_}->{abbr};
74                         },
75                 )
76         } @browsers),
77         '<th>%',
78 );
79 print '<thead>', $header;
80 # preceding row without any colspan to work around gecko bug
81 print "\n<tr>";
82 print '<td>' x 3;
83 for my $browser (@browsers) {
84         printf('<td title="%.1f%%">%s',
85                 $canihas->{$browser}->{$_}, showversions($_),
86         ) for @{ $versions{$browser} };
87 }
88 print '<td>' x 1;
89 print "</thead>\n";
90
91 sub featurescore {
92         # relative amount of support for given feature
93         state $statspts = { y=>10, 'y x'=>9, a=>5, 'a x'=>5, j=>2, 'p j'=>2, p=>1 };
94         my $rank = 0;
95         if (my $row = shift) {
96                 if ($canihas) {
97                         while (my ($browser, $versions) = each %$row) {
98                                 while (my ($version, $_) = each %$versions) {
99                                         $rank += $canihas->{$browser}->{$version} * $statspts->{$_};
100                                 }
101                         }
102                         return $rank;
103                 }
104
105                 while (my ($browser, $vercols) = each %versions) {
106                         my $div = 0;  # multiplier exponent (decreased to lower value)
107                         my @vers = map { $row->{$browser}->{$_} } @$vercols;
108                         if (my $current = $caniuse->{agents}->{$browser}->{versions}->[-3]) {
109                                 my @future;  # find upcoming releases (after current)
110                                 for (reverse @$vercols) {
111                                         last if $_ eq $current;
112                                         push @future, pop @vers;
113                                         $_ eq 'u' and $_ = $vers[-1] for $future[-1];  # inherit latest value if unknown
114                                 }
115                                 splice @vers, -1, 0, @future;  # move ahead to decrease precedence
116                         }
117                         $rank += $statspts->{$_} * 2**($div--) for reverse @vers;
118                 }
119         }
120         return $rank;
121 }
122
123 for my $id (sort {
124             featurescore($caniuse->{data}->{$b}->{stats})
125         <=> featurescore($caniuse->{data}->{$a}->{stats})
126 } keys %{ $caniuse->{data} }) {
127         my $row = $caniuse->{data}->{$id};
128         my $data = $row->{stats} or next;  # skip metadata [summary]
129         printf '<tr id="%s">', $id;
130         for ($row->{categories}) {
131                 my $cell = $_ ? lc $_->[0] : '-';
132                 print '<th>', $cell;
133         }
134         printf '<td title="%s">%s', $row->{description}, $row->{title};
135         for ($row->{status}) {
136                 my $cell = $_ // '-';
137                 $cell = sprintf '<a href="%s">%s</a>', $_, $cell for $row->{spec} // ();
138                 printf '<td title="%s" class="%s">%s',
139                         $caniuse->{statuses}->{$_}, $CSTATUS{$_} // '', $cell;
140         }
141         for my $browser (@browsers) {
142                 my ($prev, @span);
143                 for my $ver (@{ $versions{$browser} }, undef) {
144                         unless (!defined $prev
145                         or $data->{$browser}->{$prev} ~~ $data->{$browser}->{$ver}) {
146                                 my $usage = sum(map { $canihas->{$browser}->{$_} } @span);
147                                 printf '<td class="%s" colspan="%d" title="%.1f%%">%s',
148                                         join(' ',
149                                                 X => $CSTATS{ $data->{$browser}->{$prev} },
150                                                 sprintf('opacity%.0f', $usage / $scorediv),
151                                         ),
152                                         scalar @span,
153                                         $usage,
154                                         showversions(@span),
155                                 undef $prev;
156                                 @span = ();
157                         }
158                         push @span, $ver;
159                         $prev = $ver;
160                 }
161         }
162         state $maxscore = featurescore({  # yes for every possible version
163                 map { $_ => { map {$_ => 'y'} @{$versions{$_}} } } keys %versions
164         });
165         print '<td>', int featurescore($caniuse->{data}->{$id}->{stats}) / $maxscore * 100;
166 }
167 print '<tfoot>', $header;
168 print '</table>';
169
170 sub paddedver {
171         # normalised version number comparable as string (cmp)
172         shift =~ /^(\d*)(.*)/;
173         return sprintf('%02d', $1) . $2;
174 }
175
176 sub showversions {
177         my @span = ($_[0], @_>1 ? $_[-1] : ());
178         for (@span) {
179                 s/^\./0./;
180                 s/x$/.*/;
181         }
182         return join('‒', @span);
183 }
184
185 :>
186 <script>
187 var classmatch = /\bopacity(\d+)/;
188 Array.forEach(document.getElementsByTagName('TD'), function(val) {
189         var opacity;
190         if (opacity = classmatch.exec(val.className)) {
191                 var c = document.defaultView.getComputedStyle(val, null).getPropertyValue('background-color');
192                 var o = opacity[1] / 100;
193                 val.style.backgroundColor = c.replace(/rgb\((.*)\)/, 'rgba($1, '+o+')');
194                 val.style.textShadow = c+' 1px 1px 2px, ' + c+' -1px -1px 2px';
195         }
196 });
197 alert(t);
198 </script>
199 <hr>
200
201 <div class="legend">
202         <table class="glyphs"><tr>
203         <td class="X l6">supported
204         <td class="X l5">prefixed
205         <td class="X l4">partial
206         <td class="X l2">external (js/plugin)
207         <td class="X l0">missing
208         </table>
209
210         <div class="right">
211                 <ul class="legend legend-set">
212                 <li>default <strong>style</strong> is
213                         <:= defined $get{style} && 'set to ' :><em><:= $style :></em>
214                 </ul>
215         </div>
216 </div>
217