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