Skip to content

Commit 71d0fb7

Browse files
committed
Improves sass values handling in xs code
Map sass values more directly to perl data types. Lists are real arrays and maps are real hashes.
1 parent aabd135 commit 71d0fb7

File tree

12 files changed

+1435
-442
lines changed

12 files changed

+1435
-442
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ CSS::Sass
33

44
### Compile .scss and .sass files using libsass
55

6-
CSS::Sass provides a perl interface to [libsass][1], a fairly complete
7-
Sass compiler written in C++. It is currently somewhere around ruby sass
8-
3.2 feature parity. It can compile .scss and .sass files.
6+
CSS::Sass provides a perl interface to [libsass][1], a fairly complete Sass
7+
compiler written in C++. It is currently somewhere around ruby sass 3.2/3.3
8+
feature parity and heading towards 3.4. It can compile .scss and .sass files.
99

1010
[1]: https://github.com/sass/libsass
1111

lib/CSS/Sass.md

Lines changed: 65 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,18 @@ CSS::Sass - Compile .scss files using libsass
6565
# convert indented syntax
6666
my $scss = sass2scss($sass);
6767

68+
# Import quoting functions
69+
use CSS::Sass qw(quote unquote);
70+
71+
# Exchange quoted strings
72+
my $string = unquote($from_sass);
73+
my $to_sass = quote($string, '"');
74+
6875
# DESCRIPTION
6976

7077
CSS::Sass provides a perl interface to libsass, a fairly complete Sass
71-
compiler written in C++. It is currently somewhere around ruby sass 3.2
72-
feature parity. It can compile .scss and .sass files.
78+
compiler written in C++. It is currently somewhere around ruby sass 3.2/3.3
79+
feature parity and heading towards 3.4. It can compile .scss and .sass files.
7380

7481
# OBJECT ORIENTED INTERFACE
7582

@@ -111,10 +118,9 @@ feature parity. It can compile .scss and .sass files.
111118
- `$css = sass_compile(source_code, options)`
112119
- `($css, $err, $srcmap) = sass_compile(source_code, options)`
113120

114-
This compiles the Sass string that is passed in the first parameter. It
115-
returns both the CSS and the error in list context and just the CSS in
116-
scalar context. One of the returned values will always be `undef`, but
117-
never both.
121+
Compiles the given Sass source code. It returns CSS, error and source map in
122+
list context or just the CSS in scalar context. Either CSS or error will be
123+
`undef`, but never both.
118124

119125
# OPTIONS
120126

