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