browser: support new caniuse data
[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 light'],
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 <div id="browser">
24 <:
25 my $caniuse = do 'browser-support.inc.pl' or die $! || $@;
26
27 my %CSTATS = (
28         'n'   => 'l0',
29         'y'   => 'l6',
30         'y x' => 'l5',
31         'a'   => 'l4',
32         'a x' => 'l4',
33         'p j' => 'l2',
34         'j'   => 'l2',
35         'p'   => 'l2',
36         'u'   => 'l9',
37 );
38 my %CSTATUS = (
39         unoff => 'l0', # unofficial
40         wd    => 'l4', # draft
41         cr    => 'l5', # candidate
42         pr    => 'l5', # proposed
43         rec   => 'l6', # recommendation
44         ietf  => 'l6', # standard
45 );
46 my @browsers = qw(trident gecko webkit_saf webkit_chr presto);
47 my %versions;
48 if (my ($somerow) = values %{ $caniuse->{data} }) {
49         while (my ($browser, $row) = each %{ $somerow->{stats} }) {
50                 $versions{$browser} = [ sort { paddedver($a) cmp paddedver($b) } keys %$row ];
51         }
52 }
53
54 my $canihas = do 'browser-usage.inc.pl' || do {
55         printf "<p>Browser usage data not found: <em>%s</em>.</p>\n", $_
56                 for $! || $@;
57         +{
58                 map {
59                         $_ => +{
60                                 map {
61                                         ($_->[4] =>  0 ), # future
62                                         ($_->[3] =>  .5), # next
63                                         ($_->[0] =>  5 ), # past
64                                         ($_->[1] => 10 ), # previous
65                                         ($_->[2] => 30 ), # current
66                                 } $caniuse->{agents}->{$_}->{versions}
67                         }
68                 } @browsers
69         }; # fallback hash based on release semantics
70 };
71 my $scorediv = (max(map { sum(values %$_) } values %$canihas) // 1) / 100;
72
73 print '<table class="mapped">';
74 print '<col>' x 3;
75 printf '<colgroup span="%d">', scalar @{ $versions{$_} } for @browsers;
76 print "\n";
77
78 my $header = join('',
79         '<tr>',
80         '<th colspan="3">feature',
81         (map {
82                 sprintf('<th colspan="%d" title="%.1f%%">%s',
83                         scalar @{ $versions{$_} },
84                         sum(values %{ $canihas->{$_} }),
85                         do {
86                                 my $name = $caniuse->{agents}->{$_}->{browser};
87                                 length $name < 16 ? $name : $caniuse->{agents}->{$_}->{abbr};
88                         },
89                 )
90         } @browsers),
91         '<th>%',
92 );
93 print '<thead>', $header;
94 # preceding row without any colspan to work around gecko bug
95 print "\n<tr>";
96 print '<td>' x 3;
97 for my $browser (@browsers) {
98         printf('<td title="%.1f%%">%s',
99                 $canihas->{$browser}->{$_}, showversions($_),
100         ) for @{ $versions{$browser} };
101 }
102 print '<td>' x 1;
103 print "</thead>\n";
104
105 sub featurescore {
106         # relative amount of support for given feature
107         state $statspts = { y=>10, 'y x'=>9, a=>5, 'a x'=>5, j=>2, 'p j'=>2, p=>1 };
108         my $rank = 0;
109         if (my $row = shift) {
110                 if ($canihas) {
111                         while (my ($browser, $versions) = each %$row) {
112                                 ref $versions eq 'HASH' or next;
113                                 while (my ($version, $_) = each %$versions) {
114                                         $rank += $canihas->{$browser}->{$version} * $statspts->{$_};
115                                 }
116                         }
117                         return $rank;
118                 }
119
120                 while (my ($browser, $vercols) = each %versions) {
121                         my $div = 0;  # multiplier exponent (decreased to lower value)
122                         my @vers = map { $row->{$browser}->{$_} } @$vercols;
123                         if (my $current = $caniuse->{agents}->{$browser}->{versions}->[-3]) {
124                                 my @future;  # find upcoming releases (after current)
125                                 for (reverse @$vercols) {
126                                         last if $_ eq $current;
127                                         push @future, pop @vers;
128                                         $_ eq 'u' and $_ = $vers[-1] for $future[-1];  # inherit latest value if unknown
129                                 }
130                                 splice @vers, -1, 0, @future;  # move ahead to decrease precedence
131                         }
132                         $rank += $statspts->{$_} * 2**($div--) for reverse @vers;
133                 }
134         }
135         return $rank;
136 }
137
138 for my $id (sort {
139             featurescore($caniuse->{data}->{$b}->{stats})
140         <=> featurescore($caniuse->{data}->{$a}->{stats})
141 } keys %{ $caniuse->{data} }) {
142         my $row = $caniuse->{data}->{$id};
143         my $data = $row->{stats} or next;  # skip metadata [summary]
144         printf '<tr id="%s">', $id;
145         for ($row->{categories}) {
146                 my $cell = $_ ? lc $_->[0] : '-';
147                 print '<th>', $cell;
148         }
149         print '<td>', $row->{title};
150         print '<div class=aside>';
151         s/\.?$/./, print "<p>$_</p>" for map { ref $_ ? @$_ : $_ || () }
152                 $row->{description}, $row->{notes};
153         printf 'Resources: %s.', join(', ',
154                 map { qq(<a href="$_->{url}">$_->{title}</a>) } @$_
155         ) for grep { @$_ } $row->{links} // ();
156         print '</div>';
157         for ($row->{status}) {
158                 my $cell = $_ // '-';
159                 $cell = sprintf '<a href="%s">%s</a>', $_, $cell for $row->{spec} // ();
160                 printf '<td title="%s" class="%s">%s',
161                         $caniuse->{statuses}->{$_}, $CSTATUS{$_} // '', $cell;
162         }
163         for my $browser (@browsers) {
164                 my ($prev, @span);
165                 for my $ver (@{ $versions{$browser} }, undef) {
166                         unless (!defined $prev
167                         or ref $data->{$browser} eq 'HASH'
168                         && $data->{$browser}->{$prev} ~~ $data->{$browser}->{$ver}) {
169                                 my $usage = sum(map { $canihas->{$browser}->{$_} } @span);
170                                 printf '<td class="%s" colspan="%d" title="%.1f%%">%s',
171                                         join(' ',
172                                                 X => $CSTATS{
173                                                         ref $data->{$browser} ne 'HASH' ? 'u' :
174                                                                 $data->{$browser}->{$prev} // 'u'
175                                                 },
176                                                 !$usage ? ('p0') : ('p',
177                                                         sprintf('p%01d', $usage / 10),
178                                                         sprintf('p%02d', $usage),
179                                                 ),
180                                                 sprintf('pp%02d', $usage / $scorediv),
181                                         ),
182                                         scalar @span,
183                                         $usage,
184                                         showversions(@span),
185                                 undef $prev;
186                                 @span = ();
187                         }
188                         push @span, $ver;
189                         $prev = $ver;
190                 }
191         }
192         state $maxscore = featurescore({  # yes for every possible version
193                 map { $_ => { map {$_ => 'y'} @{$versions{$_}} } } keys %versions
194         });
195         print '<td>', int featurescore($caniuse->{data}->{$id}->{stats}) / $maxscore * 100;
196 }
197 print '<tfoot>', $header;
198 print '</table>';
199
200 sub paddedver {
201         # normalised version number comparable as string (cmp)
202         shift =~ /^(\d*)(.*)/;
203         return sprintf('%02d', $1 || 0) . $2;
204 }
205
206 sub showversions {
207         my @span = ($_[0], @_>1 ? $_[-1] : ());
208         for (@span) {
209                 s/^\./0./;
210                 s/x$/.*/;
211         }
212         return join('‒', @span);
213 }
214
215 :></div>
216
217 <hr>
218
219 <div class="legend">
220         <table class="glyphs"><tr>
221         <td class="X l6">supported
222         <td class="X l5">prefixed
223         <td class="X l4">partial
224         <td class="X l2">external (js/plugin)
225         <td class="X l0">missing
226         <td class="X l9">unknown
227         </table>
228
229         <div>
230                 Usage percentage:
231                 <span class="  p0">0</span> -
232                 <span class="p p0 p00">.01</span> -
233                 <span class="p p0 p05">1-9</span> -
234                 <span class="p p1">10</span> -
235                 <span class="p p2">20</span> -
236                 <span class="p p5">majority</span>
237         </div>
238
239         <div class="right">
240                 <ul class="legend legend-set">
241                 <li>default <strong>style</strong> is
242                         <:= defined $get{style} && 'set to ' :><em><:= $style :></em>
243                 </ul>
244         </div>
245 </div>
246