browser: include perl data prepared by converter script
authorMischa POSLAWSKY <perl@shiar.org>
Wed, 10 Nov 2010 21:11:58 +0000 (22:11 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Wed, 10 Nov 2010 21:40:55 +0000 (22:40 +0100)
Avoid identical and relatively costly Javascript conversion on every run.

browser.plp
tools/convert-caniuse.pl [new file with mode: 0755]

index 330b443c1e944c6614e4087473dd8b7b6c27621a..16aad08a1a93afb61e6b44df04c947526043ab11 100644 (file)
@@ -9,7 +9,7 @@ Html({
                "caniuse.",
        keywords => [qw'html css browser feature'],
        stylesheet => [qw'light dark circus mono red'],
-       data => ['caniuse.js'],
+       data => ['browser-support.inc.pl'],
 });
 
 :>
@@ -20,29 +20,7 @@ with <a href="http://stats.wikimedia.org/archive/squid_reports/">Wikimedia</a>
 browser usage statistics.</p>
 
 <:
-use JSON;
-use File::Slurp 'read_file';
-my $source = read_file('caniuse.js');
-for ($source) { # cleanup
-       # convert seperate variables to hash keys
-       s/\A/{/;
-       s/^caniuse\.(\w+) = /"$1":/gm;
-       s/;$/,/gm;
-       s/,\s*\Z/\n}/;
-       # fractions not supported by barekey
-       s/(?<=[,{]) (\d*\.\d) (?=:')/"$1"/gx;
-       # escapes not supported in singlequote
-       s{'((?:[^\\']+|\\.)*)'}{
-               my $_ = $1;
-               s/"/\\"/g;
-               s/\\'/'/g;
-               qq("$_");
-       }ge;
-}
-my $caniuse = from_json($source, {
-#      allow_singlequote => 1,
-       allow_barekey => 1,
-});
+my $caniuse = do 'browser-support.inc.pl' or die $! || $@;
 
 my %CSTATS = (
        n => 'di-b',
diff --git a/tools/convert-caniuse.pl b/tools/convert-caniuse.pl
new file mode 100755 (executable)
index 0000000..7006b18
--- /dev/null
@@ -0,0 +1,47 @@
+#!/usr/bin/env perl
+use 5.010;
+use strict;
+use warnings;
+
+use Data::Dump 'pp';
+use File::Slurp 'read_file';
+use JSON;
+
+local $/;  # slurp
+my $source = readline;
+
+for ($source) { # cleanup
+       # convert seperate variables to hash keys
+       s/\A/{/;
+       s/^caniuse\.(\w+) = /"$1":/gm;
+       s/;$/,/gm;
+       s/,\s*\Z/\n}/;
+       # fractions not supported by barekey
+       s/(?<=[,{]) (\d*\.\d) (?=:')/"$1"/gx;
+       # escapes not supported in singlequote
+       s{'((?:[^\\']+|\\.)*)'}{
+               my $_ = $1;
+               s/"/\\"/g;
+               s/\\'/'/g;
+               qq("$_");
+       }ge;
+}
+
+my $data = from_json($source, {
+       allow_singlequote => 1,
+       allow_barekey => 1,
+});
+
+print pp($data);
+
+__END__
+
+=head1 NAME
+
+convert-caniuse - Turn Javascript data into an equivalent Perl structure
+
+=head1 SYNOPSIS
+
+       curl http://caniuse.com/js/data.js |
+       tools/convert-caniuse.pl > browser-support.inc.pl
+