termcol: monospaced terminal samples
[sheet.git] / browser.plp
index 3dc8d806eda37a7bb4c57c57fdeb377f99ee3a2c..4ed3f1b8b8fc2f8bbc9ee2da6ed874588bf50415 100644 (file)
@@ -8,7 +8,11 @@ Html({
        version => 'v1.0',
        description =>
                "caniuse.",
-       keywords => [qw'html css browser feature'],
+       keywords => [qw'
+               web browser support compatibility usage available feature
+               html html5 css css3 svg javascript js dom mobile
+               ie internet explorer firefox chrome safari webkit opera
+       '],
        stylesheet => [qw'circus dark mono red light'],
        data => ['browser-support.inc.pl'],
 });
@@ -39,7 +43,6 @@ given ($get{usage} // 'wm') {
 :>.
 </p>
 
-<div id="browser">
 <:
 my $caniuse = do 'browser-support.inc.pl' or die $! || $@;
 $_->{verrelease} = {
@@ -59,6 +62,20 @@ my %CSTATS = (
        'p p' => 'l2',
        'u'   => 'l0',
 );
+my %DSTATS = (
+       u => 'unknown',
+       n => 'unsupported',
+       p => 'plugin required',
+       j => 'javascript required',
+       a => 'partial',
+       y => 'supported',
+       x => sub {
+               join(' ',
+                       'requires prefix',
+                       (map "-$_-", $caniuse->{agents}->{$_[0]}->{prefix} // ()),
+               );
+       },
+);
 my %CSTATUS = (
        unoff => 'l1', # unofficial
        wd    => 'l3', # draft
@@ -94,17 +111,20 @@ $canihas ||= {
 my $scorediv = (max(map { ref $_ eq 'HASH' && sum(values %$_) } values %$canihas) // 1) / 100;
 
 print '<table class="mapped">';
-print '<col>' x 3;
+print '<col span="3">';  # should match first thead row
 printf '<colgroup span="%d">', scalar @{ $versions{$_} } for @browsers;
-print "\n";
+say '</colgroup><col>';
 
 my $header = join('',
        '<tr>',
        '<th colspan="3">feature',
        (map {
                my $name = $caniuse->{agents}->{$_}->{browser};
-               sprintf('<th colspan="%d" title="%s">%s',
+               sprintf('<th colspan="%d" class="%s" title="%s">%s',
                        scalar @{ $versions{$_} },
+                       join(' ', map {"b-a-$_"} grep {$_}
+                               $_, @{ $caniuse->{agents}->{$_} }{'prefix', 'type'},
+                       ),
                        join(' ',
                                sprintf('%.1f%%', sum(values %{ $canihas->{$_} })),
                                $name,
@@ -122,8 +142,11 @@ print '<thead>', $header;
 print "\n<tr>";
 print '<td>' x 3;
 for my $browser (@browsers) {
-       printf('<td title="%.1f%%"%s>%s',
-               $canihas->{$browser}->{$_},
+       printf('<td title="%s"%s>%s',
+               join(' ',
+                       sprintf('%.1f%%', $canihas->{$browser}->{$_}),
+                       "version $_",
+               ),
                (map {
                        defined $_ && !$_ && ' class="ex"'
                } $caniuse->{agents}->{$browser}->{verrelease}->{$_}),
@@ -131,18 +154,19 @@ for my $browser (@browsers) {
        ) for @{ $versions{$browser} };
 }
 print '<td>' x 1;
-print "</thead>\n";
+say '</thead>';
+say '<tfoot>', $header, '</tfoot>';
 
 sub featurescore {
        # relative amount of support for given feature
-       state $statspts = { y=>10, 'y x'=>10, a=>5, 'a x'=>5, j=>2, 'p j'=>2, p=>1 };
+       state $statspts = { y=>10, 'y x'=>10, a=>5, 'a x'=>5, j=>2, 'p j'=>2, 'p p'=>2, p=>1 };
        my $rank = 0;
        if (my $row = shift) {
                if ($canihas) {
                        while (my ($browser, $versions) = each %$row) {
                                ref $versions eq 'HASH' or next;
                                while (my ($version, $_) = each %$versions) {
-                                       $rank += $canihas->{$browser}->{$version} * $statspts->{$_};
+                                       $rank += ($canihas->{$browser}->{$version} || .001) * $statspts->{$_};
                                }
                        }
                        return $rank;
@@ -172,7 +196,8 @@ sub saytitlecol {
 
        for ($row->{categories}) {
                my $cell = $_ ? lc $_->[0] : '-';
-               print '<th>', $cell;
+               $cell =~ s/ api$//;  # trim unessential fluff in 'js api'
+               printf '<th title="%s">%s', join(' + ', @$_), $cell;
        }
 
        print '<td>', map {
@@ -181,15 +206,15 @@ sub saytitlecol {
                        sprintf("try { %s; return false } catch(err) { return true }",
                                "document.getElementById('$id').classList.toggle('target')",
                        ),
-                       $_,
+                       Entity($_),
                );
        } $row->{title};
        print '<div class=aside>';
        s/\.?$/./, print "<p>$_</p>" for map { ref $_ ? @$_ : $_ || () }
-               $row->{description}, $row->{notes};
-       printf 'Resources: %s.', join(', ',
-               map { qq(<a href="$_->{url}">$_->{title}</a>) } @$_
-       ) for grep { @$_ } $row->{links} // ();
+               Entity($row->{description}), formathtml($row->{notes});  # sic
+       printf 'Resources: %s.', join(', ', map {
+               sprintf '<a href="%s">%s</a>', EscapeHTML($_->{url}), $_->{title}
+       } @$_) for grep { @$_ } $row->{links} // ();
        print '</div>';
 }
 
@@ -214,7 +239,7 @@ sub saybrowsercols {
                unless (!defined $prev
                or ref $data eq 'HASH' && $data->{$prev} ~~ $data->{$ver}) {
                        my $usage = sum(map { $canihas->{$browser}->{$_} } @span);
-                       printf '<td class="%s" colspan="%d" title="%.1f%%">%s',
+                       printf '<td class="%s" colspan="%d" title="%s">%s',
                                join(' ',
                                        X => $CSTATS{ ref $data eq 'HASH' && $data->{$prev} || 'u' },
                                        !$usage ? ('p0') : ('p',
@@ -224,7 +249,12 @@ sub saybrowsercols {
                                        sprintf('pp%02d', $usage / $scorediv),
                                ),
                                scalar @span,
-                               $usage,
+                               sprintf('%.1f%% %s', $usage, join(', ',
+                                       map { ref $_ eq 'CODE' ? $_->($browser) : $_ }
+                                       map { $DSTATS{$_} // () }
+                                       map { split / /, $_ }
+                                       ref $data eq 'HASH' && $data->{$prev} || 'u'
+                               )),
                                showversions(@span),
                        undef $prev;
                        @span = ();
@@ -242,6 +272,7 @@ sub sayusagecol {
        print '<td>', int featurescore($caniuse->{data}->{$id}->{stats}) / $maxscore * 100;
 }
 
+say '<tbody>';
 for my $id (sort {
            featurescore($caniuse->{data}->{$b}->{stats})
        <=> featurescore($caniuse->{data}->{$a}->{stats})
@@ -254,8 +285,18 @@ for my $id (sort {
        sayusagecol($id);
        say '</tr>';
 }
-print '<tfoot>', $header;
-print '</table>';
+say '</tbody>';
+say '</table>';
+
+sub formathtml {
+       my $ref = defined wantarray ? [@_] : \@_;
+       for (@$ref) {
+               s/& (?!\w)/&amp;/gx;
+               s/< \s/&lt;/gx;
+               s/\n\K\n/<br>/g;
+       }
+       return @$ref;
+}
 
 sub paddedver {
        # normalised version number comparable as string (cmp)
@@ -272,8 +313,7 @@ sub showversions {
        return join('‒', @span);
 }
 
-:></div>
-
+:>
 <hr>
 
 <div class="legend">
@@ -313,6 +353,6 @@ sub showversions {
        </div>
 </div>
 
-<script src="/searchlocal.js"></script>
-<script> prependsearch(document.getElementById('intro')) </script>
+<script type="text/javascript" src="/searchlocal.js"></script>
+<script type="text/javascript"> prependsearch(document.getElementById('intro')) </script>