require "hache" class Mote PATTERN = /^(\n)|^\s*(%)\s*(.*?)(?:\n|\Z)|(<\?)\s+(.*?)\s+\?>|(\{\{>?)(.*?)\}\}/m def self.parse(template, context = self, vars = []) terms = template.split(PATTERN) parts = "Proc.new do |params, __o|\n params ||= {}; __o ||= ''\n" vars.each do |var| parts << "%s = params[%s]\n" % [var, var.inspect] end while term = terms.shift case term when "" then parts << "__o << (#{terms.shift}).to_s\n" else parts << "__o << #{term.dump}\n" end end parts << "__o; end" compile(context, parts) end def self.compile(context, parts) context.instance_eval(parts) end module Helpers def mote(file, params = {}, context = self) mote_cache[file] ||= Mote.parse(File.read(file), context, params.keys) mote_cache[file][params] end def mote_cache Thread.current[:_mote_cache] ||= {} end end end