File Coverage

blib/lib/CSS/Sass.pm
Criterion Covered Total %
statement 29 43 67.4
branch 3 18 16.6
condition 0 6 0.0
subroutine 10 12 83.3
pod n/a
total 42 79 53.1


line stmt bran cond sub pod time code
1             # Copyright (c) 2013-2014 David Caldwell.
2             # Copyright (c) 2014-2017 Marcel Greter.
3             #
4             # Permission is hereby granted, free of charge, to any person obtaining a copy
5             # of this software and associated documentation files (the "Software"), to deal
6             # in the Software without restriction, including without limitation the rights
7             # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8             # copies of the Software, and to permit persons to whom the Software is
9             # furnished to do so, subject to the following conditions:
10             #
11             # The above copyright notice and this permission notice shall be included in
12             # all copies or substantial portions of the Software.
13             #
14             # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15             # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16             # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17             # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18             # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19             # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20             # THE SOFTWARE.
21              
22             package CSS::Sass;
23              
24 11     11   737564 use strict;
  11         119  
  11         378  
25 11     11   72 use warnings;
  11         25  
  11         375  
26 11     11   67 use Carp;
  11         23  
  11         8940  
27              
28             require Exporter;
29              
30             our @ISA = qw(Exporter);
31              
32             our @EXPORT_OK = qw(
33             quote
34             unquote
35             auto_quote
36             need_quotes
37             resolve_file
38             sass2scss
39             import_sv
40             sass_compile
41             sass_compile_file
42             libsass_version
43             sass2scss_version
44             sass_operation
45             sass_stringify
46             SASS_COMMA
47             SASS_SPACE
48             SASS_ERROR
49             SASS_NULL
50             SASS_BOOLEAN
51             SASS_NUMBER
52             SASS_STRING
53             SASS_COLOR
54             SASS_LIST
55             SASS_MAP
56             );
57              
58             our @EXPORT = qw(
59             SASS_STYLE_NESTED
60             SASS_STYLE_EXPANDED
61             SASS_STYLE_COMPACT
62             SASS_STYLE_COMPRESSED
63             SASS2SCSS_PRETTIFY_0
64             SASS2SCSS_PRETTIFY_1
65             SASS2SCSS_PRETTIFY_2
66             SASS2SCSS_PRETTIFY_3
67             SASS2SCSS_KEEP_COMMENT
68             SASS2SCSS_STRIP_COMMENT
69             SASS2SCSS_CONVERT_COMMENT
70             );
71              
72             our $VERSION = "3.4.8";
73              
74             require XSLoader;
75             XSLoader::load('CSS::Sass', $VERSION);
76             require CSS::Sass::Value;
77              
78             sub new
79             {
80 1     1   157065 my ($class, %options) = @_;
81             # Ensure initial sub structures on options
82 1 50       11 $options{plugin_paths} = [] unless exists $options{plugin_paths};
83 1 50       8 $options{include_paths} = [] unless exists $options{include_paths};
84 1 50       7 $options{sass_functions} = {} unless exists $options{sass_functions};
85             # Create and return new object with options
86 1         9 bless { options => \%options }, $class;
87             };
88              
89             sub options
90             {
91             shift->{options}
92 1     1   16 }
93              
94             sub last_error
95             {
96 0     0   0 my ($self) = @_;
97             $self->{last_error}
98 0         0 }
99              
100             my @path_types = (
101             'plugin_paths',
102             'include_paths'
103             );
104              
105             # directory delimiter according to platform
106             my $dir_delim = $^O eq 'MSWin32' ? ';' : ':';
107              
108             # normalize option hash
109             my $normalize_options = sub
110             {
111             my ($options) = @_;
112             # gather all functions
113             # they need to be hashes
114             my %functions =
115             (
116             %{$options->{'functions'} || {}},
117             %{$options->{'sass_functions'} || {}}
118             );
119             # create functions array
120             # help the c code a little
121             my @functions = map { [
122             $_, $functions{$_}
123             ] } keys %functions;
124             # gather all importers
125             # they need to be arrays
126             my @importers =
127             map {
128             ref($_) eq "ARRAY" ?
129             $_ : [ $_, 0 ];
130             }
131             grep { defined }
132             (
133             $options->{'importer'},
134             @{$options->{'importers'} || []},
135             @{$options->{'sass_importers'} || []}
136             );
137             # gather all paths strings
138             foreach my $type (@path_types)
139             {
140             $options->{$type} = join $dir_delim,
141             map { split $dir_delim, $_ }
142             @{$options->{$type} || []};
143             }
144             # now normalize the original hash
145             $options->{'functions'} = \@functions;
146             $options->{'importers'} = \@importers;
147             # remove importer from options
148             # it is now included in importers
149             delete $options->{'importer'};
150             # return pointer
151             return $options;
152             };
153              
154             sub sass_compile
155             {
156 1     1   116 my ($sass_code, %options) = @_;
157 11     11   107 no warnings 'uninitialized';
  11         34  
  11         1806  
158 1         189 $normalize_options->(\%options);
159 0         0 my $r = compile_sass($sass_code, \%options);
160             # decode the streams (maybe move directly to XS code)
161             #utf8::decode($r->{output_string}) if defined $r->{output_string};
162             #utf8::decode($r->{output_string}) if defined $r->{output_string};
163             #utf8::decode($r->{error_message}) if defined $r->{error_message};
164             wantarray ? ($r->{output_string}, $r->{error_message}, $r) : $r->{output_string}
165 0 0       0 }
166              
167             sub sass_compile_file
168             {
169 1     1   7 my ($input_path, %options) = @_;
170 11     11   86 no warnings 'uninitialized';
  11         32  
  11         4133  
171 1         175 $normalize_options->(\%options);
172 0         0 my $r = compile_sass_file($input_path, \%options);
173             # decode the streams (maybe move directly to XS code)
174             #utf8::decode($r->{output_string}) if defined $r->{output_string};
175             #utf8::decode($r->{error_message}) if defined $r->{error_message};
176             wantarray ? ($r->{output_string}, $r->{error_message}, $r) : $r->{output_string}
177 0 0       0 }
178              
179             sub compile
180             {
181 0     0   0 my ($self, $sass_code) = @_;
182 0         0 my ($compiled, $stats);
183 0         0 ($compiled, $self->{last_error}, $stats) = sass_compile($sass_code, %{$self->options});
  0         0  
184 0 0 0     0 croak $self->{last_error} if $self->{last_error} && !$self->options->{dont_die};
185 0 0       0 wantarray ? ($compiled, $stats) : $compiled
186             }
187              
188             sub compile_file
189             {
190 1     1   1668 my ($self, $sass_file) = @_;
191 1         4 my ($compiled, $stats);
192 1         3 ($compiled, $self->{last_error}, $stats) = sass_compile_file($sass_file, %{$self->options});
  1         5  
193 0 0 0       croak $self->{last_error} if $self->{last_error} && !$self->options->{dont_die};
194 0 0         wantarray ? ($compiled, $stats) : $compiled
195             }
196              
197             1;
198             __END__