From 5abc03457e1bdc3e36f9392b913cf6130c33141d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Drouyer?= Date: Fri, 4 Jan 2013 18:59:44 +0100 Subject: [PATCH] Added support for external templates system --- lib/guard/templates.rb | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/guard/templates.rb b/lib/guard/templates.rb index 96c26dd..ae9c6a7 100644 --- a/lib/guard/templates.rb +++ b/lib/guard/templates.rb @@ -58,7 +58,7 @@ def run_on_change(paths) dir = Pathname.new(target[:path]).dirname.to_s FileUtils.mkdir_p(dir) if !File.exist?(dir) File.open(target[:path], 'w') do |f| - f.write("#{@options[:namespace]}['#{target[:name]}'] = #{compile(contents, target[:type])}") + f.write("#{@options[:namespace]}['#{target[:name]}'] = #{compile(contents, target)}") end end end @@ -66,7 +66,7 @@ def run_on_change(paths) end if @single_file_mode File.open(@options[:output], 'w') do |f| - js = templates.map{|target,content| "#{target[:name].dump}: #{compile(content, target[:type])}" }.join(",\n") + js = templates.map{|target,content| "#{target[:name].dump}: #{compile(content, target)}" }.join(",\n") f.write "#{@options[:namespace]}.templates = {#{js}}" end end @@ -103,11 +103,17 @@ def get_target(path, watcher) end end - def compile(str, type) + def compile(str, target) + type = target[:type] if Compilers.methods.include?("compile_#{type}") Compilers.send("compile_#{type}", str) else - str.dump + begin + require "guard/templates/#{type}/compiler" + return Templates.const_get(type.capitalize)::Compiler.compile(str, target) + rescue LoadError + return str.dump + end end end