@@ -169,12 +175,11 @@ feature parity. It can compile .scss and .sass files.
169175
function should be the function's Sass signature and the value should be a
170176
Perl subroutine reference. This subroutine will be called whenever the
171177
function is used in the Sass being compiled. The arguments to the subroutine
172-
are [CSS::Sass::Type](https://metacpan.org/pod/CSS::Sass::Type) objects and the return value _must_ also be one of
173-
those types. It may also return `undef` which is just a shortcut for
174-
CSS::Sass::Type::String->new('').
175-
178+
are [CSS::Sass::Type](https://metacpan.org/pod/CSS::Sass::Type) objects, which map to native perl types if possible.
179+
You can return either [CSS::Sass::Type](https://metacpan.org/pod/CSS::Sass::Type) objects or supported native perl data
180+
structures. `undef` is an equivalent of CSS::Sass::Type::Null->new.
176181
The function is called with an `eval` statement so you may use "die" to
177-
throw errors back to libsass.
182+
throw errors back to libsass (`CSS::Sass::Type::Error`).
178183

179184
A simple example:
180185

@@ -183,6 +188,7 @@ feature parity. It can compile .scss and .sass files.
183188
my ($str) = @_;
184189
die '$str should be a string' unless $str->isa("CSS::Sass::Type::String");
185190
return CSS::Sass::Type::String->new($str->value . " hello");
191+
# equivalent to return $str->value . " hello";
186192
}
187193
}
188194

@@ -194,36 +200,76 @@ feature parity. It can compile .scss and .sass files.
194200

195201
some_rule: Well, hello;
196202

203+
- `Sass_Value` Types
204+
205+
Sass knowns various `Sass_Value` types. We export the constants for completeness.
206+
Each type is mapped to a package inside the `CSS::Sass::Type` namespace.
207+
208+
# Value types
209+
SASS_ERROR
210+
SASS_NULL
211+
SASS_BOOLEAN
212+
SASS_NUMBER
213+
SASS_STRING
214+
SASS_COLOR
215+
SASS_LIST
216+
SASS_MAP
217+
# List styles
218+
SASS_COMMA
219+
SASS_SPACE
220+
221+
- Autodetection for value types returned by custom function
222+
223+
Many `Sass_Value` types can be mapped directly to perl data structures.
224+
`maps` and `lists` map directly to `hashes` and `arrays`. Scalars are
225+
mapped to `string`, `number` or `null`. You can directly return these
226+
native data types from your custom functions or use the datastructures
227+
to access maps and lists.
228+
229+
undef; # same as CSS::Sass::Type::Null->new;
230+
42; # same as CSS::Sass::Type::Number->new(42);
231+
"foobar"; # same as CSS::Sass::Type::String->new("foobar");
232+
[ 'foo', 'bar' ]; # same as CSS::Sass::Type::List->new('foo', 'bar');
233+
{ key => 'value' }; # same as CSS::Sass::Type::Map->new(key => 'value');
234+
235+
We bless native return values from custom functions into the correct package.
236+
237+
# sub get-map { return { key: "value" } };
238+
.class { content: map-get(get-map(), key); }
239+
240+
# sub get-list { return [ 'foo', 42, 'bar' ] };
241+
.class { content: nth(get-list(), 2); }
242+
197243
# MISCELLANEOUS
198244

199245
- `SASS2SCSS_PRETTIFY_0`
200246

201-
Write everything on one line (minimized)
247+
Write everything on one line (minimized)
202248

203249
- `SASS2SCSS_PRETTIFY_1`
204250

205-
Add lf after opening bracket (lisp style)
251+
Add lf after opening bracket (lisp style)
206252

207253
- `SASS2SCSS_PRETTIFY_2`
208254

209-
Add lf after opening and before closing bracket (1TBS style)
255+
Add lf after opening and before closing bracket (1TBS style)
210256

211257
- `SASS2SCSS_PRETTIFY_3`
212258

213-
Add lf before/after opening and before closing (allman style)
259+
Add lf before/after opening and before closing (allman style)
214260

215261
- `SASS2SCSS_KEEP_COMMENT`
216262

217-
Keep multi-line source code comments.
218-
Single-line comments are removed by default.
263+
Keep multi-line source code comments.
264+
Single-line comments are removed by default.
219265

220266
- `SASS2SCSS_STRIP_COMMENT`
221267

222-
Strip all source code (single- and multi-line) comments.
268+
Strip all source code (single- and multi-line) comments.
223269

224270
- `SASS2SCSS_CONVERT_COMMENT`
225271

226-
Convert single-line comments to mutli-line comments.
272+
Convert single-line comments to mutli-line comments.
227273

228274
- `sass2scss($sass, $options)`
229275

@@ -258,11 +304,3 @@ Copyright (C) 2014 by Marcel Greter
258304
This library is free software; you can redistribute it and/or modify
259305
it under the same terms as Perl itself, either Perl version 5.12.4 or,
260306
at your option, any later version of Perl 5 you may have available.
261-
262-
# POD ERRORS
263-
264-
Hey! **The above document had some coding errors, which are explained below:**
265-
266-
- Around line 217:
267-
268-
'=item' outside of any '=over'

lib/CSS/Sass.pm

Lines changed: 101 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,22 @@ require Exporter;
1616
our @ISA = qw(Exporter);
1717

1818
our @EXPORT_OK = qw(
19+
quote
20+
unquote
1921
sass2scss
22+
import_sv
2023
sass_compile
2124
sass_compile_file
25+
SASS_COMMA
26+
SASS_SPACE
27+
SASS_ERROR
28+
SASS_NULL
29+
SASS_BOOLEAN
30+
SASS_NUMBER
31+
SASS_STRING
32+
SASS_COLOR
33+
SASS_LIST
34+
SASS_MAP
2235
);
2336

2437
our @EXPORT = qw(
@@ -107,12 +120,31 @@ sub compile_file {
107120

108121
sub sass_function_callback {
109122
my $cb = shift;
110-
my $ret = eval { $cb->(map { CSS::Sass::Type->new_from_xs_rep($_) } @_) };
111-
return CSS::Sass::Type::Error->new("$@")->xs_rep if $@;
112-
return CSS::Sass::Type::String->new('')->xs_rep if !defined $ret;
113-
return CSS::Sass::Type::Error->new("Perl Sass function returned something that wasn't a CSS::Sass::Type")->xs_rep
114-
unless ref $ret && $ret->isa("CSS::Sass::Type");
115-
$ret->xs_rep;
123+
my $ret = eval { $cb->(@_) };
124+
use Data::Dumper;
125+
126+
127+
unless (UNIVERSAL::isa($ret, "CSS::Sass::Type")) {
128+
if (UNIVERSAL::isa($ret, "HASH")) {
129+
bless $ret, "CSS::Sass::Type::Map"
130+
} elsif (UNIVERSAL::isa($ret, "ARRAY")) {
131+
bless $ret, "CSS::Sass::Type::List"
132+
} elsif (UNIVERSAL::isa($ret, "REF")) {
133+
die "got refg";
134+
}
135+
} else {
136+
# we are a sass type, sweet
137+
# warn "pass only by $ret";
138+
}
139+
140+
# warn Dumper $ret;
141+
142+
return CSS::Sass::Type::Error->new("$@") if $@;
143+
144+
# return CSS::Sass::Type::Error->new("Perl Sass function returned something that wasn't a CSS::Sass::Type")
145+
# unless ref $ret && $ret->isa("CSS::Sass::Type");
146+
147+
$ret;
116148
}
117149

118150
1;
@@ -185,11 +217,18 @@ CSS::Sass - Compile .scss files using libsass
185217
# convert indented syntax
186218
my $scss = sass2scss($sass);
187219
220+
# Import quoting functions
221+
use CSS::Sass qw(quote unquote);
222+
223+
# Exchange quoted strings
224+
my $string = unquote($from_sass);
225+
my $to_sass = quote($string, '"');
226+
188227
=head1 DESCRIPTION
189228
190229
CSS::Sass provides a perl interface to libsass, a fairly complete Sass
191-
compiler written in C++. It is currently somewhere around ruby sass 3.2
192-
feature parity. It can compile .scss and .sass files.
230+
compiler written in C++. It is currently somewhere around ruby sass 3.2/3.3
231+
feature parity and heading towards 3.4. It can compile .scss and .sass files.
193232
194233
=head1 OBJECT ORIENTED INTERFACE
195234
@@ -238,10 +277,9 @@ Allows you to inspect or change the options after a call to C<new>.
238277
239278
=item C<($css, $err, $srcmap) = sass_compile(source_code, options)>
240279
241-
This compiles the Sass string that is passed in the first parameter. It
242-
returns both the CSS and the error in list context and just the CSS in
243-
scalar context. One of the returned values will always be C<undef>, but
244-
never both.
280+
Compiles the given Sass source code. It returns CSS, error and source map in
281+
list context or just the CSS in scalar context. Either CSS or error will be
282+
C<undef>, but never both.
245283
246284
=back
247285
@@ -305,12 +343,12 @@ This is a hash of Sass functions implemented in Perl. The key for each
305343
function should be the function's Sass signature and the value should be a
306344
Perl subroutine reference. This subroutine will be called whenever the
307345
function is used in the Sass being compiled. The arguments to the subroutine
308-
are L<CSS::Sass::Type> objects and the return value I<must> also be one of
309-
those types. It may also return C<undef> which is just a shortcut for
310-
CSS::Sass::Type::String->new('').
346+
are L<CSS::Sass::Type> objects, which map to native perl types if possible.
347+
You can return either L<CSS::Sass::Type> objects or supported native perl data
348+
structures. C<undef> is an equivalent of CSS::Sass::Type::Null->new.
311349
312350
The function is called with an C<eval> statement so you may use "die" to
313-
throw errors back to libsass.
351+
throw errors back to libsass (C<CSS::Sass::Type::Error>).
314352
315353
A simple example:
316354
@@ -319,6 +357,7 @@ A simple example:
319357
my ($str) = @_;
320358
die '$str should be a string' unless $str->isa("CSS::Sass::Type::String");
321359
return CSS::Sass::Type::String->new($str->value . " hello");
360+
# equivalent to return $str->value . " hello";
322361
}
323362
}
324363
@@ -330,10 +369,54 @@ Then the ouput would be:
330369
331370
some_rule: Well, hello;
332371
372+
=item C<Sass_Value> Types
373+
374+
Sass knowns various C<Sass_Value> types. We export the constants for completeness.
375+
Each type is mapped to a package inside the C<CSS::Sass::Type> namespace.
376+
377+
# Value types
378+
SASS_ERROR
379+
SASS_NULL
380+
SASS_BOOLEAN
381+
SASS_NUMBER
382+
SASS_STRING
383+
SASS_COLOR
384+
SASS_LIST
385+
SASS_MAP
386+
# List styles
387+
SASS_COMMA
388+
SASS_SPACE
389+
390+
=item Autodetection for value types returned by custom function
391+
392+
Many C<Sass_Value> types can be mapped directly to perl data structures.
393+
C<maps> and C<lists> map directly to C<hashes> and C<arrays>. Scalars are
394+
mapped to C<string>, C<number> or C<null>. You can directly return these
395+
native data types from your custom functions or use the datastructures
396+
to access maps and lists.
397+
398+
undef; # same as CSS::Sass::Type::Null->new;
399+
42; # same as CSS::Sass::Type::Number->new(42);
400+
"foobar"; # same as CSS::Sass::Type::String->new("foobar");
401+
[ 'foo', 'bar' ]; # same as CSS::Sass::Type::List->new('foo', 'bar');
402+
{ key => 'value' }; # same as CSS::Sass::Type::Map->new(key => 'value');
403+
404+
We bless native return values from custom functions into the correct package.
405+
406+
# sub get-map { return { key: "value" } };
407+
.class { content: map-get(get-map(), key); }
408+
409+
# sub get-list { return [ 'foo', 42, 'bar' ] };
410+
.class { content: nth(get-list(), 2); }
411+
412+
413+
333414
=back
334415
335416
=head1 MISCELLANEOUS
336417
418+
=over 4
419+
337420
=item C<SASS2SCSS_PRETTIFY_0>
338421
339422
Write everything on one line (minimized)
@@ -387,12 +470,12 @@ L<The CSS::Sass Home Page|https://github.com/sass/perl-libsass>
387470
388471
=head1 AUTHOR
389472
390-
David Caldwell E<lt>[email protected]E<gt>
473+
David Caldwell E<lt>[email protected]E<gt>
391474
Marcel Greter E<lt>[email protected]E<gt>
392475
393476
=head1 COPYRIGHT AND LICENSE
394477
395-
Copyright (C) 2013 by David Caldwell
478+
Copyright (C) 2013 by David Caldwell
396479
Copyright (C) 2014 by Marcel Greter
397480
398481
This library is free software; you can redistribute it and/or modify

0 commit comments

Comments
 (0)