Skip to content
This repository was archived by the owner on Oct 24, 2025. It is now read-only.

Commit 607b141

Browse files
committed
Add output_style option to build_directory()
1 parent aad1a39 commit 607b141

File tree

3 files changed

+53
-21
lines changed

3 files changed

+53
-21
lines changed

docs/changes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ To be released.
2222
:const:`bool` instead of :const:`str`. String values like ``'none'``,
2323
``'line_numbers'``, and ``'map'`` become deprecated, and will be obsolete
2424
soon.
25+
- :func:`~sassutils.builder.build_directory()` function has a new optional
26+
parameter ``output_style``.
2527

2628
__ https://github.com/sass/libsass/releases/tag/3.0
2729
.. _partial import: http://sass-lang.com/documentation/file.SASS_REFERENCE.html#partials

sasstests.py

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -305,40 +305,58 @@ def test_regression_issue_11(self):
305305

306306
class BuilderTestCase(unittest.TestCase):
307307

308+
def setUp(self):
309+
self.temp_path = tempfile.mkdtemp()
310+
self.sass_path = os.path.join(self.temp_path, 'sass')
311+
self.css_path = os.path.join(self.temp_path, 'css')
312+
shutil.copytree('test', self.sass_path)
313+
314+
def tearDown(self):
315+
shutil.rmtree(self.temp_path)
316+
308317
def test_builder_build_directory(self):
309-
temp_path = tempfile.mkdtemp()
310-
sass_path = os.path.join(temp_path, 'sass')
311-
css_path = os.path.join(temp_path, 'css')
312-
shutil.copytree('test', sass_path)
313-
result_files = build_directory(sass_path, css_path)
314-
assert len(result_files) == 6
315-
assert result_files['a.scss'] == 'a.scss.css'
318+
css_path = self.css_path
319+
result_files = build_directory(self.sass_path, css_path)
320+
self.assertEqual(6, len(result_files))
321+
self.assertEqual('a.scss.css', result_files['a.scss'])
316322
with open(os.path.join(css_path, 'a.scss.css'), **utf8_if_py3) as f:
317323
css = f.read()
318-
assert css == A_EXPECTED_CSS
319-
assert result_files['b.scss'] == 'b.scss.css'
324+
self.assertEqual(A_EXPECTED_CSS, css)
325+
self.assertEqual('b.scss.css', result_files['b.scss'])
320326
with open(os.path.join(css_path, 'b.scss.css'), **utf8_if_py3) as f:
321327
css = f.read()
322-
assert css == B_EXPECTED_CSS
323-
assert result_files['c.scss'] == 'c.scss.css'
328+
self.assertEqual(B_EXPECTED_CSS, css)
329+
self.assertEqual('c.scss.css', result_files['c.scss'])
324330
with open(os.path.join(css_path, 'c.scss.css'), **utf8_if_py3) as f:
325331
css = f.read()
326-
assert css == C_EXPECTED_CSS
327-
assert result_files['d.scss'] == 'd.scss.css'
332+
self.assertEqual(C_EXPECTED_CSS, css)
333+
self.assertEqual('d.scss.css', result_files['d.scss'])
328334
with open(os.path.join(css_path, 'd.scss.css'), **utf8_if_py3) as f:
329335
css = f.read()
330336
self.assertEqual(D_EXPECTED_CSS, css)
331-
assert result_files['e.scss'] == 'e.scss.css'
337+
self.assertEqual('e.scss.css', result_files['e.scss'])
332338
with open(os.path.join(css_path, 'e.scss.css'), **utf8_if_py3) as f:
333339
css = f.read()
334-
assert css == E_EXPECTED_CSS
335-
assert (result_files[os.path.join('subdir', 'recur.scss')] ==
336-
os.path.join('subdir', 'recur.scss.css'))
340+
self.assertEqual(E_EXPECTED_CSS, css)
341+
self.assertEqual(
342+
os.path.join('subdir', 'recur.scss.css'),
343+
result_files[os.path.join('subdir', 'recur.scss')]
344+
)
337345
with open(os.path.join(css_path, 'subdir', 'recur.scss.css'),
338346
**utf8_if_py3) as f:
339347
css = f.read()
340348
self.assertEqual(SUBDIR_RECUR_EXPECTED_CSS, css)
341-
shutil.rmtree(temp_path)
349+
350+
def test_output_style(self):
351+
css_path = self.css_path
352+
result_files = build_directory(self.sass_path, css_path,
353+
output_style='compressed')
354+
self.assertEqual(6, len(result_files))
355+
self.assertEqual('a.scss.css', result_files['a.scss'])
356+
with open(os.path.join(css_path, 'a.scss.css'), **utf8_if_py3) as f:
357+
css = f.read()
358+
self.assertEqual('body{background-color:green}body a{color:blue}',
359+
css)
342360

343361

344362
class ManifestTestCase(unittest.TestCase):

sassutils/builder.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,25 @@
2525
SUFFIX_PATTERN = re.compile('[.](' + '|'.join(map(re.escape, SUFFIXES)) + ')$')
2626

2727

28-
def build_directory(sass_path, css_path, _root_sass=None, _root_css=None):
28+
def build_directory(sass_path, css_path, output_style='nested',
29+
_root_sass=None, _root_css=None):
2930
"""Compiles all SASS/SCSS files in ``path`` to CSS.
3031
3132
:param sass_path: the path of the directory which contains source files
3233
to compile
3334
:type sass_path: :class:`str`, :class:`basestring`
3435
:param css_path: the path of the directory compiled CSS files will go
3536
:type css_path: :class:`str`, :class:`basestring`
37+
:param output_style: an optional coding style of the compiled result.
38+
choose one of: ``'nested'`` (default), ``'expanded'``,
39+
``'compact'``, ``'compressed'``
40+
:type output_style: :class:`str`
3641
:returns: a dictionary of source filenames to compiled CSS filenames
3742
:rtype: :class:`collections.Mapping`
3843
44+
.. versionadded:: 0.6.0
45+
The ``output_style`` parameter.
46+
3947
"""
4048
if _root_sass is None or _root_css is None:
4149
_root_sass = sass_path
@@ -50,15 +58,19 @@ def build_directory(sass_path, css_path, _root_sass=None, _root_css=None):
5058
# Do not compile if it's partial
5159
continue
5260
css_fullname = os.path.join(css_path, name) + '.css'
53-
css = compile(filename=sass_fullname, include_paths=[_root_sass])
61+
css = compile(filename=sass_fullname,
62+
output_style=output_style,
63+
include_paths=[_root_sass])
5464
with io.open(css_fullname, 'w', encoding='utf-8') as css_file:
5565
css_file.write(css)
5666
result[os.path.relpath(sass_fullname, _root_sass)] = \
5767
os.path.relpath(css_fullname, _root_css)
5868
elif os.path.isdir(sass_fullname):
5969
css_fullname = os.path.join(css_path, name)
6070
subresult = build_directory(sass_fullname, css_fullname,
61-
_root_sass, _root_css)
71+
output_style=output_style,
72+
_root_sass=_root_sass,
73+
_root_css=_root_css)
6274
result.update(subresult)
6375
return result
6476

0 commit comments

Comments
 (0)