Skip to content

Commit 734d4ab

Browse files
committed
Add more cli opts and source map config args
1 parent 889f0ba commit 734d4ab

File tree

5 files changed

+225
-12
lines changed

5 files changed

+225
-12
lines changed

pysass.cpp

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -512,19 +512,24 @@ PySass_compile_string(PyObject *self, PyObject *args) {
512512
struct Sass_Context *ctx;
513513
struct Sass_Data_Context *context;
514514
struct Sass_Options *options;
515-
char *string, *include_paths;
515+
char *string, *include_paths, *source_map_file;
516516
const char *error_message, *output_string;
517517
Sass_Output_Style output_style;
518-
int source_comments, error_status, precision, indented;
518+
int source_comments, error_status, precision, indented,
519+
source_map_embed, source_map_contents, source_map_file_urls,
520+
omit_source_map_url;
519521
PyObject *custom_functions;
520522
PyObject *custom_importers;
523+
PyObject *source_map_root;
521524
PyObject *result;
522525

523526
if (!PyArg_ParseTuple(args,
524-
PySass_IF_PY3("yiiyiOiO", "siisiOiO"),
527+
PySass_IF_PY3("yiiyiOiOiiiO", "siisiOiOiiiO"),
525528
&string, &output_style, &source_comments,
526529
&include_paths, &precision,
527-
&custom_functions, &indented, &custom_importers)) {
530+
&custom_functions, &indented, &custom_importers,
531+
&source_map_contents, &source_map_embed,
532+
&omit_source_map_url, &source_map_root)) {
528533
return NULL;
529534
}
530535

@@ -535,6 +540,16 @@ PySass_compile_string(PyObject *self, PyObject *args) {
535540
sass_option_set_include_path(options, include_paths);
536541
sass_option_set_precision(options, precision);
537542
sass_option_set_is_indented_syntax_src(options, indented);
543+
sass_option_set_source_map_contents(options, source_map_contents);
544+
sass_option_set_source_map_embed(options, source_map_embed);
545+
sass_option_set_omit_source_map_url(options, omit_source_map_url);
546+
547+
if (PyBytes_Check(source_map_root) && PyBytes_GET_SIZE(source_map_root)) {
548+
sass_option_set_source_map_root(
549+
options, PyBytes_AS_STRING(source_map_root)
550+
);
551+
}
552+
538553
_add_custom_functions(options, custom_functions);
539554
_add_custom_importers(options, custom_importers);
540555
sass_compile_data_context(context);
@@ -560,16 +575,19 @@ PySass_compile_filename(PyObject *self, PyObject *args) {
560575
char *filename, *include_paths;
561576
const char *error_message, *output_string, *source_map_string;
562577
Sass_Output_Style output_style;
563-
int source_comments, error_status, precision;
578+
int source_comments, error_status, precision, source_map_embed,
579+
source_map_contents, source_map_file_urls, omit_source_map_url;
564580
PyObject *source_map_filename, *custom_functions, *custom_importers,
565-
*result, *output_filename_hint;
581+
*result, *output_filename_hint, *source_map_root;
566582

567583
if (!PyArg_ParseTuple(args,
568-
PySass_IF_PY3("yiiyiOOOO", "siisiOOOO"),
584+
PySass_IF_PY3("yiiyiOOOOiiiO", "siisiOOOOiiiO"),
569585
&filename, &output_style, &source_comments,
570586
&include_paths, &precision,
571587
&source_map_filename, &custom_functions,
572-
&custom_importers, &output_filename_hint)) {
588+
&custom_importers, &output_filename_hint,
589+
&source_map_contents, &source_map_embed,
590+
&omit_source_map_url, &source_map_root)) {
573591
return NULL;
574592
}
575593

@@ -590,10 +608,20 @@ PySass_compile_filename(PyObject *self, PyObject *args) {
590608
);
591609
}
592610
}
611+
612+
if (PyBytes_Check(source_map_root) && PyBytes_GET_SIZE(source_map_root)) {
613+
sass_option_set_source_map_root(
614+
options, PyBytes_AS_STRING(source_map_root)
615+
);
616+
}
617+
593618
sass_option_set_output_style(options, output_style);
594619
sass_option_set_source_comments(options, source_comments);
595620
sass_option_set_include_path(options, include_paths);
596621
sass_option_set_precision(options, precision);
622+
sass_option_set_source_map_contents(options, source_map_contents);
623+
sass_option_set_source_map_embed(options, source_map_embed);
624+
sass_option_set_omit_source_map_url(options, omit_source_map_url);
597625
_add_custom_functions(options, custom_functions);
598626
_add_custom_importers(options, custom_importers);
599627
sass_compile_file_context(context);

