browser: explain unknown support style in legend
[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 <div id="browser">
24 <:
25 my $caniuse = do 'browser-support.inc.pl' or die $! || $@;
26
27 my %CSTATS = (
28         'n'   => 'l0',
29         'y'   => 'l6',
30         'y x' => 'l5',
31         'a'   => 'l4',
32         'a x' => 'l4',
33         'p j' => 'l2',
34         'j'   => 'l2',
35         'p'   => 'l2',
36         'u'   => 'l9',
37 );
38 my %CSTATUS = (
39         unoff => 'l0', # unofficial
40         wd    => 'l4', # draft
41         cr    => 'l5', # candidate
42         pr    => 'l5', # proposed
43         rec   => 'l6', # recommendation
44         ietf  => 'l6', # standard
45 );
46 my @browsers = qw(trident gecko webkit_saf webkit_chr presto);
47 my %versions;
48 if (my ($somerow) = values %{ $caniuse->{data} }) {
49         while (my ($browser, $row) = each %{ $somerow->{stats} }) {
50                 $versions{$browser} = [ sort { paddedver($a) cmp paddedver($b) } keys %$row ];
51         }
52 }
53
54 my $canihas = do 'browser-usage.inc.pl' || do {
55         printf "<p>Browser usage data not found: <em>%s</em>.</p>\n", $_
56                 for $! || $@;
57         +{
58                 map {
59                         $_ => +{
60                                 map {
61                                         ($_->[4] =>  0 ), # future
62                                         ($_->[3] =>  .5), # next
63                                         ($_->[0] =>  5 ), # past
64                                         ($_->[1] => 10 ), # previous
65                                         ($_->[2] => 30 ), # current
66                                 } $caniuse->{agents}->{$_}->{versions}
67                         }
68                 } @browsers
69         }; # fallback hash based on release semantics
70 };
71 my $scorediv = (max(map { sum(values %$_) } values %$canihas) // 1) / 100;
72
73 print '<table class="mapped">';
74 print '<col>' x 3;
75 printf '<colgroup span="%d">', scalar @{ $versions{$_} } for @browsers;
76 print "\n";
77
78 my $header = join('',
79         '<tr>',
80         '<th colspan="3">feature',
81         (map {
82                 sprintf('<th colspan="%d" title="%.1f%%">%s',
83                         scalar @{ $versions{$_} },
84                         sum(values %{ $canihas->{$_} }),
85                         do {
86                                 my $name = $caniuse->{agents}->{$_}->{browser};
87                                 length $name < 16 ? $name : $caniuse->{agents}->{$_}->{abbr};
88                         },
89                 )
90         } @browsers),
91         '<th>%',
92 );
93 print '<thead>', $header;
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         print '<td>', $row->{title};
149         print '<div class=aside>';
150         s/\.?$/./, print "<p>$_</p>" for map { ref $_ ? @$_ : $_ || () }
151                 $row->{description}, $row->{notes};
152         printf 'Resources: %s.', join(', ',
153                 map { qq(<a href="$_->{url}">$_->{title}</a>) } @$_
154         ) for $row->{links} // ();
155         print '</div>';
156         for ($row->{status}) {
157                 my $cell = $_ // '-';
158                 $cell = sprintf '<a href="%s">%s</a>', $_, $cell for $row->{spec} // ();
159                 printf '<td title="%s" class="%s">%s',
160                         $caniuse->{statuses}->{$_}, $CSTATUS{$_} // '', $cell;
161         }
162         for my $browser (@browsers) {
163                 my ($prev, @span);
164                 for my $ver (@{ $versions{$browser} }, undef) {
165                         unless (!defined $prev
166                         or $data->{$browser}->{$prev} ~~ $data->{$browser}->{$ver}) {
167                                 my $usage = sum(map { $canihas->{$browser}->{$_} } @span);
168                                 printf '<td class="%s" colspan="%d" title="%.1f%%">%s',
169                                         join(' ',
170                                                 X => $CSTATS{ $data->{$browser}->{$prev} },
171                                                 !$usage ? ('p0') : ('p',
172                                                         sprintf('p%01d', $usage / 10),
173                                                         sprintf('p%02d', $usage),
174                                                 ),
175                                                 sprintf('pp%02d', $usage / $scorediv),
176                                         ),
177                                         scalar @span,
178                                         $usage,
179                                         showversions(@span),
180                                 undef $prev;
181                                 @span = ();
182                         }
183                         push @span, $ver;
184                         $prev = $ver;
185                 }
186         }
187         state $maxscore = featurescore({  # yes for every possible version
188                 map { $_ => { map {$_ => 'y'} @{$versions{$_}} } } keys %versions
189         });
190         print '<td>', int featurescore($caniuse->{data}->{$id}->{stats}) / $maxscore * 100;
191 }
192 print '<tfoot>', $header;
193 print '</table>';
194
195 sub paddedver {
196         # normalised version number comparable as string (cmp)
197         shift =~ /^(\d*)(.*)/;
198         return sprintf('%02d', $1 || 0) . $2;
199 }
200
201 sub showversions {
202         my @span = ($_[0], @_>1 ? $_[-1] : ());
203         for (@span) {
204                 s/^\./0./;
205                 s/x$/.*/;
206         }
207         return join('‒', @span);
208 }
209
210 :></div>
211
212 <hr>
213
214 <div class="legend">
215         <table class="glyphs"><tr>
216         <td class="X l6">supported
217         <td class="X l5">prefixed
218         <td class="X l4">partial
219         <td class="X l2">external (js/plugin)
220         <td class="X l0">missing
221         <td class="X l9">unknown
222         </table>
223
224         <div>
225                 Usage percentage:
226                 <span class="  p0">0</span> -
227                 <span class="p p0 p00">.01</span> -
228                 <span class="p p0 p05">1-9</span> -
229                 <span class="p p1">10</span> -
230                 <span class="p p2">20</span> -
231                 <span class="p p5">majority</span>
232         </div>
233
234         <div class="right">
235                 <ul class="legend legend-set">
236                 <li>default <strong>style</strong> is
237                         <:= defined $get{style} && 'set to ' :><em><:= $style :></em>
238                 </ul>
239         </div>
240 </div>
241