aboutsummaryrefslogtreecommitdiff
path: root/app/views/sass.py
diff options
context:
space:
mode:
Diffstat (limited to 'app/views/sass.py')
-rw-r--r--app/views/sass.py67
1 files changed, 67 insertions, 0 deletions
diff --git a/app/views/sass.py b/app/views/sass.py
new file mode 100644
index 0000000..2ae3896
--- /dev/null
+++ b/app/views/sass.py
@@ -0,0 +1,67 @@
+# -*- coding: utf-8 -*-
+"""
+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 flask import *
+from scss import Scss
+
+from app import app
+
+def _convert(dir, src, dst):
+ original_wd = os.getcwd()
+ os.chdir(dir)
+
+ css = Scss()
+ source = codecs.open(src, 'r', encoding='utf-8').read()
+ output = css.compile(source)
+
+ os.chdir(original_wd)
+
+ outfile = codecs.open(dst, 'w', encoding='utf-8')
+ outfile.write(output)
+ outfile.close()
+
+def _getDirPath(originalPath, create=False):
+ path = originalPath
+
+ if not os.path.isdir(path):
+ path = os.path.join(app.root_path, path)
+
+ if not os.path.isdir(path):
+ if create:
+ os.mkdir(path)
+ else:
+ raise IOError("Unable to find " + originalPath)
+
+ return path
+
+def sass(app, inputDir='scss', outputPath='static', force=False, cacheDir=None):
+ static_url_path = app.static_url_path
+ inputDir = _getDirPath(inputDir)
+ cacheDir = _getDirPath(cacheDir or outputPath, True)
+
+ def _sass(filepath):
+ sassfile = "%s/%s.scss" % (inputDir, filepath)
+ cacheFile = "%s/%s.css" % (cacheDir, filepath)
+
+ # Source file exists, and needs regenerating
+ if os.path.isfile(sassfile) and (force or not os.path.isfile(cacheFile) or \
+ os.path.getmtime(sassfile) > os.path.getmtime(cacheFile)):
+ _convert(inputDir, sassfile, cacheFile)
+ app.logger.debug('Compiled %s into %s' % (sassfile, cacheFile))
+
+ return send_from_directory(cacheDir, filepath + ".css")
+
+ app.add_url_rule("/%s/<path:filepath>.css" % (outputPath), 'sass', _sass)
+
+sass(app)