handlebars_assets 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- handlebars_assets (0.1.2)
4
+ handlebars_assets (0.1.3)
5
5
  execjs (>= 1.2.9)
6
6
  tilt
7
7
 
data/README.md CHANGED
@@ -13,7 +13,11 @@ Load `handlebars_assets` and `execjs` in your `Gemfile`
13
13
  gem 'handlebars_assets'
14
14
  gem 'execjs'
15
15
 
16
- Require `handlebars.js` in your Javascript manifest (i.e. `application.js`)
16
+ Require `handlebars.vm.js` in your Javascript manifest (i.e. `application.js`)
17
+
18
+ //= require handlebars.vm
19
+
20
+ If you need to compile your Javascript templates in the browser as well, you should instead require `handlebars.js` (which is significantly larger)
17
21
 
18
22
  //= require handlebars
19
23
 
@@ -1,3 +1,3 @@
1
1
  module HandlebarsAssets
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -20,7 +20,7 @@ module HandlebarsAssets
20
20
 
21
21
 
22
22
  buffer += "This is ";
23
- stack1 = helpers.handlebars || depth0.handlebars
23
+ stack1 = helpers.handlebars || depth0.handlebars;
24
24
  if(typeof stack1 === functionType) { stack1 = stack1.call(depth0, { hash: {} }); }
25
25
  else if(stack1=== undef) { stack1 = helperMissing.call(depth0, "handlebars", { hash: {} }); }
26
26
  buffer += escapeExpression(stack1);
@@ -85,6 +85,10 @@ Handlebars.registerHelper('unless', function(context, options) {
85
85
  Handlebars.registerHelper('with', function(context, options) {
86
86
  return options.fn(context);
87
87
  });
88
+
89
+ Handlebars.registerHelper('log', function(context) {
90
+ Handlebars.log(context);
91
+ });
88
92
  ;
89
93
  // lib/handlebars/compiler/parser.js
90
94
  /* Jison generated parser */
@@ -533,14 +537,16 @@ case 21: return 29;
533
537
  break;
534
538
  case 22: return 33;
535
539
  break;
536
- case 23: return 'INVALID';
540
+ case 23: yy_.yytext = yy_.yytext.substr(1, yy_.yyleng-2); return 33;
541
+ break;
542
+ case 24: return 'INVALID';
537
543
  break;
538
- case 24: return 5;
544
+ case 25: return 5;
539
545
  break;
540
546
  }
541
547
  };
