7006b18f99b371dbaef57005a524e6c6da214b11
[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         # convert seperate variables to hash keys
15         s/\A/{/;
16         s/^caniuse\.(\w+) = /"$1":/gm;
17         s/;$/,/gm;
18         s/,\s*\Z/\n}/;
19         # fractions not supported by barekey
20         s/(?<=[,{]) (\d*\.\d) (?=:')/"$1"/gx;
21         # escapes not supported in singlequote
22         s{'((?:[^\\']+|\\.)*)'}{
23                 my $_ = $1;
24                 s/"/\\"/g;
25                 s/\\'/'/g;
26                 qq("$_");
27         }ge;
28 }
29
30 my $data = from_json($source, {
31         allow_singlequote => 1,
32         allow_barekey => 1,
33 });
34
35 print pp($data);
36
37 __END__
38
39 =head1 NAME
40
41 convert-caniuse - Turn Javascript data into an equivalent Perl structure
42
43 =head1 SYNOPSIS
44
45         curl http://caniuse.com/js/data.js |
46         tools/convert-caniuse.pl > browser-support.inc.pl
47