hogan_assets 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/README.md +5 -1
- data/lib/hogan_assets/tilt.rb +2 -7
- data/lib/hogan_assets/version.rb +1 -1
- data/test/hogan_assets/tilt_test.rb +4 -8
- data/vendor/assets/javascripts/hogan.js +44 -39
- metadata +8 -8
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
|
5
5
|
**hogan.js** is a templating engine developed at [Twitter](http://twitter.com) that follows the **mustache** spec and compiles the templates to JavaScript. The first bit is *cool*, since **mustache** is *cool*. The second bit is **awesome and full of win** because we can now compile our **mustache** templates on the server using the asset pipeline/sprockets.
|
6
6
|
|
7
|
-
This gem contains **hogan.js v1.0.
|
7
|
+
This gem contains **hogan.js v1.0.5**
|
8
8
|
|
9
9
|
## Installation
|
10
10
|
|
@@ -55,6 +55,10 @@ Templates are compiled to a global JavaScript object named `HoganTemplates`. To
|
|
55
55
|
|
56
56
|
I made this because I <3 **mustache** and want to use it in Rails. Follow me on [Github](https://github.com/leshill) and [Twitter](https://twitter.com/leshill).
|
57
57
|
|
58
|
+
# Contributors
|
59
|
+
|
60
|
+
* Matthew Nelson (@mdavidn) : Remove unnecessary template source
|
61
|
+
|
58
62
|
## Contributing
|
59
63
|
|
60
64
|
1. Fork it
|
data/lib/hogan_assets/tilt.rb
CHANGED
@@ -6,15 +6,10 @@ module HoganAssets
|
|
6
6
|
|
7
7
|
def evaluate(scope, locals, &block)
|
8
8
|
compiled_template = Hogan.compile(data)
|
9
|
-
code = data.inspect
|
10
9
|
template_name = scope.logical_path.inspect
|
11
10
|
<<-TEMPLATE
|
12
|
-
(
|
13
|
-
|
14
|
-
this.HoganTemplates[#{template_name}] = new Hogan.Template(#{code});
|
15
|
-
this.HoganTemplates[#{template_name}].r = #{compiled_template};
|
16
|
-
return HoganTemplates[#{template_name}];
|
17
|
-
}).call(this);
|
11
|
+
this.HoganTemplates || (this.HoganTemplates = {});
|
12
|
+
this.HoganTemplates[#{template_name}] = new Hogan.Template(#{compiled_template});
|
18
13
|
TEMPLATE
|
19
14
|
end
|
20
15
|
|
data/lib/hogan_assets/version.rb
CHANGED
@@ -12,14 +12,10 @@ module HoganAssets
|
|
12
12
|
end.new
|
13
13
|
|
14
14
|
template = HoganAssets::Tilt.new('/myapp/app/assets/templates/path/to/template.mustache') { "This is {{mustache}}" }
|
15
|
-
assert_equal
|
16
|
-
(
|
17
|
-
|
18
|
-
|
19
|
-
this.HoganTemplates["path/to/template"].r = function(c,p,i){i = i || "";var b = i + "";var _ = this;b += "This is ";b += (_.v(_.f("mustache",c,p,0)));return b;;};
|
20
|
-
return HoganTemplates["path/to/template"];
|
21
|
-
}).call(this);
|
22
|
-
END_EXPECTED
|
15
|
+
assert_equal <<-END_EXPECTED, template.render(scope, {})
|
16
|
+
this.HoganTemplates || (this.HoganTemplates = {});
|
17
|
+
this.HoganTemplates[\"path/to/template\"] = new Hogan.Template(function(c,p,i){var _=this;_.b(i=i||\"\");_.b(\"This is \");_.b(_.v(_.f(\"mustache\",c,p,0)));return _.fl();;});
|
18
|
+
END_EXPECTED
|
23
19
|
end
|
24
20
|
end
|
25
21
|
end
|
@@ -17,13 +17,13 @@
|
|
17
17
|
|
18
18
|
var Hogan = {};
|
19
19
|
|
20
|
-
(function (Hogan) {
|
21
|
-
Hogan.Template = function
|
22
|
-
|
23
|
-
this.r = renderFunc;
|
24
|
-
}
|
20
|
+
(function (Hogan, useArrayBuffer) {
|
21
|
+
Hogan.Template = function (renderFunc, text, compiler, options) {
|
22
|
+
this.r = renderFunc || this.r;
|
25
23
|
this.c = compiler;
|
24
|
+
this.options = options;
|
26
25
|
this.text = text || '';
|
26
|
+
this.buf = (useArrayBuffer) ? [] : '';
|
27
27
|
}
|
28
28
|
|
29
29
|
Hogan.Template.prototype = {
|
@@ -33,6 +33,9 @@ var Hogan = {};
|
|
33
33
|
// variable escaping
|
34
34
|
v: hoganEscape,
|
35
35
|
|
36
|
+
// triple stache
|
37
|
+
t: coerceToString,
|
38
|
+
|
36
39
|
render: function render(context, partials, indent) {
|
37
40
|
return this.ri([context], partials || {}, indent);
|
38
41
|
},
|
@@ -51,7 +54,7 @@ var Hogan = {};
|
|
51
54
|
}
|
52
55
|
|
53
56
|
if (this.c && typeof partial == 'string') {
|
54
|
-
partial = this.c.compile(partial);
|
57
|
+
partial = this.c.compile(partial, this.options);
|
55
58
|
}
|
56
59
|
|
57
60
|
return partial.ri(context, partials, indent);
|
@@ -59,20 +62,18 @@ var Hogan = {};
|
|
59
62
|
|
60
63
|
// render a section
|
61
64
|
rs: function(context, partials, section) {
|
62
|
-
var
|
63
|
-
tail = context[context.length - 1];
|
65
|
+
var tail = context[context.length - 1];
|
64
66
|
|
65
67
|
if (!isArray(tail)) {
|
66
|
-
|
68
|
+
section(context, partials, this);
|
69
|
+
return;
|
67
70
|
}
|
68
71
|
|
69
72
|
for (var i = 0; i < tail.length; i++) {
|
70
73
|
context.push(tail[i]);
|
71
|
-
|
74
|
+
section(context, partials, this);
|
72
75
|
context.pop();
|
73
76
|
}
|
74
|
-
|
75
|
-
return buf;
|
76
77
|
},
|
77
78
|
|
78
79
|
// maybe start a section
|
@@ -157,16 +158,20 @@ var Hogan = {};
|
|
157
158
|
// higher order templates
|
158
159
|
ho: function(val, cx, partials, text, tags) {
|
159
160
|
var compiler = this.c;
|
161
|
+
var options = this.options;
|
162
|
+
options.delimiters = tags;
|
160
163
|
var t = val.call(cx, text, function(t) {
|
161
|
-
return compiler.compile(t,
|
164
|
+
return compiler.compile(t, options).render(cx, partials);
|
162
165
|
});
|
163
|
-
|
164
|
-
this.b = s;
|
166
|
+
this.b(compiler.compile(t.toString(), options).render(cx, partials));
|
165
167
|
return false;
|
166
168
|
},
|
167
169
|
|
168
|
-
//
|
169
|
-
b:
|
170
|
+
// template result buffering
|
171
|
+
b: (useArrayBuffer) ? function(s) { this.buf.push(s); } :
|
172
|
+
function(s) { this.buf += s; },
|
173
|
+
fl: (useArrayBuffer) ? function() { var r = this.buf.join(''); this.buf = []; return r; } :
|
174
|
+
function() { var r = this.buf; this.buf = ''; return r; },
|
170
175
|
|
171
176
|
// lambda replace section
|
172
177
|
ls: function(val, ctx, partials, inverted, start, end, tags) {
|
@@ -197,10 +202,10 @@ var Hogan = {};
|
|
197
202
|
if (typeof result == 'function') {
|
198
203
|
result = result.call(cx);
|
199
204
|
}
|
200
|
-
result = result
|
205
|
+
result = coerceToString(result);
|
201
206
|
|
202
|
-
if (this.c && ~result.indexOf("{
|
203
|
-
return this.c.compile(result).render(cx, partials);
|
207
|
+
if (this.c && ~result.indexOf("{\u007B")) {
|
208
|
+
return this.c.compile(result, this.options).render(cx, partials);
|
204
209
|
}
|
205
210
|
|
206
211
|
return result;
|
@@ -215,8 +220,13 @@ var Hogan = {};
|
|
215
220
|
rQuot = /\"/g,
|
216
221
|
hChars =/[&<>\"\']/;
|
217
222
|
|
223
|
+
|
224
|
+
function coerceToString(val) {
|
225
|
+
return String((val === null || val === undefined) ? '' : val);
|
226
|
+
}
|
227
|
+
|
218
228
|
function hoganEscape(str) {
|
219
|
-
str =
|
229
|
+
str = coerceToString(str);
|
220
230
|
return hChars.test(str) ?
|
221
231
|
str
|
222
232
|
.replace(rAmp,'&')
|
@@ -457,7 +467,7 @@ var Hogan = {};
|
|
457
467
|
}
|
458
468
|
|
459
469
|
function writeCode(tree) {
|
460
|
-
return '
|
470
|
+
return 'var _=this;_.b(i=i||"");' + walk(tree) + 'return _.fl();';
|
461
471
|
}
|
462
472
|
|
463
473
|
Hogan.generate = function (code, text, options) {
|
@@ -465,7 +475,7 @@ var Hogan = {};
|
|
465
475
|
return 'function(c,p,i){' + code + ';}';
|
466
476
|
}
|
467
477
|
|
468
|
-
return new Hogan.Template(new Function('c', 'p', 'i', code), text, Hogan);
|
478
|
+
return new Hogan.Template(new Function('c', 'p', 'i', code), text, Hogan, options);
|
469
479
|
}
|
470
480
|
|
471
481
|
function esc(s) {
|
@@ -506,37 +516,36 @@ var Hogan = {};
|
|
506
516
|
|
507
517
|
function section(nodes, id, method, start, end, tags) {
|
508
518
|
return 'if(_.s(_.' + method + '("' + esc(id) + '",c,p,1),' +
|
509
|
-
'c,p,0,' + start + ',' + end + ',
|
510
|
-
'
|
511
|
-
'function(c,p){
|
519
|
+
'c,p,0,' + start + ',' + end + ',"' + tags + '")){' +
|
520
|
+
'_.rs(c,p,' +
|
521
|
+
'function(c,p,_){' +
|
512
522
|
walk(nodes) +
|
513
|
-
'
|
514
|
-
'else{b += _.b; _.b = ""};';
|
523
|
+
'});c.pop();}';
|
515
524
|
}
|
516
525
|
|
517
526
|
function invertedSection(nodes, id, method) {
|
518
|
-
return 'if
|
527
|
+
return 'if(!_.s(_.' + method + '("' + esc(id) + '",c,p,1),c,p,1,0,0,"")){' +
|
519
528
|
walk(nodes) +
|
520
529
|
'};';
|
521
530
|
}
|
522
531
|
|
523
532
|
function partial(tok) {
|
524
|
-
return 'b
|
533
|
+
return '_.b(_.rp("' + esc(tok.n) + '",c,p,"' + (tok.indent || '') + '"));';
|
525
534
|
}
|
526
535
|
|
527
536
|
function tripleStache(id, method) {
|
528
|
-
return 'b
|
537
|
+
return '_.b(_.t(_.' + method + '("' + esc(id) + '",c,p,0)));';
|
529
538
|
}
|
530
539
|
|
531
540
|
function variable(id, method) {
|
532
|
-
return 'b
|
541
|
+
return '_.b(_.v(_.' + method + '("' + esc(id) + '",c,p,0)));';
|
533
542
|
}
|
534
543
|
|
535
544
|
function text(id) {
|
536
|
-
return 'b
|
545
|
+
return '_.b(' + id + ');';
|
537
546
|
}
|
538
547
|
|
539
|
-
Hogan.parse = function(tokens, options) {
|
548
|
+
Hogan.parse = function(tokens, text, options) {
|
540
549
|
options = options || {};
|
541
550
|
return buildTree(tokens, '', [], options.sectionTags || []);
|
542
551
|
},
|
@@ -565,12 +574,8 @@ var Hogan = {};
|
|
565
574
|
return t;
|
566
575
|
}
|
567
576
|
|
568
|
-
t = this.generate(writeCode(this.parse(this.scan(text, options.delimiters), options)), text, options);
|
577
|
+
t = this.generate(writeCode(this.parse(this.scan(text, options.delimiters), text, options)), text, options);
|
569
578
|
return this.cache[key] = t;
|
570
579
|
};
|
571
580
|
})(typeof exports !== 'undefined' ? exports : Hogan);
|
572
581
|
|
573
|
-
|
574
|
-
if (typeof define === 'function' && define.amd) {
|
575
|
-
define(Hogan);
|
576
|
-
}
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hogan_assets
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-02-15 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: execjs
|
16
|
-
requirement: &
|
16
|
+
requirement: &70197692099300 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 1.2.9
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70197692099300
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: tilt
|
27
|
-
requirement: &
|
27
|
+
requirement: &70197692098520 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 1.3.3
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70197692098520
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: sprockets
|
38
|
-
requirement: &
|
38
|
+
requirement: &70197692097800 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: 2.0.3
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70197692097800
|
47
47
|
description: Use compiled hogan.js (mustache) JavaScript templates with sprockets
|
48
48
|
and the Rails asset pipeline.
|
49
49
|
email:
|