Skip to content

Feature: add precision parameter #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions pysass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ static PyObject *
PySass_compile_string(PyObject *self, PyObject *args) {
struct sass_context *context;
char *string, *include_paths, *image_path;
int output_style, source_comments;
int output_style, source_comments, precision;
PyObject *result;

if (!PyArg_ParseTuple(args,
PySass_IF_PY3("yiiyy", "siiss"),
PySass_IF_PY3("yiiyyi", "siissi"),
&string, &output_style, &source_comments,
&include_paths, &image_path)) {
&include_paths, &image_path, &precision)) {
return NULL;
}

Expand All @@ -55,6 +55,7 @@ PySass_compile_string(PyObject *self, PyObject *args) {
context->options.source_comments = source_comments;
context->options.include_paths = include_paths;
context->options.image_path = image_path;
context->options.precision = precision;

sass_compile(context);

Expand All @@ -71,13 +72,13 @@ static PyObject *
PySass_compile_filename(PyObject *self, PyObject *args) {
struct sass_file_context *context;
char *filename, *include_paths, *image_path;
int output_style, source_comments, error_status;
int output_style, source_comments, error_status, precision;
PyObject *source_map_filename, *result;

if (!PyArg_ParseTuple(args,
PySass_IF_PY3("yiiyyO", "siissO"),
PySass_IF_PY3("yiiyyiO", "siissiO"),
&filename, &output_style, &source_comments,
&include_paths, &image_path, &source_map_filename)) {
&include_paths, &image_path, &precision, &source_map_filename)) {
return NULL;
}

Expand All @@ -99,6 +100,7 @@ PySass_compile_filename(PyObject *self, PyObject *args) {
context->options.source_comments = source_comments;
context->options.include_paths = include_paths;
context->options.image_path = image_path;
context->options.precision = precision;

sass_compile_file(context);

Expand All @@ -119,14 +121,14 @@ static PyObject *
PySass_compile_dirname(PyObject *self, PyObject *args) {
struct sass_folder_context *context;
char *search_path, *output_path, *include_paths, *image_path;
int output_style, source_comments;
int output_style, source_comments, precision;
PyObject *result;

if (!PyArg_ParseTuple(args,
PySass_IF_PY3("yyiyy", "ssiss"),
PySass_IF_PY3("yyiiyyi", "ssiissi"),
&search_path, &output_path,
&output_style, &source_comments,
&include_paths, &image_path)) {
&include_paths, &image_path, precision)) {
return NULL;
}

Expand All @@ -137,6 +139,7 @@ PySass_compile_dirname(PyObject *self, PyObject *args) {
context->options.source_comments = source_comments;
context->options.include_paths = include_paths;
context->options.image_path = image_path;
context->options.precision = precision;

sass_compile_folder(context);

Expand Down
13 changes: 10 additions & 3 deletions sass.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ def compile(**kwargs):
:type include_paths: :class:`collections.Sequence`, :class:`str`
:param image_path: an optional path to find images
:type image_path: :class:`str`
:param precision: optional precision for numbers. :const:`5` by default.
:type precision: :class:`int`
:returns: the compiled CSS string
:rtype: :class:`str`
:raises sass.CompileError: when it fails for any reason
Expand Down Expand Up @@ -102,6 +104,8 @@ def compile(**kwargs):
:type include_paths: :class:`collections.Sequence`, :class:`str`
:param image_path: an optional path to find images
:type image_path: :class:`str`
:param precision: optional precision for numbers. :const:`5` by default.
:type precision: :class:`int`
:returns: the compiled CSS string, or a pair of the compiled CSS string
and the source map string if ``source_comments='map'``
:rtype: :class:`str`, :class:`tuple`
Expand Down Expand Up @@ -134,6 +138,8 @@ def compile(**kwargs):
:type include_paths: :class:`collections.Sequence`, :class:`str`
:param image_path: an optional path to find images
:type image_path: :class:`str`
:param precision: optional precision for numbers. :const:`5` by default.
:type precision: :class:`int`
:raises sass.CompileError: when it fails for any reason
(for example the given SASS has broken syntax)

Expand All @@ -158,6 +164,7 @@ def compile(**kwargs):
elif len(modes) > 1:
raise TypeError(and_join(modes) + ' are exclusive each other; '
'cannot be used at a time')
precision = kwargs.pop('precision', 5)
output_style = kwargs.pop('output_style', 'nested')
if not isinstance(output_style, string_types):
raise TypeError('output_style must be a string, not ' +
Expand Down Expand Up @@ -235,7 +242,7 @@ def compile(**kwargs):
string = string.encode('utf-8')
s, v = compile_string(string,
output_style, source_comments,
include_paths, image_path)
include_paths, image_path, precision)
if s:
return v.decode('utf-8')
elif 'filename' in modes:
Expand All @@ -249,7 +256,7 @@ def compile(**kwargs):
s, v, source_map = compile_filename(
filename,
output_style, source_comments,
include_paths, image_path, source_map_filename
include_paths, image_path, precision, source_map_filename
)
if s:
v = v.decode('utf-8')
Expand Down Expand Up @@ -299,7 +306,7 @@ def compile(**kwargs):
output_path = output_path.encode(fs_encoding)
s, v = compile_dirname(search_path, output_path,
output_style, source_comments,
include_paths, image_path)
include_paths, image_path, precision)
if s:
return
else:
Expand Down
14 changes: 12 additions & 2 deletions sassc.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@

.. versionadded:: 0.4.0

.. option:: -p, --precision

Set the precision for numbers. Default is 5.

.. versionadded:: 0.6.3

.. option:: -v, --version

Prints the program version.
Expand Down Expand Up @@ -87,6 +93,8 @@ def main(argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr):
parser.add_option('-w', '--watch', action='store_true',
help='Watch file for changes. Requires the second '
'argument (output css filename).')
parser.add_option('-p', '--precision', action='store', type="int", default=5,
help='Set the precision for numbers. [default: %default]')
options, args = parser.parse_args(argv[1:])
error = functools.partial(print,
parser.get_prog_name() + ': error:',
Expand Down Expand Up @@ -122,7 +130,8 @@ def main(argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr):
output_style=options.output_style,
source_map_filename=source_map_filename,
include_paths=options.include_paths,
image_path=options.image_path
image_path=options.image_path,
precision=options.precision
)
else:
source_map_filename = None
Expand All @@ -131,7 +140,8 @@ def main(argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr):
filename=filename,
output_style=options.output_style,
include_paths=options.include_paths,
image_path=options.image_path
image_path=options.image_path,
precision=options.precision
)
except (IOError, OSError) as e:
error(e)
Expand Down
31 changes: 29 additions & 2 deletions sasstests.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ def normalize_path(path):
color: red; }
'''

G_EXPECTED_CSS = '''\
body {
font: 100% Helvetica, sans-serif;
color: #333;
height: 1.42857; }
'''

G_EXPECTED_CSS_WITH_PRECISION_8 = '''\
body {
font: 100% Helvetica, sans-serif;
color: #333;
height: 1.42857143; }
'''

SUBDIR_RECUR_EXPECTED_CSS = '''\
body p {
color: blue; }
Expand Down Expand Up @@ -304,6 +318,12 @@ def test_compile_source_map_deprecated_source_comments_map(self):
self.assertEqual(expected, actual)
self.assert_source_map_equal(expected_map, actual_map)

