Skip to content

Add more cli opts and source map config args #279

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 1 commit into from
Dec 17, 2018
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
44 changes: 36 additions & 8 deletions pysass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,19 +512,24 @@ PySass_compile_string(PyObject *self, PyObject *args) {
struct Sass_Context *ctx;
struct Sass_Data_Context *context;
struct Sass_Options *options;
char *string, *include_paths;
char *string, *include_paths, *source_map_file;
const char *error_message, *output_string;
Sass_Output_Style output_style;
int source_comments, error_status, precision, indented;
int source_comments, error_status, precision, indented,
source_map_embed, source_map_contents, source_map_file_urls,
omit_source_map_url;
PyObject *custom_functions;
PyObject *custom_importers;
PyObject *source_map_root;
PyObject *result;

if (!PyArg_ParseTuple(args,
PySass_IF_PY3("yiiyiOiO", "siisiOiO"),
PySass_IF_PY3("yiiyiOiOiiiO", "siisiOiOiiiO"),
&string, &output_style, &source_comments,
&include_paths, &precision,
&custom_functions, &indented, &custom_importers)) {
&custom_functions, &indented, &custom_importers,
&source_map_contents, &source_map_embed,
&omit_source_map_url, &source_map_root)) {
return NULL;
}

Expand All @@ -535,6 +540,16 @@ PySass_compile_string(PyObject *self, PyObject *args) {
sass_option_set_include_path(options, include_paths);
sass_option_set_precision(options, precision);
sass_option_set_is_indented_syntax_src(options, indented);
sass_option_set_source_map_contents(options, source_map_contents);
sass_option_set_source_map_embed(options, source_map_embed);
sass_option_set_omit_source_map_url(options, omit_source_map_url);

if (PyBytes_Check(source_map_root) && PyBytes_GET_SIZE(source_map_root)) {
sass_option_set_source_map_root(
options, PyBytes_AS_STRING(source_map_root)
);
}

_add_custom_functions(options, custom_functions);
_add_custom_importers(options, custom_importers);
sass_compile_data_context(context);
Expand All @@ -560,16 +575,19 @@ PySass_compile_filename(PyObject *self, PyObject *args) {
char *filename, *include_paths;
const char *error_message, *output_string, *source_map_string;
Sass_Output_Style output_style;
int source_comments, error_status, precision;
int source_comments, error_status, precision, source_map_embed,
source_map_contents, source_map_file_urls, omit_source_map_url;
PyObject *source_map_filename, *custom_functions, *custom_importers,
*result, *output_filename_hint;
*result, *output_filename_hint, *source_map_root;

if (!PyArg_ParseTuple(args,
PySass_IF_PY3("yiiyiOOOO", "siisiOOOO"),
PySass_IF_PY3("yiiyiOOOOiiiO", "siisiOOOOiiiO"),
&filename, &output_style, &source_comments,
&include_paths, &precision,
&source_map_filename, &custom_functions,
&custom_importers, &output_filename_hint)) {
&custom_importers, &output_filename_hint,
&source_map_contents, &source_map_embed,
&omit_source_map_url, &source_map_root)) {
return NULL;
}

Expand All @@ -590,10 +608,20 @@ PySass_compile_filename(PyObject *self, PyObject *args) {
);
}
}

if (PyBytes_Check(source_map_root) && PyBytes_GET_SIZE(source_map_root)) {
sass_option_set_source_map_root(
options, PyBytes_AS_STRING(source_map_root)
);
}

sass_option_set_output_style(options, output_style);
sass_option_set_source_comments(options, source_comments);
sass_option_set_include_path(options, include_paths);
sass_option_set_precision(options, precision);
sass_option_set_source_map_contents(options, source_map_contents);
sass_option_set_source_map_embed(options, source_map_embed);
sass_option_set_omit_source_map_url(options, omit_source_map_url);
_add_custom_functions(options, custom_functions);
_add_custom_importers(options, custom_importers);
sass_compile_file_context(context);
Expand Down
62 changes: 61 additions & 1 deletion pysassc.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,36 @@

.. versionadded:: 0.11.0

.. option:: --sourcemap-file

Output file for source map

.. versionadded:: 0.17.0

.. option:: --sourcemap-contents

Embed sourcesContent in source map.

.. versionadded:: 0.17.0

.. option:: --sourcemap-embed

Embed sourceMappingUrl as data URI

.. versionadded:: 0.17.0

.. option:: --omit-sourcemap-url

Omit source map URL comment from output

.. versionadded:: 0.17.0

.. option:: --sourcemap-root

Base path, will be emitted to sourceRoot in source-map as is

.. versionadded:: 0.17.0

.. option:: -v, --version

