Skip to content

Commit 65d1eb3

Browse files
committed
Merge pull request #39 from nephila/feature/precision
Feature: add precision parameter
2 parents e43c0ab + 29998e1 commit 65d1eb3

File tree

6 files changed

+80
-16
lines changed

6 files changed

+80
-16
lines changed

docs/changes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ Version 0.6.3
66

77
To be released.
88

9+
- Added ``precision`` parameter to :func:`sass.compile()` function.
10+
[:issue:`39` by Andrea Stagi]
11+
- :program:`sassc` has a new :option:`-p <sassc -p>`/:option:`--precision
12+
<sassc --precision>` option. [:issue:`39` by Andrea Stagi]
13+
914

1015
Version 0.6.2
1116
-------------

pysass.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ static PyObject *
3939
PySass_compile_string(PyObject *self, PyObject *args) {
4040
struct sass_context *context;
4141
char *string, *include_paths, *image_path;
42-
int output_style, source_comments;
42+
int output_style, source_comments, precision;
4343
PyObject *result;
4444

4545
if (!PyArg_ParseTuple(args,
46-
PySass_IF_PY3("yiiyy", "siiss"),
46+
PySass_IF_PY3("yiiyyi", "siissi"),
4747
&string, &output_style, &source_comments,
48-
&include_paths, &image_path)) {
48+
&include_paths, &image_path, &precision)) {
4949
return NULL;
5050
}
5151

