[PATCH PoC] Use webassets and lesscpy to precompile LESS into CSS and (optionally) minify JavaScript

Andrew Shadura andrew at shadura.me
Mon Apr 27 06:12:10 EDT 2015


diff --git a/kallithea/config/environment.py b/kallithea/config/environment.py
--- a/kallithea/config/environment.py
+++ b/kallithea/config/environment.py
@@ -33,6 +33,7 @@ from kallithea.config.routing import mak
 
 from kallithea.lib import helpers
 from kallithea.lib.auth import set_available_permissions
+from kallithea.lib.lessfilter import LessFilter
 from kallithea.lib.utils import repo2db_mapper, make_ui, set_app_settings,\
     load_rcextensions, check_git_version, set_vcs_config
 from kallithea.lib.utils2 import engine_from_config, str2bool
@@ -108,6 +109,29 @@ def load_environment(global_conf, app_co
         if test_index:
             create_test_index(TESTS_TMP_PATH, config, True)
 
+    import webassets
+    webassets.filter.register_filter(LessFilter)
+    wa_env = webassets.Environment(paths['static_files'], '')
+    wa_cache = os.path.join(app_conf['cache_dir'], 'webassets')
+    if not os.path.exists(wa_cache):
+        os.makedirs(wa_cache)
+
+    wa_env.cache = wa_cache
+
+    try:
+        import jsmin
+        js = webassets.Bundle('js/base.js',
+                    filters='jsmin', output='gen/packed.js')
+        wa_env.register('js_all', js)
+        wa_env['js_all'].urls()
+    except ImportError:
+        pass
+
+    css = webassets.Bundle('css/kallithea.less',
+                filters='lesscpy', output='gen/kallithea.css')
+    wa_env.register('css_all', css)
+    wa_env['css_all'].urls()
+
     DbManage.check_waitress()
     # MULTIPLE DB configs
     # Setup the SQLAlchemy database engine
diff --git a/kallithea/lib/lessfilter.py b/kallithea/lib/lessfilter.py
new file mode 100644
--- /dev/null
+++ b/kallithea/lib/lessfilter.py
@@ -0,0 +1,15 @@
+from webassets.filter import Filter
+
+import lesscpy
+
+__all__ = ('LessFilter',)
+
+
+class LessFilter(Filter):
+    """LESS compiler filter
+    """
+
+    name = 'lesscpy'
+
+    def input(self, _in, out, **kw):
+        out.write(lesscpy.compile(_in))
diff --git a/kallithea/public/css/kallithea.less b/kallithea/public/css/kallithea.less
new file mode 100644
--- /dev/null
+++ b/kallithea/public/css/kallithea.less
@@ -0,0 +1,6 @@
+// main lesscss style sheet for lesscss_example
+
+ at color: #ccc;
+ at border: thin solid black;
+
+blockquote {background-color: @color; border: @border;}
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -57,6 +57,8 @@ requirements = [
     "URLObject==2.3.4",
     "Routes==1.13",
     "dulwich>=0.9.9,<=0.9.9",
+    "webassets>=0.10",
+    "lesscpy>=0.10.2"
 ]
 
 if sys.version_info < (2, 7):


More information about the kallithea-general mailing list