From 068f457f9a60a17ca141d3b90bd7dee9aa7267bc Mon Sep 17 00:00:00 2001 From: Mischa POSLAWSKY Date: Wed, 10 Nov 2010 22:11:58 +0100 Subject: [PATCH] browser: include perl data prepared by converter script Avoid identical and relatively costly Javascript conversion on every run. --- browser.plp | 26 ++-------------------- tools/convert-caniuse.pl | 47 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 24 deletions(-) create mode 100755 tools/convert-caniuse.pl diff --git a/browser.plp b/browser.plp index 330b443..16aad08 100644 --- a/browser.plp +++ b/browser.plp @@ -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 Wikimedia browser usage statistics.

<: -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 index 0000000..7006b18 --- /dev/null +++ b/tools/convert-caniuse.pl @@ -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 + -- 2.30.0