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