browser: drop downloading feature from mkcaniuse
[sheet.git] / tools / mkcaniuse
1 #!/usr/bin/env perl
2 use 5.010;
3 use strict;
4 use warnings;
5
6 use Data::Dump 'pp';
7 use JSON;
8 use File::stat;
9 use Time::Piece;
10
11 our $VERSION = '1.01';
12
13 local $/;  # slurp
14 my $source = readline;
15
16 for ($source) { # cleanup
17         s/\A\(// and s/\);?\s*\Z//;  # empty callback
18         next if /^\{/;  # valid json
19
20         # convert seperate variables to hash keys
21         s/\A/{\n/;
22         s/^caniuse\.(\w+) *= */"$1":/gm;
23         s/;$/,/gm;
24         s/,\s*\Z/\n}/;
25         # fractions not supported by barekey
26         s/(?<=[,{\n]) (\d*\.\d) (?=:['"])/"$1"/gx;
27         # escapes not supported in singlequote
28         s{: *\K'((?:[^\\']+|\\.)*)'}{
29                 my $_ = $1;
30                 s/"/\\"/g;
31                 s/\\'/'/g;
32                 qq("$_");
33         }ge;
34 }
35
36 my $data = from_json($source, {
37         allow_singlequote => 1,
38         allow_barekey => 1,
39 });
40 my $update = eval { stat(${^LAST_FH} // $ARGV)->mtime }
41         or warn "Could not determine input time\n";
42 $data->{-date} = Time::Piece->new($update)->datetime;
43
44 print pp($data);
45
46 __END__
47
48 =head1 NAME
49
50 mkcaniuse - Turn Javascript data into an equivalent Perl structure
51
52 =head1 SYNOPSIS
53
54         tools/mkcaniuse data.json >browser-support.inc.pl
55