@@ -55,6 +55,7 @@ PySass_compile_string(PyObject *self, PyObject *args) {
5555
context->options.source_comments = source_comments;
5656
context->options.include_paths = include_paths;
5757
context->options.image_path = image_path;
58+
context->options.precision = precision;
5859

5960
sass_compile(context);
6061

@@ -71,13 +72,13 @@ static PyObject *
7172
PySass_compile_filename(PyObject *self, PyObject *args) {
7273
struct sass_file_context *context;
7374
char *filename, *include_paths, *image_path;
74-
int output_style, source_comments, error_status;
75+
int output_style, source_comments, error_status, precision;
7576
PyObject *source_map_filename, *result;
7677

7778
if (!PyArg_ParseTuple(args,
78-
PySass_IF_PY3("yiiyyO", "siissO"),
79+
PySass_IF_PY3("yiiyyiO", "siissiO"),
7980
&filename, &output_style, &source_comments,
80-
&include_paths, &image_path, &source_map_filename)) {
81+
&include_paths, &image_path, &precision, &source_map_filename)) {
8182
return NULL;
8283
}
8384

@@ -99,6 +100,7 @@ PySass_compile_filename(PyObject *self, PyObject *args) {
99100
context->options.source_comments = source_comments;
100101
context->options.include_paths = include_paths;
101102
context->options.image_path = image_path;
103+
context->options.precision = precision;
102104

103105
sass_compile_file(context);
104106

@@ -119,14 +121,14 @@ static PyObject *
119121
PySass_compile_dirname(PyObject *self, PyObject *args) {
120122
struct sass_folder_context *context;
121123
char *search_path, *output_path, *include_paths, *image_path;
122-
int output_style, source_comments;
124+
int output_style, source_comments, precision;
123125
PyObject *result;
124126

125127
if (!PyArg_ParseTuple(args,
126-
PySass_IF_PY3("yyiyy", "ssiss"),
128+
PySass_IF_PY3("yyiiyyi", "ssiissi"),
127129
&search_path, &output_path,
128130
&output_style, &source_comments,
129-
&include_paths, &image_path)) {
131+
&include_paths, &image_path, precision)) {
130132
return NULL;
131133
}
132134

@@ -137,6 +139,7 @@ PySass_compile_dirname(PyObject *self, PyObject *args) {
137139
context->options.source_comments = source_comments;
138140
context->options.include_paths = include_paths;
139141
context->options.image_path = image_path;
142+
context->options.precision = precision;
140143

141144
sass_compile_folder(context);
142145

sass.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ def compile(**kwargs):
7373
:type include_paths: :class:`collections.Sequence`, :class:`str`
7474
:param image_path: an optional path to find images
7575
:type image_path: :class:`str`
76+
:param precision: optional precision for numbers. :const:`5` by default.
77+
:type precision: :class:`int`
7678
:returns: the compiled CSS string
7779
:rtype: :class:`str`
7880
:raises sass.CompileError: when it fails for any reason
@@ -102,6 +104,8 @@ def compile(**kwargs):
102104
:type include_paths: :class:`collections.Sequence`, :class:`str`
103105
:param image_path: an optional path to find images
104106
:type image_path: :class:`str`
107+
:param precision: optional precision for numbers. :const:`5` by default.
108+
:type precision: :class:`int`
105109
:returns: the compiled CSS string, or a pair of the compiled CSS string
106110
and the source map string if ``source_comments='map'``
107111
:rtype: :class:`str`, :class:`tuple`
@@ -134,6 +138,8 @@ def compile(**kwargs):
134138
:type include_paths: :class:`collections.Sequence`, :class:`str`
135139
:param image_path: an optional path to find images
136140
:type image_path: :class:`str`
141+
:param precision: optional precision for numbers. :const:`5` by default.
142+
:type precision: :class:`int`
137143
:raises sass.CompileError: when it fails for any reason
138144
(for example the given SASS has broken syntax)
139145
@@ -148,6 +154,9 @@ def compile(**kwargs):
148154
Values like ``'none'``, ``'line_numbers'``, and ``'map'`` for
149155
the ``source_comments`` parameter are deprecated.
150156
157+
.. versionadded:: 0.6.3
158+
Added ``precision`` parameter.
159+
151160
"""
152161
modes = set()
153162
for mode_name in MODES:
@@ -158,6 +167,7 @@ def compile(**kwargs):
158167
elif len(modes) > 1:
159168
raise TypeError(and_join(modes) + ' are exclusive each other; '
160169
'cannot be used at a time')
170+
precision = kwargs.pop('precision', 5)
161171
output_style = kwargs.pop('output_style', 'nested')
162172
if not isinstance(output_style, string_types):
163173
raise TypeError('output_style must be a string, not ' +
@@ -235,7 +245,7 @@ def compile(**kwargs):
235245
string = string.encode('utf-8')
236246
s, v = compile_string(string,
237247
output_style, source_comments,
238-
include_paths, image_path)
248+
include_paths, image_path, precision)
239249
if s:
240250
return v.decode('utf-8')
241251
elif 'filename' in modes:
@@ -249,7 +259,7 @@ def compile(**kwargs):
249259
s, v, source_map = compile_filename(
250260
filename,
251261
output_style, source_comments,
252-
include_paths, image_path, source_map_filename
262+
include_paths, image_path, precision, source_map_filename
253263
)
254264
if s:
255265
v = v.decode('utf-8')
@@ -299,7 +309,7 @@ def compile(**kwargs):
299309
output_path = output_path.encode(fs_encoding)
300310
s, v = compile_dirname(search_path, output_path,
301311
output_style, source_comments,
302-
include_paths, image_path)
312+
include_paths, image_path, precision)
303313
if s:
304314
return
305315
else:

sassc.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@
4040
4141
.. versionadded:: 0.4.0
4242
43+
.. option:: -p, --precision
44+
45+
Set the precision for numbers. Default is 5.
46+
47+
.. versionadded:: 0.6.3
48+
4349
.. option:: -v, --version
4450
4551
Prints the program version.
@@ -87,6 +93,8 @@ def main(argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr):
8793
parser.add_option('-w', '--watch', action='store_true',
8894
help='Watch file for changes. Requires the second '
8995
'argument (output css filename).')
96+
parser.add_option('-p', '--precision', action='store', type="int", default=5,
97+
help='Set the precision for numbers. [default: %default]')
9098
options, args = parser.parse_args(argv[1:])
9199
error = functools.partial(print,
92100
parser.get_prog_name() + ': error:',
@@ -122,7 +130,8 @@ def main(argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr):
122130
output_style=options.output_style,
123131
source_map_filename=source_map_filename,
124132
include_paths=options.include_paths,
125-
image_path=options.image_path
133+
image_path=options.image_path,
134+
precision=options.precision
126135
)
127136
else:
128137
source_map_filename = None
@@ -131,7 +140,8 @@ def main(argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr):
131140
filename=filename,
132141
output_style=options.output_style,
133142
include_paths=options.include_paths,
134-
image_path=options.image_path
143+
image_path=options.image_path,
144+
precision=options.precision
135145
)
136146
except (IOError, OSError) as e:
137147
error(e)

sasstests.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,20 @@ def normalize_path(path):
103103
color: red; }
104104
'''
105105

106+
G_EXPECTED_CSS = '''\
107+
body {
108+
font: 100% Helvetica, sans-serif;
109+
color: #333;
110+
height: 1.42857; }
111+
'''
112+
113+
G_EXPECTED_CSS_WITH_PRECISION_8 = '''\
114+
body {
115+
font: 100% Helvetica, sans-serif;
116+
color: #333;
117+
height: 1.42857143; }
118+
'''
119+
106120
SUBDIR_RECUR_EXPECTED_CSS = '''\
107121
body p {
108122
color: blue; }
@@ -304,6 +318,12 @@ def test_compile_source_map_deprecated_source_comments_map(self):
304318
self.assertEqual(expected, actual)
305319
self.assert_source_map_equal(expected_map, actual_map)
306320

321+
def test_compile_with_precision(self):
322+
actual = sass.compile(filename='test/g.scss')
323+
assert actual == G_EXPECTED_CSS
324+
actual = sass.compile(filename='test/g.scss', precision=8)
325+
assert actual == G_EXPECTED_CSS_WITH_PRECISION_8
326+
307327
def test_regression_issue_2(self):
308328
actual = sass.compile(string='''
309329
@media (min-width: 980px) {
@@ -340,7 +360,7 @@ def tearDown(self):
340360
def test_builder_build_directory(self):
341361
css_path = self.css_path
342362
result_files = build_directory(self.sass_path, css_path)
343-
self.assertEqual(6, len(result_files))
363+
self.assertEqual(7, len(result_files))
344364
self.assertEqual('a.scss.css', result_files['a.scss'])
345365
with open(os.path.join(css_path, 'a.scss.css'), **utf8_if_py3) as f:
346366
css = f.read()
@@ -365,6 +385,13 @@ def test_builder_build_directory(self):
365385
os.path.join('subdir', 'recur.scss.css'),
366386
result_files[os.path.join('subdir', 'recur.scss')]
367387
)
388+
with open(os.path.join(css_path, 'g.scss.css'), **utf8_if_py3) as f:
389+
css = f.read()
390+
self.assertEqual(G_EXPECTED_CSS, css)
391+
self.assertEqual(
392+
os.path.join('subdir', 'recur.scss.css'),
393+
result_files[os.path.join('subdir', 'recur.scss')]
394+
)
368395
with open(os.path.join(css_path, 'subdir', 'recur.scss.css'),
369396
**utf8_if_py3) as f:
370397
css = f.read()
@@ -374,7 +401,7 @@ def test_output_style(self):
374401
css_path = self.css_path
375402
result_files = build_directory(self.sass_path, css_path,
376403
output_style='compressed')
377-
self.assertEqual(6, len(result_files))
404+
self.assertEqual(7, len(result_files))
378405
self.assertEqual('a.scss.css', result_files['a.scss'])
379406
with open(os.path.join(css_path, 'a.scss.css'), **utf8_if_py3) as f:
380407
css = f.read()

test/g.scss

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
$font-stack: Helvetica, sans-serif;
2+
$primary-color: #333;
3+
$variabile: 5 / 3 * 6 / 7;
4+
5+
body {
6+
font: 100% $font-stack;
7+
color: $primary-color;
8+
height: $variabile;
9+
}

0 commit comments

Comments
 (0)