1
1
pyScss, a Scss compiler for Python
2
2
==================================
4
German M. Bravo (Kronuz) <german.mb@gmail.com>
8
pyScss compiles Scss (Sass), a superset of CSS that is more powerful, elegant
9
and easier to maintain than plain-vanilla CSS. The library acts as a CSS source
10
code preprocesor which allows you to use variables, nested rules, mixins, and
11
have inheritance of rules, all with a CSS-compatible syntax which the
12
preprocessor then compiles to standard CSS.
14
Scss, as an extension of CSS, helps keep large stylesheets well-organized. It
15
borrows concepts and functionality from projects such as OOCSS and other similar
16
frameworks like as Sass. It's build on top of the original PHP xCSS codebase
17
structure but it's been completely rewritten, many bugs have been fixed and it
18
has been extensively extended to support almost the full range of Sass' Scss
19
syntax and functionality.
21
.. image:: http://pledgie.com/campaigns/16513.png?skin_name=chrome
22
:alt: Click here to lend your support to pyScss and make a donation at pledgie.com!
23
:target: http://pledgie.com/campaigns/16513
27
pyScss is fully compatible with SCSS (Sass) 3.2 ...it has:
29
* **Compass**: Compass 0.11 Support
31
* **Keyword arguments**
32
* **Mixins**: `@mixin`, `@include`
33
* **Functions**: `@function`, `@return`
34
* **Inheritance**: `@extend`
35
* **Conditions**: `@if`, `@else if`, `@else`
36
* **Loops**: `@for`, `@each`
37
* **Variables**: `$`, `@variables`, `@vars`
38
* **Sprites**: `sprite-map()`, `sprite()`, `sprite-position()`, `sprite-url()`, ...
39
* **Images**: `image-url()`, `image-width()`, `image-height()`, ...
40
* **Embedded (inline) images**: `inline-image()`
41
* **Colors handling**: `adjust-color()`, `scale-color()`, `opacify()`/`transparentize()`, `lighten()`/`darken()`, `mix()`, ...
42
* **Math functions**: `sin()`, `cos()`, `tan()`, `round()`, `ceil()`, `floor()`, `pi()`, ...
43
* **CSS Compression**: `@option compress:yes;`
51
pyScss should be installed using pip or setuptools::
63
css.compile("a { color: red + green; }")
65
Or compile from the command line::
67
python -mscss < file.scss
71
python -mscss --interactive
75
``-mscss`` will only work in Python 2.7 and above, for Python 2.5
76
and 2.6 you need to invoke::
107
@option compress: no;
108
$main-color: #ce4dd6;
121
border-bottom-color: #ce4dd6;
122
border-bottom-style: solid;
128
@option compress: no;
129
@mixin rounded($side, $radius: 10px) {
130
border-#{$side}-radius: $radius;
131
-moz-border-radius-#{$side}: $radius;
132
-webkit-border-#{$side}-radius: $radius;
134
#navbar li { @include rounded(top); }
135
#footer { @include rounded(top, 5px); }
136
#sidebar { @include rounded(left, 8px); }
141
border-top-radius: 10px;
142
-moz-border-radius-top: 10px;
143
-webkit-border-top-radius: 10px;
146
border-top-radius: 5px;
147
-moz-border-radius-top: 5px;
148
-webkit-border-top-radius: 5px;
151
border-left-radius: 8px;
152
-moz-border-radius-left: 8px;
153
-webkit-border-left-radius: 8px;
156
#. **Extend** (using `@extend`)
159
@option compress: no;
162
background-color: #fdd;
165
background-image: url("/image/hacked.png");
177
background-color: #fdd;
180
.seriousError.intrusion {
181
background-image: url("/image/hacked.png");
187
#. **Sprites** (using `sprite-map()`)
190
@option compress: no;
191
$icons: sprite-map("sociable/*.png"); // contains sociable/facebook.png among others.
195
@each $icon in sprites($icons) {
197
width: image-width(sprite-file($icons, $icon));
198
height: image-height(sprite-file($icons, $icon));
199
background-position: sprite-position($icons, $icon);
203
...generates a new sprite file and produces something like::
206
background: url("/static/assets/u8Y7yEQL0UffAVw5rX7yhw.png?_=1298240989") 0px 0px no-repeat;
211
background-position: 0px 0px;
216
background-position: 0px -32px;
220
#. **Interactive mode**
223
$ python scss.py --interactive
224
>>> @import "compass/css3"
226
['functions', 'mixins', 'options', 'vars']
232
>>> show(mixins, transparent)
233
@mixin transparent() {
242
pyScss is a Scss (Sass) implementation for Python.
243
Currently it implements @mixin, @include, @if, @else, @for, and @import... it
244
also implements many of the Sass functions including color functions like
245
hsla(), hsl(), darken(), lighten(), mix(), opacify(), transparentize(),
246
saturate(), desaturate(), etc.) as well as sprite-map(), sprite-file(),
247
image-width(), image-height() and the others.
249
In the file `scss.py`, by the top, you can configure the LOAD_PATHS to point to
250
your Sass frameworks path (I have `sass/frameworks/compass/*.scss` and
251
`sass/framework/blueprint/*.scss` files in my project directory:
252
`/usr/local/www/project/`, where `scss.py` lives, so it defaults to use the
253
`sass/framework/` path, relative to the `scss.py` file) or configure using the
254
command line `--load-path` option, see `python scss.py --help`.
256
I have succesfully compiled some Compass using `python scss.py < myfile.css` the
257
following `myfile.css`::
259
@option compress: no;
261
$blueprint-grid-columns : 24;
262
$blueprint-grid-width : 30px;
263
$blueprint-grid-margin : 10px;
266
@import "compass/reset";
267
@import "compass/utilities";
270
// Stuff goes here...
274
The following shows some code that can be used with django::
281
from django.conf import settings
282
from django.utils.datastructures import SortedDict
283
from django.contrib.staticfiles import finders
288
Finds all files in the django finders for a given glob,
289
returns the file path, if available, and the django storage object.
290
storage objects must implement the File storage API:
291
https://docs.djangoproject.com/en/dev/ref/files/storage/
293
for finder in finders.get_finders():
294
for path, storage in finder.list([]):
295
if fnmatch.fnmatchcase(path, glob):
299
# STATIC_ROOT is where pyScss looks for images and static data.
300
# STATIC_ROOT can be either a fully qualified path name or a "finder"
301
# iterable function that receives a filename or glob and returns a tuple
302
# of the file found and its file storage object for each matching file.
303
# (https://docs.djangoproject.com/en/dev/ref/files/storage/)
304
scss.STATIC_ROOT = finder
305
scss.STATIC_URL = settings.STATIC_URL
307
# ASSETS_ROOT is where the pyScss outputs the generated files such as spritemaps
309
scss.ASSETS_ROOT = os.path.join(settings.MEDIA_ROOT, 'assets/')
310
scss.ASSETS_URL = settings.MEDIA_URL + 'assets/'
312
# These are the paths pyScss will look ".scss" files on. This can be the path to
313
# the compass framework or blueprint or compass-recepies, etc.
315
'/usr/local/www/sass/frameworks/',
316
'/Library/Ruby/Gems/1.8/gems/compass-0.11.5/frameworks/compass/stylesheets/',
317
'/Library/Ruby/Gems/1.8/gems/compass-0.11.5/frameworks/blueprint/stylesheets/',
320
# This creates the Scss object used to compile SCSS code. In this example,
321
# _scss_vars will hold the context variables:
324
scss_vars=_scss_vars,
331
# 1. Compile from a string:
332
compiled_css_from_string = _scss.compile('@import "file2"; a {color: red + green; }')
334
# 2. Compile from a file:
335
compiled_css_from_file = _scss.compile(scss_file='file1.scss')
337
# 3. Compile from a set of files (use SortedDict or collections.OrderedDict to
338
# maintain the compile order):
339
_scss._scss_files = SortedDict((
340
('file2.scss', open('file2.scss').read()),
341
('file3.scss', open('file3.scss').read()),
342
('file4.scss', open('file4.scss').read()),
344
compiled_css_from_files = _scss.compile()
349
If you have any suggestions, bug reports or annoyances please report them to the
350
issue tracker at http://github.com/Kronuz/pyScss/issues
357
+ ``debug_info`` now properly produces rules that can be used by FireSass and Google Chrome SASS Source Maps.
358
+ Improved memory usage for large sets of files to be used as sprites.
359
+ Warns about IE 4095 maximum number of selectors.
360
+ ``debug_info`` prints info as comments if specified as ``comments``.
361
+ Better handling of undefined variables.
362
+ Added CSS filter functions and ``skewX`` ``skewY``.
363
+ Command line tool and entry point fixed.
364
+ Fix cache buster URLs when paths already include queries or fragments.
368
+ Added ``--debug-info`` command line option (for *FireSass* output).
369
+ Added compass helper function ``reject()``.
370
+ Added ``undefined`` keyword for undefined variables.
373
+ Support for the new Sass 3.2.0 features (``@content`` and placeholder selectors)
374
+ Fixed bug with line numbers throwing an exception.
377
+ Regression bug fixed from 1.1.1
380
+ Added optional C speedup module for an amazing boost in scanning speed!
381
+ Added ``headings``, ``stylesheet-url``, ``font-url``, ``font-files``, ``inline-font-files`` and ``sprite-names``.
384
+ Added ``min()`` and ``max()`` for lists.
385
+ Removed exception raise.
388
+ Optimizations in the scanner.
389
+ Added ``background-noise()`` for compass-recipes support.
390
+ ``enumerate()`` and ``range()`` can go backwards. Ex.: ``range(3, 0)`` goes from 3 to 0.
391
+ Added line numbers and files for errors.
392
+ Added support for *Firebug* with *FireSass*.
393
+ ``nth(n)`` is round (returns the ``nth mod len`` item of the list).
394
+ ``--watch`` added to the command line.
395
+ Several bugs fixed.
398
+ Changed source color (``$src-color``) default to black.
399
+ Moved the module filename to ``__init__.py`` and module renamed back to scss.
404
Development of pyScss happens at github: https://github.com/Kronuz/pyScss
409
MIT License. See *LICENSE* for details.
410
http://www.opensource.org/licenses/mit-license.php
415
Copyright (c) 2012 German M. Bravo (Kronuz)
416
*Bits of code in pyScss come from various projects:*
419
(c) 2009 Christopher M. Eppstein
420
http://compass-style.org/
422
(c) 2006-2009 Hampton Catlin and Nathan Weizenbaum
423
http://sass-lang.com/
425
(c) 2010 Anton Pawlik
426
http://xcss.antpaw.org/docs/
4
pyScss is a compiler for SCSS flavor of the Sass language, a superset of CSS3
5
that adds programming capabilities and some other syntactic sugar.
10
You need Python 2.6 or later. Python 3 is also supported.
18
python -mscss < style.scss
24
compiler.compile("a { color: red + green; }")
30
95% of Sass 3.2 is supported. If it's not supported, it's a bug! Please file
33
Most of Compass 0.11 is also built in.
39
Documentation is in Sphinx. You can build it yourself by running ``make html``
40
from within the ``docs`` directory, or read it on RTD:
41
http://pyscss.readthedocs.org/en/latest/
43
The canonical syntax reference is part of the Ruby Sass documentation:
44
http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html
50
Copyright © 2012 German M. Bravo (Kronuz). Additional credits in the
53
Licensed under the `MIT license`_, reproduced in ``LICENSE``.
55
.. _MIT license: http://www.opensource.org/licenses/mit-license.php