Skip to content

Instantly share code, notes, and snippets.

@sheehan
Created December 13, 2012 03:55
Show Gist options
  • Save sheehan/4273877 to your computer and use it in GitHub Desktop.
Save sheehan/4273877 to your computer and use it in GitHub Desktop.
package org.grails.plugin.handlebars
import org.mozilla.javascript.Scriptable
import org.mozilla.javascript.Function
import org.mozilla.javascript.Context
import org.mozilla.javascript.tools.shell.Global
class Precompiler {
private Scriptable scope
private Function precompile
Precompiler() {
ClassLoader classLoader = getClass().classLoader
URL handlebars = classLoader.getResource('handlebars-1.0.rc.1.js')
Context cx = Context.enter()
cx.optimizationLevel = 9
Global global = new Global()
global.init cx
scope = cx.initStandardObjects(global)
cx.evaluateString scope, handlebars.text, handlebars.file, 1, null
precompile = scope.get("Handlebars", scope).get("precompile", scope)
Context.exit();
}
void precompile(File input, File target, String templateName) {
String compiledTemplate = precompileTemplate(input.text)
String output = wrap(templateName, compiledTemplate)
target.write output
}
String precompileTemplate(String contents) {
call precompile, contents
}
private synchronized String call(Function fn, Object[] args) {
Context.call(null, fn, scope, scope, args)
}
private String wrap(String templateName, String compiledTemplate) {
"""
(function(){
var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
templates['$templateName'] = template($compiledTemplate);
}());
"""
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment