haml_coffee_assets 0.2.6 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Haml Coffee Assets [![Build Status](https://secure.travis-ci.org/netzpirat/haml_coffee_assets.png)](http://travis-ci.org/netzpirat/haml_coffee_assets)
2
2
 
3
- Haml Coffee Assets compiles [Haml CoffeeScript](https://github.com/9elements/haml-coffee) templates in the Rails 3.1
3
+ Haml Coffee Assets compiles [Haml Coffee](https://github.com/9elements/haml-coffee) templates in the Rails 3.1
4
4
  asset pipeline, so you can use them as JavaScript templates in your JavaScript heavy Rails application.
5
5
 
6
6
  Tested on MRI Ruby 1.8.7, 1.9.2, 1.9.3, REE and the latest version of JRuby.
@@ -214,7 +214,7 @@ config.hamlcoffee.escapeHtml = false
214
214
  By default your code block in your Haml Coffee template will be escaped through the `HAML.escape` function that is
215
215
  provided in the `hamlcoffee.js` script.
216
216
 
217
- You can set another escaping function in your `config/application.rb`:
217
+ You can set a custom escaping function in your `config/application.rb`:
218
218
 
219
219
  ```ruby
220
220
  config.hamlcoffee.customHtmlEscape = 'App.myEscape'
@@ -232,6 +232,24 @@ App.myEscape = (text) ->
232
232
  .replace(/"/g, '"')
233
233
  ```
234
234
 
235
+ #### Custom clean value function
236
+
237
+ All your evaluated CoffeeScript code will go through the clean value function. By default this simply cleans `undefined`
238
+ and `null` values, so that they appear as empty string in your template.
239
+
240
+ You can set a custom clean value function in your `config/application.rb`:
241
+
242
+ ```ruby
243
+ config.hamlcoffee.customCleanValue = 'App.myCleanValue'
244
+ ```
245
+
246
+ Your custom clean value function must take the value as parameter and returns the cleaned value.
247
+ The following default implementation comes with `hamlcoffee.js`:
248
+
249
+ ```coffeescript
250
+ App.myCleanValue = (value) -> if value is null or value is undefined then '' else value
251
+ ```
252
+
235
253
  ### Global context
236
254
 
237
255
  Haml Coffee Assets allows you to configure a global context function that gets merged into the local template context for
@@ -289,6 +307,15 @@ App.globalTemplateContext = (locals) -> _.extend({}, {
289
307
  }, locals)
290
308
  ```
291
309
 
310
+ ## Using on Heroku
311
+
312
+ Using haml_coffee_assets on Heroku with Ruby on Rails 3.1 requires you to set
313
+
314
+ config.assets.initialize_on_precompile = true
315
+
316
+ in your `config/environments/production.rb`. Read more about the it at the
317
+ [Asset Pipeline Guide](http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets).
318
+
292
319
  ## Acknowledgement
293
320
 
294
321
  * [Jeremy Ashkenas](http://twitter.com/#!/jashkenas) for CoffeeScript, that little language that compiles into
@@ -14,6 +14,7 @@ module HamlCoffeeAssets
14
14
  :escapeHtml => true,
15
15
  :escapeAttributes => true,
16
16
  :customHtmlEscape => 'HAML.escape',
17
+ :customCleanValue => 'HAML.cleanValue',
17
18
  :context => 'HAML.context'
18
19
  }
19
20
 
@@ -34,6 +35,7 @@ module HamlCoffeeAssets
34
35
  config.escapeHtml = options[:escapeHtml]
35
36
  config.escapeAttributes = options[:escapeAttributes]
36
37
  config.customHtmlEscape = options[:customHtmlEscape]
38
+ config.customCleanValue = options[:customCleanValue]
37
39
  config.context = options[:context]
38
40
  end
39
41
  end
@@ -31,6 +31,11 @@ module HamlCoffeeAssets
31
31
  mattr_accessor :customHtmlEscape
32
32
  self.customHtmlEscape = 'window.HAML.escape'
33
33
 
34
+ # Custom global code clean value function
35
+ #
36
+ mattr_accessor :customCleanValue
37
+ self.customCleanValue = 'window.HAML.cleanValue'
38
+
34
39
  # Custom global context to merge
35
40
  #
36
41
  mattr_accessor :context
@@ -53,7 +58,7 @@ module HamlCoffeeAssets
53
58
  def compile(name, source)
54
59
  runtime.call('HamlCoffeeAssets.compile', name, source,
55
60
  HamlCoffee.namespace, HamlCoffee.format, HamlCoffee.escapeHtml, HamlCoffee.escapeAttributes,
56
- HamlCoffee.customHtmlEscape, HamlCoffee.context)
61
+ HamlCoffee.customHtmlEscape, HamlCoffee.customCleanValue, HamlCoffee.context)
57
62
  end
58
63
 
59
64
  private
@@ -25,15 +25,17 @@ var HamlCoffeeAssets = (function(){
25
25
  * @param escapeHtml [Boolean] whether to escape HTML output by default or not
26
26
  * @param escapeAttributes [Boolean] whether to escape HTML attributes output by default or not
27
27
  * @param customHtmlEscape [String] the name of the function to escape the output
28
+ * @param customCleanValue [String] the name of the function to clean the code output
28
29
  * @param context [String] the name of the function to merge contexts
29
30
  */
30
- compile: function(name, source, namespace, format, escapeHtml, escapeAttributes, customHtmlEscape, context) {
31
+ compile: function(name, source, namespace, format, escapeHtml, escapeAttributes, customHtmlEscape, customCleanValue, context) {
31
32
 
32
33
  var compiler = new Compiler({
33
34
  format: format,
34
35
  escapeHtml: escapeHtml,
35
36
  escapeAttributes: escapeAttributes,
36
- customHtmlEscape: customHtmlEscape
37
+ customHtmlEscape: customHtmlEscape,
38
+ customCleanValue: customCleanValue
37
39
  });
38
40
 
39
41
  compiler.parse(source);
@@ -1,5 +1,5 @@
1
1
  # coding: UTF-8
2
2
 
3
3
  module HamlCoffeeAssets
4
- VERSION = '0.2.6' unless defined?(HamlCoffeeAssets::VERSION)
4
+ VERSION = '0.3.0' unless defined?(HamlCoffeeAssets::VERSION)
5
5
  end
@@ -578,9 +578,9 @@ require.define("/nodes/haml.js", function (require, module, exports, __dirname,
578
578
  if (identifier === '~') {
579
579
  code = p(assignment);
580
580
  } else if (identifier === '&=' || (identifier === '=' && this.escapeHtml)) {
581
- code = "\#{e " + assignment + "}";
581
+ code = "\#{e(c(" + assignment + "))}";
582
582
  } else if (identifier === '!=' || (identifier === '=' && !this.escapeHtml)) {
583
- code = "\#{" + assignment + "}";
583
+ code = "\#{c(" + assignment + ")}";
584
584
  }
585
585
  this.opener = this.markText("" + prefix + ">" + code);
586
586
  return this.closer = this.markText("</" + tokens.tag + ">");
@@ -708,9 +708,9 @@ require.define("/nodes/haml.js", function (require, module, exports, __dirname,
708
708
  bool = true;
709
709
  } else if (!value.match(/^("|').*\1$/)) {
710
710
  if (this.escapeAttributes) {
711
- value = '\'#{ e(' + value + ') }\'';
711
+ value = '\'#{ e(c(' + value + ')) }\'';
712
712
  } else {
713
- value = '\'#{ ' + value + ' }\'';
713
+ value = '\'#{ c(' + value + ') }\'';
714
714
  }
715
715
  }
716
716
  if (quoted = value.match(/^("|')(.*)\1$/)) value = quoted[2];
@@ -1191,7 +1191,7 @@ require.define("/compiler.js", function (require, module, exports, __dirname, __
1191
1191
  whitespace = result[1];
1192
1192
  expression = result[2];
1193
1193
  if (/^(\s)*$/.test(line)) continue;
1194
- while (/^%.*[{(]/.test(expression) && !/^(\s*)[.%<]/.test(lines[0]) && /(?:(\w+[\w:-]*\w?|'\w+[\w:-]*\w?'|"\w+[\w:-]*\w?")\s*=\s*("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|[\w@.]+)|(:\w+[\w:-]*\w?|'\w+[\w:-]*\w?'|"\w+[\w:-]*\w?")\s*=>\s*("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|[-\w@.()\[\]'"]+)|(\w+[\w:-]*\w?|'\w+[\w:-]*\w?'|'\w+[\w:-]*\w?'):\s*("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|[-\w@.()\[\]'"]+))/.test(lines[0])) {
1194
+ while (/^%.*[{(]/.test(expression) && !/^(\s*)[.%#<]/.test(lines[0]) && /(?:(\w+[\w:-]*\w?|'\w+[\w:-]*\w?'|"\w+[\w:-]*\w?")\s*=\s*("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|[\w@.]+)|(:\w+[\w:-]*\w?|'\w+[\w:-]*\w?'|"\w+[\w:-]*\w?")\s*=>\s*("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|[-\w@.()\[\]'"]+)|(\w+[\w:-]*\w?|'\w+[\w:-]*\w?'|'\w+[\w:-]*\w?'):\s*("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|[-\w@.()\[\]'"]+))/.test(lines[0])) {
1195
1195
  attributes = lines.shift();
1196
1196
  expression += ' ' + attributes.match(/^(\s*)(.*)/)[2];
1197
1197
  this.line_number++;
@@ -1230,7 +1230,7 @@ require.define("/compiler.js", function (require, module, exports, __dirname, __
1230
1230
  };
1231
1231
 
1232
1232
  Compiler.prototype.render = function(templateName, namespace) {
1233
- var code, escapeFn, output, segment, segments, _i, _len;
1233
+ var cleanFn, code, escapeFn, output, segment, segments, _i, _len;
1234
1234
  if (namespace == null) namespace = 'window.HAML';
1235
1235
  output = '';
1236
1236
  segments = ("" + namespace + "." + templateName).replace(/(\s|-)+/g, '_').split(/\./);
@@ -1249,12 +1249,19 @@ require.define("/compiler.js", function (require, module, exports, __dirname, __
1249
1249
  escapeFn = this.options.customHtmlEscape;
1250
1250
  } else {
1251
1251
  escapeFn = "" + namespace + ".htmlEscape";
1252
- output += escapeFn + '||= (text) ->\n "#{ text }"\n .replace(/&/g, \'&amp;\')\n .replace(/</g, \'&lt;\')\n .replace(/>/g, \'&gt;\')\n .replace(/\'/g, \'&apos;\')\n .replace(/\"/g, \'&quot;\')\n';
1252
+ output += escapeFn + '||= (text, escape) ->\n "#{ text }"\n .replace(/&/g, \'&amp;\')\n .replace(/</g, \'&lt;\')\n .replace(/>/g, \'&gt;\')\n .replace(/\'/g, \'&apos;\')\n .replace(/\"/g, \'&quot;\')\n';
1253
+ }
1254
+ if (this.options.customCleanValue) {
1255
+ cleanFn = this.options.customCleanValue;
1256
+ } else {
1257
+ cleanFn = "" + namespace + ".cleanValue";
1258
+ output += cleanFn + '||= (text) -> if text is null or text is undefined then \'\' else text\n';
1253
1259
  }
1254
1260
  output += "" + namespace + "['" + templateName + "'] = (context) ->\n";
1255
1261
  output += " fn = (context) ->\n";
1256
1262
  output += " o = []\n";
1257
1263
  output += " e = " + escapeFn + "\n";
1264
+ output += " c = " + cleanFn + "\n";
1258
1265
  code = this.createCode();
1259
1266
  output += "" + code + "\n";
1260
1267
  output += " return o.join(\"\\n\")" + (this.cleanupWhitespace(code)) + "\n";
@@ -1284,9 +1291,9 @@ require.define("/compiler.js", function (require, module, exports, __dirname, __
1284
1291
  break;
1285
1292
  case 'insert':
1286
1293
  if (line.hw === 0) {
1287
- code.push("" + (w(line.cw)) + "o.push " + (w(line.escape) ? 'e ' : '') + line.code);
1294
+ code.push("" + (w(line.cw)) + "o.push " + (w(line.escape) ? 'e ' : '') + "c(" + line.code + ")");
1288
1295
  } else {
1289
- code.push("" + (w(line.cw)) + "o.push " + (w(line.escape) ? 'e' : '') + " \"" + (w(line.hw - line.cw + 2)) + "\" + " + line.code);
1296
+ code.push("" + (w(line.cw)) + "o.push " + (w(line.escape) ? 'e' : '') + " \"" + (w(line.hw - line.cw + 2)) + "\" + c(" + line.code + ")");
1290
1297
  }
1291
1298
  }
1292
1299
  }
@@ -17,6 +17,20 @@ window.HAML.escape || (window.HAML.escape = function(text) {
17
17
  .replace(/"/g, "&quot;");
18
18
  });
19
19
 
20
+ /**
21
+ * HAML Coffee clean value function
22
+ *
23
+ * @param text [String] the text to be cleaned
24
+ * @return [String] the cleaned text
25
+ */
26
+ window.HAML.cleanValue || (window.HAML.cleanValue = function(text) {
27
+ if (text === null || text === void 0) {
28
+ return '';
29
+ } else {
30
+ return text;
31
+ }
32
+ });
33
+
20
34
  /**
21
35
  * HAML Coffee extend function.
22
36
  *
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: haml_coffee_assets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-23 00:00:00.000000000Z
12
+ date: 2011-11-28 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &70263052799780 !ruby/object:Gem::Requirement
16
+ requirement: &70271110267660 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.1'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70263052799780
24
+ version_requirements: *70271110267660
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: execjs
27
- requirement: &70263052799040 !ruby/object:Gem::Requirement
27
+ requirement: &70271110266700 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 1.2.9
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70263052799040
35
+ version_requirements: *70271110266700
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: bundler
38
- requirement: &70263052798560 !ruby/object:Gem::Requirement
38
+ requirement: &70271110264040 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70263052798560
46
+ version_requirements: *70271110264040
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &70263052792700 !ruby/object:Gem::Requirement
49
+ requirement: &70271110263360 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 2.7.0
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70263052792700
57
+ version_requirements: *70271110263360
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: guard-rspec
60
- requirement: &70263052791720 !ruby/object:Gem::Requirement
60
+ requirement: &70271110262780 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 0.5.0
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70263052791720
68
+ version_requirements: *70271110262780
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: yard
71
- requirement: &70263052789280 !ruby/object:Gem::Requirement
71
+ requirement: &70271110262040 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ~>
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: 0.7.3
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70263052789280
79
+ version_requirements: *70271110262040
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: redcarpet
82
- requirement: &70263052788280 !ruby/object:Gem::Requirement
82
+ requirement: &70271110261360 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ~>
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: 1.17.2
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *70263052788280
90
+ version_requirements: *70271110261360
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: pry
93
- requirement: &70263052785760 !ruby/object:Gem::Requirement
93
+ requirement: &70271110260900 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ~>
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: 0.9.6.2
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *70263052785760
101
+ version_requirements: *70271110260900
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: rake
104
- requirement: &70263052785120 !ruby/object:Gem::Requirement
104
+ requirement: &70271110225480 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ~>
@@ -109,7 +109,7 @@ dependencies:
109
109
  version: 0.9.2.2
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *70263052785120
112
+ version_requirements: *70271110225480
113
113
  description: Compile Haml CoffeeScript templates in the Rails asset pipeline.
114
114
  email:
115
115
  - michi@netzpiraten.ch
@@ -149,7 +149,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
149
149
  version: 1.3.6
150
150
  requirements: []
151
151
  rubyforge_project: haml_coffee_assets
152
- rubygems_version: 1.8.6
152
+ rubygems_version: 1.8.10
153
153
  signing_key:
154
154
  specification_version: 3
155
155
  summary: Haml CoffeeScript templates