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