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