tent-status 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. data/.gitignore +21 -0
  2. data/.kick +8 -0
  3. data/Gemfile +12 -0
  4. data/Gemfile.lock +232 -0
  5. data/LICENSE.txt +22 -0
  6. data/Procfile +1 -0
  7. data/README.md +14 -0
  8. data/Rakefile +43 -0
  9. data/assets/images/.gitkeep +0 -0
  10. data/assets/images/chosen-sprite.png +0 -0
  11. data/assets/images/glyphicons-halflings-white.png +0 -0
  12. data/assets/images/glyphicons-halflings.png +0 -0
  13. data/assets/javascripts/.gitkeep +0 -0
  14. data/assets/javascripts/application.js.coffee +122 -0
  15. data/assets/javascripts/backbone.js +1443 -0
  16. data/assets/javascripts/backbone_sync.js.coffee +60 -0
  17. data/assets/javascripts/boot.js.coffee +3 -0
  18. data/assets/javascripts/chosen.jquery.js +1129 -0
  19. data/assets/javascripts/collections/.gitkeep +0 -0
  20. data/assets/javascripts/collections/followers.js.coffee +5 -0
  21. data/assets/javascripts/collections/followings.js.coffee +5 -0
  22. data/assets/javascripts/collections/groups.js.coffee +5 -0
  23. data/assets/javascripts/collections/posts.js.coffee +5 -0
  24. data/assets/javascripts/fetch_pool.js.coffee +25 -0
  25. data/assets/javascripts/helpers/.gitkeep +0 -0
  26. data/assets/javascripts/helpers/formatting.js.coffee +17 -0
  27. data/assets/javascripts/hogan.js +706 -0
  28. data/assets/javascripts/http.js.coffee +18 -0
  29. data/assets/javascripts/jquery.js +9301 -0
  30. data/assets/javascripts/models/.gitkeep +0 -0
  31. data/assets/javascripts/models/follower.js.coffee +27 -0
  32. data/assets/javascripts/models/following.js.coffee +27 -0
  33. data/assets/javascripts/models/group.js.coffee +4 -0
  34. data/assets/javascripts/models/post.js.coffee +32 -0
  35. data/assets/javascripts/models/profile.js.coffee +30 -0
  36. data/assets/javascripts/moment.js +1106 -0
  37. data/assets/javascripts/paginator.js.coffee +67 -0
  38. data/assets/javascripts/router.js.coffee +71 -0
  39. data/assets/javascripts/routers/.gitkeep +0 -0
  40. data/assets/javascripts/routers/followers.js.coffee +14 -0
  41. data/assets/javascripts/routers/followings.js.coffee +14 -0
  42. data/assets/javascripts/routers/posts.js.coffee +98 -0
  43. data/assets/javascripts/templates/.gitkeep +0 -0
  44. data/assets/javascripts/templates/_follower.js.mustache.slim +17 -0
  45. data/assets/javascripts/templates/_following.js.mustache.slim +17 -0
  46. data/assets/javascripts/templates/_new_post_form.js.mustache.slim +10 -0
  47. data/assets/javascripts/templates/_post.js.mustache.slim +10 -0
  48. data/assets/javascripts/templates/_post_inner.js.mustache.slim +71 -0
  49. data/assets/javascripts/templates/_profile_stats.js.mustache.slim +11 -0
  50. data/assets/javascripts/templates/_reply_form.js.mustache.slim +13 -0
  51. data/assets/javascripts/templates/conversation.js.mustache.slim +13 -0
  52. data/assets/javascripts/templates/followers.js.mustache.slim +19 -0
  53. data/assets/javascripts/templates/followings.js.mustache.slim +22 -0
  54. data/assets/javascripts/templates/posts.js.mustache.slim +25 -0
  55. data/assets/javascripts/templates/profile.js.mustache.slim +39 -0
  56. data/assets/javascripts/underscore.js +1059 -0
  57. data/assets/javascripts/view.js.coffee +140 -0
  58. data/assets/javascripts/views/.gitkeep +0 -0
  59. data/assets/javascripts/views/container.js.coffee +6 -0
  60. data/assets/javascripts/views/expanding_textarea.js.coffee +14 -0
  61. data/assets/javascripts/views/fetch_posts_pool.js.coffee +61 -0
  62. data/assets/javascripts/views/follower_groups_form.js.coffee +26 -0
  63. data/assets/javascripts/views/followers.js.coffee +28 -0
  64. data/assets/javascripts/views/following_groups_form.js.coffee +25 -0
  65. data/assets/javascripts/views/followings.js.coffee +27 -0
  66. data/assets/javascripts/views/new_following_form.js.coffee +15 -0
  67. data/assets/javascripts/views/new_post_form.js.coffee +178 -0
  68. data/assets/javascripts/views/post.js.coffee +139 -0
  69. data/assets/javascripts/views/posts.js.coffee +55 -0
  70. data/assets/javascripts/views/posts/conversation.js.coffee +18 -0
  71. data/assets/javascripts/views/profile.js.coffee +29 -0
  72. data/assets/javascripts/views/profile_follow_button.js.coffee +29 -0
  73. data/assets/javascripts/views/profile_stats.js.coffee +38 -0
  74. data/assets/javascripts/views/remove_follower_btn.js.coffee +18 -0
  75. data/assets/javascripts/views/reply_post_form.js.coffee +30 -0
  76. data/assets/javascripts/views/unfollow_btn.js.coffee +18 -0
  77. data/assets/stylesheets/.gitkeep +0 -0
  78. data/assets/stylesheets/application.css.sass +117 -0
  79. data/assets/stylesheets/bootstrap-responsive.css +1040 -0
  80. data/assets/stylesheets/bootstrap.css.erb +5624 -0
  81. data/assets/stylesheets/chosen.css.erb +397 -0
  82. data/config.ru +14 -0
  83. data/config/asset_sync.rb +12 -0
  84. data/config/evergreen.rb +16 -0
  85. data/lib/tent-status.rb +6 -0
  86. data/lib/tent-status/app.rb +263 -0
  87. data/lib/tent-status/models/user.rb +39 -0
  88. data/lib/tent-status/sprockets/environment.rb +25 -0
  89. data/lib/tent-status/sprockets/helpers.rb +5 -0
  90. data/lib/tent-status/views/application.slim +69 -0
  91. data/lib/tent-status/views/auth.slim +8 -0
  92. data/tent-status.gemspec +34 -0
  93. metadata +415 -0