542
- lexer.rules = [/^[^\x00]*?(?=(\{\{))/,/^[^\x00]+/,/^\{\{>/,/^\{\{#/,/^\{\{\//,/^\{\{\^/,/^\{\{\s*else\b/,/^\{\{\{/,/^\{\{&/,/^\{\{![\s\S]*?\}\}/,/^\{\{/,/^=/,/^\.(?=[} ])/,/^\.\./,/^[/.]/,/^\s+/,/^\}\}\}/,/^\}\}/,/^"(\\["]|[^"])*"/,/^true(?=[}\s])/,/^false(?=[}\s])/,/^[0-9]+(?=[}\s])/,/^[a-zA-Z0-9_$-]+(?=[=}\s/.])/,/^./,/^$/];
543
- lexer.conditions = {"mu":{"rules":[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24],"inclusive":false},"INITIAL":{"rules":[0,1,24],"inclusive":true}};return lexer;})()
548
+ lexer.rules = [/^[^\x00]*?(?=(\{\{))/,/^[^\x00]+/,/^\{\{>/,/^\{\{#/,/^\{\{\//,/^\{\{\^/,/^\{\{\s*else\b/,/^\{\{\{/,/^\{\{&/,/^\{\{![\s\S]*?\}\}/,/^\{\{/,/^=/,/^\.(?=[} ])/,/^\.\./,/^[/.]/,/^\s+/,/^\}\}\}/,/^\}\}/,/^"(\\["]|[^"])*"/,/^true(?=[}\s])/,/^false(?=[}\s])/,/^[0-9]+(?=[}\s])/,/^[a-zA-Z0-9_$-]+(?=[=}\s/.])/,/^\[.*\]/,/^./,/^$/];
549
+ lexer.conditions = {"mu":{"rules":[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25],"inclusive":false},"INITIAL":{"rules":[0,1,25],"inclusive":true}};return lexer;})()
544
550
  parser.lexer = lexer;
545
551
  return parser;
546
552
  })();
@@ -849,7 +855,8 @@ Handlebars.JavaScriptCompiler = function() {};
849
855
  'each': true,
850
856
  'if': true,
851
857
  'unless': true,
852
- 'with': true
858
+ 'with': true,
859
+ 'log': true
853
860
  };
854
861
  if (knownHelpers) {
855
862
  for (var name in knownHelpers) {
@@ -1059,12 +1066,13 @@ Handlebars.JavaScriptCompiler = function() {};
1059
1066
  // PUBLIC API: You can override these methods in a subclass to provide
1060
1067
  // alternative compiled forms for name lookup and buffering semantics
1061
1068
  nameLookup: function(parent, name, type) {
1062
- if(JavaScriptCompiler.RESERVED_WORDS[name] || name.indexOf('-') !== -1 || !isNaN(name)) {
1063
- return parent + "['" + name + "']";
1064
- } else if (/^[0-9]+$/.test(name)) {
1069
+ if (/^[0-9]+$/.test(name)) {
1065
1070
  return parent + "[" + name + "]";
1066
- } else {
1067
- return parent + "." + name;
1071
+ } else if (JavaScriptCompiler.isValidJavaScriptVariableName(name)) {
1072
+ return parent + "." + name;
1073
+ }
1074
+ else {
1075
+ return parent + "['" + name + "']";
1068
1076
  }
1069
1077
  },
1070
1078
 
@@ -1266,7 +1274,8 @@ Handlebars.JavaScriptCompiler = function() {};
1266
1274
  + " || "
1267
1275
  + this.nameLookup('depth' + this.lastContext, name, 'context');
1268
1276
  }
1269
-
1277
+
1278
+ toPush += ';';
1270
1279
  this.source.push(toPush);
1271
1280
  } else {
1272
1281
  this.pushStack('depth' + this.lastContext);
@@ -1275,7 +1284,8 @@ Handlebars.JavaScriptCompiler = function() {};
1275
1284
 
1276
1285
  lookup: function(name) {
1277
1286
  var topStack = this.topStack();
1278
- this.source.push(topStack + " = " + this.nameLookup(topStack, name, 'context') + ";");
1287
+ this.source.push(topStack + " = (" + topStack + " === null || " + topStack + " === undefined || " + topStack + " === false ? " +
1288
+ topStack + " : " + this.nameLookup(topStack, name, 'context') + ");");
1279
1289
  },
1280
1290
 
1281
1291
  pushStringParam: function(string) {
@@ -1478,6 +1488,13 @@ Handlebars.JavaScriptCompiler = function() {};
1478
1488
  compilerWords[reservedWords[i]] = true;
1479
1489
  }
1480
1490
 
1491
+ JavaScriptCompiler.isValidJavaScriptVariableName = function(name) {
1492
+ if(!JavaScriptCompiler.RESERVED_WORDS[name] && /^[a-zA-Z_$][0-9a-zA-Z_$]+$/.test(name)) {
1493
+ return true;
1494
+ }
1495
+ return false;
1496
+ }
1497
+
1481
1498
  })(Handlebars.Compiler, Handlebars.JavaScriptCompiler);
1482
1499
 
1483
1500
  Handlebars.precompile = function(string, options) {
@@ -1491,10 +1508,21 @@ Handlebars.precompile = function(string, options) {
1491
1508
  Handlebars.compile = function(string, options) {
1492
1509
  options = options || {};
1493
1510
 
1494
- var ast = Handlebars.parse(string);
1495
- var environment = new Handlebars.Compiler().compile(ast, options);
1496
- var templateSpec = new Handlebars.JavaScriptCompiler().compile(environment, options, undefined, true);
1497
- return Handlebars.template(templateSpec);
1511
+ var compiled;
1512
+ function compile() {
1513
+ var ast = Handlebars.parse(string);
1514
+ var environment = new Handlebars.Compiler().compile(ast, options);
1515
+ var templateSpec = new Handlebars.JavaScriptCompiler().compile(environment, options, undefined, true);
1516
+ return Handlebars.template(templateSpec);
1517
+ }
1518
+
1519
+ // Template is only compiled on first use and cached after that point.
1520
+ return function(context, options) {
1521
+ if (!compiled) {
1522
+ compiled = compile();
1523
+ }
1524
+ return compiled.call(this, context, options);
1525
+ };
1498
1526
  };
1499
1527
  ;
1500
1528
  // lib/handlebars/vm.js
@@ -0,0 +1,216 @@
1
+ // lib/handlebars/base.js
2
+ var Handlebars = {};
3
+
4
+ Handlebars.VERSION = "1.0.beta.2";
5
+
6
+ Handlebars.helpers = {};
7
+ Handlebars.partials = {};
8
+
9
+ Handlebars.registerHelper = function(name, fn, inverse) {
10
+ if(inverse) { fn.not = inverse; }
11
+ this.helpers[name] = fn;
12
+ };
13
+
14
+ Handlebars.registerPartial = function(name, str) {
15
+ this.partials[name] = str;
16
+ };
17
+
18
+ Handlebars.registerHelper('helperMissing', function(arg) {
19
+ if(arguments.length === 2) {
20
+ return undefined;
21
+ } else {
22
+ throw new Error("Could not find property '" + arg + "'");
23
+ }
24
+ });
25
+
26
+ Handlebars.registerHelper('blockHelperMissing', function(context, options) {
27
+ var inverse = options.inverse || function() {}, fn = options.fn;
28
+
29
+
30
+ var ret = "";
31
+ var type = Object.prototype.toString.call(context);
32
+
33
+ if(type === "[object Function]") {
34
+ context = context();
35
+ }
36
+
37
+ if(context === true) {
38
+ return fn(this);
39
+ } else if(context === false || context == null) {
40
+ return inverse(this);
41
+ } else if(type === "[object Array]") {
42
+ if(context.length > 0) {
43
+ for(var i=0, j=context.length; i<j; i++) {
44
+ ret = ret + fn(context[i]);
45
+ }
46
+ } else {
47
+ ret = inverse(this);
48
+ }
49
+ return ret;
50
+ } else {
51
+ return fn(context);
52
+ }
53
+ });
54
+
55
+ Handlebars.registerHelper('each', function(context, options) {
56
+ var fn = options.fn, inverse = options.inverse;
57
+ var ret = "";
58
+
59
+ if(context && context.length > 0) {
60
+ for(var i=0, j=context.length; i<j; i++) {
61
+ ret = ret + fn(context[i]);
62
+ }
63
+ } else {
64
+ ret = inverse(this);
65
+ }
66
+ return ret;
67
+ });
68
+
69
+ Handlebars.registerHelper('if', function(context, options) {
70
+ if(!context || Handlebars.Utils.isEmpty(context)) {
71
+ return options.inverse(this);
72
+ } else {
73
+ return options.fn(this);
74
+ }
75
+ });
76
+
77
+ Handlebars.registerHelper('unless', function(context, options) {
78
+ var fn = options.fn, inverse = options.inverse;
79
+ options.fn = inverse;
80
+ options.inverse = fn;
81
+
82
+ return Handlebars.helpers['if'].call(this, context, options);
83
+ });
84
+
85
+ Handlebars.registerHelper('with', function(context, options) {
86
+ return options.fn(context);
87
+ });
88
+
89
+ Handlebars.registerHelper('log', function(context) {
90
+ Handlebars.log(context);
91
+ });
92
+ ;
93
+ // lib/handlebars/utils.js
94
+ Handlebars.Exception = function(message) {
95
+ var tmp = Error.prototype.constructor.apply(this, arguments);
96
+
97
+ for (var p in tmp) {
98
+ if (tmp.hasOwnProperty(p)) { this[p] = tmp[p]; }
99
+ }
100
+ };
101
+ Handlebars.Exception.prototype = new Error;
102
+
103
+ // Build out our basic SafeString type
104
+ Handlebars.SafeString = function(string) {
105
+ this.string = string;
106
+ };
107
+ Handlebars.SafeString.prototype.toString = function() {
108
+ return this.string.toString();
109
+ };
110
+
111
+ (function() {
112
+ var escape = {
113
+ "<": "&lt;",
114
+ ">": "&gt;",
115
+ '"': "&quot;",
116
+ "'": "&#x27;",
117
+ "`": "&#x60;"
118
+ };
119
+
120
+ var badChars = /&(?!\w+;)|[<>"'`]/g;
121
+ var possible = /[&<>"'`]/;
122
+
123
+ var escapeChar = function(chr) {
124
+ return escape[chr] || "&amp;";
125
+ };
126
+
127
+ Handlebars.Utils = {
128
+ escapeExpression: function(string) {
129
+ // don't escape SafeStrings, since they're already safe
130
+ if (string instanceof Handlebars.SafeString) {
131
+ return string.toString();
132
+ } else if (string == null || string === false) {
133
+ return "";
134
+ }
135
+
136
+ if(!possible.test(string)) { return string; }
137
+ return string.replace(badChars, escapeChar);
138
+ },
139
+
140
+ isEmpty: function(value) {
141
+ if (typeof value === "undefined") {
142
+ return true;
143
+ } else if (value === null) {
144
+ return true;
145
+ } else if (value === false) {
146
+ return true;
147
+ } else if(Object.prototype.toString.call(value) === "[object Array]" && value.length === 0) {
148
+ return true;
149
+ } else {
150
+ return false;
151
+ }
152
+ }
153
+ };
154
+ })();;
155
+ // lib/handlebars/vm.js
156
+ Handlebars.VM = {
157
+ template: function(templateSpec) {
158
+ // Just add water
159
+ var container = {
160
+ escapeExpression: Handlebars.Utils.escapeExpression,
161
+ invokePartial: Handlebars.VM.invokePartial,
162
+ programs: [],
163
+ program: function(i, fn, data) {
164
+ var programWrapper = this.programs[i];
165
+ if(data) {
166
+ return Handlebars.VM.program(fn, data);
167
+ } else if(programWrapper) {
168
+ return programWrapper;
169
+ } else {
170
+ programWrapper = this.programs[i] = Handlebars.VM.program(fn);
171
+ return programWrapper;
172
+ }
173
+ },
174
+ programWithDepth: Handlebars.VM.programWithDepth,
175
+ noop: Handlebars.VM.noop
176
+ };
177
+
178
+ return function(context, options) {
179
+ options = options || {};
180
+ return templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data);
181
+ };
182
+ },
183
+
184
+ programWithDepth: function(fn, data, $depth) {
185
+ var args = Array.prototype.slice.call(arguments, 2);
186
+
187
+ return function(context, options) {
188
+ options = options || {};
189
+
190
+ return fn.apply(this, [context, options.data || data].concat(args));
191
+ };
192
+ },
193
+ program: function(fn, data) {
194
+ return function(context, options) {
195
+ options = options || {};
196
+
197
+ return fn(context, options.data || data);
198
+ };
199
+ },
200
+ noop: function() { return ""; },
201
+ invokePartial: function(partial, name, context, helpers, partials) {
202
+ if(partial === undefined) {
203
+ throw new Handlebars.Exception("The partial " + name + " could not be found");
204
+ } else if(partial instanceof Function) {
205
+ return partial(context, {helpers: helpers, partials: partials});
206
+ } else if (!Handlebars.compile) {
207
+ throw new Handlebars.Exception("The partial " + name + " could not be compiled when running in vm mode");
208
+ } else {
209
+ partials[name] = Handlebars.compile(partial);
210
+ return partials[name](context, {helpers: helpers, partials: partials});
211
+ }
212
+ }
213
+ };
214
+
215
+ Handlebars.template = Handlebars.VM.template;
216
+ ;
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: handlebars_assets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-10-25 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: execjs
16
- requirement: &70146845057620 !ruby/object:Gem::Requirement
16
+ requirement: &70325628173200 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.2.9
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70146845057620
24
+ version_requirements: *70325628173200
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: tilt
27
- requirement: &70146845057160 !ruby/object:Gem::Requirement
27
+ requirement: &70325628172760 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70146845057160
35
+ version_requirements: *70325628172760
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: activesupport
38
- requirement: &70146845056580 !ruby/object:Gem::Requirement
38
+ requirement: &70325628172220 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 3.1.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70146845056580
46
+ version_requirements: *70325628172220
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: therubyracer
49
- requirement: &70146845056040 !ruby/object:Gem::Requirement
49
+ requirement: &70325628171760 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70146845056040
57
+ version_requirements: *70325628171760
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rake
60
- requirement: &70146845055560 !ruby/object:Gem::Requirement
60
+ requirement: &70325628171280 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70146845055560
68
+ version_requirements: *70325628171280
69
69
  description: Compile Handlebars templates in the Rails asset pipeline.
70
70
  email:
71
71
  - leshill@gmail.com
@@ -89,6 +89,7 @@ files:
89
89
  - test/handlebars_assets/tilt_handlebars_test.rb
90
90
  - test/test_helper.rb
91
91
  - vendor/assets/javascripts/handlebars.js
92
+ - vendor/assets/javascripts/handlebars.vm.js
92
93
  homepage: ''
93
94
  licenses: []
94
95
  post_install_message: