Skip to content

Commit

Permalink
pass the parser into tags
Browse files Browse the repository at this point in the history
  • Loading branch information
paularmstrong committed Feb 16, 2012
1 parent e29c8a4 commit 84219d0
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 31 deletions.
2 changes: 1 addition & 1 deletion lib/parser.js
Expand Up @@ -405,7 +405,7 @@ exports.compile = function compile(indent, parentBlock) {
if (token.strip.end && token.tokens.length && typeof _.last(token.tokens) === 'string') {
token.tokens[token.tokens.length - 1] = _.last(token.tokens).replace(/\s+$/, '');
}
code += token.compile(indent + ' ', parentBlock);
code += token.compile(indent + ' ', parentBlock, exports);
}

}, this);
Expand Down
4 changes: 1 addition & 3 deletions lib/tags/autoescape.js
@@ -1,10 +1,8 @@
var parser = require('../parser');

/**
* autoescape
* Special handling hardcoded into the parser to determine whether variable output should be escaped or not
*/
module.exports = function (indent, parentBlock) {
module.exports = function (indent, parentBlock, parser) {
return parser.compile.apply(this, [indent, parentBlock]);
};
module.exports.ends = true;
7 changes: 3 additions & 4 deletions lib/tags/else.js
@@ -1,11 +1,10 @@
var parser = require('../parser'),
parseIfArgs = require('./if').parseIfArgs,
var parseIfArgs = require('./if').parseIfArgs,
_ = require('underscore');

/**
* else
*/
module.exports = function (indent, parentBlock) {
module.exports = function (indent, parentBlock, parser) {
var last = _.last(this.parent).name,
thisArgs = _.clone(this.args),
ifarg,
Expand All @@ -24,7 +23,7 @@ module.exports = function (indent, parentBlock) {
}

ifarg = thisArgs.shift();
args = (parseIfArgs(thisArgs));
args = (parseIfArgs(thisArgs, parser));
out = '';

if (ifarg) {
Expand Down
5 changes: 2 additions & 3 deletions lib/tags/filter.js
@@ -1,11 +1,10 @@
var parser = require('../parser'),
helpers = require('../helpers'),
var helpers = require('../helpers'),
_ = require('underscore');

/**
* filter
*/
module.exports = function (indent, parentBlock) {
module.exports = function (indent, parentBlock, parser) {
var thisArgs = _.clone(this.args),
name = thisArgs.shift(),
args = (thisArgs.length) ? thisArgs.join(', ') : '',
Expand Down
5 changes: 2 additions & 3 deletions lib/tags/for.js
@@ -1,11 +1,10 @@
var parser = require('../parser'),
helpers = require('../helpers'),
var helpers = require('../helpers'),
_ = require('underscore');

/**
* for
*/
module.exports = function (indent, parentBlock) {
module.exports = function (indent, parentBlock, parser) {
var thisArgs = _.clone(this.args),
operand1 = thisArgs[0],
operator = thisArgs[1],
Expand Down
9 changes: 4 additions & 5 deletions lib/tags/if.js
@@ -1,8 +1,7 @@
var parser = require('../parser'),
helpers = require('../helpers'),
var helpers = require('../helpers'),
_ = require('underscore');

function parseIfArgs(args) {
function parseIfArgs(args, parser) {
var operators = ['==', '<', '>', '!=', '<=', '>=', '===', '!==', '&&', '||', 'in', 'and', 'or'],
errorString = 'Bad if-syntax in `{% if ' + args.join(' ') + ' %}...',
tokens = [],
Expand Down Expand Up @@ -86,9 +85,9 @@ function parseIfArgs(args) {
/**
* if
*/
module.exports = function (indent, parentBlock) {
module.exports = function (indent, parentBlock, parser) {
var thisArgs = _.clone(this.args),
args = (parseIfArgs(thisArgs)),
args = (parseIfArgs(thisArgs, parser)),
out = '(function () {\n';

_.each(args, function (token) {
Expand Down
5 changes: 2 additions & 3 deletions lib/tags/import.js
@@ -1,11 +1,10 @@
var parser = require('../parser'),
helpers = require('../helpers'),
var helpers = require('../helpers'),
_ = require('underscore');

/**
* import
*/
module.exports = function (indent) {
module.exports = function (indent, parentBlock, parser) {
if (this.args.length !== 3) {
throw new Error('Import statements require three arguments: {% import [template] as [context] %}.');
}
Expand Down
5 changes: 2 additions & 3 deletions lib/tags/include.js
@@ -1,11 +1,10 @@
var parser = require('../parser'),
helpers = require('../helpers'),
var helpers = require('../helpers'),
_ = require('underscore');

/**
* include
*/
module.exports = function (indent) {
module.exports = function (indent, parentBlock, parser) {
var args = _.clone(this.args),
template = args.shift(),
context = '_context';
Expand Down
5 changes: 2 additions & 3 deletions lib/tags/macro.js
@@ -1,10 +1,9 @@
var parser = require('../parser'),
_ = require('underscore');
var _ = require('underscore');

/**
* macro
*/
module.exports = function (indent, parentBlock) {
module.exports = function (indent, parentBlock, parser) {
var thisArgs = _.clone(this.args),
macro = thisArgs.shift(),
args = '',
Expand Down
5 changes: 2 additions & 3 deletions lib/tags/set.js
@@ -1,11 +1,10 @@
var parser = require('../parser'),
helpers = require('../helpers'),
var helpers = require('../helpers'),
_ = require('underscore');

/**
* set
*/
module.exports = function (indent, parentBlock) {
module.exports = function (indent, parentBlock, parser) {
var thisArgs = _.clone(this.args),
varname = helpers.escapeVarName(thisArgs.shift(), '_context'),
value;
Expand Down

0 comments on commit 84219d0

Please sign in to comment.