1 # -*- coding: utf-8 -*-
3 A small Flask extension that makes it easy to use Sass (SCSS) with your
6 Code unabashedly adapted from https://github.com/weapp/flask-coffee2js
8 :copyright: (c) 2012 by Ivan Miric.
9 :license: MIT, see LICENSE for more details.
18 def _convert(dir, src, dst):
19 original_wd = os.getcwd()
23 source = codecs.open(src, 'r', encoding='utf-8').read()
24 output = css.compile(source)
28 outfile = codecs.open(dst, 'w', encoding='utf-8')
32 def _getDirPath(app, originalPath, create=False):
35 if not os.path.isdir(path):
36 path = os.path.join(app.root_path, path)
38 if not os.path.isdir(path):
42 raise IOError("Unable to find " + originalPath)
46 def sass(app, inputDir='scss', outputPath='static', force=False, cacheDir="public/static"):
47 static_url_path = app.static_url_path
48 inputDir = _getDirPath(app, inputDir)
49 cacheDir = _getDirPath(app, cacheDir or outputPath, True)
52 sassfile = "%s/%s.scss" % (inputDir, filepath)
53 cacheFile = "%s/%s.css" % (cacheDir, filepath)
55 # Source file exists, and needs regenerating
56 if os.path.isfile(sassfile) and (force or not os.path.isfile(cacheFile) or \
57 os.path.getmtime(sassfile) > os.path.getmtime(cacheFile)):
58 _convert(inputDir, sassfile, cacheFile)
59 app.logger.debug('Compiled %s into %s' % (sassfile, cacheFile))
61 return send_from_directory(cacheDir, filepath + ".css")
63 app.add_url_rule("/%s/<path:filepath>.css" % (outputPath), 'sass', _sass)