def test_compile_with_precision(self):
actual = sass.compile(filename='test/g.scss')
assert actual == G_EXPECTED_CSS
actual = sass.compile(filename='test/g.scss', precision=8)
assert actual == G_EXPECTED_CSS_WITH_PRECISION_8

def test_regression_issue_2(self):
actual = sass.compile(string='''
@media (min-width: 980px) {
Expand Down Expand Up @@ -340,7 +360,7 @@ def tearDown(self):
def test_builder_build_directory(self):
css_path = self.css_path
result_files = build_directory(self.sass_path, css_path)
self.assertEqual(6, len(result_files))
self.assertEqual(7, len(result_files))
self.assertEqual('a.scss.css', result_files['a.scss'])
with open(os.path.join(css_path, 'a.scss.css'), **utf8_if_py3) as f:
css = f.read()
Expand All @@ -365,6 +385,13 @@ def test_builder_build_directory(self):
os.path.join('subdir', 'recur.scss.css'),
result_files[os.path.join('subdir', 'recur.scss')]
)
with open(os.path.join(css_path, 'g.scss.css'), **utf8_if_py3) as f:
css = f.read()
self.assertEqual(G_EXPECTED_CSS, css)
self.assertEqual(
os.path.join('subdir', 'recur.scss.css'),
result_files[os.path.join('subdir', 'recur.scss')]
)
with open(os.path.join(css_path, 'subdir', 'recur.scss.css'),
**utf8_if_py3) as f:
css = f.read()
Expand All @@ -374,7 +401,7 @@ def test_output_style(self):
css_path = self.css_path
result_files = build_directory(self.sass_path, css_path,
output_style='compressed')
self.assertEqual(6, len(result_files))
self.assertEqual(7, len(result_files))
self.assertEqual('a.scss.css', result_files['a.scss'])
with open(os.path.join(css_path, 'a.scss.css'), **utf8_if_py3) as f:
css = f.read()
Expand Down
9 changes: 9 additions & 0 deletions test/g.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
$font-stack: Helvetica, sans-serif;
$primary-color: #333;
$variabile: 5 / 3 * 6 / 7;

body {
font: 100% $font-stack;
color: $primary-color;
height: $variabile;
}