8c24298b66c1579ddafde4f3b848af16956e6cf2
[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'],
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 <:
24 my $caniuse = do 'browser-support.inc.pl' or die $! || $@;
25
26 my %CSTATS = (
27         'n'   => 'l0',
28         'y'   => 'l6',
29         'y x' => 'l5',
30         'a'   => 'l4',
31         'a x' => 'l4',
32         'p j' => 'l2',
33         'j'   => 'l2',
34         'p'   => 'l2',
35         'u'   => 'l9',
36 );
37 my %CSTATUS = (
38         unoff => 'l0', # unofficial
39         wd    => 'l4', # draft
40         cr    => 'l5', # candidate
41         pr    => 'l5', # proposed
42         rec   => 'l6', # recommendation
43         ietf  => 'l6', # standard
44 );
45 my @browsers = qw(trident gecko webkit_saf webkit_chr presto);
46 my %versions;
47 if (my ($somerow) = values %{ $caniuse->{data} }) {
48         while (my ($browser, $row) = each %{ $somerow->{stats} }) {
49                 $versions{$browser} = [ sort { paddedver($a) cmp paddedver($b) } keys %$row ];
50         }
51 }
52
53 my $canihas = do 'browser-usage.inc.pl' or do {
54         printf "<p>Browser usage data not found: <em>%s</em>.</p>\n", $_
55                 for $! || $@;
56 };
57 my $scorediv = (max(map { sum(values %$_) } values %$canihas) // 1) / 100;
58
59 print '<table class="mapped">';
60 print '<col>' x 3;
61 printf '<colgroup span="%d">', scalar @{ $versions{$_} } for @browsers;
62 print "\n";
63
64 my $header = join('',
65         '<tr>',
66         '<th colspan="3">feature',
67         (map {
68                 sprintf('<th colspan="%d" title="%.1f%%">%s',
69                         scalar @{ $versions{$_} },
70                         sum(values %{ $canihas->{$_} }),
71                         do {
72                                 my $name = $caniuse->{agents}->{$_}->{browser};
73                                 length $name < 16 ? $name : $caniuse->{agents}->{$_}->{abbr};
74                         },
75                 )
76         } @browsers),
77         '<th>%',
78 );
79 print '<thead>', $header;
80 # preceding row without any colspan to work around gecko bug
81 print "\n<tr>";
82 print '<td>' x 3;
83 for my $browser (@browsers) {
84         printf('<td title="%.1f%%">%s',
85                 $canihas->{$browser}->{$_}, showversions($_),
86         ) for @{ $versions{$browser} };
87 }
88 print '<td>' x 1;
89 print "</thead>\n";
90
91 sub featurescore {
92         # relative amount of support for given feature
93         state $statspts = { y=>10, 'y x'=>9, a=>5, 'a x'=>5, j=>2, 'p j'=>2, p=>1 };
94         my $rank = 0;
95         if (my $row = shift) {
96                 if ($canihas) {
97                         while (my ($browser, $versions) = each %$row) {
98                                 while (my ($version, $_) = each %$versions) {
99                                         $rank += $canihas->{$browser}->{$version} * $statspts->{$_};
100                                 }
101                         }
102                         return $rank;
103                 }
104
105                 while (my ($browser, $vercols) = each %versions) {
106                         my $div = 0;  # multiplier exponent (decreased to lower value)
107                         my @vers = map { $row->{$browser}->{$_} } @$vercols;
108                         if (my $current = $caniuse->{agents}->{$browser}->{versions}->[-3]) {
109                                 my @future;  # find upcoming releases (after current)
110                                 for (reverse @$vercols) {
111                                         last if $_ eq $current;
112                                         push @future, pop @vers;
113                                         $_ eq 'u' and $_ = $vers[-1] for $future[-1];  # inherit latest value if unknown
114                                 }
115                                 splice @vers, -1, 0, @future;  # move ahead to decrease precedence
116                         }
117                         $rank += $statspts->{$_} * 2**($div--) for reverse @vers;
118                 }
119         }
120         return $rank;
121 }
122
123 for my $id (sort {
124             featurescore($caniuse->{data}->{$b}->{stats})
125         <=> featurescore($caniuse->{data}->{$a}->{stats})
126 } keys %{ $caniuse->{data} }) {
127         my $row = $caniuse->{data}->{$id};
128         my $data = $row->{stats} or next;  # skip metadata [summary]
129         printf '<tr id="%s">', $id;
130         for ($row->{categories}) {
131                 my $cell = $_ ? lc $_->[0] : '-';
132                 print '<th>', $cell;
133         }
134         printf '<td title="%s">%s', $row->{description}, $row->{title};
135         for ($row->{status}) {
136                 my $cell = $_ // '-';
137                 $cell = sprintf '<a href="%s">%s</a>', $_, $cell for $row->{spec} // ();
138                 printf '<td title="%s" class="%s">%s',
139                         $caniuse->{statuses}->{$_}, $CSTATUS{$_} // '', $cell;
140         }
141         for my $browser (@browsers) {
142                 my ($prev, @span);
143                 for my $ver (@{ $versions{$browser} }, undef) {
144                         unless (!defined $prev
145                         or $data->{$browser}->{$prev} ~~ $data->{$browser}->{$ver}) {
146                                 my $usage = sum(map { $canihas->{$browser}->{$_} } @span);
147                                 printf '<td class="%s" colspan="%d" title="%.1f%%">%s',
148                                         join(' ',
149                                                 X => $CSTATS{ $data->{$browser}->{$prev} },
150                                                 !$usage ? ('p0') : ('p',
151                                                         sprintf('p%01d', $usage / 10),
152                                                         sprintf('p%02d', $usage),
153                                                 ),
154                                                 sprintf('pp%02d', $usage / $scorediv),
155                                         ),
156                                         scalar @span,
157                                         $usage,
158                                         showversions(@span),
159                                 undef $prev;
160                                 @span = ();
161                         }
162                         push @span, $ver;
163                         $prev = $ver;
164                 }
165         }
166         state $maxscore = featurescore({  # yes for every possible version
167                 map { $_ => { map {$_ => 'y'} @{$versions{$_}} } } keys %versions
168         });
169         print '<td>', int featurescore($caniuse->{data}->{$id}->{stats}) / $maxscore * 100;
170 }
171 print '<tfoot>', $header;
172 print '</table>';
173
174 sub paddedver {
175         # normalised version number comparable as string (cmp)
176         shift =~ /^(\d*)(.*)/;
177         return sprintf('%02d', $1) . $2;
178 }
179
180 sub showversions {
181         my @span = ($_[0], @_>1 ? $_[-1] : ());
182         for (@span) {
183                 s/^\./0./;
184                 s/x$/.*/;
185         }
186         return join('‒', @span);
187 }
188
189 :>
190 <script>
191 var classmatch = / p(\d{2})/;
192 Array.forEach(document.getElementsByTagName('TD'), function(val) {
193         var opacity;
194         if (opacity = classmatch.exec(val.className)) {
195                 var c = document.defaultView.getComputedStyle(val, null).getPropertyValue('background-color');
196                 var o = opacity[1] / 100;
197                 val.style.backgroundColor = c.replace(/rgb\((.*)\)/, 'rgba($1, '+o+')');
198                 val.style.textShadow = c+' 1px 1px 2px, ' + c+' -1px -1px 2px';
199         }
200 });
201 </script>
202 <hr>
203
204 <div class="legend">
205         <table class="glyphs"><tr>
206         <td class="X l6">supported
207         <td class="X l5">prefixed
208         <td class="X l4">partial
209         <td class="X l2">external (js/plugin)
210         <td class="X l0">missing
211         </table>
212
213         <div>
214                 Usage percentage:
215                 <span class="  p0">0</span> -
216                 <span class="p p0 p00">.01</span> -
217                 <span class="p p0 p05">1-9</span> -
218                 <span class="p p1">10</span> -
219                 <span class="p p2">20</span> -
220                 <span class="p p5">majority</span>
221         </div>
222
223         <div class="right">
224                 <ul class="legend legend-set">
225                 <li>default <strong>style</strong> is
226                         <:= defined $get{style} && 'set to ' :><em><:= $style :></em>
227                 </ul>
228         </div>
229 </div>
230