@@ -56,7 +56,8 @@ require XSLoader;
56
56
XSLoader::load(' CSS::Sass' , $VERSION );
57
57
require CSS::Sass::Type;
58
58
59
- sub new {
59
+ sub new
60
+ {
60
61
my ($class , %options ) = @_ ;
61
62
# Ensure initial sub structures on options
62
63
$options {plugin_paths } = [] unless exists $options {plugin_paths };
@@ -66,64 +67,100 @@ sub new {
66
67
bless { options => \%options }, $class ;
67
68
};
68
69
69
- sub options {
70
+ sub options
71
+ {
70
72
shift -> {options }
71
73
}
72
74
73
- sub last_error {
75
+ sub last_error
76
+ {
74
77
my ($self ) = @_ ;
75
78
$self -> {last_error }
76
79
}
77
80
78
- sub sass_compile {
81
+ my @path_types = (
82
+ ' plugin_paths' ,
83
+ ' include_paths'
84
+ );
85
+
86
+ # directory delimiter according to platform
87
+ my $dir_delim = $^O eq ' MSWin32' ? ' ;' : ' :' ;
88
+
89
+ # normalize option hash
90
+ my $normalize_options = sub
91
+ {
92
+ my ($options ) = @_ ;
93
+ # gather all functions
94
+ # they need to be hashes
95
+ my %functions =
96
+ (
97
+ %{$options -> {' functions' } || {}},
98
+ %{$options -> {' sass_functions' } || {}}
99
+ );
100
+ # create functions array
101
+ # help the c code a little
102
+ my @functions = map { [
103
+ $_ , $functions {$_ }
104
+ ] } keys %functions ;
105
+ # gather all importers
106
+ # they need to be arrays
107
+ my @importers =
108
+ map {
109
+ ref ($_ ) eq " ARRAY" ?
110
+ $_ : [ $_ , 0 ];
111
+ }
112
+ grep { defined }
113
+ (
114
+ $options -> {' importer' },
115
+ @{$options -> {' importers' } || []},
116
+ @{$options -> {' sass_importers' } || []}
117
+ );
118
+ # gather all paths strings
119
+ foreach my $type (@path_types )
120
+ {
121
+ $options -> {$type } = join $dir_delim ,
122
+ map { split $dir_delim , $_ }
123
+ @{$options -> {$type } || []};
124
+ }
125
+ # now normalize the original hash
126
+ $options -> {' functions' } = \@functions ;
127
+ $options -> {' importers' } = \@importers ;
128
+ # remove importer from options
129
+ # it is now included in importers
130
+ delete $options -> {' importer' };
131
+ # return pointer
132
+ return $options ;
133
+ };
134
+
135
+ sub sass_compile
136
+ {
79
137
my ($sass_code , %options ) = @_ ;
80
138
no warnings ' uninitialized' ;
81
- my $r = compile_sass($sass_code , { %options ,
82
- # Override sass_functions with the arrayref of arrayrefs that the XS expects.
83
- !$options {sass_functions } ? ()
84
- : (sass_functions => [ map { [ $_ => $options {sass_functions }-> {$_ } ]
85
- } keys %{$options {sass_functions }} ]),
86
- # Override include_paths with a ':' separated list
87
- !$options {include_paths } ? ()
88
- : (include_paths => join ($^O eq ' MSWin32' ? ' ;' : ' :' ,
89
- @{$options {include_paths }})),
90
- # Override plugin_paths with a ':' separated list
91
- !$options {plugin_paths } ? ()
92
- : (plugin_paths => join ($^O eq ' MSWin32' ? ' ;' : ' :' ,
93
- @{$options {plugin_paths }})),
94
- });
139
+ $normalize_options -> (\%options );
140
+ my $r = compile_sass($sass_code , \%options );
95
141
wantarray ? ($r -> {output_string }, $r -> {error_message }, $r ) : $r -> {output_string }
96
142
}
97
143
98
- sub sass_compile_file {
144
+ sub sass_compile_file
145
+ {
99
146
my ($input_path , %options ) = @_ ;
100
147
no warnings ' uninitialized' ;
101
- my $r = compile_sass_file($input_path , { %options ,
102
- # Override sass_functions with the arrayref of arrayrefs that the XS expects.
103
- !$options {sass_functions } ? ()
104
- : (sass_functions => [ map { [ $_ => $options {sass_functions }-> {$_ } ]
105
- } keys %{$options {sass_functions }} ]),
106
- # Override include_paths with a ':' separated list
107
- !$options {include_paths } ? ()
108
- : (include_paths => join ($^O eq ' MSWin32' ? ' ;' : ' :' ,
109
- @{$options {include_paths }})),
110
- # Override plugin_paths with a ':' separated list
111
- !$options {plugin_paths } ? ()
112
- : (plugin_paths => join ($^O eq ' MSWin32' ? ' ;' : ' :' ,
113
- @{$options {plugin_paths }})),
114
- });
148
+ $normalize_options -> (\%options );
149
+ my $r = compile_sass_file($input_path , \%options );
115
150
wantarray ? ($r -> {output_string }, $r -> {error_message }, $r ) : $r -> {output_string }
116
151
}
117
152
118
- sub compile {
153
+ sub compile
154
+ {
119
155
my ($self , $sass_code ) = @_ ;
120
156
my ($compiled , $stats );
121
157
($compiled , $self -> {last_error }, $stats ) = sass_compile($sass_code , %{$self -> options});
122
158
croak $self -> {last_error } if $self -> {last_error } && !$self -> options-> {dont_die };
123
159
wantarray ? ($compiled , $stats ) : $compiled
124
160
}
125
161
126
- sub compile_file {
162
+ sub compile_file
163
+ {
127
164
my ($self , $sass_file ) = @_ ;
128
165
my ($compiled , $stats );
129
166
($compiled , $self -> {last_error }, $stats ) = sass_compile_file($sass_file , %{$self -> options});
@@ -494,7 +531,7 @@ L<The CSS::Sass Home Page|https://github.com/sass/perl-libsass>
494
531
495
532
=head1 AUTHOR
496
533
497
- David Caldwell E<lt> [email protected] E<gt>
534
+ David Caldwell E<lt> [email protected] E<gt>
498
535
Marcel Greter E<lt> [email protected] E<gt>
499
536
500
537
=head1 LICENSE
0 commit comments