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

Commit 64dba5f

Browse files
committed
support custom import extension, fix #245
1 parent b0a6e51 commit 64dba5f

File tree

3 files changed

+91
-8
lines changed

3 files changed

+91
-8
lines changed

pysass.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,17 @@ static void _add_custom_importers(
505505
sass_option_set_c_importers(options, importer_list);
506506
}
507507

508+
static void _add_custom_import_extensions(
509+
struct Sass_Options* options, PyObject* custom_import_extensions
510+
) {
511+
Py_ssize_t i;
512+
513+
for (i = 0; i < PyList_GET_SIZE(custom_import_extensions); i += 1) {
514+
PyObject* ext = PyList_GET_ITEM(custom_import_extensions, i);
515+
sass_option_push_import_extension(options, PyBytes_AS_STRING(ext));
516+
}
517+
}
518+
508519
static PyObject *
509520
PySass_compile_string(PyObject *self, PyObject *args) {
510521
struct Sass_Context *ctx;
@@ -516,13 +527,15 @@ PySass_compile_string(PyObject *self, PyObject *args) {
516527
int source_comments, error_status, precision, indented;
517528
PyObject *custom_functions;
518529
PyObject *custom_importers;
530+
PyObject *custom_import_extensions;
519531
PyObject *result;
520532

521533
if (!PyArg_ParseTuple(args,
522-
PySass_IF_PY3("yiiyiOiO", "siisiOiO"),
534+
PySass_IF_PY3("yiiyiOiOO", "siisiOiOO"),
523535
&string, &output_style, &source_comments,
524536
&include_paths, &precision,
525-
&custom_functions, &indented, &custom_importers)) {
537+
&custom_functions, &indented, &custom_importers,
538+
&custom_import_extensions)) {
526539
return NULL;
527540
}
528541

@@ -535,6 +548,7 @@ PySass_compile_string(PyObject *self, PyObject *args) {
535548
sass_option_set_is_indented_syntax_src(options, indented);
536549
_add_custom_functions(options, custom_functions);
537550
_add_custom_importers(options, custom_importers);
551+
_add_custom_import_extensions(options, custom_import_extensions);
538552
sass_compile_data_context(context);
539553

540554
ctx = sass_data_context_get_context(context);
@@ -560,14 +574,15 @@ PySass_compile_filename(PyObject *self, PyObject *args) {
560574
Sass_Output_Style output_style;
561575
int source_comments, error_status, precision;
562576
PyObject *source_map_filename, *custom_functions, *custom_importers,
563-
*result, *output_filename_hint;
577+
*result, *output_filename_hint, *custom_import_extensions;
564578

565579
if (!PyArg_ParseTuple(args,
566-
PySass_IF_PY3("yiiyiOOOO", "siisiOOOO"),
580+
PySass_IF_PY3("yiiyiOOOOO", "siisiOOOOO"),
567581
&filename, &output_style, &source_comments,
568582
&include_paths, &precision,
569583
&source_map_filename, &custom_functions,
570-
&custom_importers, &output_filename_hint)) {
584+
&custom_importers, &output_filename_hint,
585+
&custom_import_extensions)) {
571586
return NULL;
572587
}
573588

@@ -594,6 +609,7 @@ PySass_compile_filename(PyObject *self, PyObject *args) {
594609
sass_option_set_precision(options, precision);
595610
_add_custom_functions(options, custom_functions);
596611
_add_custom_importers(options, custom_importers);
612+
_add_custom_import_extensions(options, custom_import_extensions);
597613
sass_compile_file_context(context);
598614

599615
ctx = sass_file_context_get_context(context);

sass.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def compile_dirname(
239239
input_filename = input_filename.encode(fs_encoding)
240240
s, v, _ = _sass.compile_filename(
241241
input_filename, output_style, source_comments, include_paths,
242-
precision, None, custom_functions, importers, None,
242+
precision, None, custom_functions, importers, None, [],
243243
)
244244
if s:
245245
v = v.decode('UTF-8')
@@ -584,6 +584,21 @@ def _get_file_arg(key):
584584
'not {1!r}'.format(SassFunction, custom_functions)
585585
)
586586

587+
_custom_exts = kwargs.pop('custom_import_extensions', []) or []
588+
if isinstance(_custom_exts, (text_type, bytes)):
589+
_custom_exts = [_custom_exts]
590+
custom_import_extensions = []
591+
for ext in _custom_exts:
592+
if isinstance(ext, text_type):
593+
custom_import_extensions.append(ext.encode('utf-8'))
594+
elif isinstance(ext, bytes):
595+
custom_import_extensions.append(ext)
596+
else:
597+
raise TypeError(
598+
'custom_import_extensions must be a list of strings '
599+
'or bytes not {}'.format(type(ext))
600+
)
601+
587602
importers = _validate_importers(kwargs.pop('importers', None))
588603

589604
if 'string' in modes:
@@ -597,7 +612,7 @@ def _get_file_arg(key):
597612
_check_no_remaining_kwargs(compile, kwargs)
598613
s, v = _sass.compile_string(
599614
string, output_style, source_comments, include_paths, precision,
600-
custom_functions, indented, importers,
615+
custom_functions, indented, importers, custom_import_extensions,
601616
)
602617
if s:
603618
return v.decode('utf-8')
@@ -613,7 +628,7 @@ def _get_file_arg(key):
613628
s, v, source_map = _sass.compile_filename(
614629
filename, output_style, source_comments, include_paths, precision,
615630
source_map_filename, custom_functions, importers,
616-
output_filename_hint,
631+
output_filename_hint, custom_import_extensions,
617632
)
618633
if s:
619634
v = v.decode('utf-8')

sasstests.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,3 +1409,55 @@ def test_imports_from_cwd(tmpdir):
14091409
with tmpdir.as_cwd():
14101410
out = sass.compile(filename=main_scss.strpath)
14111411
assert out == ''
1412+
1413+
1414+
@pytest.mark.parametrize('exts', [
1415+
'.css',
1416+
('.css',),
1417+
['.css'],
1418+
b'.css',
1419+
[b'.css'],
1420+
['.foobar', '.css'],
1421+
['.foobar', '.css', b'anything'],
1422+
])
1423+
def test_import_css(exts, tmpdir):
1424+
tmpdir.join('other.css').write('body {colour: green}')
1425+
main_scss = tmpdir.join('main.scss')
1426+
main_scss.write("@import 'other';")
1427+
out = sass.compile(
1428+
filename=main_scss.strpath,
1429+
custom_import_extensions=exts,
1430+
)
1431+
assert out == 'body {\n colour: green; }\n'
1432+
1433+
1434+
def test_import_css_string(tmpdir):
1435+
tmpdir.join('other.css').write('body {colour: green}')
1436+
with tmpdir.as_cwd():
1437+
out = sass.compile(
1438+
string="@import 'other';",
1439+
custom_import_extensions='.css',
1440+
)
1441+
assert out == 'body {\n colour: green; }\n'
1442+
1443+
1444+
def test_import_css_error(tmpdir):
1445+
tmpdir.join('other.css').write('body {colour: green}')
1446+
main_scss = tmpdir.join('main.scss')
1447+
main_scss.write("@import 'other';")
1448+
with pytest.raises(TypeError):
1449+
sass.compile(
1450+
filename=main_scss.strpath,
1451+
custom_import_extensions=['.css', 3],
1452+
)
1453+
1454+
1455+
def test_import_ext_other(tmpdir):
1456+
tmpdir.join('other.foobar').write('body {colour: green}')
1457+
main_scss = tmpdir.join('main.scss')
1458+
main_scss.write("@import 'other';")
1459+
out = sass.compile(
1460+
filename=main_scss.strpath,
1461+
custom_import_extensions='.foobar',
1462+
)
1463+
assert out == 'body {\n colour: green; }\n'

0 commit comments

Comments
 (0)