Permalink
Browse files

start of macros. broken tests.

  • Loading branch information...
1 parent 57d59d0 commit cc05aa42a80b851f16d07c9e8ed6ee3a4e0e4864 @paularmstrong committed Sep 23, 2011
Showing with 61 additions and 15 deletions.
  1. +19 −5 lib/parser.js
  2. +13 −0 lib/tags.js
  3. +10 −10 tests/parser.test.js
  4. +19 −0 tests/tags.test.js
View
@@ -18,6 +18,14 @@ exports.TOKEN_TYPES = {
VAR: VAR_TOKEN
};
+function getMethod(input) {
+ return input.match(/^[\w]+/)[0];
+}
+
+function getArgs(input) {
+ return JSON.parse('[' + input.replace(/^\w+\(|\)$/g, '').replace(/\\/g, '\\\\').replace(/'/g, '"') + ']');
+}
+
function parseVariable(token, escape) {
if (!token) {
return {
@@ -31,27 +39,33 @@ function parseVariable(token, escape) {
var filters = [],
parts = token.replace(/^\{\{ *| *\}\}$/g, '').split('|'),
varname = parts.shift(),
- filter_name, part;
+ args = null, filter_name, part;
+
+ if ((/\(/).test(varname)) {
+ varname = getMethod(varname);
+ args = getArgs(varname);
+ }
for (part in parts) {
if (parts.hasOwnProperty(part)) {
part = parts[part];
- filter_name = part.match(/^[\w\/]+/);
+ filter_name = getMethod(part);
if (/\(/.test(part)) {
// Faster than doing regex split on ','
filters.push({
- name: filter_name[0],
- args: JSON.parse('[' + part.replace(/^\w+\(|\)$/g, '').replace(/\\/g, '\\\\').replace(/'/g, '"') + ']')
+ name: filter_name,
+ args: getArgs(part)
});
} else {
- filters.push({ name: filter_name[0], args: [] });
+ filters.push({ name: filter_name, args: [] });
}
}
}
return {
type: VAR_TOKEN,
name: varname,
+ args: args,
filters: filters,
escape: escape
};
View
@@ -270,3 +270,16 @@ exports.set = function (indent) {
value = parser.parseVariable(this.args[0]);
return 'var ' + varname + ' = (function () { var __output = []; ' + parser.compile.call({ tokens: [value] }, indent) + ' return __output.join(""); })();';
};
+
+exports.macro = function (indent) {
+ var macro = this.args.shift();
+ console.log(this.args)
+
+ return ['var ' + macro + ' = (function () {'
+ , ' var __output = [];'
+ , parser.compile.call(this, indent + ' ')
+ , ' return __output.join("");'
+ , '})();'
+ ].join('\n' + indent);
+};
+exports.macro.ends = true;
View
@@ -80,33 +80,33 @@ exports.Comments = testCase({
exports.Variable = testCase({
'basic variable': function (test) {
var output = parser.parse('{{ foobar }}');
- test.deepEqual([{ type: parser.TOKEN_TYPES.VAR, name: 'foobar', filters: [], escape: false }], output, 'with spaces');
+ test.deepEqual([{ type: parser.TOKEN_TYPES.VAR, name: 'foobar', filters: [], escape: false, args: null }], output, 'with spaces');
output = parser.parse('{{foobar}}');
- test.deepEqual([{ type: parser.TOKEN_TYPES.VAR, name: 'foobar', filters: [], escape: false }], output, 'without spaces');
+ test.deepEqual([{ type: parser.TOKEN_TYPES.VAR, name: 'foobar', filters: [], escape: false, args: null }], output, 'without spaces');
test.done();
},
'dot-notation variable': function (test) {
var output = parser.parse('{{ foo.bar }}');
- test.deepEqual([{ type: parser.TOKEN_TYPES.VAR, name: 'foo.bar', filters: [], escape: false }], output);
+ test.deepEqual([{ type: parser.TOKEN_TYPES.VAR, name: 'foo.bar', filters: [], escape: false, args: null }], output);
test.done();
},
'variable with filter': function (test) {
var output = parser.parse('{{ foobar|awesome }}');
- test.deepEqual([{ type: parser.TOKEN_TYPES.VAR, name: 'foobar', escape: false, filters: [{ name: 'awesome', args: [] }] }], output, 'filter by name');
+ test.deepEqual([{ type: parser.TOKEN_TYPES.VAR, name: 'foobar', escape: false, args: null, filters: [{ name: 'awesome', args: [] }] }], output, 'filter by name');
output = parser.parse('{{ foobar|awesome("param", 2) }}');
- test.deepEqual([{ type: parser.TOKEN_TYPES.VAR, name: 'foobar', escape: false, filters: [{ name: 'awesome', args: ['param', 2] }] }], output, 'filter with params');
+ test.deepEqual([{ type: parser.TOKEN_TYPES.VAR, name: 'foobar', escape: false, args: null, filters: [{ name: 'awesome', args: ['param', 2] }] }], output, 'filter with params');
test.done();
},
'multiple filters': function (test) {
var output = parser.parse('{{ foobar|baz(1)|rad|awesome("param", 2) }}');
- test.deepEqual([{ type: parser.TOKEN_TYPES.VAR, name: 'foobar', escape: false, filters: [
+ test.deepEqual([{ type: parser.TOKEN_TYPES.VAR, name: 'foobar', escape: false, args: null, filters: [
{ name: 'baz', args: [1] },
{ name: 'rad', args: [] },
{ name: 'awesome', args: ['param', 2] }
@@ -118,24 +118,24 @@ exports.Variable = testCase({
'filters do not carry over': function (test) {
var output = parser.parse('{{ foo|baz }}{{ bar }}');
test.deepEqual([
- { type: parser.TOKEN_TYPES.VAR, name: 'foo', filters: [{ name: 'baz', args: [] }], escape: false },
- { type: parser.TOKEN_TYPES.VAR, name: 'bar', filters: [], escape: false }
+ { type: parser.TOKEN_TYPES.VAR, name: 'foo', filters: [{ name: 'baz', args: [] }], escape: false, args: null },
+ { type: parser.TOKEN_TYPES.VAR, name: 'bar', filters: [], escape: false, args: null }
], output);
test.done();
},
'filters with all kinds of characters in params': function (test) {
var output = parser.parse("{{ foo|blah('01a,;?./¨œ∑´®†][{}]') }}");
test.deepEqual([
- { type: parser.TOKEN_TYPES.VAR, name: 'foo', filters: [{ name: 'blah', args: ["01a,;?./¨œ∑´®†][{}]"] }], escape: false }
+ { type: parser.TOKEN_TYPES.VAR, name: 'foo', filters: [{ name: 'blah', args: ["01a,;?./¨œ∑´®†][{}]"] }], escape: false, args: null }
], output);
test.done();
},
'escapements carry over in filter args': function (test) {
var output = parser.parse('{{ foo|blah("\\s") }}');
test.deepEqual([
- { type: parser.TOKEN_TYPES.VAR, name: 'foo', filters: [{ name: 'blah', args: ["\\s"] }], escape: false }
+ { type: parser.TOKEN_TYPES.VAR, name: 'foo', filters: [{ name: 'blah', args: ["\\s"] }], escape: false, args: null }
], output);
test.done();
}
View
@@ -167,3 +167,22 @@ exports.set = testCase({
test.done();
}
});
+
+exports.macro = testCase({
+ setUp: function (callback) {
+ swig.init({});
+ callback();
+ },
+
+ basic: function (test) {
+ var tmpl8 = swig.fromString('{% macro foo %}hi!{% endmacro %}oh, {{ foo }}');
+ test.strictEqual(tmpl8.render({}), 'oh, hi!');
+ test.done();
+ },
+
+ args: function (test) {
+ var tmpl8 = swig.fromString('{% macro foo(input) %}{{ input }}{% endmacro %}oh, {{ foo("yep") }}');
+ test.strictEqual(tmpl8.render({}), 'oh, yep');
+ test.done();
+ }
+});

0 comments on commit cc05aa4

Please sign in to comment.