Carlos Daniel Ruvalcaba Valenzuela avatar Carlos Daniel Ruvalcaba Valenzuela committed 0ce67c1

Added Jinja2 extension to compile embedded coffeescript

Comments (0)

Files changed (4)

     app = CoffeeScriptMiddleware(app, bare=False)
 
 This will disable the bare options for all files served with tgext.coffeescript, more granular control of this option
-may be available later depending on the need for it.
+may be available later depending on the need for it.
+
+
+Jinja2 Extension
+----------------------------------
+
+tgext.coffeescript provides an extension for jinja2 templates to compile CoffeeScript embedded directly on your
+templates. To activate it just add to your config/app_config.py file the following::
+
+    from tgext.coffeescript.jinja import CoffeeExtension
+
+    base_config.jinja_extensions = [CoffeeExtension]
+
+Now you can use the coffee tag in your templates::
+
+    <style type="text/javascript">
+    {% coffee "main" %}
+        console.log state for state in ['open', 'close', 'full']
+    {% endless %}
+    </style>
+
+Where "main" is a unique identifier for that CoffeeScript section, this should output nicely as::
+
+    <style type="text/javascript">
+    var state, _i, _len, _ref;
+
+    _ref = ['open', 'close', 'full'];
+    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+      state = _ref[_i];
+      console.log(state);
+    }
+    </style>
+
 [egg_info]
-tag_build = dev
 tag_svn_revision = true
 from setuptools import setup, find_packages
 import sys, os
 
-version = '0.2'
+version = '0.3'
 
 here = os.path.abspath(os.path.dirname(__file__))
 try:
         "License :: OSI Approved :: MIT License",
         "Framework :: TurboGears"
         ],
-      keywords='turbogears2.extension CoffeeScript WSGI',
+      keywords='turbogears2.extension CoffeeScript WSGI jinja2.extension',
       author='Carlos Daniel Ruvalcaba Valenzuela',
       author_email='clsdaniel@gmail.com',
       url='http://bitbucket.org/clsdaniel/tgext.coffeescript',

tgext/coffeescript/jinja.py

+import subprocess
+import os
+from tg import config
+from jinja2 import nodes
+from jinja2.ext import Extension
+
+class CoffeeExtension(Extension):
+    tags = set(['coffee'])
+    
+    def __init__(self, environment):
+        super(CoffeeExtension, self).__init__(environment)
+        self.static_files_path = os.path.abspath(config['pylons.paths']['static_files'])
+
+    def compile_coffee(self, name, caller):
+        body = caller()
+
+        # We are assuming Jinja only calls this function when the template
+        # is loaded or reloaded (cached), thus no caching on our part.
+        coffee = subprocess.Popen(['coffee', "-c", "-b", "-s"], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
+        out, err = coffee.communicate(input=body+"\n")
+
+        # TODO: Handle returned error code from compiler
+        if err:
+            return ""
+
+        return out
+
+    def parse(self, parser):
+        lineno = parser.stream.next().lineno
+        args = [parser.parse_expression()]
+
+        body = parser.parse_statements(['name:endcoffee'], drop_needle=True)
+
+        return nodes.CallBlock(self.call_method('compile_coffee', args), [], [], body).set_lineno(lineno)
+    
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.