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