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