browser: indicate browser usage from separate include
[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         j => 'di-prop',
52         p => 'di-prop',
53         'y x' => 'di-a',
54 );
55 my %CSTATUS = (
56         unoff => 'di-rare', # unofficial
57         wd    => 'di-b', # draft
58         pr    => 'di-prop', # proposed
59         cr    => 'di-d', # candidate
60         rec   => 'di-a', # recommendation
61         ietf  => 'di-aa', # standard
62 );
63 my @browsers = qw(trident gecko webkit_saf webkit_chr presto);
64 my %versions;
65 if (my ($somerow) = values %{ $caniuse->{data} }) {
66         while (my ($browser, $row) = each %{ $somerow->{stats} }) {
67                 $versions{$browser} = [ sort { paddedver($a) cmp paddedver($b) } keys %$row ];
68         }
69 }
70
71 my $canihas = do 'browser-usage.inc.pl';
72 for ($! || $@ || ()) {
73         printf "<p>Browser usage data not found: <em>%s</em>.</p>\n", $_;
74 }
75 my $scorediv = (max(map { sum(values %$_) } values %$canihas) // 1) / 100;
76
77 print '<table class="mapped">';
78 print '<col>' x 3;
79 printf '<colgroup span="%d">', scalar @{ $versions{$_} } for @browsers;
80 print "\n";
81
82 print '<thead><tr>';
83 print '<th colspan="3">feature';
84 printf '<th colspan="%d">%s',
85         scalar @{ $versions{$_} }, $caniuse->{agents}->{$_}->{browser}
86                 for @browsers;
87 # preceding row without any colspan to work around gecko bug
88 print '<tr>';
89 print '<td>' x $_ for 3, (map { scalar @{ $versions{$_} } } @browsers), 1;
90 print "</thead>\n";
91
92 sub featurescore {
93         # relative amount of support for given feature
94         state $statspts = { y=>10, 'y x'=>9, a=>5, j=>2, p=>1 };
95         my $rank = 0;
96         if (my $row = shift) {
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) = @_;
169         splice @$span, 1, -1;
170         for (@$span) {
171                 s/^\./0./;
172                 s/x$/.*/;
173         }
174         return join('‒', @$span);
175 }
176
177 :>
178 <hr>
179
180 <div class="legend">
181         <table class="glyphs"><tr>
182         <td class="X di-aa">supported
183         <td class="X di-a">prefixed
184         <td class="X di-d">partial
185         <td class="X di-prop">external (js/plugin)
186         <td class="X di-b">missing
187         </table>
188
189         <div class="right">
190                 <ul class="legend legend-set">
191                 <li>default <strong>style</strong> is
192                         <:= defined $get{style} && 'set to ' :><em><:= $style :></em>
193                 </ul>
194         </div>
195 </div>
196