line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package CSS::SpriteMaker::Simple; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '1.001001'; # VERSION |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
21074
|
use Mojo::Base -base; |
|
1
|
|
|
|
|
10883
|
|
|
1
|
|
|
|
|
7
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
191
|
use Carp qw/croak /; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
48
|
|
8
|
1
|
|
|
1
|
|
787
|
use File::Copy qw/copy /; |
|
1
|
|
|
|
|
5256
|
|
|
1
|
|
|
|
|
62
|
|
9
|
1
|
|
|
1
|
|
6
|
use File::Find qw/find /; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
59
|
|
10
|
1
|
|
|
1
|
|
1117
|
use File::Temp qw/tempdir /; |
|
1
|
|
|
|
|
14839
|
|
|
1
|
|
|
|
|
67
|
|
11
|
1
|
|
|
1
|
|
749
|
use File::Spec::Functions qw/catdir catfile/; |
|
1
|
|
|
|
|
819
|
|
|
1
|
|
|
|
|
67
|
|
12
|
1
|
|
|
1
|
|
937
|
use Mojo::Util qw/b64_encode/; |
|
1
|
|
|
|
|
51749
|
|
|
1
|
|
|
|
|
181
|
|
13
|
1
|
|
|
1
|
|
902
|
use Mojolicious; |
|
1
|
|
|
|
|
292219
|
|
|
1
|
|
|
|
|
12
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
has 'css'; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub spritify { |
18
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
19
|
0
|
0
|
|
|
|
|
my @ignore = @{ ref $_[-1] ? pop : [] }; |
|
0
|
|
|
|
|
|
|
20
|
0
|
0
|
|
|
|
|
@_ or croak 'Missing list of paths to search for pictures'; |
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
|
|
|
|
|
|
|
sub spurt { |
51
|
0
|
|
|
0
|
1
|
|
my ( $self, $file ) = @_; |
52
|
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
|
Mojo::Util::spurt $self->css => $file; |
54
|
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
|
$self; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub _gather_pics { |
59
|
0
|
|
|
0
|
|
|
my ( $self, $ignore, @locations ) = @_; |
60
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
|
my %ignore = map +( $_ => 1 ), @$ignore; |
62
|
0
|
|
|
|
|
|
my @pics = grep -f, @locations; |
63
|
|
|
|
|
|
|
find sub { |
64
|
0
|
0
|
0
|
0
|
|
|
return unless -f and /\.(png|gif|jpg|jpeg)$/ and not $ignore{$_}; |
|
|
|
0
|
|
|
|
|
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
|
0
|
|
|
|
|
mkdir $sprite_dir |
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
|
|
|
|
|
|
|
1; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
__END__ |