handlebars_assets 0.6.2 → 0.6.4

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ ## 0.6.4 (2012-08-25)
2
+
3
+ * Fix bug with the `template\_namespace` config option - @kmayer
4
+
5
+ ## 0.6.3 (2012-08-22)
6
+
7
+ * Added ability to change client-side template namespace from `HandlebarsTemplates` using `template\_namespace` config option - @spikebrehm
8
+ * Added `handlebars.js` compiler options
9
+ * Refactored tests; Config.reset! moved to tests
10
+
1
11
  ## 0.6.2 (2012-07-27)
2
12
 
3
13
  * Added support for knownHelpers and knownHelperOnly compiler options
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- handlebars_assets (0.6.2)
4
+ handlebars_assets (0.6.4)
5
5
  execjs (>= 1.2.9)
6
6
  sprockets (>= 2.0.3)
7
7
  tilt
@@ -9,13 +9,13 @@ PATH
9
9
  GEM
10
10
  remote: http://rubygems.org/
11
11
  specs:
12
- execjs (1.3.0)
12
+ execjs (1.4.0)
13
13
  multi_json (~> 1.0)
14
14
  hike (1.2.1)
15
- multi_json (1.2.0)
15
+ multi_json (1.3.6)
16
16
  rack (1.4.1)
17
17
  rake (0.9.2.2)
18
- sprockets (2.4.0)
18
+ sprockets (2.4.5)
19
19
  hike (~> 1.2)
20
20
  multi_json (~> 1.0)
21
21
  rack (~> 1.0)
data/README.md CHANGED
@@ -81,6 +81,14 @@ You can then invoke the resulting template in your application's Javascript
81
81
 
82
82
  HandlebarsTemplates['contacts/new'](context);
83
83
 
84
+ ## The template namespace
85
+
86
+ By default, the global Javascript object that holds the compiled templates is `HandlebarsTemplates`, but it can
87
+ be easily renamed. Another common template namespace is `JST`. Just change the `template_namespace` configuration option
88
+ when you initialize your application.
89
+
90
+ HandlebarsAssets::Config.template_namespace = 'JST'
91
+
84
92
  ## Partials
85
93
 
86
94
  If you begin the name of the template with an underscore, it will be recognized as a partial. You can invoke partials inside a template using the Handlebars partial syntax:
@@ -98,6 +106,13 @@ If you begin the name of the template with an underscore, it will be recognized
98
106
 
99
107
  You will get three partials named `_form`, `_contacts_form`, and `_todos_form`; note that the partials begin with `_`.
100
108
 
109
+ ## Using another version of `handlebars.js`
110
+
111
+ Occasionally you might need to use a version of `handlebars.js` other than the included version. You can set the `compiler_path` and `compiler` options to use a custom version of `handlebars.js`.
112
+
113
+ HandlebarsAssets::Config.compiler = 'my_handlebars.js' # Change the name of the compiler file
114
+ HandlebarsAssets::Config.compiler_path = Rails.root.join('app/assets/javascripts') # Change the location of the compiler file
115
+
101
116
  # Thanks
102
117
 
103
118
  This gem is standing on the shoulders of giants.
