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