forked from imiric/flask-sass
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ivan Miric
committed
Sep 9, 2012
1 parent
c1b2280
commit 12323e6
Showing
5 changed files
with
111 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,39 @@ | ||
flask-coffee2js | ||
=============== | ||
flask-sass | ||
========== | ||
|
||
A small Flask extension that makes it easy to use CoffeeScript with your Flask application. | ||
A small Flask extension that makes it easy to use Sass (SCSS) with your Flask application. | ||
|
||
Why another one? Because [Flask-Scss](https://bitbucket.org/bcarlin/flask-scss) doesn't | ||
compile ``@import``ed files, and I liked the simple way [flask-coffee2js](https://github.com/weapp/flask-coffee2js) | ||
compiled CoffeeScript files. | ||
|
||
Code unabashedly adapted from https://github.com/weapp/flask-coffee2js. | ||
|
||
|
||
## Installation | ||
|
||
### Install with PIP | ||
|
||
pip install flask-coffee2js | ||
pip install git+https://github.com/imiric/flask-sass.git#egg=flask-sass | ||
|
||
|
||
## Usage | ||
|
||
You can activate it by calling the `coffee2js` function with your Flask app as a parameter: | ||
You can activate it by calling the ``sass`` function with your Flask app as a parameter: | ||
|
||
from flaskext.coffee2js import coffee2js | ||
coffee2js(app, js_folder='js', coffee_folder='src/coffee') | ||
from flaskext.sass import sass | ||
sass(app, input_dir='assets/scss', output_dir='static/css') | ||
|
||
This will intercept the request to `js_folder` and compile de file if is necesary using the files from `coffee_folder`. | ||
This will intercept the request for ``output_dir/*.css`` and compile the file if it is | ||
necesary using the files from ``input_dir/*.scss``. | ||
|
||
When you deploy your app you might not want to accept the overhead of checking the modification time of your `.coffee` and `.js` files on each request. A simple way to avoid this is wrapping the coffee2js call in an if statement: | ||
When you deploy your app you might not want to accept the overhead of checking | ||
the modification time of your ``.scss`` and ``.css`` files on each request. A | ||
simple way to avoid this is wrapping the sass call in an if statement: | ||
|
||
if app.debug: | ||
from flaskext.coffee2js import coffee2js | ||
coffee2js(app) | ||
If you do this you’ll be responsible for rendering the `.coffee` files into `.js` when you deploy in non-debug mode to your production server. | ||
if app.debug: | ||
from flaskext.sass import sass | ||
sass(app) | ||
If you do this you'll be responsible for rendering the ``.scss`` files into | ||
``.css`` when you deploy in non-debug mode to your production server. |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
flask.ext.sass | ||
~~~~~~~~~~~~~~ | ||
A small Flask extension that makes it easy to use Sass (SCSS) with your | ||
Flask application. | ||
Code unabashedly adapted from https://github.com/weapp/flask-coffee2js | ||
:copyright: (c) 2012 by Ivan Miric. | ||
:license: MIT, see LICENSE for more details. | ||
""" | ||
|
||
import os | ||
import os.path | ||
import codecs | ||
|
||
from scss import Scss | ||
|
||
def _convert(src, dst): | ||
css = Scss() | ||
source = codecs.open(src, 'r', encoding='utf-8').read() | ||
output = css.compile(source) | ||
outfile = codecs.open(dst, 'w', encoding='utf-8') | ||
outfile.write(output) | ||
outfile.close() | ||
|
||
def sass(app, input_dir='assets/sass', output_dir='css', force=False): | ||
if not hasattr(app, 'static_url_path'): | ||
app.logger.warning(DeprecationWarning('static_path is called ' | ||
'static_url_path since Flask 0.7')) | ||
static_url_path = app.static_path | ||
else: | ||
static_url_path = app.static_url_path | ||
|
||
original_wd = os.getcwd() | ||
|
||
if not os.path.isdir(input_dir): | ||
input_dir = os.path.join(app.root_path, input_dir) | ||
|
||
def _sass(filepath): | ||
sassfile = "%s/%s.scss" % (input_dir, filepath) | ||
filename = "%s/%s.css" % (output_dir, filepath) | ||
cssfile = "%s%s/%s" % (app.root_path, static_url_path, filename) | ||
|
||
if os.path.isfile(sassfile) and (force or not os.path.isfile(cssfile) or \ | ||
os.path.getmtime(sassfile) > os.path.getmtime(cssfile)): | ||
if os.path.isdir(input_dir): | ||
# TODO: Sigh... fix this. Needed so that pyScss can find all the assets. | ||
os.chdir(input_dir) | ||
_convert(sassfile, cssfile) | ||
app.logger.debug('Compiled %s into %s' % (sassfile, cssfile)) | ||
if os.getcwd() != original_wd: | ||
os.chdir(original_wd) | ||
|
||
return app.send_static_file(filename) | ||
|
||
app.add_url_rule("%s/%s/<path:filepath>.css" %(static_url_path, output_dir), 'sass', _sass) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters