CSS-Prepare
view release on metacpan or search on metacpan
lib/CSS/Prepare.pm view on Meta::CPAN
return $self->read_file( $target );
}
sub read_file {
my $self = shift;
my $file = shift;
my $handle = FileHandle->new( $file );
if ( defined $handle ) {
local $/;
return <$handle>;
}
return;
}
sub read_url {
my $self = shift;
my $url = shift;
my $provider = $self->get_http_provider();
given ( $provider ) {
when ( 'lite' ) { return $self->get_url_lite( $url ); }
when ( 'lwp' ) { return $self->get_url_lwp( $url ); }
}
return;
}
sub get_url_lite {
my $self = shift;
my $url = shift;
my $depth = shift // 1;
# don't follow infinite redirections
return unless $depth <= MAX_REDIRECT;
my $http = HTTP::Lite->new();
$http->{'timeout'} = $self->get_http_timeout;
my $code = $http->request( $url );
given ( $code ) {
when ( 200 ) { return $http->body(); }
when ( 301 || 302 || 303 || 307 ) {
my $location = $http->get_header( 'Location' );
return $self->get_url_lite( $location, $depth+1 );
}
default { return; }
}
}
sub get_url_lwp {
my $self = shift;
my $url = shift;
my $http = LWP::UserAgent->new( max_redirect => MAX_REDIRECT );
$http->timeout( $self->get_http_timeout );
my $resp = $http->get( $url );
my $code = $resp->code();
given ( $code ) {
when ( 200 ) { return $resp->decoded_content(); }
default { return; }
}
}
sub copy_file_to_staging {
my $self = shift;
my $file = shift;
my $location = $self->location()
// shift;
return unless $self->assets_output;
my $content = $self->fetch_file( $file, $location );
return unless $content;
my $hex = sha1_hex $content;
my $filename = basename $file;
my $assets_file = sprintf "%s/%s/%s-%s",
$self->{'assets_base'},
substr( $hex, 0, 3 ),
substr( $hex, 4 ),
$filename;
my $output_file = sprintf "%s/%s/%s-%s",
$self->{'assets_output'},
substr( $hex, 0, 3 ),
substr( $hex, 4 ),
$filename;
my $output_dir = dirname $output_file;
mkpath $output_dir;
my $handle = FileHandle->new( $output_file, 'w' );
print {$handle} $content;
return $assets_file;
}
sub parse {
my $self = shift;
my $string = shift;
my $location = shift;
return unless defined $string;
my( $charset, $stripped ) = strip_charset( $string );
return { errors => [{ fatal => "Unsupported charset '${charset}'" }] }
unless 'UTF-8' eq $charset;
$stripped = $self->strip_comments( $stripped );
$string = escape_braces_in_strings( $stripped );
my @split = $self->split_into_statements( $string, $location );
my @statements;
my @appended;
foreach my $statement ( @split ) {
my $type = $statement->{'type'};
if ( 'appended' eq $type ) {
push @statements, @{$statement->{'content'}};
}
elsif ( 'import' eq $type ) {
push @statements, $statement;
( run in 3.150 seconds using v1.01-cache-2.11-cpan-df04353d9ac )