File Coverage

blib/lib/Hazy.pm
Criterion Covered Total %
statement 89 90 98.8
branch 26 36 72.2
condition 17 21 80.9
subroutine 11 11 100.0
pod 0 6 0.0
total 143 164 87.2


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