Source

tgext.less / tgext / less / jinja.py

import subprocess
import os
from tg import config
from jinja2 import nodes
from jinja2.ext import Extension

class LessExtension(Extension):
    tags = set(['less'])
    
    def __init__(self, environment):
        super(LessExtension, self).__init__(environment)
        self.static_files_path = os.path.abspath(config['pylons.paths']['static_files'])

    def compile_less(self, name, caller):
        less_body = caller()

        # We are assuming Jinja only calls this function when the template
        # is loaded or reloaded (cached), thus no caching on our part.
        lessc = subprocess.Popen(['lessc', "-x", "-I" + self.static_files_path, '-'], stdout=subprocess.PIPE, stdin=subprocess.PIPE)

        # TODO: EOF Character is not crossplatform, 125 works for linux need to find what works for windows (probably 26)
        out, err = lessc.communicate(input=less_body+chr(125))

        # 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()]

        less_body = parser.parse_statements(['name:endless'], drop_needle=True)

        return nodes.CallBlock(self.call_method('compile_less', args), [], [], less_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.