line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Hazy; |
2
|
|
|
|
|
|
|
|
3
|
6
|
|
|
6
|
|
82169
|
use strict; |
|
6
|
|
|
|
|
7
|
|
|
6
|
|
|
|
|
145
|
|
4
|
6
|
|
|
6
|
|
18
|
use warnings; |
|
6
|
|
|
|
|
15
|
|
|
6
|
|
|
|
|
112
|
|
5
|
6
|
|
|
6
|
|
96
|
use 5.012; |
|
6
|
|
|
|
|
18
|
|
6
|
6
|
|
|
6
|
|
19
|
use Cwd qw/abs_path/; |
|
6
|
|
|
|
|
8
|
|
|
6
|
|
|
|
|
5674
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub new { |
10
|
6
|
|
|
6
|
0
|
3368
|
my ( $pkg, @new ) = @_; |
11
|
6
|
50
|
|
|
|
27
|
my %args = scalar @new == 1 ? @{ shift @new } : @new; |
|
0
|
|
|
|
|
0
|
|
12
|
6
|
|
100
|
|
|
30
|
$args{file_name} //= 'test'; |
13
|
6
|
|
100
|
|
|
17
|
$args{find} //= 'css'; |
14
|
6
|
|
|
|
|
19
|
my @caller = caller(); |
15
|
6
|
|
|
|
|
127
|
$args{abs_path} = abs_path( |
16
|
|
|
|
|
|
|
substr($caller[1], 0, rindex($caller[1], 0), '/') |
17
|
|
|
|
|
|
|
); |
18
|
6
|
100
|
|
|
|
42
|
if ($args{write_dir}) { |
19
|
1
|
|
|
|
|
4
|
$args{write_dir} = sprintf "%s/%s", $args{abs_path}, $args{write_dir}; |
20
|
1
|
50
|
|
|
|
20
|
unless(-d $args{write_dir}) { |
21
|
1
|
|
|
|
|
2
|
my $dir = ''; |
22
|
|
|
|
|
|
|
map { |
23
|
8
|
100
|
|
|
|
186
|
(-e ($dir .= "/$_")) or mkdir $dir; |
24
|
1
|
|
|
|
|
5
|
} (split /\//, $args{write_dir}); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
} |
27
|
6
|
|
|
|
|
42
|
bless {%args}, $pkg; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub process { |
31
|
1
|
|
50
|
1
|
0
|
10
|
$_[1] //= $_[0]->{read_dir} // die 'No read_dir provided'; |
|
|
|
33
|
|
|
|
|
32
|
1
|
|
|
|
|
4
|
$_[1] = sprintf "%s/%s", $_[0]->{abs_path}, $_[1]; |
33
|
1
|
|
|
|
|
4
|
my ( $spec, @files ) = $_[0]->lookup_dir( $_[1] ); |
34
|
1
|
|
|
|
|
2
|
my $build_css; |
35
|
1
|
|
|
|
|
1
|
for my $css_file (@files) { |
36
|
2
|
50
|
|
|
|
52
|
open my $fh, "<$css_file" or die "Cannot open $css_file"; |
37
|
2
|
|
|
|
|
3
|
my $css = do { local $/; <$fh> }; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
29
|
|
38
|
2
|
|
|
|
|
8
|
$css = $_[0]->make_replacements( $spec, $css ); |
39
|
2
|
|
|
|
|
14
|
$build_css .= $css; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
my $write = exists $_[0]->{write_dir} |
42
|
|
|
|
|
|
|
? sprintf "%s/%s", $_[0]->{write_dir}, $_[0]->{file_name} |
43
|
1
|
50
|
|
|
|
7
|
: $_[0]->{file_name}; |
44
|
1
|
|
|
|
|
3
|
write_file( "$write.css", $build_css ); |
45
|
1
|
|
|
|
|
5
|
write_file( "$write.min.css", $_[0]->min_css($build_css) ); |
46
|
1
|
|
|
|
|
5
|
return 1; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub write_file { |
50
|
2
|
50
|
|
2
|
0
|
110
|
open( my $fh, '>', $_[0] ) or die "could not open file $_[0]"; |
51
|
2
|
|
|
|
|
12
|
print $fh $_[1]; |
52
|
2
|
|
|
|
|
55
|
close $fh; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub make_replacements { |
56
|
5
|
|
|
5
|
0
|
1847
|
my $regx = join "|", map { quotemeta($_) } keys %{ $_[1] }; |
|
12
|
|
|
|
|
24
|
|
|
5
|
|
|
|
|
15
|
|
57
|
5
|
|
|
|
|
143
|
$_[2] =~ s/($regx)/$_[1]->{$1}/g; |
58
|
5
|
50
|
|
|
|
15
|
( !$_[2] =~ m/\n$/ ) and $_[2] .= "\n"; |
59
|
5
|
|
|
|
|
18
|
return $_[2]; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub lookup_dir { |
63
|
4
|
|
|
4
|
0
|
14
|
my $look = $_[0]->{find}; |
64
|
4
|
50
|
|
|
|
110
|
opendir( my $dh, $_[1] ) or die "Could not open dir - $_[1]"; |
65
|
22
|
|
|
|
|
52
|
my %files = map { $_ => sprintf "%s/%s", $_[1], $_ } |
66
|
4
|
|
|
|
|
74
|
grep { /config|\.$look$/ } readdir $dh; |
|
30
|
|
|
|
|
174
|
|
67
|
4
|
|
|
|
|
33
|
closedir($dh); |
68
|
4
|
50
|
|
|
|
12
|
my $spec = delete $files{config} or die 'no config found'; |
69
|
4
|
|
|
|
|
10
|
return ( _read_spec($spec), sort values %files ); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub min_css { |
73
|
4
|
|
|
4
|
0
|
1525
|
$_[1] =~ s/[\s]{2,}|[\t\r\n]+//g; |
74
|
4
|
|
|
|
|
29
|
my %minify = (' {', '{', '{ ', '{', ' }', '}', '} ', '}', ': ', |
75
|
|
|
|
|
|
|
':', ';}', '}', ' ,', ',', ', ', ',', '( ', '(', ' )', ')' ); |
76
|
4
|
|
|
|
|
22
|
my $regx = join "|", map { quotemeta($_) } sort keys %minify; |
|
40
|
|
|
|
|
45
|
|
77
|
4
|
|
|
|
|
140
|
$_[1] =~ s/($regx)/$minify{$1}/g; |
78
|
4
|
|
|
|
|
21
|
$_[1]; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub _read_spec { |
82
|
9
|
|
|
9
|
|
1058
|
my ( %spec, %arg ); |
83
|
9
|
|
|
|
|
214
|
open( my $fh, "<$_[0]" ); |
84
|
9
|
|
|
|
|
57
|
$arg{end} = ';'; |
85
|
9
|
|
|
|
|
73
|
while ( sysread( $fh, $arg{buffer}, 1 ) ) { |
86
|
235
|
100
|
100
|
|
|
595
|
if ( ! exists $arg{value} && $arg{buffer} =~ m/\s/ ) { next } |
|
30
|
|
|
|
|
85
|
|
87
|
205
|
100
|
66
|
|
|
593
|
if ( !$arg{flag} && !$arg{multi} && $arg{buffer} eq ':' ) { $arg{flag} = 1; next; } |
|
15
|
|
100
|
|
|
19
|
|
|
15
|
|
|
|
|
52
|
|
88
|
190
|
100
|
|
|
|
248
|
if ($arg{buffer} eq $arg{end}) { |
89
|
15
|
|
|
|
|
24
|
map { $arg{$_} =~ s/^\s+|\s+$// } qw/key value/; |
|
30
|
|
|
|
|
102
|
|
90
|
15
|
|
|
|
|
33
|
$spec{"$arg{key}"} = $arg{value}; |
91
|
15
|
|
|
|
|
20
|
map { delete $arg{$_} } qw/key value flag multi/; |
|
60
|
|
|
|
|
80
|
|
92
|
15
|
|
|
|
|
14
|
$arg{end} = ';'; |
93
|
15
|
|
|
|
|
63
|
next; |
94
|
|
|
|
|
|
|
} |
95
|
175
|
100
|
100
|
|
|
357
|
if ( exists $arg{flag} && ! exists $arg{value} ) { |
96
|
16
|
50
|
|
|
|
31
|
next if ($arg{buffer} =~ m/\s/); |
97
|
16
|
100
|
|
|
|
32
|
if ($arg{buffer} =~ m/[\@\$\^\&\*\{\/\\\~\`\>\<\+\_\]\[\?\|\"\'\=\!]/ ) { |
98
|
1
|
|
|
|
|
2
|
$arg{multi} = 1; |
99
|
1
|
|
|
|
|
2
|
$arg{end} = $arg{buffer}; |
100
|
1
|
|
|
|
|
3
|
next; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
} |
103
|
174
|
100
|
|
|
|
878
|
exists $arg{flag} ? ( $arg{value} .= $arg{buffer} ) : ( $arg{key} .= $arg{buffer} ); |
104
|
|
|
|
|
|
|
} |
105
|
9
|
|
|
|
|
44
|
close($fh); |
106
|
9
|
|
|
|
|
77
|
return \%spec; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
1; |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
__END__ |