pysassc.py

100644100755
Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,36 @@
4747
4848
.. versionadded:: 0.11.0
4949
50+
.. option:: --sourcemap-file
51+
52+
Output file for source map
53+
54+
.. versionadded:: 0.17.0
55+
56+
.. option:: --sourcemap-contents
57+
58+
Embed sourcesContent in source map.
59+
60+
.. versionadded:: 0.17.0
61+
62+
.. option:: --sourcemap-embed
63+
64+
Embed sourceMappingUrl as data URI
65+
66+
.. versionadded:: 0.17.0
67+
68+
.. option:: --omit-sourcemap-url
69+
70+
Omit source map URL comment from output
71+
72+
.. versionadded:: 0.17.0
73+
74+
.. option:: --sourcemap-root
75+
76+
Base path, will be emitted to sourceRoot in source-map as is
77+
78+
.. versionadded:: 0.17.0
79+
5080
.. option:: -v, --version
5181
5282
Prints the program version.
@@ -92,6 +122,32 @@ def main(argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr):
92122
help='Emit source map. Requires the second argument '
93123
'(output css filename).',
94124
)
125+
parser.add_option(
126+
'--sourcemap-file', dest='source_map_file', metavar='FILE',
127+
action='store',
128+
help='Output file for source map. If omitted, source map is based on '
129+
'the output css filename',
130+
)
131+
parser.add_option(
132+
'--sourcemap-contents', dest='source_map_contents',
133+
action='store_true', default=False,
134+
help='Embed sourcesContent in source map',
135+
)
136+
parser.add_option(
137+
'--sourcemap-embed', dest='source_map_embed',
138+
action='store_true', default=False,
139+
help='Embed sourceMappingUrl as data URI',
140+
)
141+
parser.add_option(
142+
'--omit-sourcemap-url', dest='omit_source_map_url',
143+
action='store_true', default=False,
144+
help='Omit source map URL comment from output',
145+
)
146+
parser.add_option(
147+
'--sourcemap-root', metavar='DIR',
148+
dest='source_map_root', action='store',
149+
help='Base path, will be emitted to sourceRoot in source-map as is',
150+
)
95151
parser.add_option(
96152
'-I', '--include-path', metavar='DIR',
97153
dest='include_paths', action='append',
@@ -139,12 +195,16 @@ def main(argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr):
139195

140196
try:
141197
if options.source_map:
142-
source_map_filename = args[1] + '.map' # FIXME
198+
source_map_filename = options.source_map_file or args[1] + '.map'
143199
css, source_map = sass.compile(
144200
filename=filename,
145201
output_style=options.style,
146202
source_comments=options.source_comments,
147203
source_map_filename=source_map_filename,
204+
source_map_contents=options.source_map_contents,
205+
source_map_embed=options.source_map_embed,
206+
omit_source_map_url=options.omit_source_map_url,
207+
source_map_root=options.source_map_root,
148208
output_filename_hint=args[1],
149209
include_paths=options.include_paths,
150210
precision=options.precision,

sass.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ def _raise(e):
225225

226226
def compile_dirname(
227227
search_path, output_path, output_style, source_comments, include_paths,
228-
precision, custom_functions, importers,
228+
precision, custom_functions, importers, source_map_contents,
229+
source_map_embed, omit_source_map_url, source_map_root,
229230
):
230231
fs_encoding = sys.getfilesystemencoding() or sys.getdefaultencoding()
231232
for dirpath, _, filenames in os.walk(search_path, onerror=_raise):
@@ -243,6 +244,8 @@ def compile_dirname(
243244
s, v, _ = _sass.compile_filename(
244245
input_filename, output_style, source_comments, include_paths,
245246
precision, None, custom_functions, importers, None,
247+
source_map_contents, source_map_embed, omit_source_map_url,
248+
source_map_root,
246249
)
247250
if s:
248251
v = v.decode('UTF-8')
@@ -284,6 +287,14 @@ def compile(**kwargs):
284287
:param source_comments: whether to add comments about source lines.
285288
:const:`False` by default
286289
:type source_comments: :class:`bool`
290+
:param source_map_contents: embed include contents in map
291+
:type source_map_contents: :class:`bool`
292+
:param source_map_embed: embed sourceMappingUrl as data URI
293+
:type source_map_embed: :class:`bool`
294+
:param omit_source_map_url: omit source map URL comment from output
295+
:type omit_source_map_url: :class:`bool`
296+
:param source_map_root: base path, will be emitted in source map as is
297+
:type source_map_root: :class:`str`
287298
:param include_paths: an optional list of paths to find ``@import``\ ed
288299
Sass/CSS source files
289300
:type include_paths: :class:`collections.abc.Sequence`
@@ -325,6 +336,14 @@ def compile(**kwargs):
325336
output filename. :const:`None` means not
326337
using source maps. :const:`None` by default.
327338
:type source_map_filename: :class:`str`
339+
:param source_map_contents: embed include contents in map
340+
:type source_map_contents: :class:`bool`
341+
:param source_map_embed: embed sourceMappingUrl as data URI
342+
:type source_map_embed: :class:`bool`
343+
:param omit_source_map_url: omit source map URL comment from output
344+
:type omit_source_map_url: :class:`bool`
345+
:param source_map_root: base path, will be emitted in source map as is
346+
:type source_map_root: :class:`str`
328347
:param include_paths: an optional list of paths to find ``@import``\ ed
329348
Sass/CSS source files
330349
:type include_paths: :class:`collections.abc.Sequence`
@@ -368,6 +387,14 @@ def compile(**kwargs):
368387
:param source_comments: whether to add comments about source lines.
369388
:const:`False` by default
370389
:type source_comments: :class:`bool`
390+
:param source_map_contents: embed include contents in map
391+
:type source_map_contents: :class:`bool`
392+
:param source_map_embed: embed sourceMappingUrl as data URI
393+
:type source_map_embed: :class:`bool`
394+
:param omit_source_map_url: omit source map URL comment from output
395+
:type omit_source_map_url: :class:`bool`
396+
:param source_map_root: base path, will be emitted in source map as is
397+
:type source_map_root: :class:`str`
371398
:param include_paths: an optional list of paths to find ``@import``\ ed
372399
Sass/CSS source files
373400
:type include_paths: :class:`collections.abc.Sequence`
@@ -499,6 +526,10 @@ def my_importer(path):
499526
.. versionadded:: 0.11.0
500527
``source_map_filename`` no longer implies ``source_comments``.
501528
529+
.. versionadded:: 0.17.0
530+
Added ``source_map_contents``, ``source_map_embed``,
531+
``omit_source_map_url``, and ``source_map_root`` parameters.
532+
502533
"""
503534
modes = set()
504535
for mode_name in MODES:
@@ -568,6 +599,14 @@ def _get_file_arg(key):
568599
source_map_filename = _get_file_arg('source_map_filename')
569600
output_filename_hint = _get_file_arg('output_filename_hint')
570601

602+
source_map_contents = kwargs.pop('source_map_contents', False)
603+
source_map_embed = kwargs.pop('source_map_embed', False)
604+
omit_source_map_url = kwargs.pop('omit_source_map_url', False)
605+
source_map_root = kwargs.pop('source_map_root', None)
606+
607+
if isinstance(source_map_root, text_type):
608+
source_map_root = source_map_root.encode('utf-8')
609+
571610
# #208: cwd is always included in include paths
572611
include_paths = (os.getcwd(),)
573612
include_paths += tuple(kwargs.pop('include_paths', ()) or ())
@@ -620,6 +659,8 @@ def _get_file_arg(key):
620659
s, v = _sass.compile_string(
621660
string, output_style, source_comments, include_paths, precision,
622661
custom_functions, indented, importers,
662+
source_map_contents, source_map_embed, omit_source_map_url,
663+
source_map_root,
623664
)
624665
if s:
625666
return v.decode('utf-8')
@@ -636,6 +677,8 @@ def _get_file_arg(key):
636677
filename, output_style, source_comments, include_paths, precision,
637678
source_map_filename, custom_functions, importers,
638679
output_filename_hint,
680+
source_map_contents, source_map_embed, omit_source_map_url,
681+
source_map_root,
639682
)
640683
if s:
641684
v = v.decode('utf-8')
@@ -655,6 +698,8 @@ def _get_file_arg(key):
655698
s, v = compile_dirname(
656699
search_path, output_path, output_style, source_comments,
657700
include_paths, precision, custom_functions, importers,
701+
source_map_contents, source_map_embed, omit_source_map_url,
702+
source_map_root,
658703
)
659704
if s:
660705
return

0 commit comments

Comments
 (0)