@@ -0,0 +1,5 @@
1
+ class TentStatus.Collections.Followers extends Backbone.Collection
2
+ model: TentStatus.Models.Follower
3
+ url: "#{TentStatus.api_root}/followers"
4
+
5
+ TentStatus.Collections.followers = new TentStatus.Collections.Followers
@@ -0,0 +1,5 @@
1
+ class TentStatus.Collections.Followings extends Backbone.Collection
2
+ model: TentStatus.Models.Following
3
+ url: "#{TentStatus.api_root}/followings"
4
+
5
+ TentStatus.Collections.followings = new TentStatus.Collections.Followings
@@ -0,0 +1,5 @@
1
+ class TentStatus.Collections.Groups extends Backbone.Collection
2
+ model: TentStatus.Models.Group
3
+ url: "#{TentStatus.api_root}/groups"
4
+
5
+ TentStatus.Collections.groups = new TentStatus.Collections.Groups
@@ -0,0 +1,5 @@
1
+ class TentStatus.Collections.Posts extends Backbone.Collection
2
+ model: TentStatus.Models.Post
3
+ url: "#{TentStatus.api_root}/posts"
4
+
5
+ TentStatus.Collections.posts = new TentStatus.Collections.Posts
@@ -0,0 +1,25 @@
1
+ class TentStatus.FetchPool extends TentStatus.Paginator
2
+ constructor: (@collection, @options = {}) ->
3
+ super
4
+ @sinceId = @options.sinceId
5
+
6
+ paramsForOffsetAndLimit: (sinceId, limit) =>
7
+ params = { limit: limit }
8
+ params.since_id = sinceId if sinceId
9
+ params
10
+
11
+ fetch: (options = {}) =>
12
+ _options = {
13
+ success: (items) =>
14
+ newItems = []
15
+ existingItems = TentStatus.Collections.posts.toArray()
16
+ for i in items
17
+ newItems.push i unless existingItems.indexOf(i)
18
+
19
+ if newItems.length
20
+ @sinceId = items.last()?.get('id') || @sinceId
21
+ @options.success?()
22
+ @trigger 'fetch:success'
23
+ }
24
+ options = _.extend _options, options
25
+ super(options)
File without changes
@@ -0,0 +1,17 @@
1
+ _.extend TentStatus.Helpers,
2
+ formatTime: (time_or_int) ->
3
+ now = moment()
4
+ time = moment.unix(time_or_int)
5
+
6
+ formatted_time = if time.format('YYYY-MM-DD') == now.format('YYYY-MM-DD')
7
+ time.format('HH:mm') # time only
8
+ else
9
+ time.format('DD/MM/YY') # date and time
10
+
11
+ "#{formatted_time}"
12
+
13
+ rawTime: (time_or_int) ->
14
+ moment.unix(time_or_int).format()
15
+
16
+ formatUrl: (url='') ->
17
+ url.replace(/^\w+:\/\/([^\/]+).*?$/, '$1')
@@ -0,0 +1,706 @@
1
+ /*
2
+ * Copyright 2011 Twitter, Inc.
3
+ * Licensed under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License.
5
+ * You may obtain a copy of the License at
6
+ *
7
+ * http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software
10
+ * distributed under the License is distributed on an "AS IS" BASIS,
11
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ * See the License for the specific language governing permissions and
13
+ * limitations under the License.
14
+ */
15
+
16
+
17
+
18
+ var Hogan = {};
19
+
20
+ (function (Hogan, useArrayBuffer) {
21
+ Hogan.Template = function (codeObj, text, compiler, options) {
22
+ codeObj = codeObj || {};
23
+ this.r = codeObj.code || this.r;
24
+ this.c = compiler;
25
+ this.options = options;
26
+ this.text = text || '';
27
+ this.partials = codeObj.partials || {};
28
+ this.subs = codeObj.subs || {};
29
+ this.ib();
30
+ }
31
+
32
+ Hogan.Template.prototype = {
33
+ // render: replaced by generated code.
34
+ r: function (context, partials, indent) { return ''; },
35
+
36
+ // variable escaping
37
+ v: hoganEscape,
38
+
39
+ // triple stache
40
+ t: coerceToString,
41
+
42
+ render: function render(context, partials, indent) {
43
+ return this.ri([context], partials || {}, indent);
44
+ },
45
+
46
+ // render internal -- a hook for overrides that catches partials too
47
+ ri: function (context, partials, indent) {
48
+ return this.r(context, partials, indent);
49
+ },
50
+
51
+ // ensurePartial
52
+ ep: function(symbol, partials) {
53
+ var partial = this.partials[symbol];
54
+
55
+ // check to see that if we've instantiated this partial before
56
+ var template = partials[partial.name];
57
+ if (partial.instance && partial.base == template) {
58
+ return partial.instance;
59
+ }
60
+
61
+ if (typeof template == 'string') {
62
+ if (!this.c) {
63
+ throw new Error("No compiler available.");
64
+ }
65
+ template = this.c.compile(template, this.options);
66
+ }
67
+
68
+ if (!template) {
69
+ return null;
70
+ }
71
+
72
+ // We use this to check whether the partials dictionary has changed
73
+ this.partials[symbol].base = template;
74
+
75
+ if (partial.subs) {
76
+ template = createSpecializedPartial(template, partial.subs, partial.partials);
77
+ }
78
+
79
+ this.partials[symbol].instance = template;
80
+ return template;
81
+ },
82
+
83
+ // tries to find a partial in the curent scope and render it
84
+ rp: function(symbol, context, partials, indent) {
85
+ var partial = this.ep(symbol, partials);
86
+ if (!partial) {
87
+ return '';
88
+ }
89
+
90
+ return partial.ri(context, partials, indent);
91
+ },
92
+
93
+ // render a section
94
+ rs: function(context, partials, section) {
95
+ var tail = context[context.length - 1];
96
+
97
+ if (!isArray(tail)) {
98
+ section(context, partials, this);
99
+ return;
100
+ }
101
+
102
+ for (var i = 0; i < tail.length; i++) {
103
+ context.push(tail[i]);
104
+ section(context, partials, this);
105
+ context.pop();
106
+ }
107
+ },
108
+
109
+ // maybe start a section
110
+ s: function(val, ctx, partials, inverted, start, end, tags) {
111
+ var pass;
112
+
113
+ if (isArray(val) && val.length === 0) {
114
+ return false;
115
+ }
116
+
117
+ if (typeof val == 'function') {
118
+ val = this.ms(val, ctx, partials, inverted, start, end, tags);
119
+ }
120
+
121
+ pass = (val === '') || !!val;
122
+
123
+ if (!inverted && pass && ctx) {
124
+ ctx.push((typeof val == 'object') ? val : ctx[ctx.length - 1]);
125
+ }
126
+
127
+ return pass;
128
+ },
129
+
130
+ // find values with dotted names
131
+ d: function(key, ctx, partials, returnFound) {
132
+ var names = key.split('.'),
133
+ val = this.f(names[0], ctx, partials, returnFound),
134
+ cx = null;
135
+
136
+ if (key === '.' && isArray(ctx[ctx.length - 2])) {
137
+ return ctx[ctx.length - 1];
138
+ }
139
+
140
+ for (var i = 1; i < names.length; i++) {
141
+ if (val && typeof val == 'object' && val[names[i]] != null) {
142
+ cx = val;
143
+ val = val[names[i]];
144
+ } else {
145
+ val = '';
146
+ }
147
+ }
148
+
149
+ if (returnFound && !val) {
150
+ return false;
151
+ }
152
+
153
+ if (!returnFound && typeof val == 'function') {
154
+ ctx.push(cx);
155
+ val = this.mv(val, ctx, partials);
156
+ ctx.pop();
157
+ }
158
+
159
+ return val;
160
+ },
161
+
162
+ // find values with normal names
163
+ f: function(key, ctx, partials, returnFound) {
164
+ var val = false,
165
+ v = null,
166
+ found = false;
167
+
168
+ for (var i = ctx.length - 1; i >= 0; i--) {
169
+ v = ctx[i];
170
+ if (v && typeof v == 'object' && v[key] != null) {
171
+ val = v[key];
172
+ found = true;
173
+ break;
174
+ }
175
+ }
176
+
177
+ if (!found) {
178
+ return (returnFound) ? false : "";
179
+ }
180
+
181
+ if (!returnFound && typeof val == 'function') {
182
+ val = this.mv(val, ctx, partials);
183
+ }
184
+
185
+ return val;
186
+ },
187
+
188
+ // higher order templates
189
+ ls: function(func, cx, partials, text, tags) {
190
+ var oldTags = this.options.delimiters;
191
+
192
+ this.options.delimiters = tags;
193
+ this.b(this.ct(coerceToString(func.call(cx, text)), cx, partials));
194
+ this.options.delimiters = oldTags;
195
+
196
+ return false;
197
+ },
198
+
199
+ // compile text
200
+ ct: function(text, cx, partials) {
201
+ if (this.options.disableLambda) {
202
+ throw new Error('Lambda features disabled.');
203
+ }
204
+ return this.c.compile(text, this.options).render(cx, partials);
205
+ },
206
+
207
+ // template result buffering
208
+ b: (useArrayBuffer) ? function(s) { this.buf.push(s); } :
209
+ function(s) { this.buf += s; },
210
+
211
+ fl: (useArrayBuffer) ? function() { var r = this.buf.join(''); this.buf = []; return r; } :
212
+ function() { var r = this.buf; this.buf = ''; return r; },
213
+ // init the buffer
214
+ ib: function () {
215
+ this.buf = (useArrayBuffer) ? [] : '';
216
+ },
217
+
218
+ // method replace section
219
+ ms: function(func, ctx, partials, inverted, start, end, tags) {
220
+ var cx = ctx[ctx.length - 1],
221
+ result = func.call(cx);
222
+
223
+ if (typeof result == 'function') {
224
+ if (inverted) {
225
+ return true;
226
+ } else {
227
+ return this.ls(result, cx, partials, this.text.substring(start, end), tags);
228
+ }
229
+ }
230
+
231
+ return result;
232
+ },
233
+
234
+ // method replace variable
235
+ mv: function(func, ctx, partials) {
236
+ var cx = ctx[ctx.length - 1];
237
+ var result = func.call(cx);
238
+
239
+ if (typeof result == 'function') {
240
+ return this.ct(coerceToString(result.call(cx)), cx, partials);
241
+ }
242
+
243
+ return result;
244
+ },
245
+
246
+ sub: function(name, context, partials, indent) {
247
+ var f = this.subs[name];
248
+ if (f) {
249
+ f(context, partials, this, indent);
250
+ }
251
+ }
252
+
253
+ };
254
+
255
+ function createSpecializedPartial(instance, subs, partials) {
256
+ function PartialTemplate() {};
257
+ PartialTemplate.prototype = instance;
258
+ function Substitutions() {};
259
+ Substitutions.prototype = instance.subs;
260
+ var key;
261
+ var partial = new PartialTemplate();
262
+ partial.subs = new Substitutions();
263
+ partial.ib();
264
+
265
+ for (key in subs) {
266
+ partial.subs[key] = subs[key];
267
+ }
268
+
269
+ for (key in partials) {
270
+ partial.partials[key] = partials[key];
271
+ }
272
+
273
+ return partial;
274
+ }
275
+
276
+ var rAmp = /&/g,
277
+ rLt = /</g,
278
+ rGt = />/g,
279
+ rApos =/\'/g,
280
+ rQuot = /\"/g,
281
+ hChars =/[&<>\"\']/;
282
+
283
+ function coerceToString(val) {
284
+ return String((val === null || val === undefined) ? '' : val);
285
+ }
286
+
287
+ function hoganEscape(str) {
288
+ str = coerceToString(str);
289
+ return hChars.test(str) ?
290
+ str
291
+ .replace(rAmp,'&amp;')
292
+ .replace(rLt,'&lt;')
293
+ .replace(rGt,'&gt;')
294
+ .replace(rApos,'&#39;')
295
+ .replace(rQuot, '&quot;') :
296
+ str;
297
+ }
298
+
299
+ var isArray = Array.isArray || function(a) {
300
+ return Object.prototype.toString.call(a) === '[object Array]';
301
+ };
302
+
303
+ })(typeof exports !== 'undefined' ? exports : Hogan);
304
+
305
+
306
+
307
+ (function (Hogan) {
308
+ // Setup regex assignments
309
+ // remove whitespace according to Mustache spec
310
+ var rIsWhitespace = /\S/,
311
+ rQuot = /\"/g,
312
+ rNewline = /\n/g,
313
+ rCr = /\r/g,
314
+ rSlash = /\\/g;
315
+
316
+ Hogan.tags = {
317
+ '#': 1, '^': 2, '<': 3, '$': 4,
318
+ '/': 5, '!': 6, '>': 7, '=': 8, '_v': 9,
319
+ '{': 10, '&': 11, '_t': 12
320
+ };
321
+
322
+ Hogan.scan = function scan(text, delimiters) {
323
+ var len = text.length,
324
+ IN_TEXT = 0,
325
+ IN_TAG_TYPE = 1,
326
+ IN_TAG = 2,
327
+ state = IN_TEXT,
328
+ tagType = null,
329
+ tag = null,
330
+ buf = '',
331
+ tokens = [],
332
+ seenTag = false,
333
+ i = 0,
334
+ lineStart = 0,
335
+ otag = '{{',
336
+ ctag = '}}';
337
+
338
+ function addBuf() {
339
+ if (buf.length > 0) {
340
+ tokens.push({tag: '_t', text: new String(buf)});
341
+ buf = '';
342
+ }
343
+ }
344
+
345
+ function lineIsWhitespace() {
346
+ var isAllWhitespace = true;
347
+ for (var j = lineStart; j < tokens.length; j++) {
348
+ isAllWhitespace =
349
+ (Hogan.tags[tokens[j].tag] < Hogan.tags['_v']) ||
350
+ (tokens[j].tag == '_t' && tokens[j].text.match(rIsWhitespace) === null);
351
+ if (!isAllWhitespace) {
352
+ return false;
353
+ }
354
+ }
355
+
356
+ return isAllWhitespace;
357
+ }
358
+
359
+ function filterLine(haveSeenTag, noNewLine) {
360
+ addBuf();
361
+
362
+ if (haveSeenTag && lineIsWhitespace()) {
363
+ for (var j = lineStart, next; j < tokens.length; j++) {
364
+ if (tokens[j].text) {
365
+ if ((next = tokens[j+1]) && next.tag == '>') {
366
+ // set indent to token value
367
+ next.indent = tokens[j].text.toString()
368
+ }
369
+ tokens.splice(j, 1);
370
+ }
371
+ }
372
+ } else if (!noNewLine) {
373
+ tokens.push({tag:'\n'});
374
+ }
375
+
376
+ seenTag = false;
377
+ lineStart = tokens.length;
378
+ }
379
+
380
+ function changeDelimiters(text, index) {
381
+ var close = '=' + ctag,
382
+ closeIndex = text.indexOf(close, index),
383
+ delimiters = trim(
384
+ text.substring(text.indexOf('=', index) + 1, closeIndex)
385
+ ).split(' ');
386
+
387
+ otag = delimiters[0];
388
+ ctag = delimiters[1];
389
+
390
+ return closeIndex + close.length - 1;
391
+ }
392
+
393
+ if (delimiters) {
394
+ delimiters = delimiters.split(' ');
395
+ otag = delimiters[0];
396
+ ctag = delimiters[1];
397
+ }
398
+
399
+ for (i = 0; i < len; i++) {
400
+ if (state == IN_TEXT) {
401
+ if (tagChange(otag, text, i)) {
402
+ --i;
403
+ addBuf();
404
+ state = IN_TAG_TYPE;
405
+ } else {
406
+ if (text.charAt(i) == '\n') {
407
+ filterLine(seenTag);
408
+ } else {
409
+ buf += text.charAt(i);
410
+ }
411
+ }
412
+ } else if (state == IN_TAG_TYPE) {
413
+ i += otag.length - 1;
414
+ tag = Hogan.tags[text.charAt(i + 1)];
415
+ tagType = tag ? text.charAt(i + 1) : '_v';
416
+ if (tagType == '=') {
417
+ i = changeDelimiters(text, i);
418
+ state = IN_TEXT;
419
+ } else {
420
+ if (tag) {
421
+ i++;
422
+ }
423
+ state = IN_TAG;
424
+ }
425
+ seenTag = i;
426
+ } else {
427
+ if (tagChange(ctag, text, i)) {
428
+ tokens.push({tag: tagType, n: trim(buf), otag: otag, ctag: ctag,
429
+ i: (tagType == '/') ? seenTag - otag.length : i + ctag.length});
430
+ buf = '';
431
+ i += ctag.length - 1;
432
+ state = IN_TEXT;
433
+ if (tagType == '{') {
434
+ if (ctag == '}}') {
435
+ i++;
436
+ } else {
437
+ cleanTripleStache(tokens[tokens.length - 1]);
438
+ }
439
+ }
440
+ } else {
441
+ buf += text.charAt(i);
442
+ }
443
+ }
444
+ }
445
+
446
+ filterLine(seenTag, true);
447
+
448
+ return tokens;
449
+ }
450
+
451
+ function cleanTripleStache(token) {
452
+ if (token.n.substr(token.n.length - 1) === '}') {
453
+ token.n = token.n.substring(0, token.n.length - 1);
454
+ }
455
+ }
456
+
457
+ function trim(s) {
458
+ if (s.trim) {
459
+ return s.trim();
460
+ }
461
+
462
+ return s.replace(/^\s*|\s*$/g, '');
463
+ }
464
+
465
+ function tagChange(tag, text, index) {
466
+ if (text.charAt(index) != tag.charAt(0)) {
467
+ return false;
468
+ }
469
+
470
+ for (var i = 1, l = tag.length; i < l; i++) {
471
+ if (text.charAt(index + i) != tag.charAt(i)) {
472
+ return false;
473
+ }
474
+ }
475
+
476
+ return true;
477
+ }
478
+
479
+ // the tags allowed inside super templates
480
+ var allowedInSuper = {'_t': true, '\n': true, '$': true, '/': true};
481
+
482
+ function buildTree(tokens, kind, stack, customTags) {
483
+ var instructions = [],
484
+ opener = null,
485
+ tail = null,
486
+ token = null;
487
+
488
+ tail = stack[stack.length - 1];
489
+
490
+ while (tokens.length > 0) {
491
+ token = tokens.shift();
492
+
493
+ if (tail && tail.tag == '<' && !(token.tag in allowedInSuper)) {
494
+ throw new Error('Illegal content in < super tag.');
495
+ }
496
+
497
+ if (Hogan.tags[token.tag] <= Hogan.tags['$'] || isOpener(token, customTags)) {
498
+ stack.push(token);
499
+ token.nodes = buildTree(tokens, token.tag, stack, customTags);
500
+ } else if (token.tag == '/') {
501
+ if (stack.length === 0) {
502
+ throw new Error('Closing tag without opener: /' + token.n);
503
+ }
504
+ opener = stack.pop();
505
+ if (token.n != opener.n && !isCloser(token.n, opener.n, customTags)) {
506
+ throw new Error('Nesting error: ' + opener.n + ' vs. ' + token.n);
507
+ }
508
+ opener.end = token.i;
509
+ return instructions;
510
+ } else if (token.tag == '\n') {
511
+ token.last = (tokens.length == 0) || (tokens[0].tag == '\n');
512
+ }
513
+
514
+ instructions.push(token);
515
+ }
516
+
517
+ if (stack.length > 0) {
518
+ throw new Error('missing closing tag: ' + stack.pop().n);
519
+ }
520
+
521
+ return instructions;
522
+ }
523
+
524
+ function isOpener(token, tags) {
525
+ for (var i = 0, l = tags.length; i < l; i++) {
526
+ if (tags[i].o == token.n) {
527
+ token.tag = '#';
528
+ return true;
529
+ }
530
+ }
531
+ }
532
+
533
+ function isCloser(close, open, tags) {
534
+ for (var i = 0, l = tags.length; i < l; i++) {
535
+ if (tags[i].c == close && tags[i].o == open) {
536
+ return true;
537
+ }
538
+ }
539
+ }
540
+
541
+ function stringifySubstitutions(obj) {
542
+ var items = [];
543
+ for (var key in obj) {
544
+ items.push('"' + esc(key) + '": function(c,p,t,i) {' + obj[key] + '}');
545
+ }
546
+ return "{ " + items.join(",") + " }";
547
+ }
548
+
549
+ function stringifyPartials(codeObj) {
550
+ var partials = [];
551
+ for (var key in codeObj.partials) {
552
+ partials.push('"' + esc(key) + '":{name:"' + esc(codeObj.partials[key].name) + '", ' + stringifyPartials(codeObj.partials[key]) + "}");
553
+ }
554
+ return "partials: {" + partials.join(",") + "}, subs: " + stringifySubstitutions(codeObj.subs);
555
+ }
556
+
557
+ Hogan.stringify = function(codeObj, text, options) {
558
+ return "{code: function (c,p,i) { " + Hogan.wrapMain(codeObj.code) + " }," + stringifyPartials(codeObj) + "}";
559
+ }
560
+
561
+ var serialNo = 0;
562
+ Hogan.generate = function(tree, text, options) {
563
+ serialNo = 0;
564
+ var context = { code: '', subs: {}, partials: {} };
565
+ Hogan.walk(tree, context);
566
+
567
+ if (options.asString) {
568
+ return this.stringify(context, text, options);
569
+ }
570
+
571
+ return this.makeTemplate(context, text, options);
572
+ }
573
+
574
+ Hogan.wrapMain = function(code) {
575
+ return 'var t=this;t.b(i=i||"");' + code + 'return t.fl();';
576
+ }
577
+
578
+ Hogan.template = Hogan.Template;
579
+
580
+ Hogan.makeTemplate = function(codeObj, text, options) {
581
+ var template = this.makePartials(codeObj);
582
+ template.code = new Function('c', 'p', 'i', this.wrapMain(codeObj.code));
583
+ return new this.template(template, text, this, options);
584
+ }
585
+
586
+ Hogan.makePartials = function(codeObj) {
587
+ var key, template = {subs: {}, partials: codeObj.partials, name: codeObj.name};
588
+ for (key in template.partials) {
589
+ template.partials[key] = this.makePartials(template.partials[key]);
590
+ }
591
+ for (key in codeObj.subs) {
592
+ template.subs[key] = new Function('c', 'p', 't', 'i', codeObj.subs[key]);
593
+ }
594
+ return template;
595
+ }
596
+
597
+ function esc(s) {
598
+ return s.replace(rSlash, '\\\\')
599
+ .replace(rQuot, '\\\"')
600
+ .replace(rNewline, '\\n')
601
+ .replace(rCr, '\\r');
602
+ }
603
+
604
+ function chooseMethod(s) {
605
+ return (~s.indexOf('.')) ? 'd' : 'f';
606
+ }
607
+
608
+ function createPartial(node, context) {
609
+ var prefix = "<" + (context.prefix || "");
610
+ var sym = prefix + node.n + serialNo++;
611
+ context.partials[sym] = {name: node.n, partials: {}};
612
+ context.code += 't.b(t.rp("' + esc(sym) + '",c,p,"' + (node.indent || '') + '"));';
613
+ return sym;
614
+ }
615
+
616
+ Hogan.codegen = {
617
+ '#': function(node, context) {
618
+ context.code += 'if(t.s(t.' + chooseMethod(node.n) + '("' + esc(node.n) + '",c,p,1),' +
619
+ 'c,p,0,' + node.i + ',' + node.end + ',"' + node.otag + " " + node.ctag + '")){' +
620
+ 't.rs(c,p,' + 'function(c,p,t){';
621
+ Hogan.walk(node.nodes, context);
622
+ context.code += '});c.pop();}';
623
+ },
624
+
625
+ '^': function(node, context) {
626
+ context.code += 'if(!t.s(t.' + chooseMethod(node.n) + '("' + esc(node.n) + '",c,p,1),c,p,1,0,0,"")){';
627
+ Hogan.walk(node.nodes, context);
628
+ context.code += '};';
629
+ },
630
+
631
+ '>': createPartial,
632
+ '<': function(node, context) {
633
+ var ctx = {partials: {}, code: '', subs: {}, inPartial: true};
634
+ Hogan.walk(node.nodes, ctx);
635
+ var template = context.partials[createPartial(node, context)];
636
+ template.subs = ctx.subs;
637
+ template.partials = ctx.partials;
638
+ },
639
+
640
+ '$': function(node, context) {
641
+ var ctx = {subs: {}, code: '', partials: context.partials, prefix: node.n};
642
+ Hogan.walk(node.nodes, ctx);
643
+ context.subs[node.n] = ctx.code;
644
+ if (!context.inPartial) {
645
+ context.code += 't.sub("' + esc(node.n) + '",c,p,i);';
646
+ }
647
+ },
648
+
649
+ '\n': function(node, context) {
650
+ context.code += write('"\\n"' + (node.last ? '' : ' + i'));
651
+ },
652
+
653
+ '_v': function(node, context) {
654
+ context.code += 't.b(t.v(t.' + chooseMethod(node.n) + '("' + esc(node.n) + '",c,p,0)));';
655
+ },
656
+
657
+ '_t': function(node, context) {
658
+ context.code += write('"' + esc(node.text) + '"');
659
+ },
660
+
661
+ '{': tripleStache,
662
+
663
+ '&': tripleStache
664
+ }
665
+
666
+ function tripleStache(node, context) {
667
+ context.code += 't.b(t.t(t.' + chooseMethod(node.n) + '("' + esc(node.n) + '",c,p,0)));';
668
+ }
669
+
670
+ function write(s) {
671
+ return 't.b(' + s + ');';
672
+ }
673
+
674
+ Hogan.walk = function (nodelist, context) {
675
+ var func;
676
+ for (var i = 0, l = nodelist.length; i < l; i++) {
677
+ func = Hogan.codegen[nodelist[i].tag];
678
+ func && func(nodelist[i], context);
679
+ }
680
+ return context;
681
+ }
682
+
683
+ Hogan.parse = function(tokens, text, options) {
684
+ options = options || {};
685
+ return buildTree(tokens, '', [], options.sectionTags || []);
686
+ },
687
+
688
+ Hogan.cache = {};
689
+
690
+ Hogan.cacheKey = function(text, options) {
691
+ return [text, !!options.asString, !!options.disableLambda].join('||');
692
+ },
693
+
694
+ Hogan.compile = function(text, options) {
695
+ options = options || {};
696
+ var key = Hogan.cacheKey(text, options);
697
+ var template = this.cache[key];
698
+
699
+ if (template) {
700
+ return template;
701
+ }
702
+
703
+ template = this.generate(this.parse(this.scan(text, options.delimiters), text, options), text, options);
704
+ return this.cache[key] = template;
705
+ };
706
+ })(typeof exports !== 'undefined' ? exports : Hogan);