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.
20 def _convert(dir, src, dst):
21 original_wd = os.getcwd()
25 source = codecs.open(src, 'r', encoding='utf-8').read()
26 output = css.compile(source)
30 outfile = codecs.open(dst, 'w', encoding='utf-8')
34 def _getDirPath(originalPath, create=False):
37 if not os.path.isdir(path):
38 path = os.path.join(app.root_path, path)
40 if not os.path.isdir(path):
44 raise IOError("Unable to find " + originalPath)
48 def sass(app, inputDir='scss', outputPath='static', force=False, cacheDir="public/static"):
49 static_url_path = app.static_url_path
50 inputDir = _getDirPath(inputDir)
51 cacheDir = _getDirPath(cacheDir or outputPath, True)
54 sassfile = "%s/%s.scss" % (inputDir, filepath)
55 cacheFile = "%s/%s.css" % (cacheDir, filepath)
57 # Source file exists, and needs regenerating
58 if os.path.isfile(sassfile) and (force or not os.path.isfile(cacheFile) or \
59 os.path.getmtime(sassfile) > os.path.getmtime(cacheFile)):
60 _convert(inputDir, sassfile, cacheFile)
61 app.logger.debug('Compiled %s into %s' % (sassfile, cacheFile))
63 return send_from_directory(cacheDir, filepath + ".css")
65 app.add_url_rule("/%s/<path:filepath>.css" % (outputPath), 'sass', _sass)