From b1d5107f3ddb3a144e066ebb8d19e2961fb3d337 Mon Sep 17 00:00:00 2001 From: Ilker Cetinkaya Date: Sat, 9 Feb 2013 23:04:48 +0100 Subject: [PATCH] first quick rjs plugin implementation. --- hyde/ext/plugins/rjs.py | 67 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 hyde/ext/plugins/rjs.py diff --git a/hyde/ext/plugins/rjs.py b/hyde/ext/plugins/rjs.py new file mode 100644 index 0000000..27c1776 --- /dev/null +++ b/hyde/ext/plugins/rjs.py @@ -0,0 +1,67 @@ +# -*- coding: utf-8 -*- +""" +requirejs plugin +""" + +from hyde.plugin import CLTransformer +from hyde.fs import File + +import re +import subprocess + + +class RequireJSPlugin(CLTransformer): + """ + The plugin class for requirejs + """ + + def __init__(self, site): + super(RequireJSPlugin, self).__init__(site) + + @property + def executable_name(self): + return "r.js" + + def _should_replace_imports(self, resource): + return getattr(resource, 'meta', {}).get('uses_template', True) + + def begin_site(self): + """ + Find all the less css files and set their relative deploy path. + """ + for resource in self.site.content.walk_resources(): + if resource.source_file.name_without_extension == "main" and resource.source_file.kind == "js": + new_name = resource.source_file.name_without_extension + "-bin.js" + target_folder = File(resource.relative_deploy_path).parent + resource.relative_deploy_path = target_folder.child(new_name) + + @property + def plugin_name(self): + """ + The name of the plugin. + """ + return "rjs" + + def text_resource_complete(self, resource, text): + """ + Save the file to a temporary place and run less compiler. + Read the generated file and return the text as output. + Set the target path to have a css extension. + """ + if not resource.source_file.kind == 'js' and not resource.source_file.name_without_extension == "main": + return + + rjs = self.app + source = File.make_temp(text) + target = File.make_temp('') + args = [unicode(rjs)] + args.extend(['-o']) + args.extend([unicode(source)]) + args.extend([("out=" + target.fully_expanded_path)]) + try: + self.call_app(args) + except subprocess.CalledProcessError: + raise self.template.exception_class( + "Cannot process %s. Error occurred when " + "processing [%s]" % (self.app.name, resource.source_file)) + return target.read_all()