browser: abbreviate browser names, version numbers
[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 => ['caniuse.js'],
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 use JSON;
24 use File::Slurp 'read_file';
25 my $source = read_file('caniuse.js');
26 for ($source) { # cleanup
27         # convert seperate variables to hash keys
28         s/\A/{/;
29         s/^caniuse\.(\w+) = /"$1":/gm;
30         s/;$/,/gm;
31         s/,\s*\Z/\n}/;
32         # fractions not supported by barekey
33         s/(?<=[,{]) (\d*\.\d) (?=:')/"$1"/gx;
34         # escapes not supported in singlequote
35         s{'((?:[^\\']+|\\.)*)'}{
36                 my $_ = $1;
37                 s/"/\\"/g;
38                 s/\\'/'/g;
39                 qq("$_");
40         }ge;
41 }
42 my $caniuse = from_json($source, {
43 #       allow_singlequote => 1,
44         allow_barekey => 1,
45 });
46
47 my %CSTATS = (
48         n => 'di-b',
49         y => 'di-aa',
50         a     => 'di-d',
51         'a x' => 'di-d',
52         j     => 'di-prop',
53         p     => 'di-prop',
54         'p j' => 'di-prop',
55         'y x' => 'di-a',
56 );
57 my %CSTATUS = (
58         unoff => 'di-rare', # unofficial
59         wd    => 'di-b', # draft
60         pr    => 'di-prop', # proposed
61         cr    => 'di-d', # candidate
62         rec   => 'di-a', # recommendation
63         ietf  => 'di-aa', # standard
64 );
65 my @browsers = qw(trident gecko webkit_saf webkit_chr presto);
66 my %versions;
67 if (my ($somerow) = values %{ $caniuse->{data} }) {
68         while (my ($browser, $row) = each %{ $somerow->{stats} }) {
69                 $versions{$browser} = [ sort { paddedver($a) cmp paddedver($b) } keys %$row ];
70         }
71 }
72
73 my $canihas = do 'browser-usage.inc.pl' or do {
74         printf "<p>Browser usage data not found: <em>%s</em>.</p>\n", $_
75                 for $! || $@;
76 };
77 my $scorediv = (max(map { sum(values %$_) } values %$canihas) // 1) / 100;
78
79 print '<table class="mapped">';
80 print '<col>' x 3;
81 printf '<colgroup span="%d">', scalar @{ $versions{$_} } for @browsers;
82 print "\n";
83
84 print '<thead><tr>';
85 print '<th colspan="3">feature';
86 printf('<th colspan="%d">%s',
87         scalar @{ $versions{$_} },
88         do {
89                 my $name = $caniuse->{agents}->{$_}->{browser};
90                 length $name < 16 ? $name : $caniuse->{agents}->{$_}->{abbr};
91         },
92 ) for @browsers;
93 print '<th>%';
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                                 while (my ($version, $_) = each %$versions) {
113                                         $rank += $canihas->{$browser}->{$version} * $statspts->{$_};
114                                 }
115                         }
116                         return $rank;
117                 }
118
119                 while (my ($browser, $vercols) = each %versions) {
120                         my $div = 0;  # multiplier exponent (decreased to lower value)
121                         my @vers = map { $row->{$browser}->{$_} } @$vercols;
122                         if (my $current = $caniuse->{agents}->{$browser}->{versions}->[-3]) {
123                                 my @future;  # find upcoming releases (after current)
124                                 for (reverse @$vercols) {
125                                         last if $_ eq $current;
126                                         push @future, pop @vers;
127                                         $_ eq 'u' and $_ = $vers[-1] for $future[-1];  # inherit latest value if unknown
128                                 }
129                                 splice @vers, -1, 0, @future;  # move ahead to decrease precedence
130                         }
131                         $rank += $statspts->{$_} * 2**($div--) for reverse @vers;
132                 }
133         }
134         return $rank;
135 }
136
137 for my $id (sort {
138             featurescore($caniuse->{data}->{$b}->{stats})
139         <=> featurescore($caniuse->{data}->{$a}->{stats})
140 } keys %{ $caniuse->{data} }) {
141         my $row = $caniuse->{data}->{$id};
142         my $data = $row->{stats} or next;  # skip metadata [summary]
143         printf '<tr id="%s">', $id;
144         for ($row->{categories}) {
145                 my $cell = $_ ? lc $_->[0] : '-';
146                 print '<th>', $cell;
147         }
148         printf '<td title="%s">%s', $row->{description}, $row->{title};
149         for ($row->{status}) {
150                 my $cell = $_ // '-';
151                 $cell = sprintf '<a href="%s">%s</a>', $_, $cell for $row->{spec} // ();
152                 printf '<td title="%s" class="%s">%s',
153                         $caniuse->{statuses}->{$_}, $CSTATUS{$_} // '', $cell;
154         }
155         for my $browser (@browsers) {
156                 my ($prev, @span);
157                 for my $ver (@{ $versions{$browser} }, undef) {
158                         unless (!defined $prev
159                         or $data->{$browser}->{$prev} ~~ $data->{$browser}->{$ver}) {
160                                 my $usage = sum(map { $canihas->{$browser}->{$_} } @span);
161                                 printf '<td class="%s" colspan="%d" title="%.1f%%">%s',
162                                         join(' ',
163                                                 $CSTATS{ $data->{$browser}->{$prev} },
164                                                 sprintf('opacity%.0f', $usage / $scorediv),
165                                         ),
166                                         scalar @span,
167                                         $usage,
168                                         showversions(@span),
169                                 undef $prev;
170                                 @span = ();
171                         }
172                         push @span, $ver;
173                         $prev = $ver;
174                 }
175         }
176         state $maxscore = featurescore({  # yes for every possible version
177                 map { $_ => { map {$_ => 'y'} @{$versions{$_}} } } keys %versions
178         });
179         print '<td>', int featurescore($caniuse->{data}->{$id}->{stats}) / $maxscore * 100;
180 }
181 print '</table>';
182
183 sub paddedver {
184         # normalised version number comparable as string (cmp)
185         shift =~ /^(\d*)(.*)/;
186         return sprintf('%02d', $1) . $2;
187 }
188
189 sub showversions {
190         my @span = ($_[0], @_>1 ? $_[-1] : ());
191         for (@span) {
192                 s/^\./0./;
193                 s/x$/.*/;
194         }
195         return join('‒', @span);
196 }
197
198 :>
199 <hr>
200
201 <div class="legend">
202         <table class="glyphs"><tr>
203         <td class="X di-aa">supported
204         <td class="X di-a">prefixed
205         <td class="X di-d">partial
206         <td class="X di-prop">external (js/plugin)
207         <td class="X di-b">missing
208         </table>
209
210         <div class="right">
211                 <ul class="legend legend-set">
212                 <li>default <strong>style</strong> is
213                         <:= defined $get{style} && 'set to ' :><em><:= $style :></em>
214                 </ul>
215         </div>
216 </div>
217