ef260e118401dd5b81649c397d4b7646801348fe
[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 id="intro">Alternate view of Fyrd's <a href="http://caniuse.com/">when can I use...</a> page<:
20 my ($canihas, $usage);
21 given ($get{usage} // 'wm') {
22         when (!$_) {
23                 # none
24         }
25         when (!/^\w+$/) {
26                 printf "<p>Invalid browser usage data request: <em>%s</em>",
27                         'identifier must be alphanumeric name or <q>0</q>';
28         }
29         $canihas = do "browser-usage-$_.inc.pl" or do {
30                 printf "<p>Browser usage data not found: <em>%s</em>", $! || $@;
31                 break;
32         };
33         $usage = $_;
34         my $ref = $canihas->{-source} || 'unknown';
35         $ref = sprintf '<a href="%s">%s</a>', $_, $ref for $canihas->{-url} || ();
36         $ref .= " $_" for $canihas->{-date} || ();
37         print "\nwith $ref browser usage statistics";
38 }
39 :>.
40 </p>
41
42 <:
43 my $caniuse = do 'browser-support.inc.pl' or die $! || $@;
44 $_->{verrelease} = {
45         # mark last two (future) versions as unreleased, ensure current isn't
46         map { $_->[-1] => 0, $_->[-2] => 0, $_->[-3] => undef } $_->{versions}
47 } for values %{ $caniuse->{agents} };
48
49 my %CSTATS = (
50         'n'   => 'l1',
51         'y'   => 'l5',
52         'y x' => 'l5 ex',
53         'a'   => 'l3',
54         'a x' => 'l3 ex',
55         'p j' => 'l2',
56         'j'   => 'l2',
57         'p'   => 'l2',
58         'p p' => 'l2',
59         'u'   => 'l0',
60 );
61 my %DSTATS = (
62         u => 'unknown',
63         n => 'unsupported',
64         p => 'plugin required',
65         j => 'javascript required',
66         a => 'partial',
67         y => 'supported',
68         x => sub {
69                 join(' ',
70                         'requires prefix',
71                         (map "-$_-", $caniuse->{agents}->{$_[0]}->{prefix} // ()),
72                 );
73         },
74 );
75 my %CSTATUS = (
76         unoff => 'l1', # unofficial
77         wd    => 'l3', # draft
78         cr    => 'l4', # candidate
79         pr    => 'l4', # proposed
80         rec   => 'l5', # recommendation
81         other => 'l2', # non-w3
82         ietf  => 'l5', # standard
83 );
84 my %versions;
85 if (my ($somerow) = values %{ $caniuse->{data} }) {
86         while (my ($browser, $row) = each %{ $somerow->{stats} }) {
87                 $versions{$browser} = [ sort { paddedver($a) cmp paddedver($b) } keys %$row ];
88         }
89 }
90 my @browsers = grep { $versions{$_} }
91         qw(trident gecko webkit_saf ios_saf webkit_chr android presto op_mob op_mini);
92
93 $canihas ||= {
94         map {
95                 $_ => +{
96                         map {
97                                 my $zero = $#$_ - 2;  # baseline index
98                                 ($_->[$zero - 2] =>  .5), # past
99                                 ($_->[$zero - 1] => 10 ), # previous
100                                 ($_->[$zero + 2] =>  0 ), # future
101                                 ($_->[$zero + 1] =>  .5), # next
102                                 ($_->[$zero    ] => 30 ), # current
103                         } $caniuse->{agents}->{$_}->{versions}
104                 }
105         } @browsers
106 }; # fallback hash based on release semantics
107 my $scorediv = (max(map { ref $_ eq 'HASH' && sum(values %$_) } values %$canihas) // 1) / 100;
108
109 print '<table class="mapped">';
110 print '<col>' x 3;
111 printf '<colgroup span="%d">', scalar @{ $versions{$_} } for @browsers;
112 print "\n";
113
114 my $header = join('',
115         '<tr>',
116         '<th colspan="3">feature',
117         (map {
118                 my $name = $caniuse->{agents}->{$_}->{browser};
119                 sprintf('<th colspan="%d" title="%s">%s',
120                         scalar @{ $versions{$_} },
121                         join(' ',
122                                 sprintf('%.1f%%', sum(values %{ $canihas->{$_} })),
123                                 $name,
124                         ),
125                         do {
126                                 length $name < 3 + @{ $versions{$_} }*2 ? $name
127                                         : $caniuse->{agents}->{$_}->{abbr};
128                         },
129                 )
130         } @browsers),
131         '<th>%',
132 );
133 print '<thead>', $header;
134 # preceding row without any colspan to work around gecko bug
135 print "\n<tr>";
136 print '<td>' x 3;
137 for my $browser (@browsers) {
138         printf('<td title="%s"%s>%s',
139                 join(' ',
140                         sprintf('%.1f%%', $canihas->{$browser}->{$_}),
141                         "version $_",
142                 ),
143                 (map {
144                         defined $_ && !$_ && ' class="ex"'
145                 } $caniuse->{agents}->{$browser}->{verrelease}->{$_}),
146                 showversions($_),
147         ) for @{ $versions{$browser} };
148 }
149 print '<td>' x 1;
150 say '</thead>';
151 say '<tfoot>', $header, '</tfoot>';
152
153 sub featurescore {
154         # relative amount of support for given feature
155         state $statspts = { y=>10, 'y x'=>10, a=>5, 'a x'=>5, j=>2, 'p j'=>2, 'p p'=>2, p=>1 };
156         my $rank = 0;
157         if (my $row = shift) {
158                 if ($canihas) {
159                         while (my ($browser, $versions) = each %$row) {
160                                 ref $versions eq 'HASH' or next;
161                                 while (my ($version, $_) = each %$versions) {
162                                         $rank += ($canihas->{$browser}->{$version} || .001) * $statspts->{$_};
163                                 }
164                         }
165                         return $rank;
166                 }
167
168                 while (my ($browser, $vercols) = each %versions) {
169                         my $div = 0;  # multiplier exponent (decreased to lower value)
170                         my @vers = map { $row->{$browser}->{$_} } @$vercols;
171                         if (my $current = $caniuse->{agents}->{$browser}->{versions}->[-3]) {
172                                 my @future;  # find upcoming releases (after current)
173                                 for (reverse @$vercols) {
174                                         last if $_ eq $current;
175                                         push @future, pop @vers;
176                                         $_ eq 'u' and $_ = $vers[-1] for $future[-1];  # inherit latest value if unknown
177                                 }
178                                 splice @vers, -1, 0, @future;  # move ahead to decrease precedence
179                         }
180                         $rank += $statspts->{$_} * 2**($div--) for reverse @vers;
181                 }
182         }
183         return $rank;
184 }
185
186 sub saytitlecol {
187         my ($id) = @_;
188         my $row = $caniuse->{data}->{$id};
189
190         for ($row->{categories}) {
191                 my $cell = $_ ? lc $_->[0] : '-';
192                 printf '<th title="%s">%s', join(' + ', @$_), $cell;
193         }
194
195         print '<td>', map {
196                 sprintf('<a href="%s" onclick="%s">%s</a>',
197                         "#$id",
198                         sprintf("try { %s; return false } catch(err) { return true }",
199                                 "document.getElementById('$id').classList.toggle('target')",
200                         ),
201                         Entity($_),
202                 );
203         } $row->{title};
204         print '<div class=aside>';
205         s/\.?$/./, print "<p>$_</p>" for map { ref $_ ? @$_ : $_ || () }
206                 Entity($row->{description}), formathtml($row->{notes});  # sic
207         printf 'Resources: %s.', join(', ', map {
208                 sprintf '<a href="%s">%s</a>', EscapeHTML($_->{url}), $_->{title}
209         } @$_) for grep { @$_ } $row->{links} // ();
210         print '</div>';
211 }
212
213 sub saystatuscol {
214         my ($id) = @_;
215         my $row = $caniuse->{data}->{$id};
216
217         for ($row->{status}) {
218                 my $cell = $_ // '-';
219                 $cell = sprintf '<a href="%s">%s</a>', $_, $cell for $row->{spec} // ();
220                 printf '<td title="%s" class="l %s">%s',
221                         $caniuse->{statuses}->{$_}, $CSTATUS{$_} // '', $cell;
222         }
223 }
224
225 sub saybrowsercols {
226         my ($id, $browser) = @_;
227         my $data = $caniuse->{data}->{$id}->{stats}->{$browser};
228
229         my ($prev, @span);
230         for my $ver (@{ $versions{$browser} }, undef) {
231                 unless (!defined $prev
232                 or ref $data eq 'HASH' && $data->{$prev} ~~ $data->{$ver}) {
233                         my $usage = sum(map { $canihas->{$browser}->{$_} } @span);
234                         printf '<td class="%s" colspan="%d" title="%s">%s',
235                                 join(' ',
236                                         X => $CSTATS{ ref $data eq 'HASH' && $data->{$prev} || 'u' },
237                                         !$usage ? ('p0') : ('p',
238                                                 sprintf('p%01d', $usage / 10),
239                                                 sprintf('p%02d', $usage),
240                                         ),
241                                         sprintf('pp%02d', $usage / $scorediv),
242                                 ),
243                                 scalar @span,
244                                 sprintf('%.1f%% %s', $usage, join(', ',
245                                         map { ref $_ eq 'CODE' ? $_->($browser) : $_ }
246                                         map { $DSTATS{$_} // () }
247                                         map { split / /, $_ }
248                                         ref $data eq 'HASH' && $data->{$prev} || 'u'
249                                 )),
250                                 showversions(@span),
251                         undef $prev;
252                         @span = ();
253                 }
254                 push @span, $ver;
255                 $prev = $ver;
256         }
257 }
258
259 sub sayusagecol {
260         my ($id) = @_;
261         state $maxscore = featurescore({  # yes for every possible version
262                 map { $_ => { map {$_ => 'y'} @{$versions{$_}} } } keys %versions
263         });
264         print '<td>', int featurescore($caniuse->{data}->{$id}->{stats}) / $maxscore * 100;
265 }
266
267 say '<tbody>';
268 for my $id (sort {
269             featurescore($caniuse->{data}->{$b}->{stats})
270         <=> featurescore($caniuse->{data}->{$a}->{stats})
271 } keys %{ $caniuse->{data} }) {
272         $caniuse->{data}->{$id}->{stats} or next;  # skip metadata [summary]
273         printf '<tr id="%s">', $id;
274         saytitlecol($id);
275         saystatuscol($id);
276         saybrowsercols($id, $_) for @browsers;
277         sayusagecol($id);
278         say '</tr>';
279 }
280 say '</tbody>';
281 say '</table>';
282
283 sub formathtml {
284         my $ref = defined wantarray ? [@_] : \@_;
285         for (@$ref) {
286                 s/& (?!\w)/&amp;/gx;
287                 s/< \s/&lt;/gx;
288                 s/\n\K\n/<br>/g;
289         }
290         return @$ref;
291 }
292
293 sub paddedver {
294         # normalised version number comparable as string (cmp)
295         shift =~ /^(\d*)(.*)/;
296         return sprintf('%02d', $1 || 0) . $2;
297 }
298
299 sub showversions {
300         my @span = ($_[0], @_>1 ? $_[-1] : ());
301         for (@span) {
302                 s/^\./0./;
303                 s/x$/.*/;
304         }
305         return join('‒', @span);
306 }
307
308 :>
309 <hr>
310
311 <div class="legend">
312         <table class="glyphs"><tr>
313         <td class="X l5">supported
314         <td class="X l3">partial
315         <td class="X l2">external (js/plugin)
316         <td class="X l1">missing
317         <td class="X l0">unknown
318         <td class="X ex">prefixed
319         </table>
320
321         <p><: if ($usage) { :>
322                 Usage percentage:
323                 <span class="  p0">0</span> -
324                 <span class="p p0 p00">.01</span> -
325                 <span class="p p0 p05">1-9</span> -
326                 <span class="p p1">10</span> -
327                 <span class="p p2">20</span> -
328                 <span class="p p5">majority</span>
329 <: } else { :>
330                 <table class="glyphs"><tr>
331                         <td class="p p1">previous version</td>
332                         <td class="p p3">current</td>
333                         <td class="p p0 p00">upcoming (within months)</td>
334                         <td class="  p0">future (within a year)</td>
335                 </table>
336 <: } :> </p>
337
338         <div class="right">
339                 <ul class="legend legend-set">
340                 <li>default <strong>style</strong> is
341                         <:= defined $get{style} && 'set to ' :><em><:= $style :></em>
342                 <li><strong>usage</strong> source is
343                         <:= !defined $get{usage} && 'default ' :><:= defined $usage ? "<em>$usage</em>" : 'not included (<em>0</em>)' :>
344                 </ul>
345         </div>
346 </div>
347
348 <script type="text/javascript" src="/searchlocal.js"></script>
349 <script type="text/javascript"> prependsearch(document.getElementById('intro')) </script>
350