1 | (function() {
|
2 | var Liquid, Q,
|
3 | __hasProp = {}.hasOwnProperty,
|
4 | __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
|
5 | __slice = [].slice;
|
6 |
|
7 | Q = require('q');
|
8 |
|
9 | Liquid = require('liquid-node');
|
10 |
|
11 | module.exports = function() {
|
12 | var engine;
|
13 | engine = new Liquid.Engine;
|
14 | engine.registerTag("block", (function() {
|
15 | var BlockBlock;
|
16 | return BlockBlock = (function(_super) {
|
17 | var Syntax, SyntaxHelp;
|
18 |
|
19 | __extends(BlockBlock, _super);
|
20 |
|
21 | Syntax = /(\w+)/;
|
22 |
|
23 | SyntaxHelp = "Syntax Error in 'block' - Valid syntax: block [templateName]";
|
24 |
|
25 | function BlockBlock(template, tagName, markup, tokens) {
|
26 | var match;
|
27 | match = Syntax.exec(markup);
|
28 | if (!match) {
|
29 | throw new Liquid.SyntaxError(SyntaxHelp);
|
30 | }
|
31 | template.exportedBlocks || (template.exportedBlocks = {});
|
32 | template.exportedBlocks[match[1]] = this;
|
33 | BlockBlock.__super__.constructor.apply(this, arguments);
|
34 | }
|
35 |
|
36 | BlockBlock.prototype.replace = function(block) {
|
37 | return this.nodelist = block.nodelist;
|
38 | };
|
39 |
|
40 | return BlockBlock;
|
41 |
|
42 | })(Liquid.Block);
|
43 | })());
|
44 | engine.registerTag("extends", (function() {
|
45 | var ExtendsTag;
|
46 | return ExtendsTag = (function(_super) {
|
47 | var Syntax, SyntaxHelp;
|
48 |
|
49 | __extends(ExtendsTag, _super);
|
50 |
|
51 | Syntax = /([a-z0-9\/\\_-]+)/i;
|
52 |
|
53 | SyntaxHelp = "Syntax Error in 'extends' - Valid syntax: extends [templateName]";
|
54 |
|
55 | function ExtendsTag(template, tagName, markup, tokens) {
|
56 | var match;
|
57 | match = Syntax.exec(markup);
|
58 | if (!match) {
|
59 | throw new Liquid.SyntaxError(SyntaxHelp);
|
60 | }
|
61 | template["extends"] = match[1];
|
62 | ExtendsTag.__super__.constructor.apply(this, arguments);
|
63 | }
|
64 |
|
65 | ExtendsTag.prototype.render = function(context) {
|
66 | return "";
|
67 | };
|
68 |
|
69 | return ExtendsTag;
|
70 |
|
71 | })(Liquid.Tag);
|
72 | })());
|
73 | engine.registerTag("include", (function() {
|
74 | var IncludeTag;
|
75 | return IncludeTag = (function(_super) {
|
76 | var Syntax, SyntaxHelp;
|
77 |
|
78 | __extends(IncludeTag, _super);
|
79 |
|
80 | Syntax = /([a-z0-9\/\\_-]+)/i;
|
81 |
|
82 | SyntaxHelp = "Syntax Error in 'include' - Valid syntax: include [templateName]";
|
83 |
|
84 | function IncludeTag(template, tagName, markup, tokens) {
|
85 | var deferred, match;
|
86 | match = Syntax.exec(markup);
|
87 | if (!match) {
|
88 | throw new Liquid.SyntaxError(SyntaxHelp);
|
89 | }
|
90 | this.filepath = match[1];
|
91 | deferred = Q.defer();
|
92 | this.included = deferred.promise;
|
93 | template.engine.importer(this.filepath, function(err, src) {
|
94 | var subTemplate;
|
95 | subTemplate = engine.extParse(src, template.engine.importer);
|
96 | return subTemplate.then(function(t) {
|
97 | return deferred.resolve(t);
|
98 | });
|
99 | });
|
100 | IncludeTag.__super__.constructor.apply(this, arguments);
|
101 | }
|
102 |
|
103 | IncludeTag.prototype.render = function(context) {
|
104 | return this.included.then(function(i) {
|
105 | return i.render(context);
|
106 | });
|
107 | };
|
108 |
|
109 | return IncludeTag;
|
110 |
|
111 | })(Liquid.Tag);
|
112 | })());
|
113 | engine.extParse = function(src, importer) {
|
114 | var baseTemplate, deferred, depth, stack, walker,
|
115 | _this = this;
|
116 | engine.importer = importer;
|
117 | baseTemplate = engine.parse(src);
|
118 | if (!baseTemplate["extends"]) {
|
119 | return Q(baseTemplate);
|
120 | }
|
121 | stack = [baseTemplate];
|
122 | depth = 0;
|
123 | deferred = Q.defer();
|
124 | walker = function(tmpl, cb) {
|
125 | if (!tmpl["extends"]) {
|
126 | return cb();
|
127 | }
|
128 | return tmpl.engine.importer(tmpl["extends"], function(err, data) {
|
129 | if (err) {
|
130 | return cb(err);
|
131 | }
|
132 | if (depth > 100) {
|
133 | return cb("too many `extends`");
|
134 | }
|
135 | depth++;
|
136 | return engine.extParse(data, importer).then(function(subTemplate) {
|
137 | stack.unshift(subTemplate);
|
138 | return walker(subTemplate, cb);
|
139 | }).fail(function(err) {
|
140 | return cb(err != null ? err : "Failed to parse template.");
|
141 | });
|
142 | });
|
143 | };
|
144 | walker(stack[0], function(err) {
|
145 | var rootTemplate, subTemplates;
|
146 | if (err) {
|
147 | return deferred.reject(err);
|
148 | }
|
149 | rootTemplate = stack[0], subTemplates = 2 <= stack.length ? __slice.call(stack, 1) : [];
|
150 | subTemplates.forEach(function(subTemplate) {
|
151 | var k, rootTemplateBlocks, subTemplateBlocks, v, _ref, _results;
|
152 | subTemplateBlocks = subTemplate.exportedBlocks || {};
|
153 | rootTemplateBlocks = rootTemplate.exportedBlocks || {};
|
154 | _results = [];
|
155 | for (k in subTemplateBlocks) {
|
156 | if (!__hasProp.call(subTemplateBlocks, k)) continue;
|
157 | v = subTemplateBlocks[k];
|
158 | _results.push((_ref = rootTemplateBlocks[k]) != null ? _ref.replace(v) : void 0);
|
159 | }
|
160 | return _results;
|
161 | });
|
162 | return deferred.resolve(rootTemplate);
|
163 | });
|
164 | return deferred.promise;
|
165 | };
|
166 | return engine;
|
167 | };
|
168 |
|
169 | }).call(this);
|