From b52e429f99cac3cd1aa2bf3ab95f1bfaf478d50d Mon Sep 17 00:00:00 2001 From: Ivo Dancet Date: Sun, 13 Feb 2011 10:25:19 +0100 Subject: [PATCH] Add support for haml.js as a jst template language by allowing newline characters as a configuration option (template_strip_newlines (on|off)). --- lib/jammit.rb | 4 +++- lib/jammit/compressor.rb | 3 ++- test/config/assets-jst-newlines.yml | 1 + test/fixtures/jammed/jst_test_newlines.js | 6 ++++++ test/unit/test_configuration.rb | 7 +++++++ 5 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 test/config/assets-jst-newlines.yml create mode 100644 test/fixtures/jammed/jst_test_newlines.js diff --git a/lib/jammit.rb b/lib/jammit.rb index 8db3ab65..5a717d4b 100644 --- a/lib/jammit.rb +++ b/lib/jammit.rb @@ -50,7 +50,8 @@ class << self :embed_assets, :package_assets, :compress_assets, :gzip_assets, :package_path, :mhtml_enabled, :include_jst_script, :config_path, :javascript_compressor, :compressor_options, :css_compressor_options, - :template_extension, :template_extension_matcher, :allow_debugging + :template_extension, :template_extension_matcher, :allow_debugging, + :template_strip_newlines end # The minimal required configuration. @@ -74,6 +75,7 @@ def self.load_configuration(config_path, soft=false) @mhtml_enabled = @embed_assets && @embed_assets != "datauri" @compressor_options = symbolize_keys(conf[:compressor_options] || {}) @css_compressor_options = symbolize_keys(conf[:css_compressor_options] || {}) + @template_strip_newlines= !(conf[:template_strip_newlines] == false) set_javascript_compressor(conf[:javascript_compressor]) set_package_assets(conf[:package_assets]) set_template_function(conf[:template_function]) diff --git a/lib/jammit/compressor.rb b/lib/jammit/compressor.rb index 7d624316..07fcfce7 100644 --- a/lib/jammit/compressor.rb +++ b/lib/jammit/compressor.rb @@ -98,7 +98,8 @@ def compile_jst(paths) base_path = find_base_path(paths) compiled = paths.map do |path| contents = read_binary_file(path) - contents = contents.gsub(/\n/, '').gsub("'", '\\\\\'') + newline = Jammit.template_strip_newlines ? "" : "\\n" + contents = contents.gsub(/\n/, newline).gsub("'", '\\\\\'') name = template_name(path, base_path) "#{namespace}['#{name}'] = #{Jammit.template_function}('#{contents}');" end diff --git a/test/config/assets-jst-newlines.yml b/test/config/assets-jst-newlines.yml new file mode 100644 index 00000000..952efe77 --- /dev/null +++ b/test/config/assets-jst-newlines.yml @@ -0,0 +1 @@ +template_strip_newlines: off diff --git a/test/fixtures/jammed/jst_test_newlines.js b/test/fixtures/jammed/jst_test_newlines.js new file mode 100644 index 00000000..bce5a45d --- /dev/null +++ b/test/fixtures/jammed/jst_test_newlines.js @@ -0,0 +1,6 @@ +(function(){ +window.JST = window.JST || {}; +var template = function(str){var fn = new Function('obj', 'var p=[],print=function(){p.push.apply(p,arguments);};with(obj){p.push(\''+str.replace(/[\r\t\n]/g, " ").replace(/'(?=[^%]*%>)/g,"\t").split("'").join("\\'").split("\t").join("'").replace(/<%=(.+?)%>/g,"',$1,'").split("<%").join("');").split("%>").join("p.push('")+"');}return p.join('');"); return fn;}; +window.JST['template1'] = template('<%= saying_something %>'); +window.JST['template2'] = template('<% _([1,2,3]).each(function(num) { %>\n
  • \n <%= num %>\n
  • \n<% }) %>'); +})(); \ No newline at end of file diff --git a/test/unit/test_configuration.rb b/test/unit/test_configuration.rb index ce0a7491..29dbb997 100644 --- a/test/unit/test_configuration.rb +++ b/test/unit/test_configuration.rb @@ -62,4 +62,11 @@ def test_jst_compilation assert packed == File.read('test/fixtures/jammed/jst_test.js') end + def test_jst_compilation_with_newlines + Jammit.load_configuration('test/config/assets-jst-newlines.yml') + assert !Jammit.template_strip_newlines + packed = @compressor.compile_jst(glob('test/fixtures/src/*.jst')) + assert packed == File.read('test/fixtures/jammed/jst_test_newlines.js') + end + end