| 1 | package CSS::SpriteMaker::Simple; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | our $VERSION = '1.001002'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Carp qw/croak /; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use File::Copy qw/copy /; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use File::Find qw/find /; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use File::Temp qw/tempdir /; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use File::Spec::Functions qw/catdir catfile/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Mojo::Util qw/b64_encode/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Mojolicious; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 2 | has 'css'; | |||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 0 | ▶ | sub spritify { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
spritify: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 19 | 0 | ▶ | my @ignore = @{ ref $_[-1] ? pop : [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | ▶ | @_ or croak 'Missing list of paths to search for pictures'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | my $asset_dir = $self->_gather_pics( \@ignore, @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | # Set up the app | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | my $s = Mojolicious->new; | |||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | $s->mode('production'); | |||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | $s->log->level('fatal'); # disable 'info' message from AssetPack | |||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | $s->static->paths([ catdir $asset_dir, 'public' ]); | |||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | # AssetPack plugin will generate the CSS and the Sprite image | ||||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | $s->plugin('AssetPack'); | |||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | $s->asset( 'app.css' => 'sprites:///sprite' ); | |||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | # Fetch CSS code and embed the sprite as base64 in it | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | my $css = $s->static->file( $s->asset->get('app.css') )->slurp; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | my ( $sprite_filename ) | |||||||||||||||||||||||||||||||||||||||||
| 36 | = $css =~ /\.sprite\{background:url\( (sprite-\w+\.png) \)/x; | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | my $sprite = $s->static->file( catfile 'packed', $sprite_filename )->slurp; | |||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | $sprite = b64_encode $sprite, ''; | |||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | $css =~ s{\Q$sprite_filename\E}{data:image/png;base64,$sprite}; | |||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | # Modify pic classnames to avoid potential clashes | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | $css =~ s{\.sprite\.(?=[\w-]+)}{.sprite.s-}g; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | $self->css( $css ); | |||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | sub spurt { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
spurt: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | my ( $self, $file ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | Mojo::Util::spurt $self->css => $file; | |||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | sub _gather_pics { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_gather_pics: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | my ( $self, $ignore, @locations ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | my %ignore = map +( $_ => 1 ), @$ignore; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | my @pics = grep -f, @locations; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | find sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | return unless -f and /\.(png|gif|jpg|jpeg)$/ and not $ignore{$_}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
-f $_ and /\.(png|gif|jpg|jpeg)$/u and not $ignore{$_}
-f $_ and /\.(png|gif|jpg|jpeg)$/u
MC/DC0%0/3
-f $_ and /\.(png|gif|jpg|jpeg)$/u and not $ignore{$_}
-f $_
/\.(png|gif|jpg|jpeg)$/u
not $ignore{$_}
Truth table0%0/4
A and B and not(C)
A= -f $_B= /\.(png|gif|jpg|jpeg)$/uC= not $ignore{$_}
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | push @pics, $File::Find::name; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | }, grep -d, @locations; | |||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | my $dir = tempdir CLEANUP => 1; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | mkdir catdir $dir, 'public'; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | my $sprite_dir = catdir $dir, 'public', 'sprite'; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | mkdir $sprite_dir | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | or croak "Failed to create sprite dir [$sprite_dir]: $!"; | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | copy $_ => $sprite_dir for @pics; | |||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | return $dir; | |||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | __END__ | ||||||||||||||||||||||||||||||||||||||||||