require "execjs" require "steering/source" module Steering module Source def self.path @path ||= ENV["HANDLEBARS_SOURCE_PATH"] || bundled_path end def self.path=(path) @contents = @version = @context = nil @path = path end def self.contents @contents ||= File.read(path) end def self.context @context ||= ExecJS.compile(contents) end def self.runtime_path @runtime_path ||= ENV["HANDLEBARS_RUNTIME_PATH"] || bundled_runtime_path end def self.runtime_path=(runtime_path) @runtime = nil @runtime_path = runtime_path end def self.runtime @runtime_contents ||= File.read(runtime_path) end def self.version @version ||= context.eval("Handlebars.VERSION") end end class << self def version Source.version end def compile(template) template = template.read if template.respond_to?(:read) Source.context.call("Handlebars.precompile", template, { :knownHelpers => known_helpers }) end def compile_to_file(template, file, options = {}) if options.is_a?(String) extension = options partial = false else extension = options[:extension] || ".handlebars" partial = options[:partial] || false end File.open(file, 'w') do |f| name = File.basename(template, extension) template = File.read(template) f.write("\nHandlebars.templates = Handlebars.templates || {};") f.write("\nHandlebars.templates['#{name}'] = Handlebars.template(#{compile(template)});\n") f.write("Handlebars.registerPartial('#{name}', Handlebars.templates['#{name}']);\n") if partial end end def context_for(template, extra = "") ExecJS.compile("#{Source.runtime}; #{extra}; var template = Handlebars.template(#{compile(template)})") end def known_helpers Source.known_helpers end def render(template, *args) locals = args.last.is_a?(Hash) ? args.pop : {} extra = args.first.to_s context_for(template, extra).call("template", locals) end end end