16aad08a1a93afb61e6b44df04c947526043ab11
[sheet.git] / browser.plp
1 <(common.inc.plp)><:
2 use 5.010;
3 use List::Util qw(sum max);
4
5 Html({
6         title => 'browser compatibility cheat sheet',
7         version => 'v1.0',
8         description =>
9                 "caniuse.",
10         keywords => [qw'html css browser feature'],
11         stylesheet => [qw'light dark circus mono red'],
12         data => ['browser-support.inc.pl'],
13 });
14
15 :>
16 <h1>Browser compatibility</h1>
17
18 <p>Alternate view of Fyrd's <a href="http://caniuse.com/">when can I use...</a> page
19 with <a href="http://stats.wikimedia.org/archive/squid_reports/">Wikimedia</a>
20 browser usage statistics.</p>
21
22 <:
23 my $caniuse = do 'browser-support.inc.pl' or die $! || $@;
24
25 my %CSTATS = (
26         n => 'di-b',
27         y => 'di-aa',
28         a     => 'di-d',
29         'a x' => 'di-d',
30         j     => 'di-prop',
31         p     => 'di-prop',
32         'p j' => 'di-prop',
33         'y x' => 'di-a',
34 );
35 my %CSTATUS = (
36         unoff => 'di-rare', # unofficial
37         wd    => 'di-b', # draft
38         pr    => 'di-prop', # proposed
39         cr    => 'di-d', # candidate
40         rec   => 'di-a', # recommendation
41         ietf  => 'di-aa', # standard
42 );
43 my @browsers = qw(trident gecko webkit_saf webkit_chr presto);
44 my %versions;
45 if (my ($somerow) = values %{ $caniuse->{data} }) {
46         while (my ($browser, $row) = each %{ $somerow->{stats} }) {
47                 $versions{$browser} = [ sort { paddedver($a) cmp paddedver($b) } keys %$row ];
48         }
49 }
50
51 my $canihas = do 'browser-usage.inc.pl' or do {
52         printf "<p>Browser usage data not found: <em>%s</em>.</p>\n", $_
53                 for $! || $@;
54 };
55 my $scorediv = (max(map { sum(values %$_) } values %$canihas) // 1) / 100;
56
57 print '<table class="mapped">';
58 print '<col>' x 3;
59 printf '<colgroup span="%d">', scalar @{ $versions{$_} } for @browsers;
60 print "\n";
61
62 print '<thead><tr>';
63 print '<th colspan="3">feature';
64 printf('<th colspan="%d">%s',
65         scalar @{ $versions{$_} },
66         do {
67                 my $name = $caniuse->{agents}->{$_}->{browser};
68                 length $name < 16 ? $name : $caniuse->{agents}->{$_}->{abbr};
69         },
70 ) for @browsers;
71 print '<th>%';
72 # preceding row without any colspan to work around gecko bug
73 print "\n<tr>";
74 print '<td>' x 3;
75 for my $browser (@browsers) {
76         printf('<td title="%.1f%%">%s',
77                 $canihas->{$browser}->{$_}, showversions($_),
78         ) for @{ $versions{$browser} };
79 }
80 print '<td>' x 1;
81 print "</thead>\n";
82
83 sub featurescore {
84         # relative amount of support for given feature
85         state $statspts = { y=>10, 'y x'=>9, a=>5, 'a x'=>5, j=>2, 'p j'=>2, p=>1 };
86         my $rank = 0;
87         if (my $row = shift) {
88                 if ($canihas) {
89                         while (my ($browser, $versions) = each %$row) {
90                                 while (my ($version, $_) = each %$versions) {
91                                         $rank += $canihas->{$browser}->{$version} * $statspts->{$_};
92                                 }
93                         }
94                         return $rank;
95                 }
96
97                 while (my ($browser, $vercols) = each %versions) {
98                         my $div = 0;  # multiplier exponent (decreased to lower value)
99                         my @vers = map { $row->{$browser}->{$_} } @$vercols;
100                         if (my $current = $caniuse->{agents}->{$browser}->{versions}->[-3]) {
101                                 my @future;  # find upcoming releases (after current)
102                                 for (reverse @$vercols) {
103                                         last if $_ eq $current;
104                                         push @future, pop @vers;
105                                         $_ eq 'u' and $_ = $vers[-1] for $future[-1];  # inherit latest value if unknown
106                                 }
107                                 splice @vers, -1, 0, @future;  # move ahead to decrease precedence
108                         }
109                         $rank += $statspts->{$_} * 2**($div--) for reverse @vers;
110                 }
111         }
112         return $rank;
113 }
114
115 for my $id (sort {
116             featurescore($caniuse->{data}->{$b}->{stats})
117         <=> featurescore($caniuse->{data}->{$a}->{stats})
118 } keys %{ $caniuse->{data} }) {
119         my $row = $caniuse->{data}->{$id};
120         my $data = $row->{stats} or next;  # skip metadata [summary]
121         printf '<tr id="%s">', $id;
122         for ($row->{categories}) {
123                 my $cell = $_ ? lc $_->[0] : '-';
124                 print '<th>', $cell;
125         }
126         printf '<td title="%s">%s', $row->{description}, $row->{title};
127         for ($row->{status}) {
128                 my $cell = $_ // '-';
129                 $cell = sprintf '<a href="%s">%s</a>', $_, $cell for $row->{spec} // ();
130                 printf '<td title="%s" class="%s">%s',
131                         $caniuse->{statuses}->{$_}, $CSTATUS{$_} // '', $cell;
132         }
133         for my $browser (@browsers) {
134                 my ($prev, @span);
135                 for my $ver (@{ $versions{$browser} }, undef) {
136                         unless (!defined $prev
137                         or $data->{$browser}->{$prev} ~~ $data->{$browser}->{$ver}) {
138                                 my $usage = sum(map { $canihas->{$browser}->{$_} } @span);
139                                 printf '<td class="%s" colspan="%d" title="%.1f%%">%s',
140                                         join(' ',
141                                                 $CSTATS{ $data->{$browser}->{$prev} },
142                                                 sprintf('opacity%.0f', $usage / $scorediv),
143                                         ),
144                                         scalar @span,
145                                         $usage,
146                                         showversions(@span),
147                                 undef $prev;
148                                 @span = ();
149                         }
150                         push @span, $ver;
151                         $prev = $ver;
152                 }
153         }
154         state $maxscore = featurescore({  # yes for every possible version
155                 map { $_ => { map {$_ => 'y'} @{$versions{$_}} } } keys %versions
156         });
157         print '<td>', int featurescore($caniuse->{data}->{$id}->{stats}) / $maxscore * 100;
158 }
159 print '</table>';
160
161 sub paddedver {
162         # normalised version number comparable as string (cmp)
163         shift =~ /^(\d*)(.*)/;
164         return sprintf('%02d', $1) . $2;
165 }
166
167 sub showversions {
168         my @span = ($_[0], @_>1 ? $_[-1] : ());
169         for (@span) {
170                 s/^\./0./;
171                 s/x$/.*/;
172         }
173         return join('‒', @span);
174 }
175
176 :>
177 <hr>
178
179 <div class="legend">
180         <table class="glyphs"><tr>
181         <td class="X di-aa">supported
182         <td class="X di-a">prefixed
183         <td class="X di-d">partial
184         <td class="X di-prop">external (js/plugin)
185         <td class="X di-b">missing
186         </table>
187
188         <div class="right">
189                 <ul class="legend legend-set">
190                 <li>default <strong>style</strong> is
191                         <:= defined $get{style} && 'set to ' :><em><:= $style :></em>
192                 </ul>
193         </div>
194 </div>
195