Skip to content

Commit

Permalink
Add indent and linefeed config options
Browse files Browse the repository at this point in the history
  • Loading branch information
mgreter committed Mar 23, 2015
1 parent 21facf0 commit 69b888c
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 5 deletions.
24 changes: 23 additions & 1 deletion bin/psass.pl
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,17 @@ sub version {
my @plugin_paths;
my @include_paths;

# output styles
my $indent = " ";
my $linefeed = "auto";

# get options
GetOptions (
'help|h' => sub { pod2usage(1); },
'watch|w' => \ $watchdog,
'version|v' => \ &version,
'indent=s' => \ $indent,
'linefeed=s' => \ $linefeed,
'precision|p=s' => \ $precision,
'output-file|o=s' => \ $output_file,
'output-style|t=s' => \ $output_style,
Expand Down Expand Up @@ -95,6 +101,18 @@ sub version {
# die with message if style is unknown
else { die "unknown output style: $output_style" }

# resolve linefeed options
if ($linefeed =~ m/^a/i)
{ $linefeed = undef; }
elsif ($linefeed =~ m/^w/i)
{ $linefeed = "\r\n"; }
elsif ($linefeed =~ m/^[u]/i)
{ $linefeed = "\n"; }
elsif ($linefeed =~ m/^[n]/i)
{ $linefeed = ""; }
# die with message if linefeed type is unknown
else { die "unknown linefeed type: $linefeed" }

# do we have output path in second arg?
if (defined $ARGV[1] && $ARGV[1] ne '-')
{ $output_file = $ARGV[1]; }
Expand All @@ -108,6 +126,8 @@ ()
{
return (
dont_die => $watchdog,
indent => $indent,
linefeed => $linefeed,
precision => $precision,
output_path => $output_file,
output_style => $output_style,
Expand Down Expand Up @@ -201,7 +221,9 @@ =head1 SYNOPSIS
-v, --version print version
-h, --help print this help
-w, --watch start watchdog mode
-p, --precision precision for float output
-p, --precision=int precision for float output
--indent=string set indent string used for output
--linefeed=type linefeed used for output [auto|unix|win|none]
-o, --output-file=file output file to write result to
-t, --output-style=style output style [expanded|nested|compressed|compact]
-L, --plugin-path=path plugin load path (repeatable)
Expand Down
12 changes: 12 additions & 0 deletions lib/CSS/Sass.pm
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,18 @@ C<undef>, but never both.
The default is C<SASS_STYLE_NESTED>. Set to C<SASS_STYLE_COMPRESSED> to
eliminate all whitespace (for your production CSS).
=item C<precision>
Set the floating point precision for output.
=item C<linefeed>
Set the linefeed string used for css output.
=item C<indentation>
Set the indentation string used for css output.
=item C<source_comments>
Set to C<true> to get extra comments in the output, indicating what input
Expand Down
9 changes: 8 additions & 1 deletion lib/CSS/Sass.xs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "sass2scss.h"
#include "sass_context.h"

#define isSafeSv(sv) sv && SvOK(*sv)
#define Constant(c) newCONSTSUB(stash, #c, newSViv(c))

#undef free
Expand Down Expand Up @@ -513,6 +514,8 @@ SV* init_sass_options(struct Sass_Options* sass_options, HV* perl_options)
SV** include_paths_sv = hv_fetchs(perl_options, "include_paths", false);
SV** plugin_paths_sv = hv_fetchs(perl_options, "plugin_paths", false);
SV** precision_sv = hv_fetchs(perl_options, "precision", false);
SV** linefeed_sv = hv_fetchs(perl_options, "linefeed", false);
SV** indent_sv = hv_fetchs(perl_options, "indent", false);
SV** source_map_root_sv = hv_fetchs(perl_options, "source_map_root", false);
SV** source_map_file_sv = hv_fetchs(perl_options, "source_map_file", false);
SV** sass_functions_sv = hv_fetchs(perl_options, "sass_functions", false);
Expand All @@ -528,10 +531,14 @@ SV* init_sass_options(struct Sass_Options* sass_options, HV* perl_options)
if (source_map_embed_sv) sass_option_set_source_map_embed (sass_options, SvTRUE(*source_map_embed_sv));
if (include_paths_sv) sass_option_set_include_path (sass_options, safe_svpv(*include_paths_sv, ""));
if (plugin_paths_sv) sass_option_set_plugin_path (sass_options, safe_svpv(*plugin_paths_sv, ""));
if (precision_sv) sass_option_set_precision (sass_options, SvUV(*precision_sv));
if (source_map_root_sv) sass_option_set_source_map_root (sass_options, safe_svpv(*source_map_root_sv, ""));
if (source_map_file_sv) sass_option_set_source_map_file (sass_options, safe_svpv(*source_map_file_sv, ""));

// do not set anything if the option is set to undef
if (isSafeSv(indent_sv)) sass_option_set_indent (sass_options, SvPV_nolen(*indent_sv));
if (isSafeSv(linefeed_sv)) sass_option_set_linefeed (sass_options, SvPV_nolen(*linefeed_sv));
if (isSafeSv(precision_sv)) sass_option_set_precision (sass_options, SvUV(*precision_sv));

if (importer_sv) { sass_option_set_importer(sass_options, sass_make_importer(sass_importer, *importer_sv)); }

if (sass_functions_sv) {
Expand Down
13 changes: 12 additions & 1 deletion t/01_xs.t
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use strict;
use warnings;

use Test::More tests => 40;
use Test::More tests => 46;
BEGIN { use_ok('CSS::Sass') };

my $r;
Expand Down Expand Up @@ -53,6 +53,17 @@ $r = CSS::Sass::compile_sass("\n.valid {\n color: red; }", { source_comments =>
is ($r->{error_status}, 0, "source_comments=>[] no error_status and doesn't crash");
is ($r->{error_message}, undef, "source_comments=>[] error_message is undef");

# $options->{indent}
$r = CSS::Sass::compile_sass('foo { color: red; }', { indent => '-äöü-' });
is ($r->{error_status}, 0, "import no error_status");
is ($r->{error_message}, undef, "import error_message is undef");
like ($r->{output_string}, qr/foo {\r?\n-äöü-color: red; }/, "import imported red");

# $options->{linefeed}
$r = CSS::Sass::compile_sass('foo { color: red; }', { linefeed => "-äöü-\r" });
is ($r->{error_status}, 0, "import no error_status");
is ($r->{error_message}, undef, "import error_message is undef");
like ($r->{output_string}, qr/foo {-äöü-\r color: red; }/, "import imported red");

# $options->{include_paths}
$r = CSS::Sass::compile_sass('@import "colors"; .valid { color: $red; }', { });
Expand Down
2 changes: 1 addition & 1 deletion t/sass-spec
Submodule sass-spec updated 272 files

0 comments on commit 69b888c

Please # to comment.