@@ -123,10 +138,14 @@ Follow me on [Github](https://github.com/leshill) and [Twitter](https://twitter.
123
138
  * Dan Evans (@danevans) : Rails 2 support
124
139
  * Ben Woosley (@empact) : Update to handlebars.js 1.0.0.beta.6
125
140
  * (@cw-moshe) : Remove 'templates/' from names
141
+ * Spike Brehm (@spikebrehm) : Config.template\_namespace option
142
+ * Ken Mayer (@kmayer) : Quick fix for template\_namespace option
126
143
 
127
144
  # Contributing
128
145
 
129
- Once you've made your great commits
146
+ Pull requests are welcome! Please do not update the version number.
147
+
148
+ In a nutshell:
130
149
 
131
150
  1. Fork
132
151
  1. Create a topic branch - git checkout -b my_branch
@@ -5,7 +5,15 @@ module HandlebarsAssets
5
5
  module Config
6
6
  extend self
7
7
 
8
- attr_writer :known_helpers, :known_helpers_only, :path_prefix
8
+ attr_writer :compiler, :compiler_path, :known_helpers, :known_helpers_only, :path_prefix, :template_namespace
9
+
10
+ def compiler
11
+ @compiler || 'handlebars.js'
12
+ end
13
+
14
+ def compiler_path
15
+ @compiler_path || HandlebarsAssets.path
16
+ end
9
17
 
10
18
  def known_helpers
11
19
  @known_helpers || []
@@ -16,14 +24,15 @@ module HandlebarsAssets
16
24
  end
17
25
 
18
26
  def options
19
- options = {}
20
- options[:knownHelpersOnly] = true if known_helpers_only
21
- options[:knownHelpers] = known_helpers_hash if known_helpers_hash.any?
22
- options
27
+ @options ||= generate_options
23
28
  end
24
29
 
25
30
  def path_prefix
26
- @path_prefix ||= 'templates'
31
+ @path_prefix || 'templates'
32
+ end
33
+
34
+ def template_namespace
35
+ @template_namespace || 'HandlebarsTemplates'
27
36
  end
28
37
 
29
38
  private
@@ -34,8 +43,12 @@ module HandlebarsAssets
34
43
  end
35
44
  end
36
45
 
37
- def known_helpers_hash
38
- @known_helpers_hash ||= generate_known_helpers_hash
46
+ def generate_options
47
+ options = {}
48
+ options[:knownHelpersOnly] = true if known_helpers_only
49
+ options[:knownHelpers] = generate_known_helpers_hash if known_helpers.any?
50
+ options
39
51
  end
52
+
40
53
  end
41
54
  end
@@ -20,11 +20,11 @@ module HandlebarsAssets
20
20
  end
21
21
 
22
22
  def path
23
- @path ||= assets_path.join('handlebars.js')
23
+ @path ||= assets_path.join(HandlebarsAssets::Config.compiler)
24
24
  end
25
25
 
26
26
  def assets_path
27
- @assets_path ||= Pathname(HandlebarsAssets.path)
27
+ @assets_path ||= Pathname(HandlebarsAssets::Config.compiler_path)
28
28
  end
29
29
  end
30
30
  end
@@ -11,6 +11,8 @@ module HandlebarsAssets
11
11
 
12
12
  compiled_hbs = Handlebars.precompile(data, HandlebarsAssets::Config.options)
13
13
 
14
+ template_namespace = HandlebarsAssets::Config.template_namespace
15
+
14
16
  if template_path.is_partial?
15
17
  <<-PARTIAL
16
18
  (function() {
@@ -20,9 +22,9 @@ module HandlebarsAssets
20
22
  else
21
23
  <<-TEMPLATE
22
24
  (function() {
23
- this.HandlebarsTemplates || (this.HandlebarsTemplates = {});
24
- this.HandlebarsTemplates[#{template_path.name}] = Handlebars.template(#{compiled_hbs});
25
- return HandlebarsTemplates[#{template_path.name}];
25
+ this.#{template_namespace} || (this.#{template_namespace} = {});
26
+ this.#{template_namespace}[#{template_path.name}] = Handlebars.template(#{compiled_hbs});
27
+ return this.#{template_namespace}[#{template_path.name}];
26
28
  }).call(this);
27
29
  TEMPLATE
28
30
  end
@@ -32,7 +34,6 @@ module HandlebarsAssets
32
34
 
33
35
  def prepare; end
34
36
 
35
-
36
37
  class TemplatePath
37
38
  def initialize(scope)
38
39
  self.template_path = scope.logical_path
@@ -1,3 +1,3 @@
1
1
  module HandlebarsAssets
2
- VERSION = "0.6.2"
2
+ VERSION = "0.6.4"
3
3
  end
@@ -0,0 +1,1917 @@
1
+ // lib/handlebars/base.js
2
+
3
+ /*jshint eqnull:true*/
4
+ this.Handlebars = {};
5
+
6
+ (function() {
7
+
8
+ Handlebars.VERSION = "1.0.rc.1";
9
+
10
+ Handlebars.helpers = {};
11
+ Handlebars.partials = {};
12
+
13
+ Handlebars.registerHelper = function(name, fn, inverse) {
14
+ if(inverse) { fn.not = inverse; }
15
+ this.helpers[name] = fn;
16
+ };
17
+
18
+ Handlebars.registerPartial = function(name, str) {
19
+ this.partials[name] = str;
20
+ };
21
+
22
+ Handlebars.registerHelper('helperMissing', function(arg) {
23
+ if(arguments.length === 2) {
24
+ return undefined;
25
+ } else {
26
+ throw new Error("Could not find property '" + arg + "'");
27
+ }
28
+ });
29
+
30
+ var toString = Object.prototype.toString, functionType = "[object Function]";
31
+
32
+ Handlebars.registerHelper('blockHelperMissing', function(context, options) {
33
+ var inverse = options.inverse || function() {}, fn = options.fn;
34
+
35
+
36
+ var ret = "";
37
+ var type = toString.call(context);
38
+
39
+ if(type === functionType) { context = context.call(this); }
40
+
41
+ if(context === true) {
42
+ return fn(this);
43
+ } else if(context === false || context == null) {
44
+ return inverse(this);
45
+ } else if(type === "[object Array]") {
46
+ if(context.length > 0) {
47
+ for(var i=0, j=context.length; i<j; i++) {
48
+ ret = ret + fn(context[i]);
49
+ }
50
+ } else {
51
+ ret = inverse(this);
52
+ }
53
+ return ret;
54
+ } else {
55
+ return fn(context);
56
+ }
57
+ });
58
+
59
+ Handlebars.K = function() {};
60
+
61
+ Handlebars.createFrame = Object.create || function(object) {
62
+ Handlebars.K.prototype = object;
63
+ var obj = new Handlebars.K();
64
+ Handlebars.K.prototype = null;
65
+ return obj;
66
+ };
67
+
68
+ Handlebars.registerHelper('each', function(context, options) {
69
+ var fn = options.fn, inverse = options.inverse;
70
+ var ret = "", data;
71
+
72
+ if (options.data) {
73
+ data = Handlebars.createFrame(options.data);
74
+ }
75
+
76
+ if(context && context.length > 0) {
77
+ for(var i=0, j=context.length; i<j; i++) {
78
+ if (data) { data.index = i; }
79
+ ret = ret + fn(context[i], { data: data });
80
+ }
81
+ } else {
82
+ ret = inverse(this);
83
+ }
84
+ return ret;
85
+ });
86
+
87
+ Handlebars.registerHelper('if', function(context, options) {
88
+ var type = toString.call(context);
89
+ if(type === functionType) { context = context.call(this); }
90
+
91
+ if(!context || Handlebars.Utils.isEmpty(context)) {
92
+ return options.inverse(this);
93
+ } else {
94
+ return options.fn(this);
95
+ }
96
+ });
97
+
98
+ Handlebars.registerHelper('unless', function(context, options) {
99
+ var fn = options.fn, inverse = options.inverse;
100
+ options.fn = inverse;
101
+ options.inverse = fn;
102
+
103
+ return Handlebars.helpers['if'].call(this, context, options);
104
+ });
105
+
106
+ Handlebars.registerHelper('with', function(context, options) {
107
+ return options.fn(context);
108
+ });
109
+
110
+ Handlebars.registerHelper('log', function(context) {
111
+ Handlebars.log(context);
112
+ });
113
+
114
+ }());
115
+ ;
116
+ // lib/handlebars/compiler/parser.js
117
+ /* Jison generated parser */
118
+ var handlebars = (function(){
119
+ var parser = {trace: function trace() { },
120
+ yy: {},
121
+ symbols_: {"error":2,"root":3,"program":4,"EOF":5,"statements":6,"simpleInverse":7,"statement":8,"openInverse":9,"closeBlock":10,"openBlock":11,"mustache":12,"partial":13,"CONTENT":14,"COMMENT":15,"OPEN_BLOCK":16,"inMustache":17,"CLOSE":18,"OPEN_INVERSE":19,"OPEN_ENDBLOCK":20,"path":21,"OPEN":22,"OPEN_UNESCAPED":23,"OPEN_PARTIAL":24,"params":25,"hash":26,"DATA":27,"param":28,"STRING":29,"INTEGER":30,"BOOLEAN":31,"hashSegments":32,"hashSegment":33,"ID":34,"EQUALS":35,"pathSegments":36,"SEP":37,"$accept":0,"$end":1},
122
+ terminals_: {2:"error",5:"EOF",14:"CONTENT",15:"COMMENT",16:"OPEN_BLOCK",18:"CLOSE",19:"OPEN_INVERSE",20:"OPEN_ENDBLOCK",22:"OPEN",23:"OPEN_UNESCAPED",24:"OPEN_PARTIAL",27:"DATA",29:"STRING",30:"INTEGER",31:"BOOLEAN",34:"ID",35:"EQUALS",37:"SEP"},
123
+ productions_: [0,[3,2],[4,3],[4,1],[4,0],[6,1],[6,2],[8,3],[8,3],[8,1],[8,1],[8,1],[8,1],[11,3],[9,3],[10,3],[12,3],[12,3],[13,3],[13,4],[7,2],[17,3],[17,2],[17,2],[17,1],[17,1],[25,2],[25,1],[28,1],[28,1],[28,1],[28,1],[28,1],[26,1],[32,2],[32,1],[33,3],[33,3],[33,3],[33,3],[33,3],[21,1],[36,3],[36,1]],
124
+ performAction: function anonymous(yytext,yyleng,yylineno,yy,yystate,$$,_$) {
125
+
126
+ var $0 = $$.length - 1;
127
+ switch (yystate) {
128
+ case 1: return $$[$0-1];
129
+ break;
130
+ case 2: this.$ = new yy.ProgramNode($$[$0-2], $$[$0]);
131
+ break;
132
+ case 3: this.$ = new yy.ProgramNode($$[$0]);
133
+ break;
134
+ case 4: this.$ = new yy.ProgramNode([]);
135
+ break;
136
+ case 5: this.$ = [$$[$0]];
137
+ break;
138
+ case 6: $$[$0-1].push($$[$0]); this.$ = $$[$0-1];
139
+ break;
140
+ case 7: this.$ = new yy.BlockNode($$[$0-2], $$[$0-1].inverse, $$[$0-1], $$[$0]);
141
+ break;
142
+ case 8: this.$ = new yy.BlockNode($$[$0-2], $$[$0-1], $$[$0-1].inverse, $$[$0]);
143
+ break;
144
+ case 9: this.$ = $$[$0];
145
+ break;
146
+ case 10: this.$ = $$[$0];
147
+ break;
148
+ case 11: this.$ = new yy.ContentNode($$[$0]);
149
+ break;
150
+ case 12: this.$ = new yy.CommentNode($$[$0]);
151
+ break;
152
+ case 13: this.$ = new yy.MustacheNode($$[$0-1][0], $$[$0-1][1]);
153
+ break;
154
+ case 14: this.$ = new yy.MustacheNode($$[$0-1][0], $$[$0-1][1]);
155
+ break;
156
+ case 15: this.$ = $$[$0-1];
157
+ break;
158
+ case 16: this.$ = new yy.MustacheNode($$[$0-1][0], $$[$0-1][1]);
159
+ break;
160
+ case 17: this.$ = new yy.MustacheNode($$[$0-1][0], $$[$0-1][1], true);
161
+ break;
162
+ case 18: this.$ = new yy.PartialNode($$[$0-1]);
163
+ break;
164
+ case 19: this.$ = new yy.PartialNode($$[$0-2], $$[$0-1]);
165
+ break;
166
+ case 20:
167
+ break;
168
+ case 21: this.$ = [[$$[$0-2]].concat($$[$0-1]), $$[$0]];
169
+ break;
170
+ case 22: this.$ = [[$$[$0-1]].concat($$[$0]), null];
171
+ break;
172
+ case 23: this.$ = [[$$[$0-1]], $$[$0]];
173
+ break;
174
+ case 24: this.$ = [[$$[$0]], null];
175
+ break;
176
+ case 25: this.$ = [[new yy.DataNode($$[$0])], null];
177
+ break;
178
+ case 26: $$[$0-1].push($$[$0]); this.$ = $$[$0-1];
179
+ break;
180
+ case 27: this.$ = [$$[$0]];
181
+ break;
182
+ case 28: this.$ = $$[$0];
183
+ break;
184
+ case 29: this.$ = new yy.StringNode($$[$0]);
185
+ break;
186
+ case 30: this.$ = new yy.IntegerNode($$[$0]);
187
+ break;
188
+ case 31: this.$ = new yy.BooleanNode($$[$0]);
189
+ break;
190
+ case 32: this.$ = new yy.DataNode($$[$0]);
191
+ break;
192
+ case 33: this.$ = new yy.HashNode($$[$0]);
193
+ break;
194
+ case 34: $$[$0-1].push($$[$0]); this.$ = $$[$0-1];
195
+ break;
196
+ case 35: this.$ = [$$[$0]];
197
+ break;
198
+ case 36: this.$ = [$$[$0-2], $$[$0]];
199
+ break;
200
+ case 37: this.$ = [$$[$0-2], new yy.StringNode($$[$0])];
201
+ break;
202
+ case 38: this.$ = [$$[$0-2], new yy.IntegerNode($$[$0])];
203
+ break;
204
+ case 39: this.$ = [$$[$0-2], new yy.BooleanNode($$[$0])];
205
+ break;
206
+ case 40: this.$ = [$$[$0-2], new yy.DataNode($$[$0])];
207
+ break;
208
+ case 41: this.$ = new yy.IdNode($$[$0]);
209
+ break;
210
+ case 42: $$[$0-2].push($$[$0]); this.$ = $$[$0-2];
211
+ break;
212
+ case 43: this.$ = [$$[$0]];
213
+ break;
214
+ }
215
+ },
216
+ table: [{3:1,4:2,5:[2,4],6:3,8:4,9:5,11:6,12:7,13:8,14:[1,9],15:[1,10],16:[1,12],19:[1,11],22:[1,13],23:[1,14],24:[1,15]},{1:[3]},{5:[1,16]},{5:[2,3],7:17,8:18,9:5,11:6,12:7,13:8,14:[1,9],15:[1,10],16:[1,12],19:[1,19],20:[2,3],22:[1,13],23:[1,14],24:[1,15]},{5:[2,5],14:[2,5],15:[2,5],16:[2,5],19:[2,5],20:[2,5],22:[2,5],23:[2,5],24:[2,5]},{4:20,6:3,8:4,9:5,11:6,12:7,13:8,14:[1,9],15:[1,10],16:[1,12],19:[1,11],20:[2,4],22:[1,13],23:[1,14],24:[1,15]},{4:21,6:3,8:4,9:5,11:6,12:7,13:8,14:[1,9],15:[1,10],16:[1,12],19:[1,11],20:[2,4],22:[1,13],23:[1,14],24:[1,15]},{5:[2,9],14:[2,9],15:[2,9],16:[2,9],19:[2,9],20:[2,9],22:[2,9],23:[2,9],24:[2,9]},{5:[2,10],14:[2,10],15:[2,10],16:[2,10],19:[2,10],20:[2,10],22:[2,10],23:[2,10],24:[2,10]},{5:[2,11],14:[2,11],15:[2,11],16:[2,11],19:[2,11],20:[2,11],22:[2,11],23:[2,11],24:[2,11]},{5:[2,12],14:[2,12],15:[2,12],16:[2,12],19:[2,12],20:[2,12],22:[2,12],23:[2,12],24:[2,12]},{17:22,21:23,27:[1,24],34:[1,26],36:25},{17:27,21:23,27:[1,24],34:[1,26],36:25},{17:28,21:23,27:[1,24],34:[1,26],36:25},{17:29,21:23,27:[1,24],34:[1,26],36:25},{21:30,34:[1,26],36:25},{1:[2,1]},{6:31,8:4,9:5,11:6,12:7,13:8,14:[1,9],15:[1,10],16:[1,12],19:[1,11],22:[1,13],23:[1,14],24:[1,15]},{5:[2,6],14:[2,6],15:[2,6],16:[2,6],19:[2,6],20:[2,6],22:[2,6],23:[2,6],24:[2,6]},{17:22,18:[1,32],21:23,27:[1,24],34:[1,26],36:25},{10:33,20:[1,34]},{10:35,20:[1,34]},{18:[1,36]},{18:[2,24],21:41,25:37,26:38,27:[1,45],28:39,29:[1,42],30:[1,43],31:[1,44],32:40,33:46,34:[1,47],36:25},{18:[2,25]},{18:[2,41],27:[2,41],29:[2,41],30:[2,41],31:[2,41],34:[2,41],37:[1,48]},{18:[2,43],27:[2,43],29:[2,43],30:[2,43],31:[2,43],34:[2,43],37:[2,43]},{18:[1,49]},{18:[1,50]},{18:[1,51]},{18:[1,52],21:53,34:[1,26],36:25},{5:[2,2],8:18,9:5,11:6,12:7,13:8,14:[1,9],15:[1,10],16:[1,12],19:[1,11],20:[2,2],22:[1,13],23:[1,14],24:[1,15]},{14:[2,20],15:[2,20],16:[2,20],19:[2,20],22:[2,20],23:[2,20],24:[2,20]},{5:[2,7],14:[2,7],15:[2,7],16:[2,7],19:[2,7],20:[2,7],22:[2,7],23:[2,7],24:[2,7]},{21:54,34:[1,26],36:25},{5:[2,8],14:[2,8],15:[2,8],16:[2,8],19:[2,8],20:[2,8],22:[2,8],23:[2,8],24:[2,8]},{14:[2,14],15:[2,14],16:[2,14],19:[2,14],20:[2,14],22:[2,14],23:[2,14],24:[2,14]},{18:[2,22],21:41,26:55,27:[1,45],28:56,29:[1,42],30:[1,43],31:[1,44],32:40,33:46,34:[1,47],36:25},{18:[2,23]},{18:[2,27],27:[2,27],29:[2,27],30:[2,27],31:[2,27],34:[2,27]},{18:[2,33],33:57,34:[1,58]},{18:[2,28],27:[2,28],29:[2,28],30:[2,28],31:[2,28],34:[2,28]},{18:[2,29],27:[2,29],29:[2,29],30:[2,29],31:[2,29],34:[2,29]},{18:[2,30],27:[2,30],29:[2,30],30:[2,30],31:[2,30],34:[2,30]},{18:[2,31],27:[2,31],29:[2,31],30:[2,31],31:[2,31],34:[2,31]},{18:[2,32],27:[2,32],29:[2,32],30:[2,32],31:[2,32],34:[2,32]},{18:[2,35],34:[2,35]},{18:[2,43],27:[2,43],29:[2,43],30:[2,43],31:[2,43],34:[2,43],35:[1,59],37:[2,43]},{34:[1,60]},{14:[2,13],15:[2,13],16:[2,13],19:[2,13],20:[2,13],22:[2,13],23:[2,13],24:[2,13]},{5:[2,16],14:[2,16],15:[2,16],16:[2,16],19:[2,16],20:[2,16],22:[2,16],23:[2,16],24:[2,16]},{5:[2,17],14:[2,17],15:[2,17],16:[2,17],19:[2,17],20:[2,17],22:[2,17],23:[2,17],24:[2,17]},{5:[2,18],14:[2,18],15:[2,18],16:[2,18],19:[2,18],20:[2,18],22:[2,18],23:[2,18],24:[2,18]},{18:[1,61]},{18:[1,62]},{18:[2,21]},{18:[2,26],27:[2,26],29:[2,26],30:[2,26],31:[2,26],34:[2,26]},{18:[2,34],34:[2,34]},{35:[1,59]},{21:63,27:[1,67],29:[1,64],30:[1,65],31:[1,66],34:[1,26],36:25},{18:[2,42],27:[2,42],29:[2,42],30:[2,42],31:[2,42],34:[2,42],37:[2,42]},{5:[2,19],14:[2,19],15:[2,19],16:[2,19],19:[2,19],20:[2,19],22:[2,19],23:[2,19],24:[2,19]},{5:[2,15],14:[2,15],15:[2,15],16:[2,15],19:[2,15],20:[2,15],22:[2,15],23:[2,15],24:[2,15]},{18:[2,36],34:[2,36]},{18:[2,37],34:[2,37]},{18:[2,38],34:[2,38]},{18:[2,39],34:[2,39]},{18:[2,40],34:[2,40]}],
217
+ defaultActions: {16:[2,1],24:[2,25],38:[2,23],55:[2,21]},
218
+ parseError: function parseError(str, hash) {
219
+ throw new Error(str);
220
+ },
221
+ parse: function parse(input) {
222
+ var self = this, stack = [0], vstack = [null], lstack = [], table = this.table, yytext = "", yylineno = 0, yyleng = 0, recovering = 0, TERROR = 2, EOF = 1;
223
+ this.lexer.setInput(input);
224
+ this.lexer.yy = this.yy;
225
+ this.yy.lexer = this.lexer;
226
+ this.yy.parser = this;
227
+ if (typeof this.lexer.yylloc == "undefined")
228
+ this.lexer.yylloc = {};
229
+ var yyloc = this.lexer.yylloc;
230
+ lstack.push(yyloc);
231
+ var ranges = this.lexer.options && this.lexer.options.ranges;
232
+ if (typeof this.yy.parseError === "function")
233
+ this.parseError = this.yy.parseError;
234
+ function popStack(n) {
235
+ stack.length = stack.length - 2 * n;
236
+ vstack.length = vstack.length - n;
237
+ lstack.length = lstack.length - n;
238
+ }
239
+ function lex() {
240
+ var token;
241
+ token = self.lexer.lex() || 1;
242
+ if (typeof token !== "number") {
243
+ token = self.symbols_[token] || token;
244
+ }
245
+ return token;
246
+ }
247
+ var symbol, preErrorSymbol, state, action, a, r, yyval = {}, p, len, newState, expected;
248
+ while (true) {
249
+ state = stack[stack.length - 1];
250
+ if (this.defaultActions[state]) {
251
+ action = this.defaultActions[state];
252
+ } else {
253
+ if (symbol === null || typeof symbol == "undefined") {
254
+ symbol = lex();
255
+ }
256
+ action = table[state] && table[state][symbol];
257
+ }
258
+ if (typeof action === "undefined" || !action.length || !action[0]) {
259
+ var errStr = "";
260
+ if (!recovering) {
261
+ expected = [];
262
+ for (p in table[state])
263
+ if (this.terminals_[p] && p > 2) {
264
+ expected.push("'" + this.terminals_[p] + "'");
265
+ }
266
+ if (this.lexer.showPosition) {
267
+ errStr = "Parse error on line " + (yylineno + 1) + ":\n" + this.lexer.showPosition() + "\nExpecting " + expected.join(", ") + ", got '" + (this.terminals_[symbol] || symbol) + "'";
268
+ } else {
269
+ errStr = "Parse error on line " + (yylineno + 1) + ": Unexpected " + (symbol == 1?"end of input":"'" + (this.terminals_[symbol] || symbol) + "'");
270
+ }
271
+ this.parseError(errStr, {text: this.lexer.match, token: this.terminals_[symbol] || symbol, line: this.lexer.yylineno, loc: yyloc, expected: expected});
272
+ }
273
+ }
274
+ if (action[0] instanceof Array && action.length > 1) {
275
+ throw new Error("Parse Error: multiple actions possible at state: " + state + ", token: " + symbol);
276
+ }
277
+ switch (action[0]) {
278
+ case 1:
279
+ stack.push(symbol);
280
+ vstack.push(this.lexer.yytext);
281
+ lstack.push(this.lexer.yylloc);
282
+ stack.push(action[1]);
283
+ symbol = null;
284
+ if (!preErrorSymbol) {
285
+ yyleng = this.lexer.yyleng;
286
+ yytext = this.lexer.yytext;
287
+ yylineno = this.lexer.yylineno;
288
+ yyloc = this.lexer.yylloc;
289
+ if (recovering > 0)
290
+ recovering--;
291
+ } else {
292
+ symbol = preErrorSymbol;
293
+ preErrorSymbol = null;
294
+ }
295
+ break;
296
+ case 2:
297
+ len = this.productions_[action[1]][1];
298
+ yyval.$ = vstack[vstack.length - len];
299
+ yyval._$ = {first_line: lstack[lstack.length - (len || 1)].first_line, last_line: lstack[lstack.length - 1].last_line, first_column: lstack[lstack.length - (len || 1)].first_column, last_column: lstack[lstack.length - 1].last_column};
300
+ if (ranges) {
301
+ yyval._$.range = [lstack[lstack.length - (len || 1)].range[0], lstack[lstack.length - 1].range[1]];
302
+ }
303
+ r = this.performAction.call(yyval, yytext, yyleng, yylineno, this.yy, action[1], vstack, lstack);
304
+ if (typeof r !== "undefined") {
305
+ return r;
306
+ }
307
+ if (len) {
308
+ stack = stack.slice(0, -1 * len * 2);
309
+ vstack = vstack.slice(0, -1 * len);
310
+ lstack = lstack.slice(0, -1 * len);
311
+ }
312
+ stack.push(this.productions_[action[1]][0]);
313
+ vstack.push(yyval.$);
314
+ lstack.push(yyval._$);
315
+ newState = table[stack[stack.length - 2]][stack[stack.length - 1]];
316
+ stack.push(newState);
317
+ break;
318
+ case 3:
319
+ return true;
320
+ }
321
+ }
322
+ return true;
323
+ }
324
+ };
325
+ /* Jison generated lexer */
326
+ var lexer = (function(){
327
+ var lexer = ({EOF:1,
328
+ parseError:function parseError(str, hash) {
329
+ if (this.yy.parser) {
330
+ this.yy.parser.parseError(str, hash);
331
+ } else {
332
+ throw new Error(str);
333
+ }
334
+ },
335
+ setInput:function (input) {
336
+ this._input = input;
337
+ this._more = this._less = this.done = false;
338
+ this.yylineno = this.yyleng = 0;
339
+ this.yytext = this.matched = this.match = '';
340
+ this.conditionStack = ['INITIAL'];
341
+ this.yylloc = {first_line:1,first_column:0,last_line:1,last_column:0};
342
+ if (this.options.ranges) this.yylloc.range = [0,0];
343
+ this.offset = 0;
344
+ return this;
345
+ },
346
+ input:function () {
347
+ var ch = this._input[0];
348
+ this.yytext += ch;
349
+ this.yyleng++;
350
+ this.offset++;
351
+ this.match += ch;
352
+ this.matched += ch;
353
+ var lines = ch.match(/(?:\r\n?|\n).*/g);
354
+ if (lines) {
355
+ this.yylineno++;
356
+ this.yylloc.last_line++;
357
+ } else {
358
+ this.yylloc.last_column++;
359
+ }
360
+ if (this.options.ranges) this.yylloc.range[1]++;
361
+
362
+ this._input = this._input.slice(1);
363
+ return ch;
364
+ },
365
+ unput:function (ch) {
366
+ var len = ch.length;
367
+ var lines = ch.split(/(?:\r\n?|\n)/g);
368
+
369
+ this._input = ch + this._input;
370
+ this.yytext = this.yytext.substr(0, this.yytext.length-len-1);
371
+ //this.yyleng -= len;
372
+ this.offset -= len;
373
+ var oldLines = this.match.split(/(?:\r\n?|\n)/g);
374
+ this.match = this.match.substr(0, this.match.length-1);
375
+ this.matched = this.matched.substr(0, this.matched.length-1);
376
+
377
+ if (lines.length-1) this.yylineno -= lines.length-1;
378
+ var r = this.yylloc.range;
379
+
380
+ this.yylloc = {first_line: this.yylloc.first_line,
381
+ last_line: this.yylineno+1,
382
+ first_column: this.yylloc.first_column,
383
+ last_column: lines ?
384
+ (lines.length === oldLines.length ? this.yylloc.first_column : 0) + oldLines[oldLines.length - lines.length].length - lines[0].length:
385
+ this.yylloc.first_column - len
386
+ };
387
+
388
+ if (this.options.ranges) {
389
+ this.yylloc.range = [r[0], r[0] + this.yyleng - len];
390
+ }
391
+ return this;
392
+ },
393
+ more:function () {
394
+ this._more = true;
395
+ return this;
396
+ },
397
+ less:function (n) {
398
+ this.unput(this.match.slice(n));
399
+ },
400
+ pastInput:function () {
401
+ var past = this.matched.substr(0, this.matched.length - this.match.length);
402
+ return (past.length > 20 ? '...':'') + past.substr(-20).replace(/\n/g, "");
403
+ },
404
+ upcomingInput:function () {
405
+ var next = this.match;
406
+ if (next.length < 20) {
407
+ next += this._input.substr(0, 20-next.length);
408
+ }
409
+ return (next.substr(0,20)+(next.length > 20 ? '...':'')).replace(/\n/g, "");
410
+ },
411
+ showPosition:function () {
412
+ var pre = this.pastInput();
413
+ var c = new Array(pre.length + 1).join("-");
414
+ return pre + this.upcomingInput() + "\n" + c+"^";
415
+ },
416
+ next:function () {
417
+ if (this.done) {
418
+ return this.EOF;
419
+ }
420
+ if (!this._input) this.done = true;
421
+
422
+ var token,
423
+ match,
424
+ tempMatch,
425
+ index,
426
+ col,
427
+ lines;
428
+ if (!this._more) {
429
+ this.yytext = '';
430
+ this.match = '';
431
+ }
432
+ var rules = this._currentRules();
433
+ for (var i=0;i < rules.length; i++) {
434
+ tempMatch = this._input.match(this.rules[rules[i]]);
435
+ if (tempMatch && (!match || tempMatch[0].length > match[0].length)) {
436
+ match = tempMatch;
437
+ index = i;
438
+ if (!this.options.flex) break;
439
+ }
440
+ }
441
+ if (match) {
442
+ lines = match[0].match(/(?:\r\n?|\n).*/g);
443
+ if (lines) this.yylineno += lines.length;
444
+ this.yylloc = {first_line: this.yylloc.last_line,
445
+ last_line: this.yylineno+1,
446
+ first_column: this.yylloc.last_column,
447
+ last_column: lines ? lines[lines.length-1].length-lines[lines.length-1].match(/\r?\n?/)[0].length : this.yylloc.last_column + match[0].length};
448
+ this.yytext += match[0];
449
+ this.match += match[0];
450
+ this.matches = match;
451
+ this.yyleng = this.yytext.length;
452
+ if (this.options.ranges) {
453
+ this.yylloc.range = [this.offset, this.offset += this.yyleng];
454
+ }
455
+ this._more = false;
456
+ this._input = this._input.slice(match[0].length);
457
+ this.matched += match[0];
458
+ token = this.performAction.call(this, this.yy, this, rules[index],this.conditionStack[this.conditionStack.length-1]);
459
+ if (this.done && this._input) this.done = false;
460
+ if (token) return token;
461
+ else return;
462
+ }
463
+ if (this._input === "") {
464
+ return this.EOF;
465
+ } else {
466
+ return this.parseError('Lexical error on line '+(this.yylineno+1)+'. Unrecognized text.\n'+this.showPosition(),
467
+ {text: "", token: null, line: this.yylineno});
468
+ }
469
+ },
470
+ lex:function lex() {
471
+ var r = this.next();
472
+ if (typeof r !== 'undefined') {
473
+ return r;
474
+ } else {
475
+ return this.lex();
476
+ }
477
+ },
478
+ begin:function begin(condition) {
479
+ this.conditionStack.push(condition);
480
+ },
481
+ popState:function popState() {
482
+ return this.conditionStack.pop();
483
+ },
484
+ _currentRules:function _currentRules() {
485
+ return this.conditions[this.conditionStack[this.conditionStack.length-1]].rules;
486
+ },
487
+ topState:function () {
488
+ return this.conditionStack[this.conditionStack.length-2];
489
+ },
490
+ pushState:function begin(condition) {
491
+ this.begin(condition);
492
+ }});
493
+ lexer.options = {};
494
+ lexer.performAction = function anonymous(yy,yy_,$avoiding_name_collisions,YY_START) {
495
+
496
+ var YYSTATE=YY_START
497
+ switch($avoiding_name_collisions) {
498
+ case 0:
499
+ if(yy_.yytext.slice(-1) !== "\\") this.begin("mu");
500
+ if(yy_.yytext.slice(-1) === "\\") yy_.yytext = yy_.yytext.substr(0,yy_.yyleng-1), this.begin("emu");
501
+ if(yy_.yytext) return 14;
502
+
503
+ break;
504
+ case 1: return 14;
505
+ break;
506
+ case 2: this.popState(); return 14;
507
+ break;
508
+ case 3: return 24;
509
+ break;
510
+ case 4: return 16;
511
+ break;
512
+ case 5: return 20;
513
+ break;
514
+ case 6: return 19;
515
+ break;
516
+ case 7: return 19;
517
+ break;
518
+ case 8: return 23;
519
+ break;
520
+ case 9: return 23;
521
+ break;
522
+ case 10: yy_.yytext = yy_.yytext.substr(3,yy_.yyleng-5); this.popState(); return 15;
523
+ break;
524
+ case 11: return 22;
525
+ break;
526
+ case 12: return 35;
527
+ break;
528
+ case 13: return 34;
529
+ break;
530
+ case 14: return 34;
531
+ break;
532
+ case 15: return 37;
533
+ break;
534
+ case 16: /*ignore whitespace*/
535
+ break;
536
+ case 17: this.popState(); return 18;
537
+ break;
538
+ case 18: this.popState(); return 18;
539
+ break;
540
+ case 19: yy_.yytext = yy_.yytext.substr(1,yy_.yyleng-2).replace(/\\"/g,'"'); return 29;
541
+ break;
542
+ case 20: yy_.yytext = yy_.yytext.substr(1,yy_.yyleng-2).replace(/\\'/g,"'"); return 29;
543
+ break;
544
+ case 21: yy_.yytext = yy_.yytext.substr(1); return 27;
545
+ break;
546
+ case 22: return 31;
547
+ break;
548
+ case 23: return 31;
549
+ break;
550
+ case 24: return 30;
551
+ break;
552
+ case 25: return 34;
553
+ break;
554
+ case 26: yy_.yytext = yy_.yytext.substr(1, yy_.yyleng-2); return 34;
555
+ break;
556
+ case 27: return 'INVALID';
557
+ break;
558
+ case 28: return 5;
559
+ break;
560
+ }
561
+ };
562
+ lexer.rules = [/^(?:[^\x00]*?(?=(\{\{)))/,/^(?:[^\x00]+)/,/^(?:[^\x00]{2,}?(?=(\{\{)))/,/^(?:\{\{>)/,/^(?:\{\{#)/,/^(?:\{\{\/)/,/^(?:\{\{\^)/,/^(?:\{\{\s*else\b)/,/^(?:\{\{\{)/,/^(?:\{\{&)/,/^(?:\{\{![\s\S]*?\}\})/,/^(?:\{\{)/,/^(?:=)/,/^(?:\.(?=[} ]))/,/^(?:\.\.)/,/^(?:[\/.])/,/^(?:\s+)/,/^(?:\}\}\})/,/^(?:\}\})/,/^(?:"(\\["]|[^"])*")/,/^(?:'(\\[']|[^'])*')/,/^(?:@[a-zA-Z]+)/,/^(?:true(?=[}\s]))/,/^(?:false(?=[}\s]))/,/^(?:[0-9]+(?=[}\s]))/,/^(?:[a-zA-Z0-9_$-]+(?=[=}\s\/.]))/,/^(?:\[[^\]]*\])/,/^(?:.)/,/^(?:$)/];
563
+ lexer.conditions = {"mu":{"rules":[3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28],"inclusive":false},"emu":{"rules":[2],"inclusive":false},"INITIAL":{"rules":[0,1,28],"inclusive":true}};
564
+ return lexer;})()
565
+ parser.lexer = lexer;function Parser () { this.yy = {}; }Parser.prototype = parser;parser.Parser = Parser;
566
+ return new Parser;
567
+ })();
568
+ if (typeof require !== 'undefined' && typeof exports !== 'undefined') {
569
+ exports.parser = handlebars;
570
+ exports.Parser = handlebars.Parser;
571
+ exports.parse = function () { return handlebars.parse.apply(handlebars, arguments); }
572
+ exports.main = function commonjsMain(args) {
573
+ if (!args[1])
574
+ throw new Error('Usage: '+args[0]+' FILE');
575
+ var source, cwd;
576
+ if (typeof process !== 'undefined') {
577
+ source = require('fs').readFileSync(require('path').resolve(args[1]), "utf8");
578
+ } else {
579
+ source = require("file").path(require("file").cwd()).join(args[1]).read({charset: "utf-8"});
580
+ }
581
+ return exports.parser.parse(source);
582
+ }
583
+ if (typeof module !== 'undefined' && require.main === module) {
584
+ exports.main(typeof process !== 'undefined' ? process.argv.slice(1) : require("system").args);
585
+ }
586
+ };
587
+ ;
588
+ // lib/handlebars/compiler/base.js
589
+ Handlebars.Parser = handlebars;
590
+
591
+ Handlebars.parse = function(string) {
592
+ Handlebars.Parser.yy = Handlebars.AST;
593
+ return Handlebars.Parser.parse(string);
594
+ };
595
+
596
+ Handlebars.print = function(ast) {
597
+ return new Handlebars.PrintVisitor().accept(ast);
598
+ };
599
+
600
+ Handlebars.logger = {
601
+ DEBUG: 0, INFO: 1, WARN: 2, ERROR: 3, level: 3,
602
+
603
+ // override in the host environment
604
+ log: function(level, str) {}
605
+ };
606
+
607
+ Handlebars.log = function(level, str) { Handlebars.logger.log(level, str); };
608
+ ;
609
+ // lib/handlebars/compiler/ast.js
610
+ (function() {
611
+
612
+ Handlebars.AST = {};
613
+
614
+ Handlebars.AST.ProgramNode = function(statements, inverse) {
615
+ this.type = "program";
616
+ this.statements = statements;
617
+ if(inverse) { this.inverse = new Handlebars.AST.ProgramNode(inverse); }
618
+ };
619
+
620
+ Handlebars.AST.MustacheNode = function(rawParams, hash, unescaped) {
621
+ this.type = "mustache";
622
+ this.escaped = !unescaped;
623
+ this.hash = hash;
624
+
625
+ var id = this.id = rawParams[0];
626
+ var params = this.params = rawParams.slice(1);
627
+
628
+ // a mustache is an eligible helper if:
629
+ // * its id is simple (a single part, not `this` or `..`)
630
+ var eligibleHelper = this.eligibleHelper = id.isSimple;
631
+
632
+ // a mustache is definitely a helper if:
633
+ // * it is an eligible helper, and
634
+ // * it has at least one parameter or hash segment
635
+ this.isHelper = eligibleHelper && (params.length || hash);
636
+
637
+ // if a mustache is an eligible helper but not a definite
638
+ // helper, it is ambiguous, and will be resolved in a later
639
+ // pass or at runtime.
640
+ };
641
+
642
+ Handlebars.AST.PartialNode = function(id, context) {
643
+ this.type = "partial";
644
+
645
+ // TODO: disallow complex IDs
646
+
647
+ this.id = id;
648
+ this.context = context;
649
+ };
650
+
651
+ var verifyMatch = function(open, close) {
652
+ if(open.original !== close.original) {
653
+ throw new Handlebars.Exception(open.original + " doesn't match " + close.original);
654
+ }
655
+ };
656
+
657
+ Handlebars.AST.BlockNode = function(mustache, program, inverse, close) {
658
+ verifyMatch(mustache.id, close);
659
+ this.type = "block";
660
+ this.mustache = mustache;
661
+ this.program = program;
662
+ this.inverse = inverse;
663
+
664
+ if (this.inverse && !this.program) {
665
+ this.isInverse = true;
666
+ }
667
+ };
668
+
669
+ Handlebars.AST.ContentNode = function(string) {
670
+ this.type = "content";
671
+ this.string = string;
672
+ };
673
+
674
+ Handlebars.AST.HashNode = function(pairs) {
675
+ this.type = "hash";
676
+ this.pairs = pairs;
677
+ };
678
+
679
+ Handlebars.AST.IdNode = function(parts) {
680
+ this.type = "ID";
681
+ this.original = parts.join(".");
682
+
683
+ var dig = [], depth = 0;
684
+
685
+ for(var i=0,l=parts.length; i<l; i++) {
686
+ var part = parts[i];
687
+
688
+ if(part === "..") { depth++; }
689
+ else if(part === "." || part === "this") { this.isScoped = true; }
690
+ else { dig.push(part); }
691
+ }
692
+
693
+ this.parts = dig;
694
+ this.string = dig.join('.');
695
+ this.depth = depth;
696
+
697
+ // an ID is simple if it only has one part, and that part is not
698
+ // `..` or `this`.
699
+ this.isSimple = parts.length === 1 && !this.isScoped && depth === 0;
700
+ };
701
+
702
+ Handlebars.AST.DataNode = function(id) {
703
+ this.type = "DATA";
704
+ this.id = id;
705
+ };
706
+
707
+ Handlebars.AST.StringNode = function(string) {
708
+ this.type = "STRING";
709
+ this.string = string;
710
+ };
711
+
712
+ Handlebars.AST.IntegerNode = function(integer) {
713
+ this.type = "INTEGER";
714
+ this.integer = integer;
715
+ };
716
+
717
+ Handlebars.AST.BooleanNode = function(bool) {
718
+ this.type = "BOOLEAN";
719
+ this.bool = bool;
720
+ };
721
+
722
+ Handlebars.AST.CommentNode = function(comment) {
723
+ this.type = "comment";
724
+ this.comment = comment;
725
+ };
726
+
727
+ })();;
728
+ // lib/handlebars/utils.js
729
+ Handlebars.Exception = function(message) {
730
+ var tmp = Error.prototype.constructor.apply(this, arguments);
731
+
732
+ for (var p in tmp) {
733
+ if (tmp.hasOwnProperty(p)) { this[p] = tmp[p]; }
734
+ }
735
+
736
+ this.message = tmp.message;
737
+ };
738
+ Handlebars.Exception.prototype = new Error();
739
+
740
+ // Build out our basic SafeString type
741
+ Handlebars.SafeString = function(string) {
742
+ this.string = string;
743
+ };
744
+ Handlebars.SafeString.prototype.toString = function() {
745
+ return this.string.toString();
746
+ };
747
+
748
+ (function() {
749
+ var escape = {
750
+ "<": "&lt;",
751
+ ">": "&gt;",
752
+ '"': "&quot;",
753
+ "'": "&#x27;",
754
+ "`": "&#x60;"
755
+ };
756
+
757
+ var badChars = /&(?!\w+;)|[<>"'`]/g;
758
+ var possible = /[&<>"'`]/;
759
+
760
+ var escapeChar = function(chr) {
761
+ return escape[chr] || "&amp;";
762
+ };
763
+
764
+ Handlebars.Utils = {
765
+ escapeExpression: function(string) {
766
+ // don't escape SafeStrings, since they're already safe
767
+ if (string instanceof Handlebars.SafeString) {
768
+ return string.toString();
769
+ } else if (string == null || string === false) {
770
+ return "";
771
+ }
772
+
773
+ if(!possible.test(string)) { return string; }
774
+ return string.replace(badChars, escapeChar);
775
+ },
776
+
777
+ isEmpty: function(value) {
778
+ if (typeof value === "undefined") {
779
+ return true;
780
+ } else if (value === null) {
781
+ return true;
782
+ } else if (value === false) {
783
+ return true;
784
+ } else if(Object.prototype.toString.call(value) === "[object Array]" && value.length === 0) {
785
+ return true;
786
+ } else {
787
+ return false;
788
+ }
789
+ }
790
+ };
791
+ })();;
792
+ // lib/handlebars/compiler/compiler.js
793
+
794
+ /*jshint eqnull:true*/
795
+ Handlebars.Compiler = function() {};
796
+ Handlebars.JavaScriptCompiler = function() {};
797
+
798
+ (function(Compiler, JavaScriptCompiler) {
799
+ // the foundHelper register will disambiguate helper lookup from finding a
800
+ // function in a context. This is necessary for mustache compatibility, which
801
+ // requires that context functions in blocks are evaluated by blockHelperMissing,
802
+ // and then proceed as if the resulting value was provided to blockHelperMissing.
803
+
804
+ Compiler.prototype = {
805
+ compiler: Compiler,
806
+
807
+ disassemble: function() {
808
+ var opcodes = this.opcodes, opcode, out = [], params, param;
809
+
810
+ for (var i=0, l=opcodes.length; i<l; i++) {
811
+ opcode = opcodes[i];
812
+
813
+ if (opcode.opcode === 'DECLARE') {
814
+ out.push("DECLARE " + opcode.name + "=" + opcode.value);
815
+ } else {
816
+ params = [];
817
+ for (var j=0; j<opcode.args.length; j++) {
818
+ param = opcode.args[j];
819
+ if (typeof param === "string") {
820
+ param = "\"" + param.replace("\n", "\\n") + "\"";
821
+ }
822
+ params.push(param);
823
+ }
824
+ out.push(opcode.opcode + " " + params.join(" "));
825
+ }
826
+ }
827
+
828
+ return out.join("\n");
829
+ },
830
+
831
+ guid: 0,
832
+
833
+ compile: function(program, options) {
834
+ this.children = [];
835
+ this.depths = {list: []};
836
+ this.options = options;
837
+
838
+ // These changes will propagate to the other compiler components
839
+ var knownHelpers = this.options.knownHelpers;
840
+ this.options.knownHelpers = {
841
+ 'helperMissing': true,
842
+ 'blockHelperMissing': true,
843
+ 'each': true,
844
+ 'if': true,
845
+ 'unless': true,
846
+ 'with': true,
847
+ 'log': true
848
+ };
849
+ if (knownHelpers) {
850
+ for (var name in knownHelpers) {
851
+ this.options.knownHelpers[name] = knownHelpers[name];
852
+ }
853
+ }
854
+
855
+ return this.program(program);
856
+ },
857
+
858
+ accept: function(node) {
859
+ return this[node.type](node);
860
+ },
861
+
862
+ program: function(program) {
863
+ var statements = program.statements, statement;
864
+ this.opcodes = [];
865
+
866
+ for(var i=0, l=statements.length; i<l; i++) {
867
+ statement = statements[i];
868
+ this[statement.type](statement);
869
+ }
870
+ this.isSimple = l === 1;
871
+
872
+ this.depths.list = this.depths.list.sort(function(a, b) {
873
+ return a - b;
874
+ });
875
+
876
+ return this;
877
+ },
878
+
879
+ compileProgram: function(program) {
880
+ var result = new this.compiler().compile(program, this.options);
881
+ var guid = this.guid++, depth;
882
+
883
+ this.usePartial = this.usePartial || result.usePartial;
884
+
885
+ this.children[guid] = result;
886
+
887
+ for(var i=0, l=result.depths.list.length; i<l; i++) {
888
+ depth = result.depths.list[i];
889
+
890
+ if(depth < 2) { continue; }
891
+ else { this.addDepth(depth - 1); }
892
+ }
893
+
894
+ return guid;
895
+ },
896
+
897
+ block: function(block) {
898
+ var mustache = block.mustache,
899
+ program = block.program,
900
+ inverse = block.inverse;
901
+
902
+ if (program) {
903
+ program = this.compileProgram(program);
904
+ }
905
+
906
+ if (inverse) {
907
+ inverse = this.compileProgram(inverse);
908
+ }
909
+
910
+ var type = this.classifyMustache(mustache);
911
+
912
+ if (type === "helper") {
913
+ this.helperMustache(mustache, program, inverse);
914
+ } else if (type === "simple") {
915
+ this.simpleMustache(mustache);
916
+
917
+ // now that the simple mustache is resolved, we need to
918
+ // evaluate it by executing `blockHelperMissing`
919
+ this.opcode('pushProgram', program);
920
+ this.opcode('pushProgram', inverse);
921
+ this.opcode('pushLiteral', '{}');
922
+ this.opcode('blockValue');
923
+ } else {
924
+ this.ambiguousMustache(mustache, program, inverse);
925
+
926
+ // now that the simple mustache is resolved, we need to
927
+ // evaluate it by executing `blockHelperMissing`
928
+ this.opcode('pushProgram', program);
929
+ this.opcode('pushProgram', inverse);
930
+ this.opcode('pushLiteral', '{}');
931
+ this.opcode('ambiguousBlockValue');
932
+ }
933
+
934
+ this.opcode('append');
935
+ },
936
+
937
+ hash: function(hash) {
938
+ var pairs = hash.pairs, pair, val;
939
+
940
+ this.opcode('push', '{}');
941
+
942
+ for(var i=0, l=pairs.length; i<l; i++) {
943
+ pair = pairs[i];
944
+ val = pair[1];
945
+
946
+ this.accept(val);
947
+ this.opcode('assignToHash', pair[0]);
948
+ }
949
+ },
950
+
951
+ partial: function(partial) {
952
+ var id = partial.id;
953
+ this.usePartial = true;
954
+
955
+ if(partial.context) {
956
+ this.ID(partial.context);
957
+ } else {
958
+ this.opcode('push', 'depth0');
959
+ }
960
+
961
+ this.opcode('invokePartial', id.original);
962
+ this.opcode('append');
963
+ },
964
+
965
+ content: function(content) {
966
+ this.opcode('appendContent', content.string);
967
+ },
968
+
969
+ mustache: function(mustache) {
970
+ var options = this.options;
971
+ var type = this.classifyMustache(mustache);
972
+
973
+ if (type === "simple") {
974
+ this.simpleMustache(mustache);
975
+ } else if (type === "helper") {
976
+ this.helperMustache(mustache);
977
+ } else {
978
+ this.ambiguousMustache(mustache);
979
+ }
980
+
981
+ if(mustache.escaped && !options.noEscape) {
982
+ this.opcode('appendEscaped');
983
+ } else {
984
+ this.opcode('append');
985
+ }
986
+ },
987
+
988
+ ambiguousMustache: function(mustache, program, inverse) {
989
+ var id = mustache.id, name = id.parts[0];
990
+
991
+ this.opcode('getContext', id.depth);
992
+
993
+ this.opcode('pushProgram', program);
994
+ this.opcode('pushProgram', inverse);
995
+
996
+ this.opcode('invokeAmbiguous', name);
997
+ },
998
+
999
+ simpleMustache: function(mustache, program, inverse) {
1000
+ var id = mustache.id;
1001
+
1002
+ if (id.type === 'DATA') {
1003
+ this.DATA(id);
1004
+ } else if (id.parts.length) {
1005
+ this.ID(id);
1006
+ } else {
1007
+ // Simplified ID for `this`
1008
+ this.addDepth(id.depth);
1009
+ this.opcode('getContext', id.depth);
1010
+ this.opcode('pushContext');
1011
+ }
1012
+
1013
+ this.opcode('resolvePossibleLambda');
1014
+ },
1015
+
1016
+ helperMustache: function(mustache, program, inverse) {
1017
+ var params = this.setupFullMustacheParams(mustache, program, inverse),
1018
+ name = mustache.id.parts[0];
1019
+
1020
+ if (this.options.knownHelpers[name]) {
1021
+ this.opcode('invokeKnownHelper', params.length, name);
1022
+ } else if (this.knownHelpersOnly) {
1023
+ throw new Error("You specified knownHelpersOnly, but used the unknown helper " + name);
1024
+ } else {
1025
+ this.opcode('invokeHelper', params.length, name);
1026
+ }
1027
+ },
1028
+
1029
+ ID: function(id) {
1030
+ this.addDepth(id.depth);
1031
+ this.opcode('getContext', id.depth);
1032
+
1033
+ var name = id.parts[0];
1034
+ if (!name) {
1035
+ this.opcode('pushContext');
1036
+ } else {
1037
+ this.opcode('lookupOnContext', id.parts[0]);
1038
+ }
1039
+
1040
+ for(var i=1, l=id.parts.length; i<l; i++) {
1041
+ this.opcode('lookup', id.parts[i]);
1042
+ }
1043
+ },
1044
+
1045
+ DATA: function(data) {
1046
+ this.options.data = true;
1047
+ this.opcode('lookupData', data.id);
1048
+ },
1049
+
1050
+ STRING: function(string) {
1051
+ this.opcode('pushString', string.string);
1052
+ },
1053
+
1054
+ INTEGER: function(integer) {
1055
+ this.opcode('pushLiteral', integer.integer);
1056
+ },
1057
+
1058
+ BOOLEAN: function(bool) {
1059
+ this.opcode('pushLiteral', bool.bool);
1060
+ },
1061
+
1062
+ comment: function() {},
1063
+
1064
+ // HELPERS
1065
+ opcode: function(name) {
1066
+ this.opcodes.push({ opcode: name, args: [].slice.call(arguments, 1) });
1067
+ },
1068
+
1069
+ declare: function(name, value) {
1070
+ this.opcodes.push({ opcode: 'DECLARE', name: name, value: value });
1071
+ },
1072
+
1073
+ addDepth: function(depth) {
1074
+ if(isNaN(depth)) { throw new Error("EWOT"); }
1075
+ if(depth === 0) { return; }
1076
+
1077
+ if(!this.depths[depth]) {
1078
+ this.depths[depth] = true;
1079
+ this.depths.list.push(depth);
1080
+ }
1081
+ },
1082
+
1083
+ classifyMustache: function(mustache) {
1084
+ var isHelper = mustache.isHelper;
1085
+ var isEligible = mustache.eligibleHelper;
1086
+ var options = this.options;
1087
+
1088
+ // if ambiguous, we can possibly resolve the ambiguity now
1089
+ if (isEligible && !isHelper) {
1090
+ var name = mustache.id.parts[0];
1091
+
1092
+ if (options.knownHelpers[name]) {
1093
+ isHelper = true;
1094
+ } else if (options.knownHelpersOnly) {
1095
+ isEligible = false;
1096
+ }
1097
+ }
1098
+
1099
+ if (isHelper) { return "helper"; }
1100
+ else if (isEligible) { return "ambiguous"; }
1101
+ else { return "simple"; }
1102
+ },
1103
+
1104
+ pushParams: function(params) {
1105
+ var i = params.length, param;
1106
+
1107
+ while(i--) {
1108
+ param = params[i];
1109
+
1110
+ if(this.options.stringParams) {
1111
+ if(param.depth) {
1112
+ this.addDepth(param.depth);
1113
+ }
1114
+
1115
+ this.opcode('getContext', param.depth || 0);
1116
+ this.opcode('pushStringParam', param.string);
1117
+ } else {
1118
+ this[param.type](param);
1119
+ }
1120
+ }
1121
+ },
1122
+
1123
+ setupMustacheParams: function(mustache) {
1124
+ var params = mustache.params;
1125
+ this.pushParams(params);
1126
+
1127
+ if(mustache.hash) {
1128
+ this.hash(mustache.hash);
1129
+ } else {
1130
+ this.opcode('pushLiteral', '{}');
1131
+ }
1132
+
1133
+ return params;
1134
+ },
1135
+
1136
+ // this will replace setupMustacheParams when we're done
1137
+ setupFullMustacheParams: function(mustache, program, inverse) {
1138
+ var params = mustache.params;
1139
+ this.pushParams(params);
1140
+
1141
+ this.opcode('pushProgram', program);
1142
+ this.opcode('pushProgram', inverse);
1143
+
1144
+ if(mustache.hash) {
1145
+ this.hash(mustache.hash);
1146
+ } else {
1147
+ this.opcode('pushLiteral', '{}');
1148
+ }
1149
+
1150
+ return params;
1151
+ }
1152
+ };
1153
+
1154
+ var Literal = function(value) {
1155
+ this.value = value;
1156
+ };
1157
+
1158
+ JavaScriptCompiler.prototype = {
1159
+ // PUBLIC API: You can override these methods in a subclass to provide
1160
+ // alternative compiled forms for name lookup and buffering semantics
1161
+ nameLookup: function(parent, name, type) {
1162
+ if (/^[0-9]+$/.test(name)) {
1163
+ return parent + "[" + name + "]";
1164
+ } else if (JavaScriptCompiler.isValidJavaScriptVariableName(name)) {
1165
+ return parent + "." + name;
1166
+ }
1167
+ else {
1168
+ return parent + "['" + name + "']";
1169
+ }
1170
+ },
1171
+
1172
+ appendToBuffer: function(string) {
1173
+ if (this.environment.isSimple) {
1174
+ return "return " + string + ";";
1175
+ } else {
1176
+ return "buffer += " + string + ";";
1177
+ }
1178
+ },
1179
+
1180
+ initializeBuffer: function() {
1181
+ return this.quotedString("");
1182
+ },
1183
+
1184
+ namespace: "Handlebars",
1185
+ // END PUBLIC API
1186
+
1187
+ compile: function(environment, options, context, asObject) {
1188
+ this.environment = environment;
1189
+ this.options = options || {};
1190
+
1191
+ Handlebars.log(Handlebars.logger.DEBUG, this.environment.disassemble() + "\n\n");
1192
+
1193
+ this.name = this.environment.name;
1194
+ this.isChild = !!context;
1195
+ this.context = context || {
1196
+ programs: [],
1197
+ aliases: { }
1198
+ };
1199
+
1200
+ this.preamble();
1201
+
1202
+ this.stackSlot = 0;
1203
+ this.stackVars = [];
1204
+ this.registers = { list: [] };
1205
+ this.compileStack = [];
1206
+
1207
+ this.compileChildren(environment, options);
1208
+
1209
+ var opcodes = environment.opcodes, opcode;
1210
+
1211
+ this.i = 0;
1212
+
1213
+ for(l=opcodes.length; this.i<l; this.i++) {
1214
+ opcode = opcodes[this.i];
1215
+
1216
+ if(opcode.opcode === 'DECLARE') {
1217
+ this[opcode.name] = opcode.value;
1218
+ } else {
1219
+ this[opcode.opcode].apply(this, opcode.args);
1220
+ }
1221
+ }
1222
+
1223
+ return this.createFunctionContext(asObject);
1224
+ },
1225
+
1226
+ nextOpcode: function() {
1227
+ var opcodes = this.environment.opcodes, opcode = opcodes[this.i + 1];
1228
+ return opcodes[this.i + 1];
1229
+ },
1230
+
1231
+ eat: function(opcode) {
1232
+ this.i = this.i + 1;
1233
+ },
1234
+
1235
+ preamble: function() {
1236
+ var out = [];
1237
+
1238
+ if (!this.isChild) {
1239
+ var namespace = this.namespace;
1240
+ var copies = "helpers = helpers || " + namespace + ".helpers;";
1241
+ if (this.environment.usePartial) { copies = copies + " partials = partials || " + namespace + ".partials;"; }
1242
+ if (this.options.data) { copies = copies + " data = data || {};"; }
1243
+ out.push(copies);
1244
+ } else {
1245
+ out.push('');
1246
+ }
1247
+
1248
+ if (!this.environment.isSimple) {
1249
+ out.push(", buffer = " + this.initializeBuffer());
1250
+ } else {
1251
+ out.push("");
1252
+ }
1253
+
1254
+ // track the last context pushed into place to allow skipping the
1255
+ // getContext opcode when it would be a noop
1256
+ this.lastContext = 0;
1257
+ this.source = out;
1258
+ },
1259
+
1260
+ createFunctionContext: function(asObject) {
1261
+ var locals = this.stackVars.concat(this.registers.list);
1262
+
1263
+ if(locals.length > 0) {
1264
+ this.source[1] = this.source[1] + ", " + locals.join(", ");
1265
+ }
1266
+
1267
+ // Generate minimizer alias mappings
1268
+ if (!this.isChild) {
1269
+ var aliases = [];
1270
+ for (var alias in this.context.aliases) {
1271
+ this.source[1] = this.source[1] + ', ' + alias + '=' + this.context.aliases[alias];
1272
+ }
1273
+ }
1274
+
1275
+ if (this.source[1]) {
1276
+ this.source[1] = "var " + this.source[1].substring(2) + ";";
1277
+ }
1278
+
1279
+ // Merge children
1280
+ if (!this.isChild) {
1281
+ this.source[1] += '\n' + this.context.programs.join('\n') + '\n';
1282
+ }
1283
+
1284
+ if (!this.environment.isSimple) {
1285
+ this.source.push("return buffer;");
1286
+ }
1287
+
1288
+ var params = this.isChild ? ["depth0", "data"] : ["Handlebars", "depth0", "helpers", "partials", "data"];
1289
+
1290
+ for(var i=0, l=this.environment.depths.list.length; i<l; i++) {
1291
+ params.push("depth" + this.environment.depths.list[i]);
1292
+ }
1293
+
1294
+ if (asObject) {
1295
+ params.push(this.source.join("\n "));
1296
+
1297
+ return Function.apply(this, params);
1298
+ } else {
1299
+ var functionSource = 'function ' + (this.name || '') + '(' + params.join(',') + ') {\n ' + this.source.join("\n ") + '}';
1300
+ Handlebars.log(Handlebars.logger.DEBUG, functionSource + "\n\n");
1301
+ return functionSource;
1302
+ }
1303
+ },
1304
+
1305
+ // [blockValue]
1306
+ //
1307
+ // On stack, before: hash, inverse, program, value
1308
+ // On stack, after: return value of blockHelperMissing
1309
+ //
1310
+ // The purpose of this opcode is to take a block of the form
1311
+ // `{{#foo}}...{{/foo}}`, resolve the value of `foo`, and
1312
+ // replace it on the stack with the result of properly
1313
+ // invoking blockHelperMissing.
1314
+ blockValue: function() {
1315
+ this.context.aliases.blockHelperMissing = 'helpers.blockHelperMissing';
1316
+
1317
+ var params = ["depth0"];
1318
+ this.setupParams(0, params);
1319
+
1320
+ this.replaceStack(function(current) {
1321
+ params.splice(1, 0, current);
1322
+ return current + " = blockHelperMissing.call(" + params.join(", ") + ")";
1323
+ });
1324
+ },
1325
+
1326
+ // [ambiguousBlockValue]
1327
+ //
1328
+ // On stack, before: hash, inverse, program, value
1329
+ // Compiler value, before: lastHelper=value of last found helper, if any
1330
+ // On stack, after, if no lastHelper: same as [blockValue]
1331
+ // On stack, after, if lastHelper: value
1332
+ ambiguousBlockValue: function() {
1333
+ this.context.aliases.blockHelperMissing = 'helpers.blockHelperMissing';
1334
+
1335
+ var params = ["depth0"];
1336
+ this.setupParams(0, params);
1337
+
1338
+ var current = this.topStack();
1339
+ params.splice(1, 0, current);
1340
+
1341
+ this.source.push("if (!" + this.lastHelper + ") { " + current + " = blockHelperMissing.call(" + params.join(", ") + "); }");
1342
+ },
1343
+
1344
+ // [appendContent]
1345
+ //
1346
+ // On stack, before: ...
1347
+ // On stack, after: ...
1348
+ //
1349
+ // Appends the string value of `content` to the current buffer
1350
+ appendContent: function(content) {
1351
+ this.source.push(this.appendToBuffer(this.quotedString(content)));
1352
+ },
1353
+
1354
+ // [append]
1355
+ //
1356
+ // On stack, before: value, ...
1357
+ // On stack, after: ...
1358
+ //
1359
+ // Coerces `value` to a String and appends it to the current buffer.
1360
+ //
1361
+ // If `value` is truthy, or 0, it is coerced into a string and appended
1362
+ // Otherwise, the empty string is appended
1363
+ append: function() {
1364
+ var local = this.popStack();
1365
+ this.source.push("if(" + local + " || " + local + " === 0) { " + this.appendToBuffer(local) + " }");
1366
+ if (this.environment.isSimple) {
1367
+ this.source.push("else { " + this.appendToBuffer("''") + " }");
1368
+ }
1369
+ },
1370
+
1371
+ // [appendEscaped]
1372
+ //
1373
+ // On stack, before: value, ...
1374
+ // On stack, after: ...
1375
+ //
1376
+ // Escape `value` and append it to the buffer
1377
+ appendEscaped: function() {
1378
+ var opcode = this.nextOpcode(), extra = "";
1379
+ this.context.aliases.escapeExpression = 'this.escapeExpression';
1380
+
1381
+ if(opcode && opcode.opcode === 'appendContent') {
1382
+ extra = " + " + this.quotedString(opcode.args[0]);
1383
+ this.eat(opcode);
1384
+ }
1385
+
1386
+ this.source.push(this.appendToBuffer("escapeExpression(" + this.popStack() + ")" + extra));
1387
+ },
1388
+
1389
+ // [getContext]
1390
+ //
1391
+ // On stack, before: ...
1392
+ // On stack, after: ...
1393
+ // Compiler value, after: lastContext=depth
1394
+ //
1395
+ // Set the value of the `lastContext` compiler value to the depth
1396
+ getContext: function(depth) {
1397
+ if(this.lastContext !== depth) {
1398
+ this.lastContext = depth;
1399
+ }
1400
+ },
1401
+
1402
+ // [lookupOnContext]
1403
+ //
1404
+ // On stack, before: ...
1405
+ // On stack, after: currentContext[name], ...
1406
+ //
1407
+ // Looks up the value of `name` on the current context and pushes
1408
+ // it onto the stack.
1409
+ lookupOnContext: function(name) {
1410
+ this.pushStack(this.nameLookup('depth' + this.lastContext, name, 'context'));
1411
+ },
1412
+
1413
+ // [pushContext]
1414
+ //
1415
+ // On stack, before: ...
1416
+ // On stack, after: currentContext, ...
1417
+ //
1418
+ // Pushes the value of the current context onto the stack.
1419
+ pushContext: function() {
1420
+ this.pushStackLiteral('depth' + this.lastContext);
1421
+ },
1422
+
1423
+ // [resolvePossibleLambda]
1424
+ //
1425
+ // On stack, before: value, ...
1426
+ // On stack, after: resolved value, ...
1427
+ //
1428
+ // If the `value` is a lambda, replace it on the stack by
1429
+ // the return value of the lambda
1430
+ resolvePossibleLambda: function() {
1431
+ this.context.aliases.functionType = '"function"';
1432
+
1433
+ this.replaceStack(function(current) {
1434
+ return "typeof " + current + " === functionType ? " + current + "() : " + current;
1435
+ });
1436
+ },
1437
+
1438
+ // [lookup]
1439
+ //
1440
+ // On stack, before: value, ...
1441
+ // On stack, after: value[name], ...
1442
+ //
1443
+ // Replace the value on the stack with the result of looking
1444
+ // up `name` on `value`
1445
+ lookup: function(name) {
1446
+ this.replaceStack(function(current) {
1447
+ return current + " == null || " + current + " === false ? " + current + " : " + this.nameLookup(current, name, 'context');
1448
+ });
1449
+ },
1450
+
1451
+ // [lookupData]
1452
+ //
1453
+ // On stack, before: ...
1454
+ // On stack, after: data[id], ...
1455
+ //
1456
+ // Push the result of looking up `id` on the current data
1457
+ lookupData: function(id) {
1458
+ this.pushStack(this.nameLookup('data', id, 'data'));
1459
+ },
1460
+
1461
+ // [pushStringParam]
1462
+ //
1463
+ // On stack, before: ...
1464
+ // On stack, after: string, currentContext, ...
1465
+ //
1466
+ // This opcode is designed for use in string mode, which
1467
+ // provides the string value of a parameter along with its
1468
+ // depth rather than resolving it immediately.
1469
+ pushStringParam: function(string) {
1470
+ this.pushStackLiteral('depth' + this.lastContext);
1471
+ this.pushString(string);
1472
+ },
1473
+
1474
+ // [pushString]
1475
+ //
1476
+ // On stack, before: ...
1477
+ // On stack, after: quotedString(string), ...
1478
+ //
1479
+ // Push a quoted version of `string` onto the stack
1480
+ pushString: function(string) {
1481
+ this.pushStackLiteral(this.quotedString(string));
1482
+ },
1483
+
1484
+ // [push]
1485
+ //
1486
+ // On stack, before: ...
1487
+ // On stack, after: expr, ...
1488
+ //
1489
+ // Push an expression onto the stack
1490
+ push: function(expr) {
1491
+ this.pushStack(expr);
1492
+ },
1493
+
1494
+ // [pushLiteral]
1495
+ //
1496
+ // On stack, before: ...
1497
+ // On stack, after: value, ...
1498
+ //
1499
+ // Pushes a value onto the stack. This operation prevents
1500
+ // the compiler from creating a temporary variable to hold
1501
+ // it.
1502
+ pushLiteral: function(value) {
1503
+ this.pushStackLiteral(value);
1504
+ },
1505
+
1506
+ // [pushProgram]
1507
+ //
1508
+ // On stack, before: ...
1509
+ // On stack, after: program(guid), ...
1510
+ //
1511
+ // Push a program expression onto the stack. This takes
1512
+ // a compile-time guid and converts it into a runtime-accessible
1513
+ // expression.
1514
+ pushProgram: function(guid) {
1515
+ if (guid != null) {
1516
+ this.pushStackLiteral(this.programExpression(guid));
1517
+ } else {
1518
+ this.pushStackLiteral(null);
1519
+ }
1520
+ },
1521
+
1522
+ // [invokeHelper]
1523
+ //
1524
+ // On stack, before: hash, inverse, program, params..., ...
1525
+ // On stack, after: result of helper invocation
1526
+ //
1527
+ // Pops off the helper's parameters, invokes the helper,
1528
+ // and pushes the helper's return value onto the stack.
1529
+ //
1530
+ // If the helper is not found, `helperMissing` is called.
1531
+ invokeHelper: function(paramSize, name) {
1532
+ this.context.aliases.helperMissing = 'helpers.helperMissing';
1533
+
1534
+ var helper = this.lastHelper = this.setupHelper(paramSize, name);
1535
+ this.register('foundHelper', helper.name);
1536
+
1537
+ this.pushStack("foundHelper ? foundHelper.call(" +
1538
+ helper.callParams + ") " + ": helperMissing.call(" +
1539
+ helper.helperMissingParams + ")");
1540
+ },
1541
+
1542
+ // [invokeKnownHelper]
1543
+ //
1544
+ // On stack, before: hash, inverse, program, params..., ...
1545
+ // On stack, after: result of helper invocation
1546
+ //
1547
+ // This operation is used when the helper is known to exist,
1548
+ // so a `helperMissing` fallback is not required.
1549
+ invokeKnownHelper: function(paramSize, name) {
1550
+ var helper = this.setupHelper(paramSize, name);
1551
+ this.pushStack(helper.name + ".call(" + helper.callParams + ")");
1552
+ },
1553
+
1554
+ // [invokeAmbiguous]
1555
+ //
1556
+ // On stack, before: hash, inverse, program, params..., ...
1557
+ // On stack, after: result of disambiguation
1558
+ //
1559
+ // This operation is used when an expression like `{{foo}}`
1560
+ // is provided, but we don't know at compile-time whether it
1561
+ // is a helper or a path.
1562
+ //
1563
+ // This operation emits more code than the other options,
1564
+ // and can be avoided by passing the `knownHelpers` and
1565
+ // `knownHelpersOnly` flags at compile-time.
1566
+ invokeAmbiguous: function(name) {
1567
+ this.context.aliases.functionType = '"function"';
1568
+
1569
+ this.pushStackLiteral('{}');
1570
+ var helper = this.setupHelper(0, name);
1571
+
1572
+ var helperName = this.lastHelper = this.nameLookup('helpers', name, 'helper');
1573
+ this.register('foundHelper', helperName);
1574
+
1575
+ var nonHelper = this.nameLookup('depth' + this.lastContext, name, 'context');
1576
+ var nextStack = this.nextStack();
1577
+
1578
+ this.source.push('if (foundHelper) { ' + nextStack + ' = foundHelper.call(' + helper.callParams + '); }');
1579
+ this.source.push('else { ' + nextStack + ' = ' + nonHelper + '; ' + nextStack + ' = typeof ' + nextStack + ' === functionType ? ' + nextStack + '() : ' + nextStack + '; }');
1580
+ },
1581
+
1582
+ // [invokePartial]
1583
+ //
1584
+ // On stack, before: context, ...
1585
+ // On stack after: result of partial invocation
1586
+ //
1587
+ // This operation pops off a context, invokes a partial with that context,
1588
+ // and pushes the result of the invocation back.
1589
+ invokePartial: function(name) {
1590
+ var params = [this.nameLookup('partials', name, 'partial'), "'" + name + "'", this.popStack(), "helpers", "partials"];
1591
+
1592
+ if (this.options.data) {
1593
+ params.push("data");
1594
+ }
1595
+
1596
+ this.context.aliases.self = "this";
1597
+ this.pushStack("self.invokePartial(" + params.join(", ") + ");");
1598
+ },
1599
+
1600
+ // [assignToHash]
1601
+ //
1602
+ // On stack, before: value, hash, ...
1603
+ // On stack, after: hash, ...
1604
+ //
1605
+ // Pops a value and hash off the stack, assigns `hash[key] = value`
1606
+ // and pushes the hash back onto the stack.
1607
+ assignToHash: function(key) {
1608
+ var value = this.popStack();
1609
+ var hash = this.topStack();
1610
+
1611
+ this.source.push(hash + "['" + key + "'] = " + value + ";");
1612
+ },
1613
+
1614
+ // HELPERS
1615
+
1616
+ compiler: JavaScriptCompiler,
1617
+
1618
+ compileChildren: function(environment, options) {
1619
+ var children = environment.children, child, compiler;
1620
+
1621
+ for(var i=0, l=children.length; i<l; i++) {
1622
+ child = children[i];
1623
+ compiler = new this.compiler();
1624
+
1625
+ this.context.programs.push(''); // Placeholder to prevent name conflicts for nested children
1626
+ var index = this.context.programs.length;
1627
+ child.index = index;
1628
+ child.name = 'program' + index;
1629
+ this.context.programs[index] = compiler.compile(child, options, this.context);
1630
+ }
1631
+ },
1632
+
1633
+ programExpression: function(guid) {
1634
+ this.context.aliases.self = "this";
1635
+
1636
+ if(guid == null) {
1637
+ return "self.noop";
1638
+ }
1639
+
1640
+ var child = this.environment.children[guid],
1641
+ depths = child.depths.list, depth;
1642
+
1643
+ var programParams = [child.index, child.name, "data"];
1644
+
1645
+ for(var i=0, l = depths.length; i<l; i++) {
1646
+ depth = depths[i];
1647
+
1648
+ if(depth === 1) { programParams.push("depth0"); }
1649
+ else { programParams.push("depth" + (depth - 1)); }
1650
+ }
1651
+
1652
+ if(depths.length === 0) {
1653
+ return "self.program(" + programParams.join(", ") + ")";
1654
+ } else {
1655
+ programParams.shift();
1656
+ return "self.programWithDepth(" + programParams.join(", ") + ")";
1657
+ }
1658
+ },
1659
+
1660
+ register: function(name, val) {
1661
+ this.useRegister(name);
1662
+ this.source.push(name + " = " + val + ";");
1663
+ },
1664
+
1665
+ useRegister: function(name) {
1666
+ if(!this.registers[name]) {
1667
+ this.registers[name] = true;
1668
+ this.registers.list.push(name);
1669
+ }
1670
+ },
1671
+
1672
+ pushStackLiteral: function(item) {
1673
+ this.compileStack.push(new Literal(item));
1674
+ return item;
1675
+ },
1676
+
1677
+ pushStack: function(item) {
1678
+ this.source.push(this.incrStack() + " = " + item + ";");
1679
+ this.compileStack.push("stack" + this.stackSlot);
1680
+ return "stack" + this.stackSlot;
1681
+ },
1682
+
1683
+ replaceStack: function(callback) {
1684
+ var item = callback.call(this, this.topStack());
1685
+
1686
+ this.source.push(this.topStack() + " = " + item + ";");
1687
+ return "stack" + this.stackSlot;
1688
+ },
1689
+
1690
+ nextStack: function(skipCompileStack) {
1691
+ var name = this.incrStack();
1692
+ this.compileStack.push("stack" + this.stackSlot);
1693
+ return name;
1694
+ },
1695
+
1696
+ incrStack: function() {
1697
+ this.stackSlot++;
1698
+ if(this.stackSlot > this.stackVars.length) { this.stackVars.push("stack" + this.stackSlot); }
1699
+ return "stack" + this.stackSlot;
1700
+ },
1701
+
1702
+ popStack: function() {
1703
+ var item = this.compileStack.pop();
1704
+
1705
+ if (item instanceof Literal) {
1706
+ return item.value;
1707
+ } else {
1708
+ this.stackSlot--;
1709
+ return item;
1710
+ }
1711
+ },
1712
+
1713
+ topStack: function() {
1714
+ var item = this.compileStack[this.compileStack.length - 1];
1715
+
1716
+ if (item instanceof Literal) {
1717
+ return item.value;
1718
+ } else {
1719
+ return item;
1720
+ }
1721
+ },
1722
+
1723
+ quotedString: function(str) {
1724
+ return '"' + str
1725
+ .replace(/\\/g, '\\\\')
1726
+ .replace(/"/g, '\\"')
1727
+ .replace(/\n/g, '\\n')
1728
+ .replace(/\r/g, '\\r') + '"';
1729
+ },
1730
+
1731
+ setupHelper: function(paramSize, name) {
1732
+ var params = [];
1733
+ this.setupParams(paramSize, params);
1734
+ var foundHelper = this.nameLookup('helpers', name, 'helper');
1735
+
1736
+ return {
1737
+ params: params,
1738
+ name: foundHelper,
1739
+ callParams: ["depth0"].concat(params).join(", "),
1740
+ helperMissingParams: ["depth0", this.quotedString(name)].concat(params).join(", ")
1741
+ };
1742
+ },
1743
+
1744
+ // the params and contexts arguments are passed in arrays
1745
+ // to fill in
1746
+ setupParams: function(paramSize, params) {
1747
+ var options = [], contexts = [], param, inverse, program;
1748
+
1749
+ options.push("hash:" + this.popStack());
1750
+
1751
+ inverse = this.popStack();
1752
+ program = this.popStack();
1753
+
1754
+ // Avoid setting fn and inverse if neither are set. This allows
1755
+ // helpers to do a check for `if (options.fn)`
1756
+ if (program || inverse) {
1757
+ if (!program) {
1758
+ this.context.aliases.self = "this";
1759
+ program = "self.noop";
1760
+ }
1761
+
1762
+ if (!inverse) {
1763
+ this.context.aliases.self = "this";
1764
+ inverse = "self.noop";
1765
+ }
1766
+
1767
+ options.push("inverse:" + inverse);
1768
+ options.push("fn:" + program);
1769
+ }
1770
+
1771
+ for(var i=0; i<paramSize; i++) {
1772
+ param = this.popStack();
1773
+ params.push(param);
1774
+
1775
+ if(this.options.stringParams) {
1776
+ contexts.push(this.popStack());
1777
+ }
1778
+ }
1779
+
1780
+ if (this.options.stringParams) {
1781
+ options.push("contexts:[" + contexts.join(",") + "]");
1782
+ }
1783
+
1784
+ if(this.options.data) {
1785
+ options.push("data:data");
1786
+ }
1787
+
1788
+ params.push("{" + options.join(",") + "}");
1789
+ return params.join(", ");
1790
+ }
1791
+ };
1792
+
1793
+ var reservedWords = (
1794
+ "break else new var" +
1795
+ " case finally return void" +
1796
+ " catch for switch while" +
1797
+ " continue function this with" +
1798
+ " default if throw" +
1799
+ " delete in try" +
1800
+ " do instanceof typeof" +
1801
+ " abstract enum int short" +
1802
+ " boolean export interface static" +
1803
+ " byte extends long super" +
1804
+ " char final native synchronized" +
1805
+ " class float package throws" +
1806
+ " const goto private transient" +
1807
+ " debugger implements protected volatile" +
1808
+ " double import public let yield"
1809
+ ).split(" ");
1810
+
1811
+ var compilerWords = JavaScriptCompiler.RESERVED_WORDS = {};
1812
+
1813
+ for(var i=0, l=reservedWords.length; i<l; i++) {
1814
+ compilerWords[reservedWords[i]] = true;
1815
+ }
1816
+
1817
+ JavaScriptCompiler.isValidJavaScriptVariableName = function(name) {
1818
+ if(!JavaScriptCompiler.RESERVED_WORDS[name] && /^[a-zA-Z_$][0-9a-zA-Z_$]+$/.test(name)) {
1819
+ return true;
1820
+ }
1821
+ return false;
1822
+ };
1823
+
1824
+ })(Handlebars.Compiler, Handlebars.JavaScriptCompiler);
1825
+
1826
+ Handlebars.precompile = function(string, options) {
1827
+ options = options || {};
1828
+
1829
+ var ast = Handlebars.parse(string);
1830
+ var environment = new Handlebars.Compiler().compile(ast, options);
1831
+ return new Handlebars.JavaScriptCompiler().compile(environment, options);
1832
+ };
1833
+
1834
+ Handlebars.compile = function(string, options) {
1835
+ options = options || {};
1836
+
1837
+ var compiled;
1838
+ function compile() {
1839
+ var ast = Handlebars.parse(string);
1840
+ var environment = new Handlebars.Compiler().compile(ast, options);
1841
+ var templateSpec = new Handlebars.JavaScriptCompiler().compile(environment, options, undefined, true);
1842
+ return Handlebars.template(templateSpec);
1843
+ }
1844
+
1845
+ // Template is only compiled on first use and cached after that point.
1846
+ return function(context, options) {
1847
+ if (!compiled) {
1848
+ compiled = compile();
1849
+ }
1850
+ return compiled.call(this, context, options);
1851
+ };
1852
+ };
1853
+ ;
1854
+ // lib/handlebars/runtime.js
1855
+ Handlebars.VM = {
1856
+ template: function(templateSpec) {
1857
+ // Just add water
1858
+ var container = {
1859
+ escapeExpression: Handlebars.Utils.escapeExpression,
1860
+ invokePartial: Handlebars.VM.invokePartial,
1861
+ programs: [],
1862
+ program: function(i, fn, data) {
1863
+ var programWrapper = this.programs[i];
1864
+ if(data) {
1865
+ return Handlebars.VM.program(fn, data);
1866
+ } else if(programWrapper) {
1867
+ return programWrapper;
1868
+ } else {
1869
+ programWrapper = this.programs[i] = Handlebars.VM.program(fn);
1870
+ return programWrapper;
1871
+ }
1872
+ },
1873
+ programWithDepth: Handlebars.VM.programWithDepth,
1874
+ noop: Handlebars.VM.noop
1875
+ };
1876
+
1877
+ return function(context, options) {
1878
+ options = options || {};
1879
+ return templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data);
1880
+ };
1881
+ },
1882
+
1883
+ programWithDepth: function(fn, data, $depth) {
1884
+ var args = Array.prototype.slice.call(arguments, 2);
1885
+
1886
+ return function(context, options) {
1887
+ options = options || {};
1888
+
1889
+ return fn.apply(this, [context, options.data || data].concat(args));
1890
+ };
1891
+ },
1892
+ program: function(fn, data) {
1893
+ return function(context, options) {
1894
+ options = options || {};
1895
+
1896
+ return fn(context, options.data || data);
1897
+ };
1898
+ },
1899
+ noop: function() { return ""; },
1900
+ invokePartial: function(partial, name, context, helpers, partials, data) {
1901
+ var options = { helpers: helpers, partials: partials, data: data };
1902
+
1903
+ if(partial === undefined) {
1904
+ throw new Handlebars.Exception("The partial " + name + " could not be found");
1905
+ } else if(partial instanceof Function) {
1906
+ return partial(context, options);
1907
+ } else if (!Handlebars.compile) {
1908
+ throw new Handlebars.Exception("The partial " + name + " could not be compiled when running in runtime-only mode");
1909
+ } else {
1910
+ partials[name] = Handlebars.compile(partial);
1911
+ return partials[name](context, options);
1912
+ }
1913
+ }
1914
+ };
1915
+
1916
+ Handlebars.template = Handlebars.VM.template;
1917
+ ;