Prints the program version.
Expand Down Expand Up @@ -92,6 +122,32 @@ def main(argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr):
help='Emit source map. Requires the second argument '
'(output css filename).',
)
parser.add_option(
'--sourcemap-file', dest='source_map_file', metavar='FILE',
action='store',
help='Output file for source map. If omitted, source map is based on '
'the output css filename',
)
parser.add_option(
'--sourcemap-contents', dest='source_map_contents',
action='store_true', default=False,
help='Embed sourcesContent in source map',
)
parser.add_option(
'--sourcemap-embed', dest='source_map_embed',
action='store_true', default=False,
help='Embed sourceMappingUrl as data URI',
)
parser.add_option(
'--omit-sourcemap-url', dest='omit_source_map_url',
action='store_true', default=False,
help='Omit source map URL comment from output',
)
parser.add_option(
'--sourcemap-root', metavar='DIR',
dest='source_map_root', action='store',
help='Base path, will be emitted to sourceRoot in source-map as is',
)
parser.add_option(
'-I', '--include-path', metavar='DIR',
dest='include_paths', action='append',
Expand Down Expand Up @@ -139,12 +195,16 @@ def main(argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr):

try:
if options.source_map:
source_map_filename = args[1] + '.map' # FIXME
source_map_filename = options.source_map_file or args[1] + '.map'
css, source_map = sass.compile(
filename=filename,
output_style=options.style,
source_comments=options.source_comments,
source_map_filename=source_map_filename,
source_map_contents=options.source_map_contents,
source_map_embed=options.source_map_embed,
omit_source_map_url=options.omit_source_map_url,
source_map_root=options.source_map_root,
output_filename_hint=args[1],
include_paths=options.include_paths,
precision=options.precision,
Expand Down
47 changes: 46 additions & 1 deletion sass.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ def _raise(e):

def compile_dirname(
search_path, output_path, output_style, source_comments, include_paths,
precision, custom_functions, importers,
precision, custom_functions, importers, source_map_contents,
source_map_embed, omit_source_map_url, source_map_root,
):
fs_encoding = sys.getfilesystemencoding() or sys.getdefaultencoding()
for dirpath, _, filenames in os.walk(search_path, onerror=_raise):
Expand All @@ -243,6 +244,8 @@ def compile_dirname(
s, v, _ = _sass.compile_filename(
input_filename, output_style, source_comments, include_paths,
precision, None, custom_functions, importers, None,
source_map_contents, source_map_embed, omit_source_map_url,
source_map_root,
)
if s:
v = v.decode('UTF-8')
Expand Down Expand Up @@ -284,6 +287,14 @@ def compile(**kwargs):
:param source_comments: whether to add comments about source lines.
:const:`False` by default
:type source_comments: :class:`bool`
:param source_map_contents: embed include contents in map
:type source_map_contents: :class:`bool`
:param source_map_embed: embed sourceMappingUrl as data URI
:type source_map_embed: :class:`bool`
:param omit_source_map_url: omit source map URL comment from output
:type omit_source_map_url: :class:`bool`
:param source_map_root: base path, will be emitted in source map as is
:type source_map_root: :class:`str`
:param include_paths: an optional list of paths to find ``@import``\ ed
Sass/CSS source files
:type include_paths: :class:`collections.abc.Sequence`
Expand Down Expand Up @@ -325,6 +336,14 @@ def compile(**kwargs):
output filename. :const:`None` means not
using source maps. :const:`None` by default.
:type source_map_filename: :class:`str`
:param source_map_contents: embed include contents in map
:type source_map_contents: :class:`bool`
:param source_map_embed: embed sourceMappingUrl as data URI
:type source_map_embed: :class:`bool`
:param omit_source_map_url: omit source map URL comment from output
:type omit_source_map_url: :class:`bool`
:param source_map_root: base path, will be emitted in source map as is
:type source_map_root: :class:`str`
:param include_paths: an optional list of paths to find ``@import``\ ed
Sass/CSS source files
:type include_paths: :class:`collections.abc.Sequence`
Expand Down Expand Up @@ -368,6 +387,14 @@ def compile(**kwargs):
:param source_comments: whether to add comments about source lines.
:const:`False` by default
:type source_comments: :class:`bool`
:param source_map_contents: embed include contents in map
:type source_map_contents: :class:`bool`
:param source_map_embed: embed sourceMappingUrl as data URI
:type source_map_embed: :class:`bool`
:param omit_source_map_url: omit source map URL comment from output
:type omit_source_map_url: :class:`bool`
:param source_map_root: base path, will be emitted in source map as is
:type source_map_root: :class:`str`
:param include_paths: an optional list of paths to find ``@import``\ ed
Sass/CSS source files
:type include_paths: :class:`collections.abc.Sequence`
Expand Down Expand Up @@ -499,6 +526,10 @@ def my_importer(path):
.. versionadded:: 0.11.0
``source_map_filename`` no longer implies ``source_comments``.

.. versionadded:: 0.17.0
Added ``source_map_contents``, ``source_map_embed``,
``omit_source_map_url``, and ``source_map_root`` parameters.

"""
modes = set()
for mode_name in MODES:
Expand Down Expand Up @@ -568,6 +599,14 @@ def _get_file_arg(key):
source_map_filename = _get_file_arg('source_map_filename')
output_filename_hint = _get_file_arg('output_filename_hint')

source_map_contents = kwargs.pop('source_map_contents', False)
source_map_embed = kwargs.pop('source_map_embed', False)
omit_source_map_url = kwargs.pop('omit_source_map_url', False)
source_map_root = kwargs.pop('source_map_root', None)

if isinstance(source_map_root, text_type):
source_map_root = source_map_root.encode('utf-8')

# #208: cwd is always included in include paths
include_paths = (os.getcwd(),)
include_paths += tuple(kwargs.pop('include_paths', ()) or ())
Expand Down Expand Up @@ -620,6 +659,8 @@ def _get_file_arg(key):
s, v = _sass.compile_string(
string, output_style, source_comments, include_paths, precision,
custom_functions, indented, importers,
source_map_contents, source_map_embed, omit_source_map_url,
source_map_root,
)
if s:
return v.decode('utf-8')
Expand All @@ -636,6 +677,8 @@ def _get_file_arg(key):
filename, output_style, source_comments, include_paths, precision,
source_map_filename, custom_functions, importers,
output_filename_hint,
source_map_contents, source_map_embed, omit_source_map_url,
source_map_root,
)
if s:
v = v.decode('utf-8')
Expand All @@ -655,6 +698,8 @@ def _get_file_arg(key):
s, v = compile_dirname(
search_path, output_path, output_style, source_comments,
include_paths, precision, custom_functions, importers,
source_map_contents, source_map_embed, omit_source_map_url,
source_map_root,
)
if s:
return
Expand Down
Loading