Skip to content

Commit

Permalink
Converted coffee2js to sass
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Miric committed Sep 9, 2012
1 parent c1b2280 commit 12323e6
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 85 deletions.
6 changes: 3 additions & 3 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Copyright 2012 Manuel Albarran
twitter: @weapp
Copyright 2012 Ivan Miric
twitter: @imiric

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand All @@ -18,4 +18,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 changes: 24 additions & 14 deletions README.md
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.
49 changes: 0 additions & 49 deletions flaskext/coffee2js.py

This file was deleted.

59 changes: 59 additions & 0 deletions flaskext/sass.py
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)
44 changes: 25 additions & 19 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,62 @@
"""
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.
Package unabashedly adapted from https://github.com/weapp/flask-coffee2js.
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 ``.jss`` 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)
from flaskext.sass import sass
sass(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 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.
- documentation_
- development_
.. _documentation: https://github.com/weapp/flask-coffee2js
.. _development: https://github.com/weapp/flask-coffee2js
.. _documentation: https://github.com/imiric/flask-sass
.. _development: https://github.com/imiric/flask-sass
"""

from setuptools import setup


setup(
name='flask-coffee2js',
version='0.1.2',
url='https://github.com/weapp/flask-coffee2js',
name='flask-sass',
version='0.1',
url='https://github.com/imiric/flask-sass',
license='MIT',
author='Manuel Albarran',
author='Ivan Miric',
#author_email='',
description='A small Flask extension that adds CoffeScript support to Flask.',
description='A small Flask extension that adds Sass (SCSS) support to Flask.',
long_description=__doc__,
packages=['flaskext'],
namespace_packages=['flaskext'],
zip_safe=False,
platforms='any',
install_requires=[
'Flask',
'CoffeeScript'
'pyScss'
],
classifiers=[
'Development Status :: 4 - Beta',
Expand Down

0 comments on commit 12323e6

Please sign in to comment.